forked from llaske/sugarizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn.js
334 lines (300 loc) · 10.2 KB
/
learn.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Collections size on the screen
var entriesByScreen = 8;
// Learn app class
enyo.kind({
name: "Abcd.Learn",
kind: enyo.Control,
classes: "board",
published: {
context: null,
},
components: [
{components: [
{name: "colorBar", classes: "colorBar"},
{name: "home", kind: "Abcd.HomeButton"},
{name: "startSlideshow", kind: "Image", src: "images/slideshow.png", showing: false, ontap: "startSlideshow", classes: "standardButton slideshow"},
{name: "stopSlideshow", kind: "Image", src: "images/pause.png", showing: false, ontap: "stopSlideshow", classes: "standardButton slideshow"},
{kind: "Abcd.CaseButton"},
{kind: "Abcd.LanguageButton"}
]},
{components: [
{name: "pageCount", content: "-/-", classes: "pageCount", showing: false},
{name: "back", kind: "Image", src: "images/back.png", showing: false, classes: "standardButton backButton", ontap: "backTaped"},
{name: "prev", kind: "Image", src: "images/previous.png", showing: false, classes: "standardButton prevButton", ontap: "displayPrevEntries"},
{name: "next", kind: "Image", src: "images/next.png", showing: false, classes: "standardButton nextButton", ontap: "displayNextEntries"}
]},
{name: "box", classes: "learnBox", components: [
]}
],
// Constructor, save home
create: function() {
// Initialize
this.inherited(arguments);
this.theme = this.collection = this.entry = this.position = -1;
this.collections = [];
this.playing = null;
this.slideshowIndex = -1;
this.exportPNG = enyo.bind(this, "clickExportPNG");
// Set previous context
this.restoreContext();
if (this.theme != -1) {
if (this.theme == 4)
this.displayLetters({letter: this.collection});
else if (this.collection != -1)
this.displayEntries({index: this.collection});
else
this.displayCollections({index: this.theme});
} else
this.displayThemes();
},
// Context handling
restoreContext: function() {
if (this.context == null || this.context == "")
return;
var values = this.context.split('|');
this.theme = values[0];
this.collection = values[1];
this.entry = parseInt(values[2]);
},
saveContext: function() {
var values = [];
values.push(this.theme);
values.push(this.collection);
values.push(this.position);
return values.join("|");
},
// Localization changed
setLocale: function() {
// If on an entry, redisplay from the beginning to avoid displaying non translated text
var current = this.$.box.getControls()[0];
if (current.kind == "Abcd.Entry") {
this.entry = -1;
if (this.theme != 4)
this.displayEntries({index: Abcd.entries[current.index].coll});
else
this.displayLetters({letter:this.collection});
return;
}
// Change entry localization if any
enyo.forEach(this.$.box.getControls(), function(entry) {
if (entry.kind != 'Control')
entry.setLocale();
});
},
// Case changed
setCase: function() {
// Redraw entry
enyo.forEach(this.$.box.getControls(), function(entry) {
if (entry.kind == 'Abcd.Letter')
entry.letterChanged();
else if (entry.kind != 'Control')
entry.indexChanged();
});
},
// Handle click on exportPNG buttons
clickExportPNG: function() {
var pngButton = document.getElementById("png-button");
var tojournal;
if (pngButton.classList.contains("active")) {
tojournal = false;
} else {
tojournal = true;
}
this.putEntriesJournalModeTo(tojournal);
},
putEntriesJournalModeTo: function(tojournal) {
if (tojournal) {
document.getElementById("png-button").classList.add("active");
} else {
document.getElementById("png-button").classList.remove("active");
}
enyo.forEach(this.$.box.getControls(), function(entry) {
if (entry.kind == "Abcd.Entry") {
entry.tojournal = tojournal;
entry.tojournalChanged();
}
});
},
// Display themes and letters
displayThemes: function() {
// Display themes
this.theme = -1;
var length = Abcd.themes.length;
this.cleanBox();
this.$.box.addClass("box-4-theme");
this.$.box.createComponent({ classes: "linebreak" }).render();
Abcd.changeVisibility(this, {home: true, back: false, prev: false, next: false, pageCount: false, startSlideshow: false, stopSlideshow: false});
document.getElementById("png-button").style.visibility = "hidden";
document.getElementById("png-button").classList.remove("active");
document.getElementById("png-button").removeEventListener("click", this.exportPNG);
this.$.colorBar.addClass("themeColor"+this.theme);
for (var i = 0 ; i < length ; i++) {
this.$.box.createComponent(
{ kind: "Abcd.Theme", index: i, ontap: "displayCollections" },
{ owner: this }
).render();
}
this.$.box.createComponent({ classes: "linebreak" }).render();
// Display letters
for (var i = 0 ; i < 26 ; i++) {
this.$.box.createComponent(
{ kind: "Abcd.Letter", letter: String.fromCharCode(65+i), ontap: "displayLetters" },
{ owner: this }
).render();
}
// Save context
Abcd.saveContext();
},
// Display all collection for a theme
displayCollections: function(inSender, inEvent) {
this.theme = inSender.index;
var length = Abcd.collections.length;
this.cleanBox();
Abcd.changeVisibility(this, {home: false, back: true, prev: false, next: false, pageCount: false, startSlideshow: false, stopSlideshow: false});
this.$.colorBar.removeClass("themeColor-1");
this.$.colorBar.addClass("themeColor"+this.theme);
this.$.box.removeClass("box-4-theme");
this.$.box.addClass("box-4-collection");
document.getElementById("png-button").style.visibility = "hidden";
document.getElementById("png-button").classList.remove("active");
document.getElementById("png-button").removeEventListener("click", this.exportPNG);
this.$.box.createComponent({ classes: "linebreak" }).render();
this.$.box.createComponent({ classes: "linebreak" }).render();
for (var i = 0 ; i < length ; i++) {
if (Abcd.collections[i].theme != this.theme) continue;
this.$.box.createComponent({ kind: "Abcd.Collection", index: i, ontap: "displayEntries"}, {owner: this}).render();
}
// Save context
Abcd.saveContext();
},
// Display entries in a collection
displayEntries: function(inSender, inEvent) {
// Get items in collection
this.$.box.removeClass("box-4-collection");
this.$.colorBar.addClass("themeColor"+this.theme);
var index = inSender.index;
this.collection = index;
this.collections = [];
var length = Abcd.collections[index].entries.length;
for (var i = 0 ; i < length ; i++) {
var entry = Abcd.collections[index].entries[i];
if (Abcd.entries[entry][Abcd.context.lang] == 1)
this.collections.push(entry);
}
// Display it
this.displayEntriesFrom(this.entry+1);
},
// Display entries from a letter
displayLetters: function(inSender, inEvent) {
// Play letter sound
if (inSender.kind !== undefined)
inSender.play(Abcd.sound);
// Get items with this letters
this.collection = inSender.letter;
if (Abcd.letters[this.collection] === undefined)
return;
this.collections = Abcd.letters[this.collection];
// Display it
this.theme = 4;
this.$.colorBar.addClass("themeColor"+this.theme);
this.$.box.removeClass("box-4-theme");
document.getElementById("png-button").style.visibility = "hidden";
document.getElementById("png-button").classList.remove("active");
document.getElementById("png-button").removeEventListener("click", this.exportPNG);
this.displayEntriesFrom(this.entry+1);
},
displayEntriesFrom: function(position) {
// Prepare header
this.cleanBox();
this.$.box.addClass("box-4-entry");
Abcd.changeVisibility(this, {home: false, back: true, pageCount: true, startSlideshow: true, stopSlideshow: false});
// Draw N entries
length = this.collections.length;
var count = 0;
for (var i = position ; i < length ; i++) {
this.$.box.createComponent({ kind: "Abcd.Entry", index:this.collections[i], ontap: "play", onEntrySoundEnded: "soundEnd"}, {owner: this}).render();
this.entry = i;
if (++count == entriesByScreen)
break;
}
// Change button visibility and counter
if (position != 0)
this.$.prev.show();
else
this.$.prev.hide();
if (this.entry+1 < length)
this.$.next.show();
else
this.$.next.hide();
this.$.pageCount.setContent(Math.ceil(i/entriesByScreen)+"/"+Math.ceil(length/entriesByScreen));
this.position = position-1;
document.getElementById("png-button").style.visibility = "visible";
document.getElementById("png-button").addEventListener("click", this.exportPNG);
// Save context
Abcd.saveContext();
},
displayNextEntries: function() {
if (this.entry+1 < this.collections.length) {
this.displayEntriesFrom(this.entry+1);
}
},
displayPrevEntries: function() {
this.displayEntriesFrom(Math.max(0,this.entry-entriesByScreen-this.entry%entriesByScreen));
},
backTaped: function() {
var current = this.$.box.getControls()[0];
this.entry = -1;
if (current.kind == "Abcd.Entry" && this.theme != 4) {
this.collection = -1;
this.displayCollections({index: this.theme});
this.$.box.removeClass("box-4-entry");
return;
}
this.$.colorBar.removeClass("themeColor"+this.theme);
this.$.box.removeClass("box-4-collection");
this.displayThemes();
},
// Delete all the box content
cleanBox: function() {
var items = [];
enyo.forEach(this.$.box.getControls(), function(item) {
items.push(item);
});
for (var i = 0 ; i < items.length ; i++) {
items[i].destroy();
}
},
// Slideshow handling
startSlideshow: function(inSender, inObject) {
this.slideshowIndex = 0;
Abcd.changeVisibility(this, {startSlideshow: false, stopSlideshow: true});
var first = this.$.box.getControls()[this.slideshowIndex];
this.play(first);
},
stopSlideshow: function(inSender, inObject) {
this.slideshowIndex = -1;
Abcd.changeVisibility(this, {startSlideshow: true, stopSlideshow: false});
},
// Play entry sound
play: function(inSender, inObject) {
if (this.playing != null)
this.playing.abort();
this.playing = inSender;
var tojournal = inSender.tojournal;
inSender.play(Abcd.sound);
if (tojournal) {
this.putEntriesJournalModeTo(false);
}
},
soundEnd: function(inSender, inObject) {
this.playing = null;
if (this.slideshowIndex != -1) {
var next = this.$.box.getControls()[++this.slideshowIndex];
if (next !== undefined)
this.play(next);
else
this.stopSlideshow();
}
}
});