This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
app_list.js
77 lines (63 loc) · 1.92 KB
/
app_list.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
/**
* ALA app list module.
*
* @module AppList
* @return {Object}
*/
define([],
function() {
'use strict';
function AppList() {
this.mozApps = navigator.mozApps;
}
AppList.prototype = {
get: function(filter, callback) {
var list = [];
callback = callback || function() {};
this.mozApps.mgmt.getAll().onsuccess = function(event) {
var apps = event.target.result;
apps.forEach(function(app) {
var manifest = app.manifest || app.updateManifest;
if (manifest.permissions && manifest.permissions[filter]) {
list.push(app);
}
});
callback(list);
};
},
icon: function(app) {
var manifest = app.manifest || app.updateManifest;
if (!manifest.icons || !Object.keys(manifest.icons).length) {
return '../style/images/default.png';
}
// The preferred size is 30 by the default. If we use HDPI device, we may
// use the image larger than 30 * 1.5 = 45 pixels.
var preferredIconSize = 30 * (window.devicePixelRatio || 1);
var preferredSize = Number.MAX_VALUE;
var max = 0;
for (var size in manifest.icons) {
if (manifest.icons.hasOwnProperty(size)) {
size = parseInt(size, 10);
if (size > max) {
max = size;
}
if (size >= preferredIconSize && size < preferredSize) {
preferredSize = size;
}
}
}
// If there is an icon matching the preferred size, we return the result,
// if there isn't, we will return the maximum available size.
if (preferredSize === Number.MAX_VALUE) {
preferredSize = max;
}
var url = manifest.icons[preferredSize];
if (url) {
return !(/^(http|https|data):/.test(url)) ? app.origin + url : url;
} else {
return '../style/images/default.png';
}
}
};
return new AppList();
});