This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
CollectionsSuggest.js
211 lines (164 loc) · 4.98 KB
/
CollectionsSuggest.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
'use strict';
Evme.CollectionsSuggest = new function Evme_CollectionsSuggest() {
var NAME = 'CollectionsSuggest',
CUSTOM_OPT_VALUE = 'customCollectionValue',
CUSTOM_OPT_ID = 'custom-collection',
self = this,
elList = null,
elParent = null,
active = false,
savedIcons = null,
WORLDWIDE_LOCALE = 'en_WW';
this.init = function init(options) {
elParent = options.elParent;
elList = options.elList;
elList.addEventListener('blur', onBlur);
elList.addEventListener('change', onChange);
Evme.EventHandler.trigger(NAME, 'init');
};
this.show = function show() {
if (active) {
return false;
}
active = true;
elList.classList.add('visible');
elList.focus();
Evme.EventHandler.trigger(NAME, 'show');
return true;
};
this.hide = function hide() {
if (!active) {
return false;
}
active = false;
elList.classList.remove('visible');
window.focus();
elList.blur();
Evme.EventHandler.trigger(NAME, 'hide');
return true;
};
this.newCustom = function newCustom() {
elList.blur();
var customQuery = prompt(Evme.Utils.l10n(NAME, 'prompt-create-custom'));
if (!customQuery) {
return;
}
Evme.EventHandler.trigger(NAME, 'custom', {
'query': customQuery
});
};
this.get = function get() {
var selectedShortcuts = [],
elShourtcuts = Evme.$('option', elList);
for (var i = 0, elOption; elOption = elShourtcuts[i++];) {
if (elOption.selected) {
selectedShortcuts.push({
'query': elOption.value,
'experienceId': elOption.dataset.experience || ''
});
}
}
return selectedShortcuts;
};
this.load = function load(data) {
savedIcons = data.icons;
// translate only if locale is supported by E.me service
var doTranslate = data.locale !== WORLDWIDE_LOCALE;
elList.innerHTML = '';
self.add(data.shortcuts, doTranslate);
Evme.EventHandler.trigger(NAME, 'load');
};
this.add = function add(shortcuts, doTranslate) {
var shortcutsAdded = {};
elList.innerHTML = '';
// custom collection
var optCustom = document.createElement('option');
optCustom.text = Evme.Utils.l10n('shortcut', 'custom');
optCustom.value = CUSTOM_OPT_VALUE;
optCustom.id = CUSTOM_OPT_ID;
elList.appendChild(optCustom);
for (var i = 0, shortcut, query, queryKey, experienceId, name;
shortcut = shortcuts[i++];) {
query = shortcut.query;
queryKey = query.toLowerCase();
experienceId = shortcut.experienceId || '';
name = query;
if (doTranslate && experienceId) {
var l10nkey = 'id-' + Evme.Utils.shortcutIdToKey(experienceId),
translatedName = Evme.Utils.l10n('shortcut', l10nkey);
if (translatedName) {
shortcut.query = query = name = translatedName;
}
}
name = name.replace(/</g, '<');
if (!shortcutsAdded[queryKey]) {
var opt = document.createElement('option');
opt.text = Evme.html(name);
opt.value = query.replace(/"/g, '"');
opt.dataset.experience = experienceId;
elList.appendChild(opt);
shortcutsAdded[queryKey] = true;
}
}
};
this.Loading = new function Loading() {
var active = false,
ID = 'shortcuts-customize-loading';
this.show = function loadingShow() {
if (active) { return; }
var el = Evme.$create('form',
{'id': ID, 'role': 'dialog', 'data-type': 'confirm'},
'<section>' +
'<h1 ' + Evme.Utils.l10nAttr(NAME, 'loading') + '></h1>' +
'<p class="noreset">' +
'<progress></progress>' +
'</p>' +
'</section>' +
'<menu>' +
'<button ' + Evme.Utils.l10nAttr(NAME, 'loading-cancel') +
' class="full"></button>' +
'</menu>');
Evme.$('button', el, function onItem(elButton) {
elButton.addEventListener('click', onLoadingCancel);
});
Evme.Utils.getOverlay().appendChild(el);
active = true;
Evme.EventHandler.trigger(NAME, 'loadingShow');
};
this.hide = function loadingHide() {
if (!active) { return; }
Evme.$remove('#' + ID);
active = false;
Evme.EventHandler.trigger(NAME, 'loadingHide');
};
};
function isCustomSelected() {
var optCustom = document.getElementById(CUSTOM_OPT_ID);
return (optCustom && optCustom.selected);
}
function onChange() {
if (isCustomSelected()) {
self.newCustom();
}
}
function onBlur() {
active = false;
self.Loading.hide();
Evme.EventHandler.trigger(NAME, 'hide');
if (!isCustomSelected()) {
done();
}
}
function onLoadingCancel(e) {
e.stopPropagation();
Evme.EventHandler.trigger(NAME, 'loadingCancel', {
'e': e
});
}
function done() {
Evme.EventHandler.trigger(NAME, 'done', {
'shortcuts': self.get(),
'icons': savedIcons
});
}
}