This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
install.js
137 lines (118 loc) · 3.87 KB
/
install.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
/*jslint nomen: true */
/*global define, navigator, location, window, chrome */
define(function (require) {
'use strict';
var $ = require('zepto');
var dispatcher = $('<div>');
var prop;
//Create event functions based on dispatcher object
function createDispatchFn(id) {
return function () {
return dispatcher[id].apply(dispatcher, arguments);
};
}
/**
* Detects if the current app has been installed.
*
* See https://github.com/wavysandbox/install/blob/master/README.md
* for details on how to use.
*
*/
function install() {
var fn = install[install.type + 'Install'];
if (fn) {
fn();
} else {
install.trigger('error', 'unsupported install: ' + install.type);
}
}
function triggerChange(state) {
install.state = state;
install.trigger('my:change', install.state);
}
/**
* The install state. Values are:
* 'unknown': the code has not tried to detect any state.
*
* @type {String}
*/
install.state = 'unknown';
install.check = function () {
var apps = navigator.mozApps,
request;
if (navigator.mozApps) {
//Mozilla web apps
install.type = 'mozilla';
request = navigator.mozApps.getSelf();
request.onsuccess = function () {
if (this.result) {
triggerChange('installed');
} else {
triggerChange('uninstalled');
}
};
request.onerror = function (err) {
// Just console log it, no need to bother the user.
install.error = err;
triggerChange('error');
};
} else if (typeof chrome !== 'undefined' &&
chrome.webstore &&
chrome.app) {
//Chrome web apps
install.type = 'chromeStore';
if (chrome.app.isInstalled) {
triggerChange('installed');
} else {
triggerChange('uninstalled');
}
} else if (typeof window.navigator.standalone !== 'undefined') {
install.type = 'ios';
if (window.navigator.standalone) {
triggerChange('installed');
} else {
triggerChange('uninstalled');
}
} else {
install.type = 'unsupported';
triggerChange('unsupported');
}
};
/* Mozilla/Firefox installation */
install.mozillaInstallUrl = location.href + 'manifest.webapp';
install.mozillaInstall = function () {
var installRequest = navigator.mozApps.install(install.mozillaInstallUrl);
installRequest.onsuccess = function (data) {
triggerChange('installed');
};
installRequest.onerror = function (err) {
install.error = err;
triggerChange('error');
};
};
/* Chrome installation */
install.chromeInstallUrl = null;
install.chromeInstall = function () {
chrome.webstore.install(install.chromeInstallUrl,
function () {
triggerChange('installed');
}, function (err) {
install.error = err;
triggerChange('error');
});
};
/* iOS installation */
//Right now, just asks that something show a UI
//element mentioning how to install using the Safari
//"Add to Home Screen" button.
install.iosInstall = function () {
install.trigger('showiOSInstall', navigator.platform.toLowerCase());
};
//Allow install to do events.
install.on = createDispatchFn('on');
install.off = createDispatchFn('off');
install.trigger = createDispatchFn('trigger');
//Start up the checks
install.check();
return install;
});