arnaucube
arnaucube/openworktimePublic Self-hosted repository
openworktime/webapp/libraries/toastr.js
webapp/libraries/toastr.jsRaw
  1/*
  2 * Toastr
  3 * Copyright 2012-2015
  4 * Authors: John Papa, Hans Fjällemark, and Tim Ferrell.
  5 * All Rights Reserved.
  6 * Use, reproduction, distribution, and modification of this code is subject to the terms and
  7 * conditions of the MIT license, available at http://www.opensource.org/licenses/mit-license.php
  8 *
  9 * ARIA Support: Greta Krafsig
 10 *
 11 * Project: https://github.com/CodeSeven/toastr
 12 */
 13/* global define */
 14; (function (define) {
 15    define(['jquery'], function ($) {
 16        return (function () {
 17            var $container;
 18            var listener;
 19            var toastId = 0;
 20            var toastType = {
 21                error: 'error',
 22                info: 'info',
 23                success: 'success',
 24                warning: 'warning'
 25            };
 26
 27            var toastr = {
 28                clear: clear,
 29                remove: remove,
 30                error: error,
 31                getContainer: getContainer,
 32                info: info,
 33                options: {},
 34                subscribe: subscribe,
 35                success: success,
 36                version: '2.1.1',
 37                warning: warning
 38            };
 39
 40            var previousToast;
 41
 42            return toastr;
 43
 44            ////////////////
 45
 46            function error(message, title, optionsOverride) {
 47                return notify({
 48                    type: toastType.error,
 49                    iconClass: getOptions().iconClasses.error,
 50                    message: message,
 51                    optionsOverride: optionsOverride,
 52                    title: title
 53                });
 54            }
 55
 56            function getContainer(options, create) {
 57                if (!options) { options = getOptions(); }
 58                $container = $('#' + options.containerId);
 59                if ($container.length) {
 60                    return $container;
 61                }
 62                if (create) {
 63                    $container = createContainer(options);
 64                }
 65                return $container;
 66            }
 67
 68            function info(message, title, optionsOverride) {
 69                return notify({
 70                    type: toastType.info,
 71                    iconClass: getOptions().iconClasses.info,
 72                    message: message,
 73                    optionsOverride: optionsOverride,
 74                    title: title
 75                });
 76            }
 77
 78            function subscribe(callback) {
 79                listener = callback;
 80            }
 81
 82            function success(message, title, optionsOverride) {
 83                return notify({
 84                    type: toastType.success,
 85                    iconClass: getOptions().iconClasses.success,
 86                    message: message,
 87                    optionsOverride: optionsOverride,
 88                    title: title
 89                });
 90            }
 91
 92            function warning(message, title, optionsOverride) {
 93                return notify({
 94                    type: toastType.warning,
 95                    iconClass: getOptions().iconClasses.warning,
 96                    message: message,
 97                    optionsOverride: optionsOverride,
 98                    title: title
 99                });
100            }
101
102            function clear($toastElement, clearOptions) {
103                var options = getOptions();
104                if (!$container) { getContainer(options); }
105                if (!clearToast($toastElement, options, clearOptions)) {
106                    clearContainer(options);
107                }
108            }
109
110            function remove($toastElement) {
111                var options = getOptions();
112                if (!$container) { getContainer(options); }
113                if ($toastElement && $(':focus', $toastElement).length === 0) {
114                    removeToast($toastElement);
115                    return;
116                }
117                if ($container.children().length) {
118                    $container.remove();
119                }
120            }
121
122            // internal functions
123
124            function clearContainer (options) {
125                var toastsToClear = $container.children();
126                for (var i = toastsToClear.length - 1; i >= 0; i--) {
127                    clearToast($(toastsToClear[i]), options);
128                }
129            }
130
131            function clearToast ($toastElement, options, clearOptions) {
132                var force = clearOptions && clearOptions.force ? clearOptions.force : false;
133                if ($toastElement && (force || $(':focus', $toastElement).length === 0)) {
134                    $toastElement[options.hideMethod]({
135                        duration: options.hideDuration,
136                        easing: options.hideEasing,
137                        complete: function () { removeToast($toastElement); }
138                    });
139                    return true;
140                }
141                return false;
142            }
143
144            function createContainer(options) {
145                $container = $('<div/>')
146                    .attr('id', options.containerId)
147                    .addClass(options.positionClass)
148                    .attr('aria-live', 'polite')
149                    .attr('role', 'alert');
150
151                $container.appendTo($(options.target));
152                return $container;
153            }
154
155            function getDefaults() {
156                return {
157                    tapToDismiss: true,
158                    toastClass: 'toast',
159                    containerId: 'toast-container',
160                    debug: false,
161
162                    showMethod: 'fadeIn', //fadeIn, slideDown, and show are built into jQuery
163                    showDuration: 300,
164                    showEasing: 'swing', //swing and linear are built into jQuery
165                    onShown: undefined,
166                    hideMethod: 'fadeOut',
167                    hideDuration: 1000,
168                    hideEasing: 'swing',
169                    onHidden: undefined,
170
171                    extendedTimeOut: 1000,
172                    iconClasses: {
173                        error: 'toast-error',
174                        info: 'toast-info',
175                        success: 'toast-success',
176                        warning: 'toast-warning'
177                    },
178                    iconClass: 'toast-info',
179                    positionClass: 'toast-bottom-right',
180                    timeOut: 7000, // Set timeOut and extendedTimeOut to 0 to make it sticky
181                    titleClass: 'toast-title',
182                    messageClass: 'toast-message',
183                    target: 'body',
184                    closeHtml: '<button type="button">&times;</button>',
185                    newestOnTop: false,
186                    preventDuplicates: false,
187                    progressBar: true
188                };
189            }
190
191            function publish(args) {
192                if (!listener) { return; }
193                listener(args);
194            }
195
196            function notify(map) {
197                var options = getOptions();
198                var iconClass = map.iconClass || options.iconClass;
199
200                if (typeof (map.optionsOverride) !== 'undefined') {
201                    options = $.extend(options, map.optionsOverride);
202                    iconClass = map.optionsOverride.iconClass || iconClass;
203                }
204
205                if (shouldExit(options, map)) { return; }
206
207                toastId++;
208
209                $container = getContainer(options, true);
210
211                var intervalId = null;
212                var $toastElement = $('<div/>');
213                var $titleElement = $('<div/>');
214                var $messageElement = $('<div/>');
215                var $progressElement = $('<div/>');
216                var $closeElement = $(options.closeHtml);
217                var progressBar = {
218                    intervalId: null,
219                    hideEta: null,
220                    maxHideTime: null
221                };
222                var response = {
223                    toastId: toastId,
224                    state: 'visible',
225                    startTime: new Date(),
226                    options: options,
227                    map: map
228                };
229
230                personalizeToast();
231
232                displayToast();
233
234                handleEvents();
235
236                publish(response);
237
238                if (options.debug && console) {
239                    console.log(response);
240                }
241
242                return $toastElement;
243
244                function personalizeToast() {
245                    setIcon();
246                    setTitle();
247                    setMessage();
248                    setCloseButton();
249                    setProgressBar();
250                    setSequence();
251                }
252
253                function handleEvents() {
254                    $toastElement.hover(stickAround, delayedHideToast);
255                    if (!options.onclick && options.tapToDismiss) {
256                        $toastElement.click(hideToast);
257                    }
258
259                    if (options.closeButton && $closeElement) {
260                        $closeElement.click(function (event) {
261                            if (event.stopPropagation) {
262                                event.stopPropagation();
263                            } else if (event.cancelBubble !== undefined && event.cancelBubble !== true) {
264                                event.cancelBubble = true;
265                            }
266                            hideToast(true);
267                        });
268                    }
269
270                    if (options.onclick) {
271                        $toastElement.click(function () {
272                            options.onclick();
273                            hideToast();
274                        });
275                    }
276                }
277
278                function displayToast() {
279                    $toastElement.hide();
280
281                    $toastElement[options.showMethod](
282                        {duration: options.showDuration, easing: options.showEasing, complete: options.onShown}
283                    );
284
285                    if (options.timeOut > 0) {
286                        intervalId = setTimeout(hideToast, options.timeOut);
287                        progressBar.maxHideTime = parseFloat(options.timeOut);
288                        progressBar.hideEta = new Date().getTime() + progressBar.maxHideTime;
289                        if (options.progressBar) {
290                            progressBar.intervalId = setInterval(updateProgress, 10);
291                        }
292                    }
293                }
294
295                function setIcon() {
296                    if (map.iconClass) {
297                        $toastElement.addClass(options.toastClass).addClass(iconClass);
298                    }
299                }
300
301                function setSequence() {
302                    if (options.newestOnTop) {
303                        $container.prepend($toastElement);
304                    } else {
305                        $container.append($toastElement);
306                    }
307                }
308
309                function setTitle() {
310                    if (map.title) {
311                        $titleElement.append(map.title).addClass(options.titleClass);
312                        $toastElement.append($titleElement);
313                    }
314                }
315
316                function setMessage() {
317                    if (map.message) {
318                        $messageElement.append(map.message).addClass(options.messageClass);
319                        $toastElement.append($messageElement);
320                    }
321                }
322
323                function setCloseButton() {
324                    if (options.closeButton) {
325                        $closeElement.addClass('toast-close-button').attr('role', 'button');
326                        $toastElement.prepend($closeElement);
327                    }
328                }
329
330                function setProgressBar() {
331                    if (options.progressBar) {
332                        $progressElement.addClass('toast-progress');
333                        $toastElement.prepend($progressElement);
334                    }
335                }
336
337                function shouldExit(options, map) {
338                    if (options.preventDuplicates) {
339                        if (map.message === previousToast) {
340                            return true;
341                        } else {
342                            previousToast = map.message;
343                        }
344                    }
345                    return false;
346                }
347
348                function hideToast(override) {
349                    if ($(':focus', $toastElement).length && !override) {
350                        return;
351                    }
352                    clearTimeout(progressBar.intervalId);
353                    return $toastElement[options.hideMethod]({
354                        duration: options.hideDuration,
355                        easing: options.hideEasing,
356                        complete: function () {
357                            removeToast($toastElement);
358                            if (options.onHidden && response.state !== 'hidden') {
359                                options.onHidden();
360                            }
361                            response.state = 'hidden';
362                            response.endTime = new Date();
363                            publish(response);
364                        }
365                    });
366                }
367
368                function delayedHideToast() {
369                    if (options.timeOut > 0 || options.extendedTimeOut > 0) {
370                        intervalId = setTimeout(hideToast, options.extendedTimeOut);
371                        progressBar.maxHideTime = parseFloat(options.extendedTimeOut);
372                        progressBar.hideEta = new Date().getTime() + progressBar.maxHideTime;
373                    }
374                }
375
376                function stickAround() {
377                    clearTimeout(intervalId);
378                    progressBar.hideEta = 0;
379                    $toastElement.stop(true, true)[options.showMethod](
380                        {duration: options.showDuration, easing: options.showEasing}
381                    );
382                }
383
384                function updateProgress() {
385                    var percentage = ((progressBar.hideEta - (new Date().getTime())) / progressBar.maxHideTime) * 100;
386                    $progressElement.width(percentage + '%');
387                }
388            }
389
390            function getOptions() {
391                return $.extend({}, getDefaults(), toastr.options);
392            }
393
394            function removeToast($toastElement) {
395                if (!$container) { $container = getContainer(); }
396                if ($toastElement.is(':visible')) {
397                    return;
398                }
399                $toastElement.remove();
400                $toastElement = null;
401                if ($container.children().length === 0) {
402                    $container.remove();
403                    previousToast = undefined;
404                }
405            }
406
407        })();
408    });
409}(typeof define === 'function' && define.amd ? define : function (deps, factory) {
410    if (typeof module !== 'undefined' && module.exports) { //Node
411        module.exports = factory(require('jquery'));
412    } else {
413        window['toastr'] = factory(window['jQuery']);
414    }
415}));