forked from llaske/sugarizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.js
155 lines (138 loc) · 4.49 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
// Entry component with image, text and sound
enyo.kind({
name: "Abcd.Entry",
kind: "Abcd.Item",
published: { index: "", imageonly: false, textonly: false, soundonly: false, tojournal: false },
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");
},
// Play sound using the media
play: function(media) {
// Journal mode, generate image in journal
if (this.tojournal) {
// 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),
activity: "org.olpcfrance.MediaViewerActivity",
timestamp: new Date().getTime(),
creation_time: new Date().getTime(),
file_size: 0
};
Abcd.datastore.create(metadata, function() {
console.log("image '"+__$FC(entry.text)+"' saved in journal.")
}, imgAsDataURL);
// Update entry icon
this.tojournal = false;
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");
}
});