forked from llaske/sugarizer
-
Notifications
You must be signed in to change notification settings - Fork 58
/
entry.js
212 lines (192 loc) · 6 KB
/
entry.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
// Entry component with image, text and sound
enyo.kind({
name: "Abcd.Entry",
kind: "Abcd.Item",
published: { index: "", imageonly: false, textonly: false, soundonly: false, tojournal: 0 },
classes: "entry",
components: [
{ name: "spinner", kind: "Image", src: "images/spinner-light.gif", classes: "spinner"},
{ name: "contentBox", showing: false, components: [
{ name: "itemImage", classes: "entryImage", kind: "Image", onload: "imageLoaded", onerror: "imageError" },
{ name: "soundIcon", kind: "Image", classes: "entrySoundIcon" },
{ name: "itemText", classes: "entryText" }
]},
{kind: "Signals", onEndOfSound: "endOfSound"}
],
events: {
onEntrySoundEnded: ""
},
// Constructor
create: function() {
this.inherited(arguments);
this.sound = null;
this.imageonlyChanged();
this.textonlyChanged();
this.soundonlyChanged();
this.indexChanged();
this.tojournalChanged();
},
// Display only when image is load
imageLoaded: function() {
if (this.index !== "") {
this.$.spinner.hide();
this.$.contentBox.show();
}
},
// Error loading image, probably lost connection to database
imageError: function() {
Abcd.goHome();
},
// Unique visibility options
imageonlyChanged: function() {
if (this.imageonly)
Abcd.changeVisibility(this, {itemImage: true, soundIcon: false, itemText: false});
},
textonlyChanged: function() {
if (this.textonly)
Abcd.changeVisibility(this, {itemImage: false, soundIcon: false, itemText: true});
},
soundonlyChanged: function() {
if (this.soundonly)
Abcd.changeVisibility(this, {itemImage: false, soundIcon: true, itemText: false});
},
tojournalChanged: function() {
if (this.tojournal) {
this.$.soundIcon.setSrc("icons/journal.svg");
} else {
this.$.soundIcon.setSrc("images/sound_off"+(this.soundonly?1:0)+".png");
}
this.$.soundIcon.render();
},
// Localization changed, update text & sound
setLocale: function() {
this.indexChanged();
this.inherited(arguments);
},
// Card setup
indexChanged: function() {
// Get content
var entry = Abcd.entries[this.index];
var image = Abcd.context.getDatabase()+"images/database/"+entry.code+".png";
var text = __$FC(entry.text);
if (Abcd.context.casevalue == 1)
text = text.toUpperCase();
// Get sound
if (this.soundonly) this.$.soundIcon.addClass("entrySoundIconOnly");
if (entry[Abcd.context.lang]) {
this.sound = Abcd.context.getDatabase()+"audio/"+Abcd.context.lang+"/database/"+entry.code;
this.$.soundIcon.setSrc("images/sound_off"+(this.soundonly?1:0)+".png");
} else {
this.sound = null;
this.$.soundIcon.setSrc("images/sound_none"+(this.soundonly?1:0)+".png");
}
// Display all
this.$.itemImage.setAttribute("src", image);
this.$.itemText.removeClass("entryText0");
this.$.itemText.removeClass("entryText1");
this.$.itemText.removeClass("entryText2");
this.$.itemText.addClass("entryText"+Abcd.context.casevalue);
if (this.imageonly) this.$.itemImage.addClass("entryImageOnly");
this.$.itemText.setContent(text);
if (this.textonly) this.$.itemText.addClass("entryTextOnly");
},
// Export to Image or Sound
exportToImage: function() {
// Get image into a canvas
var entry = Abcd.entries[this.index];
var image = this.$.itemImage.hasNode();
var imgCanvas = document.createElement("canvas");
var imgContext = imgCanvas.getContext("2d");
imgCanvas.width = imgCanvas.height = 210;
imgContext.drawImage(image, 0, 0, imgCanvas.width, imgCanvas.height);
var imgAsDataURL = imgCanvas.toDataURL("image/png");
// Save in datastore
var metadata = {
mimetype: "image/png",
title: __$FC(entry.text)+".png",
activity: "org.olpcfrance.MediaViewerActivity",
timestamp: new Date().getTime(),
creation_time: new Date().getTime(),
file_size: 0
};
Abcd.datastore.create(metadata, function() {
requirejs(["l10n"], function (l10n) {
Abcd.humane.log(l10n.get('AbecedariumImage'));
});
console.log("image '"+__$FC(entry.text)+"' saved in journal.")
}, imgAsDataURL);
},
exportToSound: function() {
// Compute format
var format = "";
var mimetype = ""
format = ".mp3";
mimetype = "audio/mpeg";
var url = "";
if (this.sound.indexOf("http") == 0) {
url = this.sound+format;
} else {
url = window.location.href;
url = url.substring(0, url.indexOf('/index.html'));
url += "/"+this.sound+format;
}
// Load file
var request = new XMLHttpRequest();
request.open("GET",url,true);
request.setRequestHeader("Content-type",mimetype);
request.responseType = "arraybuffer";
var that = this;
request.onload = function() {
if (request.status == 200 || request.status == 0) {
var entry = Abcd.entries[that.index];
var blob = new Uint8Array(this.response);
var base64 = "data:"+mimetype+";base64,"+Abcd.toBase64(blob);
var metadata = {
mimetype: mimetype,
title: __$FC(entry.text)+format,
activity: "org.olpcfrance.MediaViewerActivity",
timestamp: new Date().getTime(),
creation_time: new Date().getTime(),
file_size: 0
};
Abcd.datastore.create(metadata, function() {
requirejs(["l10n"], function (l10n) {
Abcd.humane.log(l10n.get('AbecedariumSound'));
});
console.log("sound '"+__$FC(entry.text)+"' saved in journal.");
}, base64);
}
};
request.send();
},
// Play sound using the media
play: function(media) {
// Journal mode, generate image/sound in journal
if (this.tojournal) {
if (this.tojournal == 1) {
this.exportToImage();
} else {
this.exportToSound();
}
// Update entry icon
this.tojournal = 0;
this.indexChanged();
return;
}
// Play sound
if (this.sound != null) {
this.$.soundIcon.setSrc("images/sound_on"+(this.soundonly?1:0)+".png");
media.play(this.sound);
}
},
endOfSound: function(e, s) {
if (s.sound == this.sound) {
this.doEntrySoundEnded();
this.$.soundIcon.setSrc("images/sound_off"+(this.soundonly?1:0)+".png");
}
},
abort: function() {
if (this.$.soundIcon !== undefined)
this.$.soundIcon.setSrc("images/sound_off"+(this.soundonly?1:0)+".png");
}
});