1/* 2 * Toastr 3 * Version 2.0.1 4 * Copyright 2012 John Papa and Hans Fjällemark. 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 * Author: John Papa and Hans Fjällemark 10 * Project: https://github.com/CodeSeven/toastr 11 */ 12; (function (define) { 13 define(['jquery'], function ($) { 14 return (function () { 15 var version = '2.0.1'; 16 var $container; 17 var listener; 18 var toastId = 0; 19 var toastType = { 20 error: 'error', 21 info: 'info', 22 success: 'success', 23 warning: 'warning' 24 }; 25 26 var toastr = { 27 clear: clear, 28 error: error, 29 getContainer: getContainer, 30 info: info, 31 options: {}, 32 subscribe: subscribe, 33 success: success, 34 version: version, 35 warning: warning 36 }; 37 38 return toastr; 39 40 //#region Accessible Methods 41 function error(message, title, optionsOverride) { 42 return notify({ 43 type: toastType.error, 44 iconClass: getOptions().iconClasses.error, 45 message: message, 46 optionsOverride: optionsOverride, 47 title: title 48 }); 49 } 50 51 function info(message, title, optionsOverride) { 52 return notify({ 53 type: toastType.info, 54 iconClass: getOptions().iconClasses.info, 55 message: message, 56 optionsOverride: optionsOverride, 57 title: title 58 }); 59 } 60 61 function subscribe(callback) { 62 listener = callback; 63 } 64 65 function success(message, title, optionsOverride) { 66 return notify({ 67 type: toastType.success, 68 iconClass: getOptions().iconClasses.success, 69 message: message, 70 optionsOverride: optionsOverride, 71 title: title 72 }); 73 } 74 75 function warning(message, title, optionsOverride) { 76 return notify({ 77 type: toastType.warning, 78 iconClass: getOptions().iconClasses.warning, 79 message: message, 80 optionsOverride: optionsOverride, 81 title: title 82 }); 83 } 84 85 function clear($toastElement) { 86 var options = getOptions(); 87 if (!$container) { getContainer(options); } 88 if ($toastElement && $(':focus', $toastElement).length === 0) { 89 $toastElement[options.hideMethod]({ 90 duration: options.hideDuration, 91 easing: options.hideEasing, 92 complete: function () { removeToast($toastElement); } 93 }); 94 return; 95 } 96 if ($container.children().length) { 97 $container[options.hideMethod]({ 98 duration: options.hideDuration, 99 easing: options.hideEasing, 100 complete: function () { $container.remove(); } 101 }); 102 } 103 } 104 //#endregion 105 106 //#region Internal Methods 107 108 function getDefaults() { 109 return { 110 tapToDismiss: true, 111 toastClass: 'toast', 112 containerId: 'toast-container', 113 debug: false, 114 115 showMethod: 'fadeIn', //fadeIn, slideDown, and show are built into jQuery 116 showDuration: 300, 117 showEasing: 'swing', //swing and linear are built into jQuery 118 onShown: undefined, 119 hideMethod: 'fadeOut', 120 hideDuration: 1000, 121 hideEasing: 'swing', 122 onHidden: undefined, 123 124 extendedTimeOut: 1000, 125 iconClasses: { 126 error: 'toast-error', 127 info: 'toast-info', 128 success: 'toast-success', 129 warning: 'toast-warning' 130 }, 131 iconClass: 'toast-info', 132 positionClass: 'toast-top-right', // toast-top-full-width 133 timeOut: 10000, // Set timeOut and extendedTimeout to 0 to make it sticky 134 titleClass: 'toast-title', 135 messageClass: 'toast-message', 136 target: 'body', 137 closeHtml: '<button>×</button>', 138 newestOnTop: true 139 }; 140 } 141 142 function publish(args) { 143 if (!listener) { 144 return; 145 } 146 listener(args); 147 } 148 149 function notify(map) { 150 var 151 options = getOptions(), 152 iconClass = map.iconClass || options.iconClass; 153 154 if (typeof (map.optionsOverride) !== 'undefined') { 155 options = $.extend(options, map.optionsOverride); 156 iconClass = map.optionsOverride.iconClass || iconClass; 157 } 158 159 toastId++; 160 161 $container = getContainer(options); 162 var 163 intervalId = null, 164 $toastElement = $('<div/>'), 165 $titleElement = $('<div/>'), 166 $messageElement = $('<div/>'), 167 $closeElement = $(options.closeHtml), 168 response = { 169 toastId: toastId, 170 state: 'visible', 171 startTime: new Date(), 172 options: options, 173 map: map 174 }; 175 176 if (map.iconClass) { 177 $toastElement.addClass(options.toastClass).addClass(iconClass); 178 } 179 180 if (map.title) { 181 $titleElement.append(map.title).addClass(options.titleClass); 182 $toastElement.append($titleElement); 183 } 184 185 if (map.message) { 186 $messageElement.append(map.message).addClass(options.messageClass); 187 $toastElement.append($messageElement); 188 } 189 190 if (options.closeButton) { 191 $closeElement.addClass('toast-close-button');
192 $toastElement.prepend($closeElement); 193 } 194 195 $toastElement.hide(); 196 if (options.newestOnTop) { 197 $container.prepend($toastElement); 198 } else { 199 $container.append($toastElement); 200 } 201 202 203 $toastElement[options.showMethod]( 204 { duration: options.showDuration, easing: options.showEasing, complete: options.onShown } 205 ); 206 if (options.timeOut > 0) { 207 intervalId = setTimeout(hideToast, options.timeOut); 208 } 209 210 $toastElement.hover(stickAround, delayedhideToast); 211 if (!options.onclick && options.tapToDismiss) { 212 $toastElement.click(hideToast); 213 } 214 if (options.closeButton && $closeElement) { 215 $closeElement.click(function (event) { 216 if( event.stopPropagation ) { 217 event.stopPropagation(); 218 } else if( event.cancelBubble !== undefined && event.cancelBubble !== true ) { 219 event.cancelBubble = true; 220 } 221 hideToast(true); 222 }); 223 } 224 225 if (options.onclick) { 226 $toastElement.click(function () { 227 options.onclick(); 228 hideToast(); 229 }); 230 } 231 232 publish(response); 233 234 if (options.debug && console) { 235 console.log(response); 236 } 237 238 return $toastElement; 239 240 function hideToast(override) { 241 if ($(':focus', $toastElement).length && !override) { 242 return; 243 } 244 return $toastElement[options.hideMethod]({ 245 duration: options.hideDuration, 246 easing: options.hideEasing, 247 complete: function () { 248 removeToast($toastElement); 249 if (options.onHidden) { 250 options.onHidden(); 251 } 252 response.state = 'hidden'; 253 response.endTime = new Date(), 254 publish(response); 255 } 256 }); 257 } 258 259 function delayedhideToast() { 260 if (options.timeOut > 0 || options.extendedTimeOut > 0) { 261 intervalId = setTimeout(hideToast, options.extendedTimeOut); 262 } 263 } 264 265 function stickAround() { 266 clearTimeout(intervalId); 267 $toastElement.stop(true, true)[options.showMethod]( 268 { duration: options.showDuration, easing: options.showEasing } 269 ); 270 } 271 } 272 function getContainer(options) { 273 if (!options) { options = getOptions(); } 274 $container = $('#' + options.containerId); 275 if ($container.length) { 276 return $container; 277 }
278 $container = $('<div/>') 279 .attr('id', options.containerId) 280 .addClass(options.positionClass); 281 $container.appendTo($(options.target)); 282 return $container; 283 } 284 285 function getOptions() { 286 return $.extend({}, getDefaults(), toastr.options); 287 } 288 289 function removeToast($toastElement) { 290 if (!$container) { $container = getContainer(); } 291 if ($toastElement.is(':visible')) { 292 return; 293 } 294 $toastElement.remove(); 295 $toastElement = null; 296 if ($container.children().length === 0) { 297 $container.remove(); 298 } 299 } 300 //#endregion 301 302 })(); 303 }); 304}(typeof define === 'function' && define.amd ? define : function (deps, factory) { 305 if (typeof module !== 'undefined' && module.exports) { //Node 306 module.exports = factory(require('jquery')); 307 } else { 308 window['toastr'] = factory(window['jQuery']); 309 } 310}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.