forked from mozilla-b2g/gaia
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Core.js
359 lines (307 loc) · 10.4 KB
/
Core.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
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
'use strict';
window.Evme = new function Evme_Core() {
var NAME = 'Core', self = this,
head_ts = new Date().getTime();
this.shouldSearchOnInputBlur = false;
this.init = function init(config, callback) {
var data = Evme.__config,
apiHost = Evme.Utils.getUrlParam('apiHost') || data.apiHost;
apiHost && Evme.api.setHost(apiHost);
// calculate the precise background image size loaded behind search results
// takes screen size and pixel density into account
data.bgImageSize = [
Math.floor(window.innerWidth * window.devicePixelRatio),
Math.floor(window.innerHeight * window.devicePixelRatio)
];
// prevent context menu from opening when holding on evme pages
preventContextMenu(Evme.Utils.getContainer());
preventContextMenu(Evme.Utils.getOverlay());
// calculate number of apps to load on every search
// if the screen height permits it - add another row of apps
var numberOfAppsToLoad = data.numberOfAppsToLoad;
if (window.innerHeight / window.innerWidth > 1.66) {
numberOfAppsToLoad += data.apps.appsPerRow;
}
Evme.Brain.init({
'numberOfAppsToLoad': numberOfAppsToLoad,
'searchSources': data.searchSources,
'pageViewSources': data.pageViewSources,
'displayInstalledApps': data.apps.displayInstalledApps
});
Evme.DoATAPI.init({
'deviceId': config.deviceId,
'apiKey': data.apiKey,
'appVersion': data.appVersion,
'authCookieName': data.authCookieName,
'callback': function initCallback() {
setupObjects(data, callback);
}
});
};
this.onShow = function onShow() {
document.body.classList.add('evme-displayed');
};
this.onHide = function onHide() {
document.body.classList.remove('evme-displayed');
Evme.Searchbar.blur();
Evme.Collection.hide();
};
this.onHomeButtonPress = function onHomeButtonPress() {
Evme.Searchbar.blur();
if (
// hide suggested collections list if open
Evme.CollectionsSuggest && Evme.CollectionsSuggest.hide() ||
// stop editing if active
Evme.Collection.toggleEditMode(false) ||
// close full screen background image if visible
Evme.Collection.hideFullscreen() ||
// hide the collection if visible
Evme.Collection.hide() ||
// close search results full screen background image if visible
Evme.BackgroundImage.closeFullScreen() ||
// clear the searchbar and apps
(Evme.Searchbar.clearIfHasQuery() &&
document.body.classList.contains('evme-displayed'))
) {
return true;
}
return false;
};
this.searchFromOutside = function searchFromOutside(query) {
Evme.Brain.Searcher.searchExactFromOutside(query);
};
function preventContextMenu(el) {
el.addEventListener('contextmenu', function onContextMenu(e) {
e.stopPropagation();
});
}
// one time setup to execute on device first launch
function setupObjects(data, callback) {
var setupCollectionStorageKey = 'collection-storage-setup';
Evme.Storage.get(setupCollectionStorageKey, function next(didSetup) {
if (didSetup) {
initObjects(data);
callback();
} else {
setupCollectionStorage(function deferInit() {
Evme.Storage.set(setupCollectionStorageKey, true);
initObjects(data);
callback();
});
}
});
}
function setupCollectionStorage(done) {
var collections = EvmeManager.getCollections(),
totalCollections = collections.length;
if (totalCollections === 0) {
done();
return;
}
var onDone = function onDone() {
if (--totalCollections === 0) {
done();
}
};
var iterateApps = function iterateApps(apps, collection) {
var infoApps = [],
total = apps.length;
apps.forEach(function iteratee(info) {
// info defines ['manifestURL', 'entry_point']
var descriptor = {};
if (info[0]) {
descriptor.manifestURL = info[0];
}
if (info[1]) {
descriptor.entry_point = info[1];
}
var app = EvmeManager.getAppByDescriptor(descriptor);
if (app) {
infoApps.push(app);
}
if (--total === 0) {
saveCollectionSettings(collection, infoApps, onDone);
}
});
};
// Populating apps from manifest file
collections.forEach(function(collection) {
var apps = collection.manifest.apps;
if (!apps || apps.length === 0) {
// There aren't pre-installed apps for this collection
saveCollectionSettings(collection, [], onDone);
} else {
iterateApps(apps, collection);
}
});
}
function saveCollectionSettings(collection, apps, done) {
var collectionSettings = new Evme.CollectionSettings({
'id': collection.id,
'experienceId': collection.providerId,
'apps': apps,
'defaultIcon': collection.getFeatures().icon
});
Evme.CollectionStorage.add(collectionSettings, done);
}
function initObjects(data) {
// lazy components
window.addEventListener('suggestcollections', initSuggestCollections);
self.ConnectionMessage = lazifyModule('ConnectionMessage',
['everything.me/modules/ConnectionMessage/ConnectionMessage.css',
'everything.me/modules/ConnectionMessage/ConnectionMessage.js'],
function initModule() {
Evme.ConnectionMessage.init();
});
self.Location = lazifyModule('Location',
['everything.me/modules/Location/Location.js'],
function initModule() {
Evme.Location.init({
'refreshInterval': data.locationInterval,
'requestTimeout': data.locationRequestTimeout
});
});
// active components
var appsEl = Evme.$('#evmeApps'),
collectionEl = document.querySelector('#collection .evme-apps');
Evme.Features.init({
'featureStateByConnection': data.featureStateByConnection
});
Evme.Searchbar.init({
'el': Evme.$('#search-q'),
'elForm': Evme.$('#search-rapper'),
'timeBeforeEventPause': data.searchbar.timeBeforeEventPause,
'timeBeforeEventIdle': data.searchbar.timeBeforeEventIdle,
'setFocusOnClear': false
});
Evme.Helper.init({
'el': Evme.$('#helper'),
'elTitle': Evme.$('#search-title'),
'elTip': Evme.$('#helper-tip'),
'elSaveSearch': Evme.$('#save-search')
});
Evme.BackgroundImage.init({
'el': Evme.$('#search-overlay'),
'elFullScreenParent': Evme.$('#evmeOverlay')
});
Evme.SearchResults = new Evme.ResultManager();
Evme.SearchResults.init({
'NAME': 'SearchResults',
'el': appsEl,
'appsPerRow': data.apps.appsPerRow,
'providers': [
{
type: Evme.PROVIDER_TYPES.INSTALLED,
config: {
'renderer': Evme.InstalledAppsRenderer,
'containerEl': Evme.$('.installed', appsEl)[0],
'containerSelector': '.installed',
'filterResults': true
}
}, {
type: Evme.PROVIDER_TYPES.CLOUD,
config: {
'renderer': Evme.CloudAppsRenderer,
'containerEl': Evme.$('.cloud', appsEl)[0]
}
}, {
type: Evme.PROVIDER_TYPES.MARKETAPPS,
config: {
'renderer': Evme.MarketAppsRenderer,
'containerEl': Evme.$('.marketapps', appsEl)[0]
}
}, {
type: Evme.PROVIDER_TYPES.MARKETSEARCH,
config: {
'renderer': Evme.MarketSearchRenderer,
'containerEl': Evme.$('.marketsearch', appsEl)[0]
}
}
]
});
Evme.CollectionResults = new Evme.ResultManager();
Evme.CollectionResults.init({
'NAME': 'CollectionResults',
'el': collectionEl,
'appsPerRow': data.apps.appsPerRow,
'providers': [{
type: Evme.PROVIDER_TYPES.STATIC,
config: {
'renderer': Evme.StaticAppsRenderer,
'containerEl': Evme.$('.static', collectionEl)[0],
'containerSelector': '.static',
'filterResults': true
}
}, {
type: Evme.PROVIDER_TYPES.CLOUD,
config: {
'renderer': Evme.CloudAppsRenderer,
'containerEl': Evme.$('.cloud', collectionEl)[0]
}
}
]
});
Evme.CollectionStorage.init();
Evme.Collection.init({
'resultsManager': Evme.CollectionResults,
'bgImage': (Evme.BackgroundImage.get() || {}).image
});
Evme.InstalledAppsService.init();
Evme.IconManager.init({});
Evme.SearchHistory.init({
'maxEntries': data.maxHistoryEntries
});
Evme.Analytics.init({
'config': data.analytics,
'namespace': Evme,
'DoATAPI': Evme.DoATAPI,
'Brain': Evme.Brain,
'connectionLow': Evme.Utils.connection().speed !=
Evme.Utils.connection().SPEED_HIGH,
'sessionObj': Evme.DoATAPI.Session.get(),
'pageRenderStartTs': head_ts,
'SEARCH_SOURCES': data.searchSources,
'PAGEVIEW_SOURCES': data.pageViewSources
});
Evme.EventHandler.trigger(NAME, 'init', {
'deviceId': Evme.DoATAPI.getDeviceId()
});
}
function initSuggestCollections(e) {
LazyLoader.load(
['everything.me/modules/CollectionsSuggest/CollectionsSuggest.js',
'everything.me/modules/CollectionsSuggest/CollectionsSuggest.css'],
function onLoad() {
Evme.CollectionsSuggest.init({
'elList': document.getElementById('collections-select'),
'elParent': Evme.Utils.getContainer()
});
window.removeEventListener('suggestcollections',
initSuggestCollections);
window.addEventListener('suggestcollections', onSuggestCollections);
window.dispatchEvent(e);
}
);
}
function onSuggestCollections(e) {
Evme.Brain.CollectionsSuggest.showUI();
}
// create a 'lazy' version of a module using the Proxy API
// calling any method of the 'lazy module' will load and initialize
// the real module and then call the matching method
function lazifyModule(moduleName, resources, initModule) {
var handler = {
get: function get(target, name) {
return function wrapper() {
var args = Array.prototype.slice.call(arguments);
LazyLoader.load(resources, function onLoad() {
initModule();
var module = Evme[moduleName];
module[name].apply(module, args);
});
}
}
};
return new Proxy({}, handler);
};
}