-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomMessages.js
667 lines (571 loc) · 19 KB
/
CustomMessages.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/**
* Shows and hides messages to the user.
*
* @public
* @module CustomMessages
*/
// lodash
import isFunction from "../lodash/isFunction.js";
const HOOK_TEMPLATE = Object.freeze({
show: null,
hide: null,
});
const HOOK_TEMPLATE_DISMISS = Object.freeze({
dismissStart: null,
dismissEnd: null,
});
const HOOK_TEMPLATE_ACTION_BUTTON = Object.freeze({
actionButton: null
});
let globalHooks = Object.assign({}, HOOK_TEMPLATE, HOOK_TEMPLATE_DISMISS, HOOK_TEMPLATE_ACTION_BUTTON); /* eslint-disable-line */
let hooks = {};
let htmlElements = {};
let designClasses = {};
let ariaLabelTypes = {};
/**
* Runs a hook set by some module.
*
* It automatically also runs the global hook.
*
* @function
* @private
* @param {string} hooktype the type you want to call
* @param {Object} param the parameter to pass to the function
* @returns {void}
*/
function runGlobalHook(hooktype, param) {
const hook = globalHooks[hooktype];
if (hook !== null) {
hook(param);
}
}
/**
* Runs a hook set by some module.
*
* It automatically also runs the global hook.
*
* @function
* @private
* @param {MESSAGE_LEVEL} messageType
* @param {string} hooktype the type you want to call
* @param {Object} param the parameter to pass to the function
* @returns {void}
*/
function runHook(messageType, hooktype, param) {
runGlobalHook(hooktype, param);
if (messageType instanceof HTMLElement) {
return; // TODO: handle error
}
const hook = hooks[messageType][hooktype];
if (hook !== null) {
hook(param);
}
}
/**
* Hides a message.
*
* The following commands are implemented:
* "hide" – hides the message with animation and resolves has been hidden
* successfully
* "hideStart" – hides the message with animation and resolves if the hiding
* process started
* "hideEnd" – finishes hiding (after the animation, i.e. just makes the
* message invisible) and resolves, if this is done.
*
* The Promises resolve with elMessage and (optionally, in "hideEnd") the
* messageType.
*
* @private
* @param {HTMLElement} elMessage
* @param {string} [action="hide"] the action to do
* @returns {Promise}
*/
function hideMessageAnimate(elMessage, action = "hide") {
return new Promise((resolve) => {
// ignore invalid message boxes as events are often passed in here and
// it may not the correct one from the message box
verifyIsValidMessageHtml(elMessage);
// if button is just clicked trigger hiding
switch (action) {
case "hide":
case "hideStart": {
// trigger hiding
elMessage.classList.add("fade-hide");
// add handler to hide message completly after transition
const callback = async () => {
await hideMessageAnimate(elMessage, "hideEnd");
// remove set handler
elMessage.removeEventListener("transitionend", callback);
if (action === "hide") {
resolve();
}
};
elMessage.addEventListener("transitionend", callback);
if (action === "hideStart") {
resolve(elMessage);
}
break;
}
case "hideEnd": {
const messageType = getMessageTypeFromElement(elMessage);
// hide message (and icon)
hideMessage(messageType);
resolve(elMessage, messageType);
break;
}
default:
throw new TypeError(`invalid action command: "${action}"`);
}
});
}
/**
* Dismisses (i.e. hides with animation) a message when the dismiss button is clicked.
*
* It automatically detects whether it is run as a trigger (click event) or
* as the "finish event" ("transitionend") after the hiding is animated and
* hides the message.
*
* @function
* @private
* @param {Object} event
* @returns {Promise}
*/
function dismissMessage(event) {
const elDismissIcon = event.target;
const elMessage = elDismissIcon.parentElement;
const messageType = getMessageTypeFromElement(elMessage);
const fullyDismissed = hideMessageAnimate(elMessage, "hide").then(() => {
return runHook(messageType, "dismissEnd", {
elMessage,
messageType,
event
});
});
runHook(messageType, "dismissStart", {
elMessage,
messageType,
event
});
return fullyDismissed;
}
/**
* Returns the message type based on the passed element.
*
* @function
* @private
* @param {HTMLElement} elMessage
* @returns {MESSAGE_LEVEL|HTMLElement}
* @throws {Error}
*/
function getMessageTypeFromElement(elMessage) {
const messageType = Object.keys(htmlElements).find((messageType) => {
// skip, if element does not exist
if (!htmlElements[messageType]) {
return false;
}
return htmlElements[messageType].isEqualNode(elMessage);
});
if (messageType === undefined) {
throw new Error(`${elMessage} is no registered element of a message type.`);
}
return messageType;
}
/**
* The action button event handler, when clicked.
*
* @function
* @private
* @param {Object} event
* @returns {void}
*/
function actionButtonClicked(event) {
const elActionButtonLink = event.currentTarget;
const elMessage = elActionButtonLink.parentNode;
const messageType = getMessageTypeFromElement(elMessage);
console.info("action button clicked for ", messageType, event);
runHook(messageType, "actionButton", {
elMessage,
messageType,
event
});
}
/**
* Returns the design this message resembles.
*
* Please DO NOT use this with the built-in message elements.
*
* @function
* @param {MESSAGE_LEVEL|HTMLElement} messageBoxOrType
* @param {MESSAGE_LEVEL} newDesignType
* @returns {void}
*/
export function setMessageDesign(messageBoxOrType, newDesignType) {
const elMessage = getHtmlElementFromOptionalType(messageBoxOrType);
const newDesign = designClasses[newDesignType];
const elActionButton = elMessage.getElementsByClassName("message-action-button")[0];
// set new design
elMessage.classList.add(newDesign);
if (elActionButton) {
elActionButton.classList.add(newDesign);
}
// set aria label
elMessage.setAttribute("aria-label", ariaLabelTypes[newDesignType]);
// unset old design
Object.values(designClasses).forEach((oldDesign) => {
// do not remove newly set design
if (oldDesign === newDesign) {
return;
}
elMessage.classList.remove(oldDesign);
if (elActionButton) {
elActionButton.classList.remove(oldDesign);
}
});
}
/**
* Shows a message to the user.
*
* Pass as many strings/output as you want. They will be localized
* automatically, before presented to the user.
*
* If you pass a HTMLElement as the first parameter, you can use your own
* custom node for the message.
* Attention: This is a "low-level function" and does thus not run the show hook!
*
* @function
* @param {MESSAGE_LEVEL|HTMLElement} messageType
* @param {string} [message] optional, string to show or to translate if omitted no new text is shown
* @param {boolean} [isDismissable] optional, set to true, if user should be able to dismiss the message
* @param {Object|null} [actionButton] optional to show an action button
* @param {string} actionButton.text
* @param {string|function} actionButton.action URL to site to open on link OR function to execute
* @param {...*} args optional parameters for translation
* @returns {void}
*/
export function showMessage(...args) {
if (args.length === 0) {
console.error("showMessage has been called without parameters");
return;
}
// also log message to console
console.log(...args);
// get first element
const messageType = args.shift();
const elMessage = getHtmlElement(messageType);
// and stuff inside we need later
const elDismissIcon = elMessage.getElementsByClassName("icon-dismiss")[0];
const elActionButton = elMessage.getElementsByClassName("message-action-button")[0];
let elActionButtonLink = null;
if (elActionButton) {
elActionButtonLink = elActionButton.parentNode;
}
if (!elMessage) {
throw new Error("The message could not be shown, because the DOM element is missing.", messageType, args);
}
/* check value type/usage of first argument */
let mainMessage = null;
let isDismissable = false; // not dismissable by default
let actionButton = null; // no action button by default
if (typeof args[0] === "string") {
mainMessage = args.shift();
}
if (typeof args[0] === "boolean") {
isDismissable = args.shift();
}
if (args[0] && args[0].text && args[0].action) {
actionButton = args.shift();
}
// localize string or fallback to first string ignoring all others
if (mainMessage !== null) {
// add message to beginning of array
args.unshift(mainMessage);
const localizedString = browser.i18n.getMessage.apply(null, args) || mainMessage || browser.i18n.getMessage("errorShowingMessage");
elMessage.getElementsByClassName("message-text")[0].textContent = localizedString;
}
if (isDismissable === true && elDismissIcon) {
// add an icon which dismisses the message if clicked
elDismissIcon.classList.remove("invisible");
} else if (elDismissIcon) {
elDismissIcon.classList.add("invisible");
}
// show action button, if needed
if (actionButton !== null && elActionButton && elActionButtonLink) {
if (isFunction(actionButton.action)) {
// save option to be called later
hooks[messageType].actionButton = actionButton.action;
// potentiall remove previous set thing
elActionButtonLink.removeAttribute("href");
} else {
const url = browser.i18n.getMessage(actionButton.action) || actionButton.action;
elActionButtonLink.setAttribute("href", url);
// unset potential previously set handler
hooks[messageType].actionButton = null;
}
elActionButton.textContent = browser.i18n.getMessage(actionButton.text) || actionButton.text;
elActionButton.classList.remove("invisible");
} else if (elActionButton) {
elActionButton.classList.add("invisible");
}
elMessage.classList.remove("invisible");
elMessage.classList.remove("fade-hide");
// run hook
// TODO: gets message type first, that is wrong!
runHook(messageType, "show", args);
}
/**
* Hides the message type(s), you specify.
*
* If you pass no messageType or "null", it hides all messages. (except custom ones)
* If a HTMLElement is passed, it automatically hides the target of the event.
* Attention: This is a "low-level function" and does thus not run the hide hook!
*
* @function
* @param {string|int} messageType
* @param {Object} [options]
* @param {boolean} [options.animate=false] set to true to animate the hiding
* @returns {HTMLElement|HTMLElement[]} the element(s) hidden
*/
export function hideMessage(messageType = null, options = {}) {
// hide all messages if type is not specified
if (messageType === null) {
// hide all of them
for (const currentType of Object.keys(htmlElements)) {
// recursive call myself to hide element
hideMessage(currentType);
}
return Object.keys(htmlElements);
}
const elMessage = getHtmlElement(messageType);
// do not re-hide if already hidden
if (elMessage.classList.contains("invisible")) {
console.info("message is already hidden, skip hiding again", elMessage);
return elMessage; // silently
}
// fade, if specified
if (options.animate === true) {
// trigger hiding
hideMessageAnimate(elMessage);
// Note this will automatically come back here (to hideMessage()), so
// that the hook is called in any case
return elMessage;
}
// hide single message
const elDismissIcon = elMessage.getElementsByClassName("icon-dismiss")[0];
elMessage.classList.add("invisible");
if (elDismissIcon) {
elDismissIcon.classList.add("invisible");
}
console.info("message is hidden", elMessage);
// run hook
runHook(messageType, "hide");
return elMessage;
}
/**
* Clones a message HTMLElement you specify.
*
* It sorts the message directly after the message you clone.
* The message is hidden by default – regardless of the state of the origin
* message (type).
*
* CURRENTLY UNUSED.
*
* @function
* @param {MESSAGE_LEVEL|HTMLElement} messageBoxOrType
* @param {string} newId New ID to use for that element
* @returns {HTMLElement}
*/
export function cloneMessage(messageBoxOrType, newId) {
let elMessage = null;
elMessage = getHtmlElementFromOptionalType(messageBoxOrType);
// hide the message to reset it if needed
hideMessage(getMessageTypeFromElement(elMessage));
// clone message
const clonedElMessage = elMessage.cloneNode(true);
clonedElMessage.id = newId;
// attach to DOM
elMessage.insertAdjacentElement("afterend", clonedElMessage);
return clonedElMessage;
}
/**
* Returns whether the message type is already registred.
*
* @private
* @param {string} messageType
* @returns {boolean}
*/
function isMessageTypeRegistered(messageType) {
return messageType in htmlElements;
}
/**
* Verifies that the message type is alreayd registered.
*
* It throws, if it is not.
*
* @private
* @param {string} messageType
* @returns {void}
* @throws {Error}
*/
function verifyMessageType(messageType) {
if (!isMessageTypeRegistered(messageType)) {
throw new Error(`Unregistered message type ${messageType} passed.`);
}
}
/**
* Registers a new message type.
*
* @function
* @param {int|string} messageType
* @param {HTMLElement} elMessage element to register with it
* @param {string} [designClass] the class to apply
* @param {string} [ariaLabelType] the "aria-label" for this message type
* @returns {void}
* @throws {Error}
*/
export function registerMessageType(messageType, elMessage, designClass, ariaLabelType) {
if (isMessageTypeRegistered(messageType)) {
throw new Error(`Message type ${messageType} is already registered. Cannot register again.`);
}
// TODO: verify it's not already regsitered
verifyIsValidMessageHtml(elMessage);
// save HTMLElement
htmlElements[messageType] = elMessage;
// set empty hook
const newHook = Object.assign({}, HOOK_TEMPLATE);
// optionally set design type
if (designClass) {
designClasses[messageType] = designClass;
}
if (ariaLabelType) {
ariaLabelTypes[messageType] = ariaLabelType;
}
// add event listener
const dismissIcon = elMessage.getElementsByClassName("icon-dismiss");
const actionButton = elMessage.getElementsByClassName("message-action-button");
// add properties/hook types only if possible
if (dismissIcon.length > 0) {
dismissIcon[0].addEventListener("click", dismissMessage);
Object.assign(newHook, HOOK_TEMPLATE_DISMISS);
}
if (actionButton.length > 0) {
actionButton[0].parentElement // to bind to link element and not to button
.addEventListener("click", actionButtonClicked);
Object.assign(newHook, HOOK_TEMPLATE_ACTION_BUTTON);
}
hooks[messageType] = newHook;
}
/**
* Verifies the HTMLElement is a valid one for a message.
*
* @private
* @param {HTMLElement} elMessage
* @returns {void}
* @throws {Error}
*/
function verifyIsValidMessageHtml(elMessage) {
if (!elMessage.classList.contains("message-box")) {
throw new Error(`${elMessage} is no valid message type HTML.`);
}
}
/**
* Returns the HTML element of a message type or, if it is already an HTML
* element for a message, the element itself.
*
* @private
* @param {MESSAGE_LEVEL|HTMLElement} messageBoxOrType
* @returns {HTMLElement}
*/
function getHtmlElementFromOptionalType(messageBoxOrType) {
if (messageBoxOrType instanceof HTMLElement) {
verifyIsValidMessageHtml(messageBoxOrType);
return messageBoxOrType;
}
return getHtmlElement(messageBoxOrType);
}
/**
* Returns the message element by the given message type.
*
* @public
* @param {int|string} messageType
* @returns {HTMLElement}
*/
export function getHtmlElement(messageType) {
verifyMessageType(messageType);
return htmlElements[messageType];
}
/**
* Verifies that a hook is valid.
*
* @private
* @param {string} hookType
* @param {function|null} hookFunction the callback to run
* @returns {void}
* @throws {TypeError|Error} if invalid hook type is used
*/
function verifyHook(hookType, hookFunction) {
if (!(hookType in HOOK_TEMPLATE) &&
!(hookType in HOOK_TEMPLATE_DISMISS) &&
!(hookType in HOOK_TEMPLATE_ACTION_BUTTON)) {
throw new TypeError(`Hook type "${hookType}" is not unknown.`);
}
// verify function
if (!isFunction(hookFunction) && hookFunction !== null) {
throw new TypeError(`Hook function "${hookFunction}" is not a valid data type. It must be a function or "null".`);
}
}
/**
* Let's other functions set a hook to be called when a message type is
* shown or hidden.
*
* Set parameters to null or undefined (i.e. do not set) in order to disable
* the hook.
* The errorShown function gets one parameter: The arguments passed to the
* function, as an array.
*
* Pass "null" to it to unset it.
*
* @public
* @param {MESSAGE_LEVEL|HTMLElement} messageType
* @param {string} hookType
* @param {function|null} hookFunction the callback to run
* @returns {void}
* @throws {TypeError|Error} if invalid hook type is used
*/
export function setHook(messageType, hookType, hookFunction) {
verifyMessageType(messageType);
verifyHook(hookType, hookFunction);
if (!(hookType in hooks[messageType])) {
throw new Error(`Hook type "${hookType}" is not valid for this message type.`);
}
hooks[messageType][hookType] = hookFunction;
}
/**
* Resets whole module and thus unregisters all messages.
*
* @function
* @returns {void}
*/
export function reset() {
globalHooks = Object.assign({}, HOOK_TEMPLATE, HOOK_TEMPLATE_DISMISS, HOOK_TEMPLATE_ACTION_BUTTON); /* eslint-disable-line */
hooks = {};
htmlElements = {};
designClasses = {};
ariaLabelTypes = {};
}
/**
* Add a global hook that is triggered for all messages.
*
* @public
* @param {string} hookType
* @param {function|null} hookFunction the callback to run
* @returns {void}
* @throws {TypeError|Error} if invalid hook type is used
*/
export function setGlobalHook(hookType, hookFunction) {
verifyHook(hookType, hookFunction);
if (!(hookType in globalHooks)) {
// this should, currently, never happen as all hooks use the template
throw new Error(`Hook type "${hookType}" is not valid for the global hook.`);
}
globalHooks[hookType] = hookFunction;
}