|
1 |
| -; (function (global, root) { |
| 1 | +; (function (global) { |
2 | 2 |
|
| 3 | + // Object initialization |
3 | 4 | var Toastify = function (options) {
|
4 | 5 |
|
| 6 | + // Returning a new init object |
5 | 7 | return new Toastify.lib.init(options);
|
6 | 8 | },
|
| 9 | + // Library version |
7 | 10 | version = "0.0.1";
|
8 | 11 |
|
| 12 | + // Defining the prototype of the object |
9 | 13 | Toastify.lib = Toastify.prototype = {
|
10 | 14 |
|
11 | 15 | toastify: version,
|
12 | 16 |
|
13 | 17 | constructor: Toastify,
|
14 | 18 |
|
| 19 | + // Initializing the object with required parameters |
15 | 20 | init: function (options) {
|
16 | 21 |
|
| 22 | + // Verifying and validating the input object |
17 | 23 | if (!options) {
|
18 | 24 | options = {};
|
19 | 25 | }
|
20 | 26 |
|
| 27 | + // Creating the options object |
21 | 28 | this.options = {};
|
22 | 29 |
|
| 30 | + // Validating the options |
23 | 31 | this.options.text = options.text || 'Hi there!';
|
24 | 32 | this.options.duration = options.duration || 3000;
|
25 |
| - this.options.root = options.root || root; |
26 |
| - |
27 |
| - this.buildToast(); |
| 33 | + this.options.selector = options.selector; |
| 34 | + this.options.callback = options.callback || function () { }; |
28 | 35 |
|
| 36 | + // Returning the current object for chaining functions |
29 | 37 | return this;
|
30 | 38 | },
|
31 | 39 |
|
| 40 | + // Building the DOM element |
32 | 41 | buildToast: function () {
|
33 | 42 |
|
| 43 | + // Validating if the options are defined |
34 | 44 | if (!this.options) {
|
35 | 45 | throw "Toastify is not initialized";
|
36 | 46 | }
|
37 | 47 |
|
| 48 | + // Creating the DOM object |
38 | 49 | var divElement = document.createElement("div");
|
39 | 50 | divElement.className = 'toastify on';
|
| 51 | + |
| 52 | + // Adding the toast message |
40 | 53 | divElement.innerHTML = this.options.text;
|
41 | 54 |
|
42 |
| - this.toast = divElement; |
| 55 | + // Callback for on-click |
| 56 | + divElement.addEventListener('click', this.options.callback); |
| 57 | + |
| 58 | + // Returning the generated element |
| 59 | + return divElement; |
43 | 60 | },
|
44 | 61 |
|
| 62 | + // Displaying the toast |
45 | 63 | showToast: function () {
|
46 | 64 |
|
47 |
| - var toastElement = this.toast; |
| 65 | + // Creating the DOM object for the toast |
| 66 | + var toastElement = this.buildToast(); |
48 | 67 |
|
49 |
| - var rootElement = document.getElementById(this.options.root); |
| 68 | + // Getting the root element to with the toast needs to be added |
| 69 | + var rootElement; |
| 70 | + if (typeof this.options.selector == "undefined") { |
| 71 | + rootElement = document.body; |
| 72 | + } else { |
| 73 | + rootElement = document.getElementById(this.options.selector); |
| 74 | + } |
50 | 75 |
|
51 | 76 | if (!rootElement) {
|
52 | 77 | throw "Root element is not defined";
|
53 | 78 | }
|
54 | 79 |
|
| 80 | + // Adding the DOM element |
55 | 81 | rootElement.insertBefore(toastElement, rootElement.firstChild);
|
56 | 82 |
|
57 |
| - this.reposition(); |
| 83 | + // Repositioning the toasts in case multiple toasts are present |
| 84 | + Toastify.reposition(); |
58 | 85 |
|
59 | 86 | window.setTimeout(function () {
|
| 87 | + |
| 88 | + // Hiding the element |
60 | 89 | toastElement.classList.remove("on");
|
| 90 | + |
| 91 | + // Removing the element from DOM after transition end |
61 | 92 | window.setTimeout(function () {
|
| 93 | + |
| 94 | + // Remove the elemenf from the DOM |
62 | 95 | toastElement.remove();
|
63 |
| - this.reposition(); |
64 |
| - }.bind(this), 400); |
65 |
| - }.bind(this), this.options.duration); |
66 | 96 |
|
67 |
| - }, |
| 97 | + // Repositioning the toasts again |
| 98 | + Toastify.reposition(); |
| 99 | + |
| 100 | + }.bind(this), 400); // Binding `this` for function invocation |
| 101 | + |
| 102 | + }.bind(this), this.options.duration); // Binding `this` for function invocation |
68 | 103 |
|
69 |
| - reposition: function () { |
| 104 | + // Supporting function chaining |
| 105 | + return this; |
70 | 106 |
|
71 |
| - var topOffset = 15; |
72 |
| - |
73 |
| - var allToasts = document.getElementsByClassName('toastify'); |
| 107 | + } |
74 | 108 |
|
75 |
| - for (var i = 0; i < allToasts.length; i++) { |
| 109 | + } |
76 | 110 |
|
77 |
| - var height = allToasts[i].offsetHeight; |
78 |
| - var offset = 15; |
79 |
| - allToasts[i].style.top = topOffset + 'px'; |
| 111 | + // Positioning the toasts on the DOM |
| 112 | + Toastify.reposition = function () { |
80 | 113 |
|
81 |
| - topOffset += height + offset; |
82 |
| - } |
| 114 | + // Top margin |
| 115 | + var topOffsetSize = 15; |
| 116 | + |
| 117 | + // Get all toast messages on the DOM |
| 118 | + var allToasts = document.getElementsByClassName('toastify'); |
83 | 119 |
|
| 120 | + // Modifying the position of each toast element |
| 121 | + for (var i = 0; i < allToasts.length; i++) { |
| 122 | + |
| 123 | + var height = allToasts[i].offsetHeight; |
| 124 | + |
| 125 | + // Spacing between toasts |
| 126 | + var offset = 15; |
| 127 | + |
| 128 | + // Setting the position |
| 129 | + allToasts[i].style.top = topOffsetSize + 'px'; |
| 130 | + |
| 131 | + topOffsetSize += height + offset; |
84 | 132 | }
|
| 133 | + |
| 134 | + // Supporting function chaining |
| 135 | + return this; |
85 | 136 | }
|
86 | 137 |
|
| 138 | + // Setting up the prototype for the init object |
87 | 139 | Toastify.lib.init.prototype = Toastify.lib;
|
88 | 140 |
|
| 141 | + // Adding the Toastify function to the window object |
89 | 142 | global.Toastify = Toastify;
|
90 | 143 |
|
91 |
| -}(window, 'root')); |
| 144 | +}(window)); |
0 commit comments