|
20 | 20 | // Library version |
21 | 21 | version = "1.10.0"; |
22 | 22 |
|
| 23 | + // Set the default global options |
| 24 | + Toastify.defaults = { |
| 25 | + oldestFirst: false, |
| 26 | + text: "Toastify is awesome!", |
| 27 | + node: null, |
| 28 | + duration: 3000, |
| 29 | + selector: null, |
| 30 | + callback: function () { |
| 31 | + }, |
| 32 | + destination: null, |
| 33 | + newWindow: false, |
| 34 | + close: false, |
| 35 | + gravity: "toastify-top", |
| 36 | + positionLeft: false, |
| 37 | + position: '', |
| 38 | + backgroundColor: '', |
| 39 | + avatar: "", |
| 40 | + className: "", |
| 41 | + stopOnFocus: true, |
| 42 | + onClick: function () { |
| 43 | + }, |
| 44 | + offset: {x: 0, y: 0}, |
| 45 | + escapeMarkup: true, |
| 46 | + style: {background: ''} |
| 47 | + }; |
| 48 | + |
23 | 49 | // Defining the prototype of the object |
24 | 50 | Toastify.lib = Toastify.prototype = { |
25 | 51 | toastify: version, |
|
39 | 65 | this.toastElement = null; |
40 | 66 |
|
41 | 67 | // Validating the options |
42 | | - this.options.text = options.text || "Hi there!"; // Display message |
43 | | - this.options.node = options.node // Display content as node |
44 | | - this.options.duration = options.duration === 0 ? 0 : options.duration || 3000; // Display duration |
45 | | - this.options.selector = options.selector; // Parent selector |
46 | | - this.options.callback = options.callback || function() {}; // Callback after display |
47 | | - this.options.destination = options.destination; // On-click destination |
48 | | - this.options.newWindow = options.newWindow || false; // Open destination in new window |
49 | | - this.options.close = options.close || false; // Show toast close icon |
50 | | - this.options.gravity = options.gravity === "bottom" ? "toastify-bottom" : "toastify-top"; // toast position - top or bottom |
51 | | - this.options.positionLeft = options.positionLeft || false; // toast position - left or right |
52 | | - this.options.position = options.position || ''; // toast position - left or right |
53 | | - this.options.backgroundColor = options.backgroundColor; // toast background color |
54 | | - this.options.avatar = options.avatar || ""; // img element src - url or a path |
55 | | - this.options.className = options.className || ""; // additional class names for the toast |
56 | | - this.options.stopOnFocus = options.stopOnFocus === undefined? true: options.stopOnFocus; // stop timeout on focus |
57 | | - this.options.onClick = options.onClick; // Callback after click |
58 | | - |
59 | | - this.options.offset = options.offset || { x: 0, y: 0 }; // toast offset |
60 | | - |
61 | | - this.options.escapeMarkup = options.escapeMarkup !== undefined ? options.escapeMarkup : true; |
62 | | - this.options.style = options.style || {}; |
63 | | - |
64 | | - this.options.style.background = this.options.style.background || options.backgroundColor; |
| 68 | + this.options.text = options.text || Toastify.defaults.text; // Display message |
| 69 | + this.options.node = options.node || Toastify.defaults.node; // Display content as node |
| 70 | + this.options.duration = options.duration === 0 ? 0 : options.duration || Toastify.defaults.duration; // Display duration |
| 71 | + this.options.selector = options.selector || Toastify.defaults.selector; // Parent selector |
| 72 | + this.options.callback = options.callback || Toastify.defaults.callback; // Callback after display |
| 73 | + this.options.destination = options.destination || Toastify.defaults.destination; // On-click destination |
| 74 | + this.options.newWindow = options.newWindow || Toastify.defaults.newWindow; // Open destination in new window |
| 75 | + this.options.close = options.close || Toastify.defaults.close; // Show toast close icon |
| 76 | + this.options.gravity = options.gravity === "bottom" ? "toastify-bottom" : Toastify.defaults.gravity; // toast position - top or bottom |
| 77 | + this.options.positionLeft = options.positionLeft || Toastify.defaults.positionLeft; // toast position - left or right |
| 78 | + this.options.position = options.position || Toastify.defaults.position; // toast position - left or right |
| 79 | + this.options.backgroundColor = options.backgroundColor || Toastify.defaults.backgroundColor; // toast background color |
| 80 | + this.options.avatar = options.avatar || Toastify.defaults.avatar; // img element src - url or a path |
| 81 | + this.options.className = options.className || Toastify.defaults.className; // additional class names for the toast |
| 82 | + this.options.stopOnFocus = options.stopOnFocus === undefined ? Toastify.defaults.stopOnFocus : options.stopOnFocus; // stop timeout on focus |
| 83 | + this.options.onClick = options.onClick || Toastify.defaults.onClick; // Callback after click |
| 84 | + this.options.offset = options.offset || Toastify.defaults.offset; // toast offset |
| 85 | + this.options.escapeMarkup = options.escapeMarkup !== undefined ? options.escapeMarkup : Toastify.defaults.escapeMarkup; |
| 86 | + this.options.style = options.style || Toastify.defaults.style; |
| 87 | + this.options.style.background = Toastify.defaults.backgroundColor || options.backgroundColor; |
65 | 88 |
|
66 | 89 | // Returning the current object for chaining functions |
67 | 90 | return this; |
|
188 | 211 | } |
189 | 212 | ) |
190 | 213 | } |
191 | | - |
| 214 | + |
192 | 215 | // Adding an on-click destination path |
193 | 216 | if (typeof this.options.destination !== "undefined") { |
194 | 217 | divElement.addEventListener( |
|
209 | 232 | "click", |
210 | 233 | function(event) { |
211 | 234 | event.stopPropagation(); |
212 | | - this.options.onClick(); |
| 235 | + this.options.onClick(); |
213 | 236 | }.bind(this) |
214 | 237 | ); |
215 | 238 | } |
|
219 | 242 |
|
220 | 243 | var x = getAxisOffsetAValue("x", this.options); |
221 | 244 | var y = getAxisOffsetAValue("y", this.options); |
222 | | - |
| 245 | + |
223 | 246 | var xOffset = this.options.position == "left" ? x : "-" + x; |
224 | 247 | var yOffset = this.options.gravity == "toastify-top" ? y : "-" + y; |
225 | 248 |
|
|
252 | 275 | } |
253 | 276 |
|
254 | 277 | // Adding the DOM element |
255 | | - rootElement.insertBefore(this.toastElement, rootElement.firstChild); |
| 278 | + var elementToInsert = Toastify.defaults.oldestFirst ? rootElement.firstChild : rootElement.lastChild; |
| 279 | + rootElement.insertBefore(this.toastElement, elementToInsert); |
256 | 280 |
|
257 | 281 | // Repositioning the toasts in case multiple toasts are present |
258 | 282 | Toastify.reposition(); |
|
0 commit comments