-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostette.js
1680 lines (1568 loc) · 62.6 KB
/
postette.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;
(function (root, factory) {
/**
* POSTETTE
* A zero-dependency gadget for the management and execution of notifications to a person interacting with a web client application.
*
* @license MIT
* @requires "HTML5", "ECMA-262 Edition 5.1"
* @requires IE 10+, Edge 14+, FF 52+, Chrome 49+, Safari 10+
*
* ES6-COMPLIANT IMPLEMENTATION:
*
* import Postette from "path/to/postette.js";
*
* function start () {
* Postette.init({ SomeConfigParameter: "SomeConfigSetting" });
* };
*
*
* AMD IMPLEMENTATION (RequireJS):
*
* require.config({
* paths: { postette: "path/to/postette"},
* shim: { postette: { exports: "postette" }}
* });
*
* define(["postette"], function(Postette) {
* Postette.init({ SomeConfigParamter: "SomeConfigSetting" });
* });
*
*
* ECMA262-COMPLIANT IMPLEMENTATION:
*
* <script type="text/javascript" src="./path/to/postette.js"></script>
*
* window.addEventListener("DOMContentLoaded", function(event) {
* window.postette.init({ SomeConfigParamter: "SomeConfigSetting" });
* }, true);
*
*/
var namespace = "postette";
typeof exports === "object" && typeof module !== "undefined"
? module.exports = factory()
: typeof define === "function" && "amd" in define
? define([], factory)
: ((root||window)[namespace] = factory());
}(this, (function () {
"use strict";
/**
* Postette is singleton class for management and execution of notifications to the person interacting with a web client application.
* Notifications are queued, so every one will show subsequent to any previous one(s) that may be in process.
*
* Note: No dependencies here since user may need to be notified of application failures.
*/
/* PRIVATE MEMBERS */
/**
* @private
* @property {String} version // the semantic versioning of this code.
* @property {String} identity // the namespace prefixed to unique properties and attributes.
* @property {String} markupId // the element ID of the HTML to be injected.
* @property {String} logId // an optional instance identifier for logging.
* @property {Array} queue // a collection of the messages in queue for toast.
* @property {Object} currentNotification // properties of current message in process of notification.
* @property {Object} previousNotification // properties of most recent message in process of notification.
* @property {Element} styles // the node appended into the document <head>.
* @property {Element} element // the node appended into the parentElement.
* @property {Element} logContainer // the currently renedered ASIDE (for log).
* @property {Element} messageContainer // the currently rendered INS (the animating container).
* @property {Element} messageElement // the currently rendered SPAN (for text).
* @property {Element} clickerElement // the currently rendered BUTTON (to dismiss).
*/
var version = "0.4.1",
identity = "postette",
markupId = identity + Date.now(),
logId = null,
queue = [],
currentNotification = {},
previousNotification = {},
styles,
element,
logContainer,
messageContainer,
messageElement,
clickerElement;
/**
* CONFIG (with DEFAULTS)
*
* @public via init(settings);
* @property {Boolean} echo // when true, also send message to browser console.log/warn/error.
* @property {Boolean} trace // when true, sends computed toast properties to console.log().
* @property {Boolean} reiterate // when true, will queue messages even if duplicate(s) exist in view or queue.
* @property {Boolean} prefix // when true, renders error|warning|success state preceding message (e.g. "Error! 404 not found.").
* @property {Boolean|null} integration // when true, integrate always, never integrate when false, and automatic when null (default).
* @property {Integer} tldr // too-long-didnt-read is the minimum length for defaulting to integrate:true (when integration is auto).
* @property {Array} modalSelector // collection of querySelector-compliant string(s) for determining if a modal exists (sets integrate to false).
* @property {Integer} zIndex // relative display layer as defined by CSS z-index property (default is 100 less than DOM maximum).
* @property {Integer} zIndexLog // relative display layer as defined by CSS z-index property (default is 300 less than DOM maximum).
* @property {String} top // value attributable to CSS top (in sanctioned units, e/g "px", "rem", "%"). Integers will be set as "px".
* @property {Integer} transition // the shortcut value for reveal/retreat of notification.
* @property {Integer} transitionBuffered // the shortcut value for transitions incorporating DOM manipulation (reconfiguring the notification).
* @property {Boolean} downwards // when true, notifications animate downwards into view, elsewise upwards. @todo
* @property {Element} parentElement // node to which element is appended (if null, then document.body will bet set).
* @property {Element} clickAway // enables this.globalClickHandler to quit message/log upon click that bubbles to document.body.
* @setter {Function} transitionComponent // builds transition shortcuts with supplied duration miliseconds or timing-function keyword.
*/
var ZMAX = 2147483647; /* CSS: Maximum value for z-index property of absolute-positioned DOM element. */
var CONFIG = {
echo: false,
trace: false,
reiterate: false,
prefix: true,
integration: null,
tldr: 72,
modalSelector: ["[id^=modal].active"],
zIndex: ZMAX-100,
zIndexLog: ZMAX-500,
top: "50px",
transition: "all 300ms ease",
transitionBuffered: "all 400ms ease",
downwards: true,
parentElement: null,
clickAway: true,
set transitionComponent (value) {
if (/\d+/.test(value)) {
// transitionMilliseconds
this.transition = this.transition.replace(/\d+ms?/, value == 0 ? 0 : value + "ms");
this.transitionBuffered = this.transitionBuffered.replace(/\d+ms?/, (value + TIME.buffer) + "ms");
} else {
// transitionEasing
this.transition = this.transition.replace(/\b([a-z\-]+)$/, value);
this.transitionBuffered = this.transitionBuffered.replace(/\b([a-z\-]+)$/, value);
}
}
};
/**
* STATE are the CSS classNames that determine notification display.
*
* @public via Postette.getStates();
* @property active // when message is visible (or becoming visible).
* @property persist // when messaging an ongoing process (e.g. "spinner").
* @property update // when canceling a "persist" state.
* @property updated // when a "persist" state is superceded.
* @property detached // when messaging is not integrated (e.g. "pill").
* @property log when // log is visible (or becoming visible).
*/
var STATE = {
active: "active",
persist: "persist",
update: "update",
updated: "updated",
detached: "detached",
log: "log"
};
/**
* LEVEL are the CSS classNames that determine notification intent.
*
* @public via Postette.getLevels();
* @property error (red) messaging a failure.
* @property warning (yellow) messaging something ominous.
* @property alert (blue) messaging something significant.
* @property success (green) messaging a success.
*/
var LEVEL = {
error: "error",
warning: "warning",
alert: "alert",
success: "success"
};
/**
* TIME are the milliseconds configured for expanses of time.
*
* @public via Postette.getTimes();
* @property immediate // time required to accommodate DOM manipulation.
* @property minimal // time required to accommodate reveal animation.
* @property brief // time required for a person to acknowledge a message.
* @property moderate // time required for a person to absorb a (shortish) message.
* @property ample // time required for a person to be impeded by a message.
* @property persisting // time given to a "persist" message (seemingly fixed, a spinner).
* @property perCharacterFactor // computes sufficient duration to read an indeterminate message.
* @property transition // time allowed for CSS transition-duration (reconfigurable via CONFIG.transitionMilliseconds).
* @property buffer // minimum time allowed for DOM manipulation (reconfigure of message element).
*/
var TIME = {
immediate: 300,
minimal: 400,
brief: 2000,
moderate: 3000,
ample: 8000,
persisting: 1000 * 60 * 60,
perCharacterFactor: 100,
transition: 300,
buffer: 100
};
/**
* SPAN are @public API abstract references to TIME.
* @example Postette.toast("Hello World!", { level:"warning", pause:Postette.getSpans().ample });
*
* @public via Postette.getSpans();
* @method getValue accepts string/number and message, returns corresponding milliseconds (positive integer).
*/
var SPAN = {
immediate: -1,
brief: "brief",
moderate: "moderate",
ample: "ample",
persist: Infinity,
compute: "compute",
getValue: function (term, msg) {
if (/^\d+$/.test(term)) {
return Math.max(TIME.immediate, Math.abs(parseInt(term)));
} else if (/$infini|$spin/i.test(term)) {
return TIME.persisting;
} else if (/$update|$stop/i.test(term)) {
return TIME.immediate;
} else {
switch (term) {
case this.brief:
return TIME.brief;
case this.moderate:
return TIME.moderate;
case this.ample:
return TIME.ample;
default:
return msg !== undefined && msg.length > 0
? Math.max(TIME.brief, Math.ceil(msg.length * TIME.perCharacterFactor))
: TIME.moderate;
}
}
}
};
/* UTILITIES */
var findPropertyValue = function (obj, key, memo) {
/* stackoverflow.com/questions/15642494/find-property-by-name-in-a-deep-object */
var prop, proto = Object.prototype, ts = proto.toString;
("[object Array]" !== ts.call(memo)) && (memo = []);
for (prop in obj) {
if (proto.hasOwnProperty.call(obj, prop)) {
if (prop === key) {
memo.push(obj[prop]);
} else if ("[object Array]" === ts.call(obj[prop]) || "[object Object]" === ts.call(obj[prop])) {
this.findPropertyValue(obj[prop], key, memo);
}
}
}
return memo;
};
/**
* isUnique assists in the prevention of reiterated messages within the queue.
* @param {Model} instance
* @returns {boolean} true when model instance is unique among queue.
*/
var isUnique = function (instance) {
var profile = instance.level + instance.message;
var current = "level" in currentNotification ? currentNotification : queue.length > 0 ? queue[queue.length-1] : {};
var unique = profile !== (current.level + current.message);
if (unique) {
queue.forEach(function (item) {
if (profile === (item.level + item.message)) {
unique = false;
}
});
}
return unique;
};
/**
* queryUser will attempt to discern a unique identifier.
*
* @private
* @return {Whatever} The identifier.
*/
var queryUser = function () {
var result = "unknown-user";
try {
result = logId || markupId;
} catch (err) {
console.warn(identity, "queryUser:", "An instance identifier was expected but not found.", err);
}
return result;
};
/**
* inferIntegration deduces if message should/not be integrated.
* NOTE: Integration is the desired appearance for error messages and longish messages.
* NOTE: When Config.integration is not set, this routine determines the automatic appearance.
*
* @private
* @param {Object} model (required) is an instance of Model(); All properties are assumed.
* @return {Boolean} true when integration should be forced.
*/
var inferIntegration = function (model) {
var obstructionFree = true;
if (!!model && "message" in model && model.message.length > 0) {
if (model.integrate !== null) {
// an instance setting trumps all
return model.integrate;
} else if (CONFIG.integration !== null) {
// then an initialization setting acts as default
return CONFIG.integration;
} else {
// deferring to automatic...
CONFIG.modalSelector.forEach(function (selector) {
if (!!document.querySelector(selector)) {
obstructionFree = false;
}
});
if (model.level === LEVEL.error || model.message.length > CONFIG.tldr) {
// when message is an error or longish, integrate when possible
return obstructionFree;
} else {
// otherwise, default to not
return false;
}
}
} else {
return false;
}
};
/* POLYFILL for Object.assign(); */
/* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign */
if (typeof Object.assign != "function") {
// Must be writable: true, enumerable: false, configurable: true
Object.defineProperty(Object, "assign", {
value: function assign(target, varArgs) { // .length of function is 2
if (target == null) { // TypeError if undefined or null
throw new TypeError("Cannot convert undefined or null to object");
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}
/* MODEL & COLLECTION */
var collection = [];
var Model = function (customizations) {
/**
* Customizable Characteristics
* @property {String} message is the text to message in a notification.
* @property {Boolean} integrate determines whether or not message appears integral to layout.
* @property {Boolean} once determines if message should be shown only once or subsequently.
* @property {String} level determines the styled appearance of the notification.
* @property {Number} pause stipulates amount of time for notification to be in view.
* @property {Number} delay stipulates amount of time before display of notification.
* @callback {Function} executes after the notification has been rebuffed or displayed.
*/
var defaults = {
id: collection.length,
user: "",
message: "",
created: Date.now(),
integrate: null,
once: false,
href: "",
level: LEVEL.alert,
pause: TIME.moderate,
delay: TIME.minimal,
callback: function () {}
};
defaults.href = window.location.hash || window.location.pathname + window.location.search;
defaults.user = queryUser();
return Object.assign(defaults, customizations);
};
var setCurrentToast = function (instance) {
previousNotification = Object.assign({}, currentNotification);
currentNotification = Object.assign({}, instance);
};
/* PRIVATE HANDLERS */
var clickHandler = function (event) {
if (!!event) { event.stopPropagation(); }
clearTimeout(timerStart);
clearTimeout(timerShow);
clearTimeout(timerDone);
clearClassList(element, STATE.active);
timerDone = setTimeout(function () {
clearClassList(element, STATE);
try {
if (queue.length > 0) {
execute(queue.pop());
} else {
setCurrentToast({});
dismissUI();
}
} catch (err) {
console.warn(identity, "clickHandler", err);
}
}, TIME.transition);
};
var transitionCompleteHandler = function (event) {
};
var transitionUpdateHandler = function (event) {
var ele = event.target;
element.classList.add(currentNotification.level);
element.classList.add(STATE.updated);
element.classList.remove(STATE.persist);
ele.removeEventListener("transitionend", transitionUpdateHandler);
};
/* USER INTERFACE CONSTRUCTION */
/**
* appendStyles parses JSON-formatted styles into CSS.
* @todo support media queries, import
*
* @private
* @param {JSON} jsonStyles is objectified CSS rules.
* @return {Element} the style element as appended to DOM
*/
var appendStyles = function (jsonStyles) {
var parseStyles = function (json) {
var parseRules = function (rules) {
var css = [];
for (var rule in rules) {
if (rules.hasOwnProperty(rule)) {
if (/\/CONFIG\./.test(rules[rule])) {
rules[rule] = CONFIG[rules[rule].toString().replace(/\/CONFIG\.(.+)\//, "$1")];
}
if (/^(to|from|\d+%)$/.test(rule)) {
css.push("\t" + rule + " { " + rules[rule] + " }");
} else {
css.push("\t" + rule + ": " + rules[rule] + ";");
}
}
}
return css.join("\n");
};
var css = [], scope = "div#" + markupId;
for (var selector in json) {
if (/^@.*keyframes/.test(selector)) {
css.push(selector + "\n{\n" + parseRules(json[selector]) + "\n}\n");
} else if (/div#/.test(selector)) {
css.push(selector.replace(/div#/g, scope) + "\n{\n" + parseRules(json[selector]) + "\n}\n");
} else {
css.push(selector.replace(/(^|,)\s*(\b)/g, "$1"+scope+" $2") + "\n{\n" + parseRules(json[selector]) + "\n}\n");
}
}
return css.join("\n");
};
var styleElement = document.createElement("style");
styleElement.setAttribute("type", "text/css");
styleElement.appendChild(document.createTextNode(""));
styleElement.appendChild(document.createTextNode(parseStyles(jsonStyles)));
document.head.appendChild(styleElement);
return styleElement;
};
/**
* appendMarkup parses JSON-formatted markup into HTML
*
* @private
* @param {JSON} jsonMarkup
* @return {Element} root of the UI element as appended to DOM.
*/
var appendMarkup = function (jsonMarkup) {
var parseMarkup = function (root, parent) {
if ("element" in root) {
var ele = document.createElement(root.element);
if ("content" in root) {
ele.appendChild(document.createTextNode(root.content));
}
if ("attributes" in root) {
for (var attribute in root.attributes) {
if (root.attributes.hasOwnProperty(attribute)) {
ele.setAttribute(attribute, root.attributes[attribute]);
}
}
}
if ("childNodes" in root) {
for (var i = 0; i < root.childNodes.length; i++) {
parseMarkup(root.childNodes[i], ele);
}
}
parent.appendChild(ele);
}
};
var container = document.getElementById(markupId);
if (!!container) {
parseMarkup(jsonMarkup, container);
} else {
if (!CONFIG.parentElement) {
CONFIG.parentElement = document.querySelector("body");
}
container = document.createElement("DIV");
container.setAttribute("id", markupId);
CONFIG.parentElement.appendChild(container);
parseMarkup(jsonMarkup, container);
}
return container;
};
/**
* removeMarkup safely removes markup.
*
* @private
* @param {DOMElement} ele is the target element.
*/
var removeMarkup = function (ele) {
if (ele !== undefined && "parentNode" in ele) {
ele.parentNode.removeChild(ele);
}
};
/**
* clearClassList removes all|select class attribute value(s)
*
* @private
* @param {DOMElement} ele is the target element.
* @param {String|Array|Object} target is one className or list or Object.keys to remove.
* @param {String} exempt is a className to preserve.
*/
var clearClassList = function (ele, target, exempt) {
if (!!ele && "classList" in ele) {
if (!!target) {
if (target === Object(target)) {
try {
Object.keys(target).forEach(function(key) {
if (target[key] !== exempt) {
ele.classList.remove(target[key]);
}
});
} catch(err) {
console.warn(identity, "clearClassList() could not find classNames to remove!", err);
}
} else {
ele.classList.remove(target);
}
} else {
/* remove all */
var hadExempt = ele.classList.contains(exempt);
var classList = ele.classList;
while (classList.length > 0) {
classList.remove(classList.item(0));
}
if (hadExempt) {
ele.classList.add(exempt);
}
}
}
};
/**
* dispatchUI injects CSS+HTML and attaches listeners.
*
* @private
* @callback {Function} executes when/if markup does not exist.
* @param {Any} info (optional) to pass to callback.
* @return {null}
*/
var dispatchUI = function (callback, payload) {
if (!styles) {
// once appended, styles will persist
styles = appendStyles(UI.CSS);
}
if (messageContainer === undefined) {
element = appendMarkup(UI.HTML);
setTimeout(function () {
messageContainer = element.querySelector("INS");
messageElement = element.querySelector("SPAN");
clickerElement = element.querySelector("BUTTON");
clickerElement.addEventListener("click", clickHandler, false);
if (callback !== undefined && callback instanceof Function) {
callback(payload);
}
}, 0);
} else if (callback !== undefined && callback instanceof Function) {
callback(payload);
}
};
/**
* dismissUI removes HTML content and listeners and unsets styles.
* NOTE: element (container) and CSS <style> node will persist.
*
* @private
* @return {null}
*/
var dismissUI = function () {
if (messageContainer !== undefined) {
try {
clickerElement.removeEventListener("click", clickHandler);
messageContainer.removeEventListener("transitionend", transitionUpdateHandler);
removeMarkup(messageContainer);
clearClassList(element, null, STATE.log);
messageContainer = undefined;
messageElement = undefined;
clickerElement = undefined;
} catch (err) {
console.warn(identity, "dismissUI", err);
}
}
};
var UI = {
HTML: {
"element": "ins",
"childNodes": [
{
"element": "p",
"childNodes": [
{
"element": "em"
}, {
"element": "span"
}, {
"element": "button",
"content": "×"
}
]
}
]
},
CSS: {
"ins": {
"position": "fixed",
"z-index": /CONFIG.zIndex/,
"top": /CONFIG.top/,
"left": 0,
"right": 0,
"margin": "unset",
"padding": "unset",
"width": "auto",
"text-align": "center",
"opacity": 1,
"-webkit-transform": "none",
"-moz-transform": "none",
"-ms-transform": "none",
"transform": "none",
"transition": /CONFIG.transitionBuffered/,
"clip-path": "inset(0px 0px 0px 0px)"
},
"ins p": {
"position": "relative",
"display": "inline-block",
"top": "-100px",
"width": "100%",
"height": "100%",
"min-width": "200px",
"margin": "0 auto",
"padding": "0.5em 0.5em 0.5em 2em",
"box-sizing": "border-box",
"box-shadow": "none",
"border": "none",
"border-radius": 0,
"border-bottom": "1px solid black",
"text-align": "center",
"font-size": "1.1em",
"line-height": 1.6,
"color": "white",
"-webkit-background-clip": "padding-box",
"-moz-background-clip": "padding",
"background-clip": "padding-box",
"transition": /CONFIG.transitionBuffered/
},
"ins p span": {
"display": "inline-block",
"max-width": "50em"
},
"div#.error p": {
"background-color": "red",
"color": "white"
},
"div#.alert p": {
"background-color": "#0094d1",
"color": "white"
},
"div#.warning p": {
"background-color": "#fcd209",
"color": "black"
},
"div#.success p": {
"background-color": "#00bb55",
"color": "white"
},
"button": {
"float": "right",
"margin": 0,
"padding": "0 8px 0 16px",
"font-size": "1.5em",
"font-weight": "bold",
"line-height": 1,
"background": "none",
"border": "none",
"color": "rgba(0, 0, 0, 0.5)",
"-webkit-box-sizing": "content-box",
"-moz-box-sizing": "content-box",
"box-sizing": "content-box",
"outline": "none"
},
"button:hover": {
"color": "white"
},
"button:active": {
"color": "black"
},
"div#.persist button": {
"visibility": "hidden"
},
"@-webkit-keyframes progress": {
"to": "background-position: 40px 0;"
},
"@-moz-keyframes progress": {
"to": "background-position: 40px 0;"
},
"@keyframes progress": {
"to": "background-position: 40px 0;"
},
"div#.persist:not(.update) p": {
"-webkit-animation": "progress 1s linear infinite !important",
"-moz-animation": "progress 1s linear infinite !important",
"animation": "progress 1s linear infinite !important",
"background-repeat": "repeat !important",
"background-size": "40px 40px !important",
"background-image": "linear-gradient(-45deg, rgba(255, 255, 255, 0.2) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.2) 50%, rgba(255, 255, 255, 0.2) 75%, transparent 75%, transparent) !important"
},
"em": {
"font-style": "normal",
"font-weight": "bold",
"text-transform": "capitalize"
},
"em:not(:empty)::after": {
"content": "'!'",
"padding-right": "1em"
},
"div#.detached ins": {
"top": "-100px",
"left": "50%",
"right": "unset",
"width": "unset",
"opacity": 0,
"-webkit-transform": "none",
"-moz-transform": "none",
"-ms-transform": "none",
"transform": "none",
"clip-path": "unset"
},
"div#.detached ins > p": {
"position": "unset",
"border": "3px solid white",
"box-shadow": "0px 2px 12px rgba(0, 0, 0, 0.5)",
"-webkit-border-radius": "2em",
"-moz-border-radius": "2em",
"border-radius": "2em",
"-webkit-box-sizing": "content-box",
"-moz-box-sizing": "content-box",
"box-sizing": "content-box"
},
"div#.active ins": {
"-webkit-transform": "rotateX(0deg)",
"-moz-transform": "rotateX(0deg)",
"-ms-transform": "rotateX(0deg)",
"transform": "rotateX(0deg)",
"-webkit-transition": /CONFIG.transition/,
"-moz-transition": /CONFIG.transition/,
"-ms-transition": /CONFIG.transition/,
"transition": /CONFIG.transition/
},
"div#.detached.active ins": {
"top": "30px",
"opacity": 1
},
"div#.active ins > p": {
"top": 0
},
"div#.detached.update ins": {
"-webkit-transform": "rotateX(360deg)",
"-moz-transform": "rotateX(360deg)",
"-ms-transform": "rotateX(360deg)",
"transform": "rotateX(360deg)"
},
"div#.updated ins": {
"-webkit-transform": "rotateX(0deg)",
"-moz-transform": "rotateX(0deg)",
"-ms-transform": "rotateX(0deg)",
"transform": "rotateX(0deg)"
},
"div#.detached ins:not([style*=width])": {
"-webkit-transition": "none",
"-moz-transition": "none",
"-ms-transition": "none",
"transition": "none"
},
"aside": {
"position": "fixed",
"z-index": /CONFIG.zIndexLog/,
"top": /CONFIG.top/,
"left": 0,
"right": 0,
"margin": "unset",
"padding": "unset",
"width": "auto",
"text-align": "center",
"clip-path": "inset(0px 0px 0px 0px)",
"max-height": "50%",
"overflow-y": "auto"
},
"aside:hover": {
"border-bottom": "1px solid black"
},
"aside > div": {
"position": "relative",
"top": "-100px",
"width": "100%",
"background": "whitesmoke",
"border-bottom": "1px solid black",
"opacity": 0,
"transition": "all 200ms ease-in"
},
"div#.log aside > div": {
"top": 0,
"opacity": 1,
"transition": "all 200ms ease-out"
},
"aside table": {
"width": "inherit",
"border-collapse": "separate",
"border-spacing": "2px"
},
"aside table tr:nth-child(odd)": {
"background-color": "rgba(120, 110, 100, 0.1)"
},
"aside table tr:hover": {
"background": "#fefe88"
},
"aside table tr td": {
"width": "5%",
"margin": "unset",
"padding": "0.3em 1em",
"text-align": "center",
"white-space": "nowrap",
"border": "none",
"border-radius": "unset"
},
"aside table tr td.message": {
"width": "80%",
"padding-right": "1em",
"white-space": "normal",
"text-align": "left"
},
"aside table tr td.href": {
"max-width": "16em",
"padding-right": "1em",
"white-space": "nowrap",
"overflow": "hidden",
"text-align": "left",
"text-overflow": "ellipsis"
},
"aside table tr td.error": {
"background-color": "red",
"color": "black",
"font-variant": "small-caps"
},
"aside table tr td.alert": {
"background-color": "#0094d1",
"color": "white",
"font-variant": "small-caps"
},
"aside table tr td.warning": {
"background-color": "#fcd209",
"color": "black",
"font-variant": "small-caps"
},
"aside table tr td.success": {
"background-color": "#00bb55",
"color": "white",
"font-variant": "small-caps"
}
}
};
/* USER INTERFACE IMPLEMENTATION */
var timerStart = 0,
timerShow = 0,
timerDone = 0;
/**
* execute renders a new message notification, triggering CSS transitions.
* NOTE: DOM elements are assumed to exist, e.g. dispatchUI(execute);
* NOTE: When delay is TIME.immediate, any pre-existing message is usurped.
*
* @private
* @param {Object} model (required) is an instance of Model(); All properties are assumed.
*/
var execute = function (model) {
var delay, elapsed, levelEle, textEle, width, offset;
setCurrentToast(model);
clearTimeout(timerStart);
clearTimeout(timerShow);
clearTimeout(timerDone);
// MESSAGE
messageElement.innerHTML = "";
if (CONFIG.prefix && /warning|error|success/.test(model.level)) {
levelEle = document.createTextNode(model.level);
messageContainer.querySelector("EM").innerHTML = "";
messageContainer.querySelector("EM").appendChild(levelEle);
}
model.message.split("\n").forEach(function (line, linebreak) {
textEle = document.createTextNode(line);
if (!!linebreak) {
messageElement.appendChild(document.createElement("BR"));
}
messageElement.appendChild(textEle);
});
// DELAY
delay = model.delay;
if (model.delay === TIME.immediate && element.classList.contains(STATE.active)) {
/* an update is going directly to a pre-existing toast (usually a "spinner") */
messageContainer.addEventListener("transitionend", transitionUpdateHandler, true);
model.integrate = !element.classList.contains(STATE.detached);
clearClassList(element, LEVEL);
element.classList.add(model.level);
element.classList.add(STATE.update);
} else {
clearClassList(element, LEVEL);
element.classList.add(model.level);
if (model.delay > TIME.minimal) {
/* prescribed delay may already be eclipsed by a wait in queue */
elapsed = Date.now()-model.created;
delay = elapsed < delay ? delay-elapsed : TIME.minimal;
}
}
// INTEGRATION
if (inferIntegration(model)) {
element.classList.remove(STATE.detached);
messageContainer.setAttribute("style", "");
} else {
element.classList.add(STATE.detached);
width = messageContainer.offsetWidth;
offset = -Math.floor(width / 2) - 25;
messageContainer.setAttribute("style", "width:" + width + "px;margin-left:" + offset + "px;z-index:" + ZMAX);
}
// PAUSE
if (model.pause === TIME.persisting) {
element.classList.add(STATE.persist);
}
// NOTIFICATION
timerStart = setTimeout(function () {
element.classList.add(STATE.active);
timerShow = setTimeout(function () {
element.classList.remove(STATE.active);
timerDone = setTimeout(function () {
model.callback(true);
if (queue.length > 0) {
execute(queue.pop());