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
171
172
173
174
175
176
177
178
179
180
181
182
183
function createElements( phrase )
{
var s = '';
for( var i = 0; i < phrase.length; i++ )
{
ch = phrase.charAt( i );
if( ch == " " ){
ch = " ";
}
s += '<div class="letter">' + ch + '</div>' +
'<div class="dash"> </div>';
}
return s;
}
$(document).ready(function()
{
var w = new wcontainer( "#ub-widget" );
var sentence = "";
if(window.sankore)
sentence = (sankore.preference("ordSplPhrases", ""))?sankore.preference("ordSplPhrases", ""):sankoreLang.example;
else
sentence = sankoreLang.example;
w.maxWidth = 600;
w.setEditContent( '<div class="inputwrap"><textarea class="percent">' + 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 phrase = w.elements.container.find( "textarea" ).val()
.replace( /\r/g, '' ).replace( /\n/g, ' ' ).replace( / /g, ' ' ).trim();
// store the text
w.setData( "phrase", phrase );
// remove all dots (they are to be set during the exercise)
phrase = phrase.replace( / /g, '' );
// create the html
w.setViewContent( createElements( phrase ) );
// 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 phrase = "";
var ch = "";
this.elements.containerView.find( "div:visible" ).each( function()
{
if( this.className.indexOf( "fixed" ) != -1 ){
phrase += ' ';
}
else if( this.className.indexOf( "dash" ) != -1 ){
return;
}
else{
ch = $( this ).html();
phrase += ch;
}
});
if( phrase == this.getData( "phrase" ) ){
this.elements.containerView.addClass( "answerRight" );
}
else{
this.elements.containerView.removeClass( "answerRight" );
}
};
window.w = w;
window.winstance = w;
w.modeView();
if (window.widget) {
window.widget.onleave = function(){
if(w.editMode)
sankore.setPreference("ordSplPhrases", w.elements.container.find( "textarea" ).val());
else
sankore.setPreference("ordSplPhrases", w.getData("phrase"));
}
}
});