-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodules-forceCanvasSystem-hardwareEvents.js
196 lines (180 loc) · 6.21 KB
/
modules-forceCanvasSystem-hardwareEvents.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
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
(function() {
/**
* window.force.modules.forceCanvasSystem.hardwareEvents
*
* usage: for the time being only the back button is managed:
* hardwareEvents.propagateBackTo(obj, deviceRange)
* where obj is a ForceSystem entity
* and deviceRange can be null/empty (= triggered on all devices supported) or equal to: 'tizen' or 'desktopBrowser'
* => propagate a 'hardwareBackKeyDown' event
*/
var mod = {};
// internal variables
var systemEventsTracked = {};
var keysTracked = null;
var documentTracked = null;
// listners
var listners = {
'window.keydown': function keydown(evt) {
if (keysTracked && evt.keyCode) {
if (keysTracked.back && evt.keyCode === 37) {
triggers.back();
} else if (keysTracked.menu && evt.keyCode === 36) {
triggers.menu();
}
}
},
'window.tizenhwkey': function tizenhwkey(evt) {
if (keysTracked && evt.keyName) {
if (keysTracked.back && evt.keyName === 'back') {
triggers.back();
} else if (keysTracked.menu && evt.keyName === 'menu') {
triggers.menu();
}
}
},
'window.keypressed': function keypressed(evt) {
if (keysTracked && evt.keyCode) {
// nothing checked on key press yet
}
},
'document.webkitvisibilitychange': function webkitvisibilitychange(evt) {
if (documentTracked && document.webkitHidden !== undefined) {
if (documentTracked.applicationHided && document.webkitHidden) {
triggers.applicationHided();
} else if (documentTracked.applicationShown && !document.webkitHidden) {
triggers.applicationShown();
}
}
}
};
// triggers
var triggers = {
back: function back() {
for (var i = 0, l = keysTracked.back.length; i < l; i++) {
keysTracked.back[i]._on({type: 'hardwareBackKeyDown'});
}
},
menu: function() {
for (var i = 0, l = keysTracked.menu.length; i < l; i++) {
keysTracked.menu[i]._on({type: 'hardwareMenuKeyDown'});
}
},
applicationHided: function() {
for (var i = 0, l = documentTracked.applicationHided.length; i < l; i++) {
documentTracked.applicationHided[i]._on({type: 'applicationHided'});
}
},
applicationShown: function() {
for (var i = 0, l = documentTracked.applicationShown.length; i < l; i++) {
documentTracked.applicationShown[i]._on({type: 'applicationShown'});
}
}
};
// -------------------------------------------
// Registering generic top level behaviors
// -------------------------------------------
function registerSystemEvent(target, eventCode) {
var targetString;
if (target === window) {
targetString = 'window';
} else if (target === window.document) {
targetString = 'document';
} else {
target = window;
targetString = 'window';
}
var fullCode = targetString + '.' + eventCode;
if (!systemEventsTracked[fullCode]) {
systemEventsTracked[fullCode] = target.addEventListener(eventCode, listners[fullCode], false);
}
}
// -------------------------------------------
// Registering specifics behaviors
// -------------------------------------------
function registerKeyTracked(keyName, obj) {
if (!keysTracked) {
keysTracked = {};
}
if (!keysTracked[keyName]) {
keysTracked[keyName] = [];
}
keysTracked[keyName].push(obj);
}
function registerDocumentEventTracked(forceEventName, obj) {
if (!documentTracked) {
documentTracked = {};
}
if (!documentTracked[forceEventName]) {
documentTracked[forceEventName] = [];
}
documentTracked[forceEventName].push(obj);
}
// -------------------------------------------
// Exposed methods
// -------------------------------------------
mod.propagateBackBtnTo = function(obj, deviceRange) {
if (window.force.modules.environment.isTizen() && (!deviceRange || deviceRange.tizen)) {
registerSystemEvent(window, 'tizenhwkey');
registerKeyTracked('back', obj);
}
else if (window.force.modules.environment.isDesktopBrowser() && (!deviceRange || deviceRange.desktopBrowser)) {
registerSystemEvent(window, 'keydown'); // direction keys are only detect on keydown, not on keypress
registerKeyTracked('back', obj);
}
else {
// do nothing
}
};
mod.propagateMenuBtnTo = function(obj, deviceRange) {
if (window.force.modules.environment.isTizen() && (!deviceRange || deviceRange.tizen)) {
registerSystemEvent(window, 'tizenhwkey');
registerKeyTracked('menu', obj);
}
else if (window.force.modules.environment.isDesktopBrowser() && (!deviceRange || deviceRange.desktopBrowser)) {
registerSystemEvent(window, 'keydown');
registerKeyTracked('menu', obj);
}
else {
// do nothing
}
};
mod.propagateAppHideTo = function(obj, deviceRange) {
if (window.force.modules.environment.isTizen() && (!deviceRange || deviceRange.tizen)) {
registerSystemEvent(window.document, 'webkitvisibilitychange');
registerDocumentEventTracked('applicationHided', obj);
}
else if (window.force.modules.environment.isDesktopBrowser() && (!deviceRange || deviceRange.desktopBrowser)) {
if (window.document.webkitHidden !== undefined) {
// webkit based engine
registerSystemEvent(window.document, 'webkitvisibilitychange');
registerDocumentEventTracked('applicationHided', obj);
} else {
// TODO: add code to support more browsers here..
}
}
else {
// do nothing
}
};
mod.propagateAppShowTo = function(obj, deviceRange) {
if (window.force.modules.environment.isTizen() && (!deviceRange || deviceRange.tizen)) {
registerSystemEvent(window.document, 'webkitvisibilitychange');
registerDocumentEventTracked('applicationShown', obj);
}
else if (window.force.modules.environment.isDesktopBrowser() && (!deviceRange || deviceRange.desktopBrowser)) {
if (window.document.webkitHidden !== undefined) {
// webkit based engine
registerSystemEvent(window.document, 'webkitvisibilitychange');
registerDocumentEventTracked('applicationShown', obj);
} else {
// TODO: add code to support more browsers here..
}
}
else {
// do nothing
}
};
// expose module
window.force.expose('window.force.modules.forceCanvasSystem.hardwareEvents', mod);
})();