Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
526 lines (430 loc) · 12.9 KB
/
Copy pathapp.js
File metadata and controls
526 lines (430 loc) · 12.9 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
Calendar.App = (function(window) {
function PendingManager() {
this.objects = [];
this.pending = 0;
this.>
this.>
}
PendingManager.prototype = {
onpending: function() {},
oncomplete: function() {},
register: function(object) {
object.on(object.startEvent, this.onstart);
object.on(object.completeEvent, this.onend);
var wasPending = this.isPending();
this.objects.push(object);
if (object.pending) {
this.pending++;
if (!wasPending) {
this.onpending();
}
}
},
/**
* Unregister an object.
* Note it is intended that objects that
* are unregistered are never in a state
* where we are waiting for their pending
* status to complete. If an incomplete
* object is removed it will break .pending.
*/
unregister: function(object) {
var idx = this.objects.indexOf(object);
if (idx !== -1) {
var object = this.objects[idx];
this.objects.splice(idx, 1);
return true;
}
return false;
},
isPending: function() {
var len = this.objects.length;
var i = 0;
for (; i < len; i++) {
if (this.objects[i].pending)
return true;
}
return false;
},
onstart: function() {
if (!this.pending) {
this.onpending();
}
this.pending++;
},
onend: function() {
this.pending--;
if (!this.pending) {
this.oncomplete();
}
}
};
var DateL10n = {
/**
* Localizes all elements with data-l10n-date-format.
*/
localizeElements: function(parent) {
var elements = document.querySelectorAll(
'[data-l10n-date-format]'
);
var len = elements.length;
var i = 0;
for (; i < len; i++) {
DateL10n.localizeElement(elements[i]);
}
},
/**
* Localize a single element expected to have data-l10n-date-format.
*/
localizeElement: function(element) {
var date = element.dataset.date;
var formatKey = element.dataset.l10nDateFormat;
var format = navigator.mozL10n.get(formatKey);
if (date) {
element.textContent = Calendar.App.dateFormat.localeFormat(
new Date(date),
format
);
}
}
};
/**
* Focal point for state management
* within calendar application.
*
8000
* Contains tools for routing and central
* location to reference database.
*/
var App = {
PendingManager: PendingManager,
DateL10n: DateL10n,
//XXX: always assumes that app is never lazy loaded
startingURL: window.location.href,
_location: window.location,
_mozTimeRefreshTimeout: 3000,
pendingClass: 'pending-operation',
/**
* Entry point for application
* must be called at least once before
* using other methods.
*/
configure: function(db, router) {
this.db = db;
this.router = router;
this._providers = Object.create(null);
this._views = Object.create(null);
this._routeViewFn = Object.create(null);
this._pendingManger = new PendingManager();
var self = this;
this._pendingManger. onpending() {
document.body.classList.remove(self.pendingClass);
};
this._pendingManger. oncomplete() {
document.body.classList.add(self.pendingClass);
};
this.timeController = new Calendar.Controllers.Time(this);
this.syncController = new Calendar.Controllers.Sync(this);
this.serviceController = new Calendar.Controllers.Service(this);
this.alarmController = new Calendar.Controllers.Alarm(this);
this.errorController = new Calendar.Controllers.Error(this);
// observe sync events
this.observePendingObject(this.syncController);
},
/**
* Observes localized events and localizes elements
* with data-l10n-date-format should be registered
* after the first localized event.
*
*
* Example:
*
*
* <span
* data-date="Wed Jan 09 2013 19:25:38 GMT+0100 (CET)"
* data-l10n-date-format="%x">
*
* 2013/9/19
*
* </span>
*
*/
observeDateLocalization: function() {
window.addEventListener('localized', DateL10n.localizeElements);
},
/**
* Adds observers to objects capable of being pending.
*
* Object must emit some kind of start/complete events
* and have the following properties:
*
* - startEvent (used to register an observer)
* - endEvent ( ditto )
* - pending
*
* @param {Object} object to observe.
*/
observePendingObject: function(object) {
this._pendingManger.register(object);
},
isPending: function() {
return this._pendingManger.isPending();
},
loadObject: function initializeLoadObject(name, callback) {
function loadObject(name, callback) {
this._loader.load('group', name, callback);
}
if (!this._pendingObjects) {
this._pendingObjects = [[name, callback]];
} else {
this._pendingObjects.push([name, callback]);
return;
}
// Loading NotAnd and the load config is not really needed
// for the initial load so we lazily load them the first time we
// need to load a file...
var self = this;
function next() {
// initialize loader
NotAmd.nextTick = Calendar.nextTick;
self._loader = NotAmd(Calendar.LoadConfig);
self.loadObject = loadObject;
// begin processing existing requests
self._pendingObjects.forEach(function(pair) {
// ['ObjectName', function() { ... }]
loadObject.call(self, pair[0], pair[1]);
});
delete self._pendingObjects;
}
LazyLoader.load(['/js/ext/notamd.js', '/js/load_config.js'], next);
},
/**
* Internally restarts the application.
*/
forceRestart: function() {
if (!this.restartPending) {
this.restartPending = true;
this._location.href = this.startingURL;
}
},
/**
* Navigates app to a new location.
*
* @param {String} url new view url.
*/
go: function(url) {
this.router.show(url);
},
/**
* Shortcut for app.router.state
*/
state: function() {
this.router.state.apply(this.router, arguments);
},
/**
* Shortcut for app.router.modifier
*/
modifier: function() {
this.router.modifier.apply(this.router, arguments);
},
/**
* Shortcut for app.router.resetState
*/
resetState: function() {
this.router.resetState();
},
_routes: function() {
/* routes */
this.state('/week/', 'Week');
this.state('/day/', 'Day');
this.state('/month/', ['Month', 'MonthsDay']);
this.modifier('/settings/', 'Settings', { clear: false });
this.modifier('/advanced-settings/', 'AdvancedSettings');
this.state('/alarm-display/:id', 'ViewEvent', { path: false });
this.state('/event/add/', 'ModifyEvent');
this.state('/event/edit/:id', 'ModifyEvent');
this.state('/event/show/:id', 'ViewEvent');
this.modifier('/select-preset/', 'CreateAccount');
this.modifier('/create-account/:preset', 'ModifyAccount');
this.modifier('/update-account/:id', 'ModifyAccount');
this.router.start();
var pathname = window.location.pathname;
// default view
if (pathname === '/index.html' || pathname === '/') {
this.go('/month/');
}
},
_init: function() {
var self = this;
// quick hack for today button
var today = document.querySelector('#view-selector .today');
today.addEventListener('click', function(e) {
var date = new Date();
self.timeController.move(date);
self.timeController.selectedDay = date;
e.preventDefault();
});
this.dateFormat = navigator.mozL10n.DateTimeFormat();
// re-localize dates on screen
this.observeDateLocalization();
this.timeController.observe();
this.alarmController.observe();
// turn on the auto queue this means that when
// alarms are added to the database we manage them
// transparently. Defaults to off for tests.
this.store('Alarm').autoQueue = true;
this.timeController.move(new Date());
this.view('TimeHeader', function(header) {
header.render();
});
this.view('CalendarColors', function(colors) {
colors.render();
});
document.body.classList.remove('loading');
this._routes();
//lazy load recurring event expander so as not to impact initial load.
this.loadObject('Controllers.RecurringEvents', function() {
self.recurringEventsController =
new Calendar.Controllers.RecurringEvents(self);
self.observePendingObject(
self.recurringEventsController
);
self.recurringEventsController.observe();
});
// go ahead and show the first time use view if necessary
this.view('FirstTimeUse', function(firstTimeUse) {
firstTimeUse.doFirstTime();
});
setTimeout(function nextTick() {
this.view('Errors');
}.bind(this), 0);
},
/**
* Primary code for app can go here.
*/
init: function() {
var self = this;
var pending = 2;
function next() {
pending--;
if (!pending) {
self._init();
}
}
if (!this.db) {
this.configure(
new Calendar.Db('b2g-calendar'),
new Calendar.Router(page)
);
}
// start the workers
this.serviceController.start(false);
// localize && pre-initialize the database
if (navigator.mozL10n && (navigator.mozL10n.readyState == 'interactive' ||
navigator.mozL10n.readyState == 'complete')) {
// document is already localized
next();
} else {
// waiting for the document to be localized (= standard case)
window.addEventListener('localized', function() {
next();
});
}
this.db.load(function() {
next();
});
},
/**
* Initializes a provider.
*/
provider: function(name) {
if (!(name in this._providers)) {
this._providers[name] = new Calendar.Provider[name]({
app: this
});
}
return this._providers[name];
},
_initView: function(name) {
this._views[name] = new Calendar.Views[name]({
app: this
});
},
/**
* Initializes a view and stores
* a internal reference so when
* view is called a second
* time the same view is used.
*
* Makes an asynchronous call to
* load the script if we do not
* have the view cached.
*
* // for example if you have
* // a calendar view Foo
*
* Calendar.Views.Foo = Klass;
*
* app.view('Foo', function(view) {
* (view instanceof Calendar.Views.Foo) === true
* });
*
* @param {String} name view name.
* @param {Function} view loaded callback.
*/
view: function(name, cb) {
var self = this;
if (!(name in this._views)) {
if (name in Calendar.Views) {
this._initView(name);
if (cb) {
cb.call(self, self._views[name]);
}
} else {
this.loadObject('Views.' + name, function() {
self._initView(name);
if (cb) {
cb.call(self, self._views[name]);
}
});
}
} else if (cb) {
Calendar.nextTick(function() {
cb.call(self, self._views[name]);
});
}
},
/**
* Pure convenience function for
* referencing a object store.
*
* @param {String} name store name. (e.g events).
* @return {Calendar.Store.Abstact} store.
*/
store: function(name) {
return this.db.getStore(name);
},
/**
* Returns the offline status.
*/
offline: function() {
return (navigator && 'onLine' in navigator) ? !navigator.onLine : true;
}
};
// Restart the calendar when the timezone changes.
// We do this on a timer because this event may fire
// many times. Refreshing the url of the calendar frequently
// can result in crashes so we attempt to do this only after
// the user has completed their selection.
var _changeTimerId;
window.add
3C6D
EventListener('moztimechange', function onMozTimeChange() {
clearTimeout(_changeTimerId);
_changeTimerId = setTimeout(function() {
App.forceRestart();
}, App._mozTimeRefreshTimeout);
});
window.addEventListener('load', function onLoad() {
window.removeEventListener('load', onLoad);
App.init();
});
return App;
}(this));
You can’t perform that action at this time.