Skip to content
Permalink
Newer
Older
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
8
settings.init = function() {
June 22, 2021 22:00
9
this.initialized = false;
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";
23
else if (navigatorLanguage.indexOf("ar") != -1)
24
this.language = "ar";
April 5, 2015 17:58
25
else if (navigatorLanguage.indexOf("ja") != -1)
26
this.language = "ja";
January 31, 2016 22:25
27
else if (navigatorLanguage.indexOf("pl") != -1)
August 28, 2016 16:21
28
this.language = "pl";
29
else if (navigatorLanguage.indexOf("pt") != -1)
30
this.language = "pt";
32
this.options = {stats: true, sync: true};
33
this.connected = false;
34
this.server = null;
35
this.networkId = null;
36
this.privateJournal = null;
37
this.sharedJournal = null;
39
this.scrollValue = 0;
40
}
41
settings.init();
August 28, 2016 16:21
42
43
// Load settings from local storage
44
settings.load = function(then) {
45
// Load settings
46
var that = this;
47
datastore.localStorage.load(function() {
48
var preferences = datastore.localStorage.getValue('sugar_settings');
49
if (preferences == null || preferences.name === undefined) {
August 28, 2016 16:21
50
if (then) {
June 22, 2021 22:00
51
then(null);
August 28, 2016 16:21
52
}
53
return;
54
}
June 22, 2021 22:00
55
that.initialized = true;
56
that.name = preferences.name;
57
that.color = preferences.color;
58
that.colorvalue = preferences.colorvalue;
59
that.view = preferences.view;
60
that.scrollValue = preferences.scrollValue;
August 28, 2016 16:21
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
}
November 4, 2017 22:39
82
if (preferences.options !== undefined) {
83
that.options = preferences.options;
84
}
August 28, 2016 16:21
85
86
// Set language
August 28, 2016 16:21
87
l10n.language.code = that.language;
88
89
if (then) {
June 22, 2021 22:00
90
then(preferences);
August 28, 2016 16:21
91
}
93
};
August 28, 2016 16:21
94
95
// Save settings to local storage
96
settings.save = function() {
December 16, 2013 21:30
97
datastore.localStorage.setValue('sugar_settings', {
98
name: this.name,
99
color: this.color,
November 4, 2017 22:39
100
options: this.options,
December 16, 2013 21:30
101
colorvalue: xoPalette.colors[this.color],
June 22, 2021 22:00
102
activities: activities.get(),
103
view: app.getView(),
104
language: this.language,
105
connected: this.connected,
106
server: this.server,
107
networkId: this.networkId,
108
privateJournal: this.privateJournal,
109
sharedJournal: this.sharedJournal,
110
token: this.token,
111
scrollValue: this.scrollValue
112
});
September 3, 2016 16:03
113
};
August 28, 2016 16:21
114
115
// Save settings to the server
October 21, 2017 18:15
116
settings.saveToServer = function(remoteserver, then, error) {
117
if (this.networkId != null) {
118
var that = this;
119
remoteserver.putUser(
120
this.networkId,
121
{
August 28, 2016 16:21
122
name: that.name,
123
color: xoPalette.colors[that.color],
124
language: that.language,
November 4, 2017 22:39
125
options: that.options,
June 22, 2021 22:00
126
favorites: activities.getFavoritesName()
127
},
128
function(inSender, inResponse) {
August 28, 2016 16:21
129
if (then) {
130
then();
131
}
132
},
October 21, 2017 18:15
133
function(inResponse, inCode) {
134
console.log("WARNING: Error updating network user");
October 21, 2017 18:15
135
if (error) {
136
error(inResponse, inCode);
August 28, 2016 16:21
137
}
138
}
139
);
140
}
August 28, 2016 16:21
141
else {
142
if (then) {
143
then();
144
}
145
}
146
};
August 28, 2016 16:21
147
148
// Merge current settings with the one in parameter
149
settings.merge = function(preferences) {
150
var changed = false;
June 22, 2021 22:00
151
this.initialized = true;
152
if (preferences.name !== undefined && preferences.name != this.name) { this.name = preferences.name; changed = true; }
July 22, 2017 22:26
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; }
159
if (preferences.favorites !== undefined) {
June 22, 2021 22:00
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;
164
for (var j = 0 ; j < preferences.favorites.length ; j++) {
June 22, 2021 22:00
165
if (preferences.favorites[j] == list[i].id) {
166
list[i].favorite = true;
August 28, 2016 16:21
167
}
June 22, 2021 22:00
169
changed = changed || (wasfavorite != list[i].favorite);
February 11, 2023 22:42
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; }
November 4, 2017 22:39
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
}
186
return changed;
August 28, 2016 16:21
188
November 30, 2016 21:56
189
// Reset settings
October 14, 2017 15:19
190
settings.reset = function(full) {
November 30, 2016 21:56
191
datastore.localStorage.removeValue('sugar_settings');
October 14, 2017 15:19
192
if (full) {
May 25, 2021 22:21
193
historic.clean();
May 26, 2021 22:29
194
stats.clean();
October 14, 2017 15:19
195
}
November 30, 2016 21:56
196
};
197
198
// Get properties
199
settings.getName = function() {
200
return this.name;
201
};
202
settings.getColor = function() {
203
return xoPalette.colors[this.color];
204
};
December 16, 2013 21:30
205
settings.getView = function() {
206
return this.view;
207
};
208
settings.getLanguage = function() {
209
return this.language;
210
};
211
settings.getActivities = function() {
June 22, 2021 22:00
212
return activities.get();
214
settings.isConnected = function() {
215
return this.connected || util.getClientType() == constant.webAppType;
June 22, 2021 22:00
217
settings.isUserConnected = function() {
218
return this.connected;
219
};
220
settings.isInitialized = function() {
221
return this.initialized;
222
};
223
settings.getServer = function() {
224
return this.server;
225
};
226
settings.getNetworkId = function() {
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
};
239
settings.getToken = function() {
240
return this.token;
241
};
November 4, 2017 22:39
242
settings.getOptions = function(name) {
243
if (!this.options) return undefined;
244
return this.options[name];
245
}
246
settings.getScrollValue = function() {
247
return this.scrollValue;
248
};
August 28, 2016 16:21
249
250
// Set properties
251
settings.setName = function(newname) {
252
this.name = newname;
253
};
254
settings.setColor = function(newcolor) {
August 28, 2016 16:21
255
if (newcolor >= 0 && newcolor < xoPalette.colors.length) {
256
this.color = newcolor;
August 28, 2016 16:21
257
}
259
settings.setLanguage = function(newlanguage) {
260
this.language = newlanguage;
261
};
262
settings.setActivities = function(list) {
June 22, 2021 22:00
263
activities.set(list);
264
};
265
settings.setConnected = function(connected) {
266
this.connected = connected;
267
};
268
settings.setServer = function(server) {
269
this.server = server;
270
};
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
};
280
settings.setToken = function(token) {
281
this.token = token;
282
};
November 4, 2017 22:39
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
};
August 28, 2016 16:21
290
291
// Color playing
292
settings.nextColor = function() {
293
this.color = (this.color+1)%xoPalette.colors.length;
294
};
August 28, 2016 16:21
295
296
// Activity handling
297
settings.runActivity = function(activity, objectId, name, sharedId, help) {
August 28, 2016 16:21
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();
August 28, 2016 16:21
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) {
August 19, 2017 22:18
313
objectId = activity.instances[0].objectId;
314
location = location + "&o="+objectId + "&n="+activity.instances[0].metadata.title;
August 28, 2016 16:21
315
} else {
316
location = location + "&n=" +activity.name;
317
}
318
} else if (objectId != null) {
319
location = location + "&o=" + objectId + "&n="+name;
320
} else {
321
location = location + "&n=" +activity.name;
322
}
November 13, 2014 22:48
323
if (sharedId) {
324
location = location + "&s=" + sharedId;
325
}
326
if (help) {
327
location = location + "&h=1";
328
}
329
stats.trace(constant.viewNames[app.getView()==constant.assignmentView?constant.journalView:app.getView()], (objectId ? 're' : '') + 'launch_activity', activity.id, objectId);
330
window.location = location;
331
};
332
return settings;
333
});