Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
122 lines (108 loc) · 3.25 KB
/
Copy pathapp.js
File metadata and controls
122 lines (108 loc) · 3.25 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
define(function(require) {
'use strict';
var Tabs = require('tabs');
var View = require('view');
var Panel = require('panel');
var TimerPanel = require('timer_panel');
var StopwatchPanel = require('stopwatch_panel');
var mozL10n = require('l10n');
var rAF = mozRequestAnimationFrame || requestAnimationFrame;
/**
* Global Application event handling and paging
*/
var App = {
panelClass: {
'alarm-panel': Panel,
'alarm-edit-panel': Panel,
'timer-panel': TimerPanel,
'stopwatch-panel': StopwatchPanel
},
/**
* Load the Tabs and Panels, attach events and navigate to the default view.
*/
init: function() {
this.tabs = new Tabs(document.getElementById('clock-tabs'));
this.tabs.on('selected', this.navigate.bind(this));
window.addEventListener('hashchange', this);
window.addEventListener('localized', this);
window.addEventListener('visibilitychange', this);
// we wait for the app to be l10n ready before initializing, so call
// the onlocalized once at startup
this.onlocalized();
this.visible = !document.hidden;
this.panels = Array.prototype.map.call(
document.querySelectorAll('.panel'),
function(element) {
return View.instance(element, App.panelClass[element.id] || Panel);
}
);
this.navigate({ hash: '#alarm-panel' });
return this;
},
/**
* split each event handler into it's own method
*/
handleEvent: function(event) {
var handler = this['on' + event.type];
if (handler) {
return handler.apply(this, arguments);
}
},
/**
* navigate between pages.
*
* @param {object} data Options for navigation.
* @param {string} data.hash The hash of the panel id. I.E. '#alarm-panel'.
*/
navigate: function(data) {
var currentIndex = this.panels.indexOf(this.currentPanel);
this.panels.forEach(function(panel, panelIndex) {
if ('#' + panel.id === data.hash) {
panel.active = true;
panel.visible = true;
if (currentIndex !== -1) {
var direction = currentIndex < panelIndex;
rAF(function startAnimation(oldPanel) {
panel.transition =
direction ? 'slide-in-right' : 'slide-in-left';
oldPanel.transition =
direction ? 'slide-out-left' : 'slide-out-right';
}.bind(null, this.currentPanel));
}
this.currentPanel = panel;
} else {
panel.active = false;
}
}, this);
this.currentHash = data.hash;
},
/**
* Navigate to the new hash.
*/
onhashchange: function(event) {
if (this.currentHash === location.hash) {
return;
}
this.navigate({ hash: location.hash });
},
/**
* Reset the global localization params on the html element. Called when
* the language changes, and once on application startup.
*/
onlocalized: function(event) {
document.documentElement.lang = mozL10n.language.code;
document.documentElement.dir = mozL10n.language.direction;
},
/**
* Whenever the application gains/loses focus, inform the current panel of
* its visibility loss.
*/
onvisibilitychange: function(event) {
this.visible = !document.hidden;
if (this.currentPanel) {
this.currentPanel.visible = this.visible;
}
}
};
return App;
});