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