This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
themes.js
222 lines (199 loc) · 7.39 KB
/
themes.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
define(function(require) {
'use strict';
var AppsCache = require('modules/apps_cache');
var ManifestHelper = require('shared/manifest_helper');
var SettingsCache = require('modules/settings_cache');
var ListView = require('modules/mvvm/list_view');
var template = require('./layout_template');
var THEME_SELECTED = 'theme.selected';
var WALLPAPER_LIST = '/wallpaper.json';
var WALLPAPER_KEY = 'wallpaper.image';
var Themes = function ctor_themes() {
return {
_container: null,
_settings: null,
_selectedTheme: null,
_previousTheme: null,
_themes: null,
_config: {},
onInit: function th_onInit(panel) {
this._container = panel.querySelector('.theme-list');
this._settings = navigator.mozSettings;
},
onBeforeShow: function th_onBeforeShow() {
this._themes = [];
this.getInstalledThemes((this.setTheme));
},
getInstalledThemes: function th_getInstalledThemes(callback) {
AppsCache.apps().then(function(apps) {
apps = apps.filter(function(app) {
var manifest = app.manifest || app.updateManifest;
return manifest && manifest.type &&
manifest.type === 'certified' &&
manifest.role && manifest.role === 'theme';
});
for (var app in apps) {
var manifest = new ManifestHelper(apps[app].manifest);
var theme = {
'name': manifest.name,
'manifestURL': apps[app].manifestURL,
'onclick': callback.bind(this)
};
this._themes.push(theme);
}
this._themes.sort(function(a, b) {
return a.name.localeCompare(b.name);
});
this.renderThemes();
}.bind(this));
},
renderThemes: function th_renderThemes() {
this._listView = ListView(this._container, this._themes, template);
this.updateRadioButtons();
},
updateRadioButtons: function th_updateRadioButtons() {
var currentSetting = SettingsCache.cache;
if (!currentSetting) {
return;
}
var theme = this._selectedTheme = currentSetting[THEME_SELECTED];
this._updateRow(theme, true);
},
_doSetTheme: function th_doSetTheme(theme) {
if (this._selectedTheme === theme) {
return Promise.resolve();
}
return new Promise((function(resolve, reject) {
var setting = {};
this._previousTheme = this._selectedTheme;
setting[THEME_SELECTED] = this._selectedTheme = theme;
var req = this._settings.createLock().set(setting);
req.onsuccess = (function() {
this.getWallpaperPath().
then((this.loadWallpaper).bind(this)).
then((this.setWallpaper).bind(this)).
then((this.saveConfig).bind(this)).then(resolve, reject);
}).bind(this);
req.onerror = reject;
}).bind(this));
},
setTheme: function th_setTheme(theme) {
this.disableSelection();
return this._doSetTheme(theme).then(this.enableSelection.bind(this),
this.rollbackTheme.bind(this));
},
/**
* Setting a theme will happen in a two steps process, we will first
* set the theme manifest, and after that we will save any other setting
* related to the new theme.
* If an error happens in any of those two processes, we will need to
* be sure that we enable back the previous theme selection
*/
rollbackTheme: function th_rollbackTheme() {
this._config = {};
if (this._previousTheme === this._selectedTheme ||
this._previousTheme === null) {
return Promise.reject('No previous theme to rollback');
}
var previous = '' + this._previousTheme;
var current = '' + this._selectedTheme;
return this._doSetTheme(this._previousTheme).then((function() {
this._updateRow(previous, true);
this._updateRow(current, false);
this.enableSelection();
}).bind(this));
},
_updateRow: function th_updateRow(theme, checked) {
var rule = 'input[value="' + theme + '"]';
var node = this._container.querySelector(rule);
if (node) {
node.checked = !!checked;
}
},
disableSelection: function th_disableSelection() {
var nodes = this._container.querySelectorAll('input:not([value="' +
this._selectedTheme + '"])');
if (nodes) {
Array.prototype.slice.call(nodes).forEach(function(node) {
node.parentNode.parentNode.classList.add('disabled');
});
}
},
enableSelection: function th_enableSelection() {
var nodes = this._container.querySelectorAll('input');
if (nodes) {
Array.prototype.slice.call(nodes).forEach(function(node) {
node.parentNode.parentNode.classList.remove('disabled');
});
}
},
/**
* Given the current them set, we get the path for the
* image set as background.
* @returns {Promise} fulfilled with the path to the wallpaper
*/
getWallpaperPath: function th_getWallpaper() {
var url = this._selectedTheme
.substring(0, this._selectedTheme.lastIndexOf('/')) + WALLPAPER_LIST;
var xhr = new XMLHttpRequest({ mozSystem: true });
xhr.open('GET', url, true);
xhr.responseType = 'json';
return new Promise(function(resolve, reject) {
xhr.onload = function successGettingWallpaperList() {
if (xhr.status !== 200) {
reject(xhr.status);
return;
}
var filename = xhr.response.homescreen;
resolve(filename);
};
xhr.send(null);
});
},
/**
* Load an image {blob} from another app via XHR.
* @params filename {String} path to the wallpaper on current theme.
* @returns {Promise} fulfilled with the blob.
*/
loadWallpaper: function th_loadWallpaper(filename) {
var url = this._selectedTheme.substring(0, this._selectedTheme
.lastIndexOf('/')) + filename;
var xhr = new XMLHttpRequest({ mozSystem: true });
xhr.open('GET', url, true);
xhr.responseType = 'blob';
return new Promise(function(resolve, reject) {
xhr.onload = function() {
if (xhr.status !== 200) {
reject(xhr.status);
return;
}
resolve(xhr.response);
};
xhr.send(null);
});
},
/**
* Saves in memory the configuration for wallpaper. saveConfig
* will need to be invoked to make this changes permanent.
* @returns {Promise} fulfilled inmediately.
*/
setWallpaper: function th_setWallpaper(blob) {
this._config[WALLPAPER_KEY] = blob;
return Promise.resolve(this._config);
},
/**
* Perform the operation to writing to settings
* @returns {Promise} fulfilled when config is saved
*/
saveConfig: function th_saveConfig() {
var self = this;
return new Promise(function(resolve, reject) {
var request = self._settings.createLock().set(self._config);
request.onerror = reject;
request.onsuccess = resolve;
});
}
};
};
return Themes;
});