PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/UIDS_AADHAAR.Cliente/scripts/toastr.js

https://gitlab.com/isaias-och/UIDS_IOCH
JavaScript | 374 lines | 313 code | 45 blank | 16 comment | 52 complexity | e8646af715cb6c278d63a3e952f72eeb MD5 | raw file
  1. /*
  2. * Toastr
  3. * Copyright 2012-2014
  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. ; (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. var toastr = {
  26. clear: clear,
  27. remove: remove,
  28. error: error,
  29. getContainer: getContainer,
  30. info: info,
  31. options: {},
  32. subscribe: subscribe,
  33. success: success,
  34. version: '2.1.0',
  35. warning: warning
  36. };
  37. var previousToast;
  38. return toastr;
  39. //#region Accessible Methods
  40. function error(message, title, optionsOverride) {
  41. return notify({
  42. type: toastType.error,
  43. iconClass: getOptions().iconClasses.error,
  44. message: message,
  45. optionsOverride: optionsOverride,
  46. title: title
  47. });
  48. }
  49. function getContainer(options, create) {
  50. if (!options) { options = getOptions(); }
  51. $container = $('#' + options.containerId);
  52. if ($container.length) {
  53. return $container;
  54. }
  55. if (create) {
  56. $container = createContainer(options);
  57. }
  58. return $container;
  59. }
  60. function info(message, title, optionsOverride) {
  61. return notify({
  62. type: toastType.info,
  63. iconClass: getOptions().iconClasses.info,
  64. message: message,
  65. optionsOverride: optionsOverride,
  66. title: title
  67. });
  68. }
  69. function subscribe(callback) {
  70. listener = callback;
  71. }
  72. function success(message, title, optionsOverride) {
  73. return notify({
  74. type: toastType.success,
  75. iconClass: getOptions().iconClasses.success,
  76. message: message,
  77. optionsOverride: optionsOverride,
  78. title: title
  79. });
  80. }
  81. function warning(message, title, optionsOverride) {
  82. return notify({
  83. type: toastType.warning,
  84. iconClass: getOptions().iconClasses.warning,
  85. message: message,
  86. optionsOverride: optionsOverride,
  87. title: title
  88. });
  89. }
  90. function clear($toastElement) {
  91. var options = getOptions();
  92. if (!$container) { getContainer(options); }
  93. if (!clearToast($toastElement, options)) {
  94. clearContainer(options);
  95. }
  96. }
  97. function remove($toastElement) {
  98. var options = getOptions();
  99. if (!$container) { getContainer(options); }
  100. if ($toastElement && $(':focus', $toastElement).length === 0) {
  101. removeToast($toastElement);
  102. return;
  103. }
  104. if ($container.children().length) {
  105. $container.remove();
  106. }
  107. }
  108. //#endregion
  109. //#region Internal Methods
  110. function clearContainer (options) {
  111. var toastsToClear = $container.children();
  112. for (var i = toastsToClear.length - 1; i >= 0; i--) {
  113. clearToast($(toastsToClear[i]), options);
  114. }
  115. }
  116. function clearToast ($toastElement, options) {
  117. if ($toastElement && $(':focus', $toastElement).length === 0) {
  118. $toastElement[options.hideMethod]({
  119. duration: options.hideDuration,
  120. easing: options.hideEasing,
  121. complete: function () { removeToast($toastElement); }
  122. });
  123. return true;
  124. }
  125. return false;
  126. }
  127. function createContainer(options) {
  128. $container = $('<div/>')
  129. .attr('id', options.containerId)
  130. .addClass(options.positionClass)
  131. .attr('aria-live', 'polite')
  132. .attr('role', 'alert');
  133. $container.appendTo($(options.target));
  134. return $container;
  135. }
  136. function getDefaults() {
  137. return {
  138. tapToDismiss: true,
  139. toastClass: 'toast',
  140. containerId: 'toast-container',
  141. debug: false,
  142. showMethod: 'fadeIn', //fadeIn, slideDown, and show are built into jQuery
  143. showDuration: 300,
  144. showEasing: 'swing', //swing and linear are built into jQuery
  145. onShown: undefined,
  146. hideMethod: 'fadeOut',
  147. hideDuration: 1000,
  148. hideEasing: 'swing',
  149. onHidden: undefined,
  150. extendedTimeOut: 1000,
  151. iconClasses: {
  152. error: 'toast-error',
  153. info: 'toast-info',
  154. success: 'toast-success',
  155. warning: 'toast-warning'
  156. },
  157. iconClass: 'toast-info',
  158. positionClass: 'toast-top-right',
  159. timeOut: 5000, // Set timeOut and extendedTimeOut to 0 to make it sticky
  160. titleClass: 'toast-title',
  161. messageClass: 'toast-message',
  162. target: 'body',
  163. closeHtml: '<button>&times;</button>',
  164. newestOnTop: true,
  165. preventDuplicates: false,
  166. progressBar: false
  167. };
  168. }
  169. function publish(args) {
  170. if (!listener) { return; }
  171. listener(args);
  172. }
  173. function notify(map) {
  174. var options = getOptions(),
  175. iconClass = map.iconClass || options.iconClass;
  176. if (options.preventDuplicates) {
  177. if (map.message === previousToast) {
  178. return;
  179. } else {
  180. previousToast = map.message;
  181. }
  182. }
  183. if (typeof (map.optionsOverride) !== 'undefined') {
  184. options = $.extend(options, map.optionsOverride);
  185. iconClass = map.optionsOverride.iconClass || iconClass;
  186. }
  187. toastId++;
  188. $container = getContainer(options, true);
  189. var intervalId = null,
  190. $toastElement = $('<div/>'),
  191. $titleElement = $('<div/>'),
  192. $messageElement = $('<div/>'),
  193. $progressElement = $('<div/>'),
  194. $closeElement = $(options.closeHtml),
  195. progressBar = {
  196. intervalId: null,
  197. hideEta: null,
  198. maxHideTime: null
  199. },
  200. response = {
  201. toastId: toastId,
  202. state: 'visible',
  203. startTime: new Date(),
  204. options: options,
  205. map: map
  206. };
  207. if (map.iconClass) {
  208. $toastElement.addClass(options.toastClass).addClass(iconClass);
  209. }
  210. if (map.title) {
  211. $titleElement.append(map.title).addClass(options.titleClass);
  212. $toastElement.append($titleElement);
  213. }
  214. if (map.message) {
  215. $messageElement.append(map.message).addClass(options.messageClass);
  216. $toastElement.append($messageElement);
  217. }
  218. if (options.closeButton) {
  219. $closeElement.addClass('toast-close-button').attr('role', 'button');
  220. $toastElement.prepend($closeElement);
  221. }
  222. if (options.progressBar) {
  223. $progressElement.addClass('toast-progress');
  224. $toastElement.prepend($progressElement);
  225. }
  226. $toastElement.hide();
  227. if (options.newestOnTop) {
  228. $container.prepend($toastElement);
  229. } else {
  230. $container.append($toastElement);
  231. }
  232. $toastElement[options.showMethod](
  233. {duration: options.showDuration, easing: options.showEasing, complete: options.onShown}
  234. );
  235. if (options.timeOut > 0) {
  236. intervalId = setTimeout(hideToast, options.timeOut);
  237. progressBar.maxHideTime = parseFloat(options.timeOut);
  238. progressBar.hideEta = new Date().getTime() + progressBar.maxHideTime;
  239. if (options.progressBar) {
  240. progressBar.intervalId = setInterval(updateProgress, 10);
  241. }
  242. }
  243. $toastElement.hover(stickAround, delayedHideToast);
  244. if (!options.onclick && options.tapToDismiss) {
  245. $toastElement.click(hideToast);
  246. }
  247. if (options.closeButton && $closeElement) {
  248. $closeElement.click(function (event) {
  249. if (event.stopPropagation) {
  250. event.stopPropagation();
  251. } else if (event.cancelBubble !== undefined && event.cancelBubble !== true) {
  252. event.cancelBubble = true;
  253. }
  254. hideToast(true);
  255. });
  256. }
  257. if (options.onclick) {
  258. $toastElement.click(function () {
  259. options.onclick();
  260. hideToast();
  261. });
  262. }
  263. publish(response);
  264. if (options.debug && console) {
  265. console.log(response);
  266. }
  267. return $toastElement;
  268. function hideToast(override) {
  269. if ($(':focus', $toastElement).length && !override) {
  270. return;
  271. }
  272. clearTimeout(progressBar.intervalId);
  273. return $toastElement[options.hideMethod]({
  274. duration: options.hideDuration,
  275. easing: options.hideEasing,
  276. complete: function () {
  277. removeToast($toastElement);
  278. if (options.onHidden && response.state !== 'hidden') {
  279. options.onHidden();
  280. }
  281. response.state = 'hidden';
  282. response.endTime = new Date();
  283. publish(response);
  284. }
  285. });
  286. }
  287. function delayedHideToast() {
  288. if (options.timeOut > 0 || options.extendedTimeOut > 0) {
  289. intervalId = setTimeout(hideToast, options.extendedTimeOut);
  290. progressBar.maxHideTime = parseFloat(options.extendedTimeOut);
  291. progressBar.hideEta = new Date().getTime() + progressBar.maxHideTime;
  292. }
  293. }
  294. function stickAround() {
  295. clearTimeout(intervalId);
  296. progressBar.hideEta = 0;
  297. $toastElement.stop(true, true)[options.showMethod](
  298. {duration: options.showDuration, easing: options.showEasing}
  299. );
  300. }
  301. function updateProgress() {
  302. var percentage = ((progressBar.hideEta - (new Date().getTime())) / progressBar.maxHideTime) * 100;
  303. $progressElement.width(percentage + '%');
  304. }
  305. }
  306. function getOptions() {
  307. return $.extend({}, getDefaults(), toastr.options);
  308. }
  309. function removeToast($toastElement) {
  310. if (!$container) { $container = getContainer(); }
  311. if ($toastElement.is(':visible')) {
  312. return;
  313. }
  314. $toastElement.remove();
  315. $toastElement = null;
  316. if ($container.children().length === 0) {
  317. $container.remove();
  318. }
  319. }
  320. //#endregion
  321. })();
  322. });
  323. }(typeof define === 'function' && define.amd ? define : function (deps, factory) {
  324. if (typeof module !== 'undefined' && module.exports) { //Node
  325. module.exports = factory(require('jquery'));
  326. } else {
  327. window['toastr'] = factory(window['jQuery']);
  328. }
  329. }));