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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
Sankore API
*/
function startEditing()
{
modeEdit();
}
function stopEditing()
{
modeView();
}
function initialize()
{
}
function checkResponse()
{
checkWord();
}
/*
/ END sankore api
*/
var sankoreLang = {
view: "View",
edit: "Edit",
example: "a*long*,*long*time*ago*..."
};
var word = "";
var curWord = "";
if(window.sankore){
word = (sankore.preference("rightOrdWords", ""))?sankore.preference("rightOrdWords", ""):sankoreLang.example;
curWord = (sankore.preference("currentOrdWords", ""))?sankore.preference("currentOrdWords", ""):"";
} else {
word = sankoreLang.example;
}
var doCheckWord = true;
// array of dom elements
var letters = [];
var editMode = false; // just a flag
// if use the "edit" button or rely on the api instead
var isSankore = false;
// whether to do window.resize or not (window = widget area)
var isBrowser = ( typeof( widget ) == "undefined" );
// hardcoded parameters, not very good
var input_width = 606;
var widget_padding = 65;
var min_view_width = 400;
/*
=================
createWordLetters
=================
returns array of dom elements
*/
function createWordLetters( word )
{
var ch, el;
var letters = [];
if( word.indexOf( '*' ) != -1 )
{
var tmp = word.split( '*' );
for( i in tmp )
{
ch = tmp[i];
el = document.createElement( "div" );
$(el).addClass( "letter" ).text( ch );
letters.push( el );
}
}
else
{
for( var i = 0; i < word.length; i++ )
{
ch = word.charAt( i );
el = document.createElement( "div" );
$(el).addClass( "letter" ).text( ch );
letters.push( el );
}
}
return letters;
}
/*
=====================
checkWord
=====================
scans the letters and checks
if they are in the right order
*/
function checkWord()
{
if( !doCheckWord )
return;
var str = "";
$( "#mp_word .letter" ).each( function(){
str += $(this).text();
});
var w = word;
while( w.indexOf( '*' ) != -1 )
{
w = w.replace( '*', '' );
}
if( str == w ){
$( "#mp_word .letter" ).addClass( "right" );
//message( "Right!" );
}
}
/*
==============
shuffle
==============
shuffles an array
*/
function shuffle( arr )
{
var pos, tmp;
for( var i = 0; i < arr.length; i++ )
{
pos = Math.round( Math.random() * ( arr.length - 1 ) );
tmp = arr[pos];
arr[pos] = arr[i];
arr[i] = tmp;
}
return arr;
}
/*
==================
modeView()
==================
turns the widget into the view mode
*/
function modeView()
{
if( editMode ){
word = $( "#mp_word input:text" ).attr( "value" );
}
// if no sankore api, insert edit button
if( !isSankore ){
$( "#mp_setup" ).empty().append( '<input type="button" value="' + sankoreLang.edit + '">' );
$( "#mp_setup input:button" ).click( function(){
modeEdit();
});
}
// clean the previous word
$( "#mp_word" ).empty();
// create new set of letters
var letters;
if(window.sankore && curWord && !editMode)
letters = createWordLetters( curWord );
else
letters = shuffle( createWordLetters( word ) );
for( i in letters ){
$("#mp_word").append( letters[i] );
}
// in sankore api there would be a function to check
// the answer, so no update parameter would be needed
if( !isSankore ){
$( "#mp_word" ).sortable( {
update: checkWord
} );
} else $( "#mp_word" ).sortable();
// adjustWidth
var totalLettersWidth = 0;
for( i in letters ){
var currentWidth = $( letters[i] ).outerWidth( true );
totalLettersWidth += currentWidth;
}
totalLettersWidth += 1;
var width = Math.max(
totalLettersWidth,
min_view_width
);
// shift the words to the right to center them
if( width > totalLettersWidth ){
$( "#mp_word" ).css( "margin-left", Math.round( (width - totalLettersWidth)/2 ) );
}
else{
$( "#mp_word" ).css( "margin-left", 0 );
}
// apply new width
adjustWidth( width );
}
/*
============
adjustWidth
============
*/
function adjustWidth( width )
{
$( "#ub-widget" ).animate( {
width: width
} );
// if viewed as a widget, resize the window
if( !isBrowser ){
window.resizeTo( width + widget_padding, widget.height );
}
}
/*
================
modeEdit
================
*/
function modeEdit()
{
editMode = true;
// if no sankore api, insert ok button
if( !isSankore )
{
$( "#mp_setup" ).empty().append( '<input type="button" value="' + sankoreLang.view + '">' );
$( "#mp_setup input:button" ).click( function(){
modeView();
});
}
$( "#mp_word").css( "margin-left", 0 ).empty().append('<input value="'+word+'">');
adjustWidth( input_width );
}
if (window.widget) {
window.widget.onleave = function(){
if($( "#mp_word input:text" ).attr( "value" ))
{
modeView();
var str = "";
$( "#mp_word .letter" ).each( function(){
str += $(this).text() + "*";
});
str = str.substr(0, str.length - 1);
sankore.setPreference("currentOrdWords", str);
modeEdit();
}
else{
str = "";
$( "#mp_word .letter" ).each( function(){
str += $(this).text() + "*";
});
str = str.substr(0, str.length - 1);
sankore.setPreference("currentOrdWords", str);
}
sankore.setPreference("rightOrdWords", word);
}
}