Permalink
Newer
100644
333 lines (320 sloc)
9.64 KB
1
2
3
// Settings handling
4
define(["sugar-web/datastore"], function (datastore) {
5
var settings = {};
6
7
// Default value
10
this.name = "<No name>";
11
this.color = 22;
12
this.colorvalue = null;
13
this.view = 0;
14
this.language = "en";
15
var navigatorLanguage = navigator.language;
16
if (navigatorLanguage) {
17
if (navigatorLanguage.indexOf("fr") != -1)
18
this.language = "fr";
19
else if (navigatorLanguage.indexOf("es") != -1)
20
this.language = "es";
21
else if (navigatorLanguage.indexOf("de") != -1)
22
this.language = "de";
34
this.server = null;
35
this.networkId = null;
36
this.privateJournal = null;
37
this.sharedJournal = null;
46
var that = this;
47
datastore.localStorage.load(function() {
48
var preferences = datastore.localStorage.getValue('sugar_settings');
49
if (preferences == null || preferences.name === undefined) {
56
that.name = preferences.name;
57
that.color = preferences.color;
58
that.colorvalue = preferences.colorvalue;
59
that.view = preferences.view;
61
if (preferences.language !== undefined) {
62
that.language = preferences.language;
63
}
64
if (preferences.connected !== undefined) {
65
that.connected = preferences.connected;
66
}
67
if (preferences.server !== undefined) {
68
that.server = preferences.server;
69
}
70
if (preferences.networkId !== undefined) {
71
that.networkId = preferences.networkId;
72
}
73
if (preferences.privateJournal !== undefined) {
74
that.privateJournal = preferences.privateJournal;
75
}
76
if (preferences.sharedJournal !== undefined) {
77
that.sharedJournal = preferences.sharedJournal;
78
}
79
if (preferences.token !== undefined) {
80
that.token = preferences.token;
81
}
82
if (preferences.options !== undefined) {
83
that.options = preferences.options;
84
}
97
datastore.localStorage.setValue('sugar_settings', {
98
name: this.name,
99
color: this.color,
108
privateJournal: this.privateJournal,
109
sharedJournal: this.sharedJournal,
117
if (this.networkId != null) {
118
var that = this;
119
remoteserver.putUser(
120
this.networkId,
121
{
148
// Merge current settings with the one in parameter
149
settings.merge = function(preferences) {
150
var changed = false;
152
if (preferences.name !== undefined && preferences.name != this.name) { this.name = preferences.name; changed = true; }
153
if (this.colorvalue == null || preferences.color !== undefined && (preferences.color.fill != this.colorvalue.fill || preferences.color.stroke != this.colorvalue.stroke)) {
154
this.colorvalue = preferences.color;
155
this.color = util.getColorIndex(this.colorvalue);
156
changed = true;
157
}
158
if (preferences.language !== undefined && preferences.language != this.language) { this.language = preferences.language; changed = true; }
160
var list = activities.get();
161
for(var i = 0 ; i < list.length ; i++) {
162
var wasfavorite = list[i].favorite;
163
list[i].favorite = false;
171
} else {
172
changed = true; // HACK: Force change to save current favorites list
174
if (preferences.server !== undefined && preferences.server != this.server) { this.server = preferences.server; changed = true; }
175
if (preferences.networkId !== undefined && preferences.networkId != this.networkId) { this.networkId = preferences.networkId; changed = true; }
176
if (preferences.private_journal !== undefined && preferences.private_journal != this.privateJournal) { this.privateJournal = preferences.private_journal; }
177
if (preferences.shared_journal !== undefined && preferences.shared_journal != this.sharedJournal) { this.sharedJournal = preferences.shared_journal; }
178
if (preferences.options !== undefined && this.options !== undefined) {
179
for(var prop in preferences.options) {
180
if (!this.options.hasOwnProperty(prop) || (this.options[prop] != preferences.options[prop])) {
181
this.options[prop] = preferences.options[prop];
182
changed = true;
183
}
184
}
185
}
198
// Get properties
199
settings.getName = function() {
200
return this.name;
201
};
202
settings.getColor = function() {
203
return xoPalette.colors[this.color];
204
};
217
settings.isUserConnected = function() {
218
return this.connected;
219
};
220
settings.isInitialized = function() {
221
return this.initialized;
222
};
227
var token = this.token;
228
if (!token || !token.x_key) {
229
return null;
230
}
231
return token.x_key;
232
};
233
settings.getPrivateJournal = function() {
234
return this.privateJournal;
235
};
236
settings.getSharedJournal = function() {
237
return this.sharedJournal;
238
};
242
settings.getOptions = function(name) {
243
if (!this.options) return undefined;
244
return this.options[name];
245
}
250
// Set properties
251
settings.setName = function(newname) {
252
this.name = newname;
253
};
254
settings.setColor = function(newcolor) {
259
settings.setLanguage = function(newlanguage) {
260
this.language = newlanguage;
261
};
265
settings.setConnected = function(connected) {
266
this.connected = connected;
267
};
271
settings.setNetworkId = function(nid) {
272
this.networkId = nid;
273
};
274
settings.setPrivateJournal = function(privateJournal) {
275
this.privateJournal = privateJournal;
276
};
277
settings.setSharedJournal = function(sharedJournal) {
278
this.sharedJournal = sharedJournal;
279
};
283
settings.setOptions = function(optionName, optionValue) {
284
if (!this.options) { this.options = {} }
285
this.options[optionName] = optionValue;
286
};
287
settings.setScrollValue = function (scrollBarPosition) {
288
this.scrollValue = scrollBarPosition;
289
};
291
// Color playing
292
settings.nextColor = function() {
293
this.color = (this.color+1)%xoPalette.colors.length;
294
};
298
if (activity.type != null && activity.type == "native") {
299
if (sugarizerOS) {
300
sugarizerOS.runActivity(activity.id);
301
sugarizerOS.addApplicationToJournal(sugarizerOS.log, activity, datastore);
302
return;
303
}
304
console.log("No sugarizerOS instance found");
305
}
306
if (activity.activityId == null) {
307
activity.activityId = datastore.createUUID();
308
}
309
this.save();
310
var location = activity.directory+"/index.html?aid="+activity.activityId+"&a="+activity.id;
311
if (objectId === undefined) {
312
if (activity.instances != null && activity.instances.length > 0) {
313
objectId = activity.instances[0].objectId;
314
location = location + "&o="+objectId + "&n="+activity.instances[0].metadata.title;
319
location = location + "&o=" + objectId + "&n="+name;
320
} else {
321
location = location + "&n=" +activity.name;
329
stats.trace(constant.viewNames[app.getView()==constant.assignmentView?constant.journalView:app.getView()], (objectId ? 're' : '') + 'launch_activity', activity.id, objectId);