-
-
Notifications
You must be signed in to change notification settings - Fork 422
/
util.js
253 lines (227 loc) · 6.01 KB
/
util.js
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
// Utility functions
// Namespace
Abcd = {};
// Game context handling
var app;
Abcd.context = {
database: "",
home: null,
object: null,
screen: "",
lang: "fr",
casevalue: 0,
screenContext: null
};
Abcd.saveContext = function() {
document.getElementById("stop-button").addEventListener('click', function (event) {
var values = [];
values.push(Abcd.context.object!=null?Abcd.context.object.kindName:"");
values.push(Abcd.context.lang);
values.push(Abcd.context.casevalue);
values.push(Abcd.context.object!=null?Abcd.context.object.saveContext():"");
var datastoreObject = Abcd.activity.getDatastoreObject();
var jsonData = JSON.stringify({context:values.join("#"), database:Abcd.context.getDatabase()});
datastoreObject.setDataAsText(jsonData);
datastoreObject.save(function() {});
});
};
Abcd.loadContext = function(callback) {
var datastoreObject = Abcd.activity.getDatastoreObject();
datastoreObject.loadAsText(function (error, metadata, data) {
var context = JSON.parse(data);
if (context) {
var values = context.context.split('#');
Abcd.context.screen = values[0];
Abcd.context.lang = values[1];
Abcd.context.casevalue = values[2];
Abcd.context.screenContext = values[3];
Abcd.setLocale(Abcd.context.lang);
callback();
}
});
};
// Init Sugar interface
Abcd.setLocale = function(lang) {
var texts = Abcd.getTextsFromLocal(lang);
__$FC_l10n_set(texts);
Abcd.letters = Abcd[lang+"Letters"];
if (Abcd.context.object != null)
Abcd.context.object.setLocale();
}
Abcd.getTextsFromLocal = function(lang) {
switch (lang) {
case "fr":
return Abcd.frTexts;
case "es":
return Abcd.esTexts;
default:
return Abcd.enTexts;
}
}
Abcd.getLettersFromLocal = function(lang) {
switch (lang) {
case "fr":
return Abcd.frLetters;
case "es":
return Abcd.esLetters;
default:
return Abcd.enLetters;
}
}
Abcd.setCase = function(casevalue) {
Abcd.context.casevalue = casevalue;
if (Abcd.context.object != null)
Abcd.context.object.setCase();
}
Abcd.log = function(msg) {
console.log(msg);
};
// Home handling
Abcd.goHome = function() {
if (Abcd.context.home != null) {
if (Abcd.context.object == null)
return;
Abcd.context.screen = "";
Abcd.context.object = null;
Abcd.context.home.renderInto(document.getElementById("body"));
Abcd.context.home.playTheme();
}
};
//Buttons to show and hide Language and Case Button\
var x = document.getElementsByClassName("switchCase");
var y = document.getElementsByClassName("switchLang");
Abcd.hideCase = function() {
var i;
for (i = 0; i < x.length; i++) {
x[i].style.visibility = "hidden";
}
}
Abcd.showCase = function() {
var i;
for (i = 0; i < x.length; i++) {
x[i].style.visibility = "visible";
}
}
Abcd.hideLang = function() {
var i;
for (i = 0; i < y.length; i++) {
y[i].style.visibility = "hidden";
}
}
Abcd.showLang = function() {
var i;
for (i = 0; i < y.length; i++) {
y[i].style.visibility = "visible";
}
}
//--- Utilities
// Change visibility of a set of controls
Abcd.changeVisibility = function(object, items) {
for(var item in items) {
if (items[item])
object.$[item].show();
else
object.$[item].hide();
}
}
// Randomly get an entry in the current language
Abcd.randomEntryIndex = function(excludes, filter) {
// Get first level
var value = null;
if (filter != null && filter.kind == "Abcd.Collection") {
// Get the collection
var collection = Abcd.collections[filter.index];
var length = collection.entries.length;
value = [];
for (var i = 0 ; i < length ; i++) {
var entry = collection.entries[i];
if (Abcd.entries[entry][Abcd.context.lang] == 1)
value.push(entry);
}
} else {
// Choose a letter
var firstlen = 0;
var firstindex = -1;
for(var key in Abcd.letters) {
if (filter != null && filter.letter == key) {
firstindex = firstlen;
break;
}
if (Abcd.letters.hasOwnProperty(key)) firstlen++;
}
if (firstindex == -1)
firstindex = Math.floor(Math.random()*firstlen);
// Choose an index
var i = 0;
for(var key in Abcd.letters) {
if (i++ == firstindex) {
value = Abcd.letters[key];
break;
}
}
}
// Copy without excludes
var array = [];
for (var i = 0 ; i < value.length ; i++) {
var found = false;
if (excludes !== undefined) {
for (var j = 0 ; !found && j < excludes.length ; j++) {
if (value[i] == excludes[j])
found = true;
}
}
if (!found)
array.push(value[i]);
}
// Select one randomely
var secondlen = array.length;
var secondindex = Math.floor(Math.random()*secondlen);
return array[secondindex];
}
// Mix an array into a new one
Abcd.mix = function(chain) {
// Check size
if (chain.length < 2) {
return chain;
}
// Mix cards
var mixedchain = [];
var tomix = enyo.cloneArray(chain);
while (tomix.length != 1) {
// Take a card
var i = Math.floor(Math.random()*tomix.length);
mixedchain.push(tomix[i]);
tomix[i] = null;
// Remix
var newmix = [];
for (var j = 0 ; j < tomix.length ; j++) {
if (tomix[j] != null)
newmix.push(tomix[j]);
}
tomix = newmix;
}
mixedchain.push(tomix[0]);
return mixedchain;
};
// Encoding functions taken from
// https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
function uint6ToB64 (nUint6) {
return nUint6 < 26 ?
nUint6 + 65 : nUint6 < 52 ?
nUint6 + 71 : nUint6 < 62 ?
nUint6 - 4 : nUint6 === 62 ?
43 : nUint6 === 63 ?
47 : 65;
}
Abcd.toBase64 = function(aBytes) {
var eqLen = (3 - (aBytes.length % 3)) % 3, sB64Enc = "";
for (var nMod3, nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {
nMod3 = nIdx % 3;
nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24);
if (nMod3 === 2 || aBytes.length - nIdx === 1) {
sB64Enc += String.fromCharCode(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63));
nUint24 = 0;
}
}
return eqLen === 0 ? sB64Enc : sB64Enc.substring(0, sB64Enc.length - eqLen) + (eqLen === 1 ? "=" : "==");
}