1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
function createElements( text )
{
var s = '';
var words = text.split( " " );
for( var i = 0; i < words.length; i++ )
{
if( i ){
s += '<div> </div>';
}
s += '<div class="letter">' + words[i] + '</div>' +
'<div class="dash">.</div>';
}
return s;
}
$(document).ready(function()
{
var w = new wcontainer( "#ub-widget" );
w.maxWidth = 600;
w.setEditContent( '<div class="inputwrap"><textarea class="percent">hello, this is the first sentence. hi, this is the second sentence. hello again, this is the third sentence. good morning, this is the fifth sentence. hi, sorry, i\'m late, i\'m the fourth sentence.</textarea></div>' );
w.setViewContent( '<div class="upper"><div class="dash fixed">. </div></div>' );
w.setData( "dashWidth", w.elements.container.find( ".dash" ).outerWidth() );
w.setViewContent( '<div class="upper"><div class="dash fixed">M</div></div>' );
w.setData( "lineHeight", w.elements.container.find( ".dash" ).outerHeight() );
w.setViewContent( "" );
// onViewMode
w.onViewMode = function()
{
// clean up the text
var text = w.elements.container.find( "textarea" ).val()
.replace( /\r/g, '' ).replace( /\n/g, ' ' ).replace( / /g, ' ' );
// store the text
w.setData( "text", text );
// remove all dots (they are to be set during the exercise)
text = text.replace( /\. /g, ' ' ).trim( ["."] );
// create the html
w.setViewContent( createElements( text ) );
// the behaviour
w.elements.containerView.find( ".letter" )
.mouseover( function()
{
var el = $( this ).next();
// determine new hover class
var is_fixed = ( el.get( 0 ).className.indexOf( "fixed" ) != -1 );
var hover_class = is_fixed?
"dash_hover_fixed" : "dash_hover";
// assign new hover class
el.addClass( hover_class )
.data( "hc", hover_class );
})
.mouseout( function()
{
var el = $( this ).next();
// remove current hover class
var hc = el.data( "hc" );
el.removeClass( hc );
})
.click( function()
{
var el = $( this ).next();
// remove current hover class
$( this ).trigger( "mouseout" );
// toggle fixed class
el.toggleClass( "fixed" );
// determine new hover class
// assign new hover class
$( this ).trigger( "mouseover" );
w.checkAnswer();
w.adjustSize();
});
w.checkAnswer();
};
// viewSize
w.viewSize = function()
{
var w = 0;
var h = 0;
var dh = winstance.getData( "lineHeight" );
var dw = winstance.getData( "dashWidth" );
winstance.elements.containerView.find( "div:visible" ).each( function()
{
w += $( this ).outerWidth();
h = Math.max( h, $( this ).outerHeight( true ) );
});
var square = w*h;
h = Math.max( h, $( winstance.elements.containerView ).height() );
if( winstance.maxWidth )
{
w = Math.min( w, winstance.maxWidth );
h = parseInt( square / w );
}
return { w: w, h: h+dh };
};
// editSize
w.editSize = function()
{
return {
w: winstance.elements.containerEdit.find( "textarea" ).parent().outerWidth( true ),
h: winstance.elements.containerEdit.find( "textarea" ).parent().outerHeight( true ),
};
};
w.checkAnswer = function()
{
var text = "";
var ch = "";
this.elements.containerView.find( "div:visible" ).each( function()
{
if( this.className.indexOf( "fixed" ) != -1 ){
text += '.';
}
else if( this.className.indexOf( "dash" ) != -1 ){
return;
}
else{
ch = $( this ).html();
if( ch == " " ){
ch = " ";
}
text += ch;
}
});
console.log( text );
if( text == this.getData( "text" ) ){
this.elements.containerView.addClass( "answerRight" );
}
else{
this.elements.containerView.removeClass( "answerRight" );
}
};
window.w = w;
window.winstance = w;
w.modeView();
});