/wp-content/plugins/wp-user-frontend/assets/vendor/sweetalert2/dist/sweetalert2.js

https://bitbucket.org/drennio12/vacanze-animali · JavaScript · 1641 lines · 1301 code · 207 blank · 133 comment · 288 complexity · b99b75f38de7217ec6ed1533c307f916 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*!
  2. * sweetalert2 v6.6.4
  3. * Released under the MIT License.
  4. */
  5. (function (global, factory) {
  6. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  7. typeof define === 'function' && define.amd ? define(factory) :
  8. (global.Sweetalert2 = factory());
  9. }(this, (function () { 'use strict';
  10. var defaultParams = {
  11. title: '',
  12. titleText: '',
  13. text: '',
  14. html: '',
  15. type: null,
  16. customClass: '',
  17. target: 'body',
  18. animation: true,
  19. allowOutsideClick: true,
  20. allowEscapeKey: true,
  21. allowEnterKey: true,
  22. showConfirmButton: true,
  23. showCancelButton: false,
  24. preConfirm: null,
  25. confirmButtonText: 'OK',
  26. confirmButtonColor: '#3085d6',
  27. confirmButtonClass: null,
  28. cancelButtonText: 'Cancel',
  29. cancelButtonColor: '#aaa',
  30. cancelButtonClass: null,
  31. buttonsStyling: true,
  32. reverseButtons: false,
  33. focusCancel: false,
  34. showCloseButton: false,
  35. showLoaderOnConfirm: false,
  36. imageUrl: null,
  37. imageWidth: null,
  38. imageHeight: null,
  39. imageClass: null,
  40. timer: null,
  41. width: 500,
  42. padding: 20,
  43. background: '#fff',
  44. input: null,
  45. inputPlaceholder: '',
  46. inputValue: '',
  47. inputOptions: {},
  48. inputAutoTrim: true,
  49. inputClass: null,
  50. inputAttributes: {},
  51. inputValidator: null,
  52. progressSteps: [],
  53. currentProgressStep: null,
  54. progressStepsDistance: '40px',
  55. onOpen: null,
  56. onClose: null,
  57. useRejections: true
  58. };
  59. var swalPrefix = 'swal2-';
  60. var prefix = function prefix(items) {
  61. var result = {};
  62. for (var i in items) {
  63. result[items[i]] = swalPrefix + items[i];
  64. }
  65. return result;
  66. };
  67. var swalClasses = prefix(['container', 'shown', 'iosfix', 'modal', 'overlay', 'fade', 'show', 'hide', 'noanimation', 'close', 'title', 'content', 'buttonswrapper', 'confirm', 'cancel', 'icon', 'image', 'input', 'file', 'range', 'select', 'radio', 'checkbox', 'textarea', 'inputerror', 'validationerror', 'progresssteps', 'activeprogressstep', 'progresscircle', 'progressline', 'loading', 'styled']);
  68. var iconTypes = prefix(['success', 'warning', 'info', 'question', 'error']);
  69. /*
  70. * Set hover, active and focus-states for buttons (source: http://www.sitepoint.com/javascript-generate-lighter-darker-color)
  71. */
  72. var colorLuminance = function colorLuminance(hex, lum) {
  73. // Validate hex string
  74. hex = String(hex).replace(/[^0-9a-f]/gi, '');
  75. if (hex.length < 6) {
  76. hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
  77. }
  78. lum = lum || 0;
  79. // Convert to decimal and change luminosity
  80. var rgb = '#';
  81. for (var i = 0; i < 3; i++) {
  82. var c = parseInt(hex.substr(i * 2, 2), 16);
  83. c = Math.round(Math.min(Math.max(0, c + c * lum), 255)).toString(16);
  84. rgb += ('00' + c).substr(c.length);
  85. }
  86. return rgb;
  87. };
  88. var uniqueArray = function uniqueArray(arr) {
  89. var result = [];
  90. for (var i in arr) {
  91. if (result.indexOf(arr[i]) === -1) {
  92. result.push(arr[i]);
  93. }
  94. }
  95. return result;
  96. };
  97. /* global MouseEvent */
  98. // Remember state in cases where opening and handling a modal will fiddle with it.
  99. var states = {
  100. previousWindowKeyDown: null,
  101. previousActiveElement: null,
  102. previousBodyPadding: null
  103. };
  104. /*
  105. * Add modal + overlay to DOM
  106. */
  107. var init = function init(params) {
  108. if (typeof document === 'undefined') {
  109. console.error('SweetAlert2 requires document to initialize');
  110. return;
  111. }
  112. var container = document.createElement('div');
  113. container.className = swalClasses.container;
  114. container.innerHTML = sweetHTML;
  115. var targetElement = document.querySelector(params.target);
  116. if (!targetElement) {
  117. console.warn('SweetAlert2: Can\'t find the target "' + params.target + '"');
  118. targetElement = document.body;
  119. }
  120. targetElement.appendChild(container);
  121. var modal = getModal();
  122. var input = getChildByClass(modal, swalClasses.input);
  123. var file = getChildByClass(modal, swalClasses.file);
  124. var range = modal.querySelector('.' + swalClasses.range + ' input');
  125. var rangeOutput = modal.querySelector('.' + swalClasses.range + ' output');
  126. var select = getChildByClass(modal, swalClasses.select);
  127. var checkbox = modal.querySelector('.' + swalClasses.checkbox + ' input');
  128. var textarea = getChildByClass(modal, swalClasses.textarea);
  129. input.oninput = function () {
  130. sweetAlert.resetValidationError();
  131. };
  132. input.onkeydown = function (event) {
  133. setTimeout(function () {
  134. if (event.keyCode === 13 && params.allowEnterKey) {
  135. event.stopPropagation();
  136. sweetAlert.clickConfirm();
  137. }
  138. }, 0);
  139. };
  140. file.onchange = function () {
  141. sweetAlert.resetValidationError();
  142. };
  143. range.oninput = function () {
  144. sweetAlert.resetValidationError();
  145. rangeOutput.value = range.value;
  146. };
  147. range.onchange = function () {
  148. sweetAlert.resetValidationError();
  149. range.previousSibling.value = range.value;
  150. };
  151. select.onchange = function () {
  152. sweetAlert.resetValidationError();
  153. };
  154. checkbox.onchange = function () {
  155. sweetAlert.resetValidationError();
  156. };
  157. textarea.oninput = function () {
  158. sweetAlert.resetValidationError();
  159. };
  160. return modal;
  161. };
  162. /*
  163. * Manipulate DOM
  164. */
  165. var sweetHTML = ('\n <div role="dialog" aria-labelledby="' + swalClasses.title + '" aria-describedby="' + swalClasses.content + '" class="' + swalClasses.modal + '" tabindex="-1">\n <ul class="' + swalClasses.progresssteps + '"></ul>\n <div class="' + swalClasses.icon + ' ' + iconTypes.error + '">\n <span class="swal2-x-mark"><span class="swal2-x-mark-line-left"></span><span class="swal2-x-mark-line-right"></span></span>\n </div>\n <div class="' + swalClasses.icon + ' ' + iconTypes.question + '">?</div>\n <div class="' + swalClasses.icon + ' ' + iconTypes.warning + '">!</div>\n <div class="' + swalClasses.icon + ' ' + iconTypes.info + '">i</div>\n <div class="' + swalClasses.icon + ' ' + iconTypes.success + '">\n <div class="swal2-success-circular-line-left"></div>\n <span class="swal2-success-line-tip"></span> <span class="swal2-success-line-long"></span>\n <div class="swal2-success-ring"></div> <div class="swal2-success-fix"></div>\n <div class="swal2-success-circular-line-right"></div>\n </div>\n <img class="' + swalClasses.image + '">\n <h2 class="' + swalClasses.title + '" id="' + swalClasses.title + '"></h2>\n <div id="' + swalClasses.content + '" class="' + swalClasses.content + '"></div>\n <input class="' + swalClasses.input + '">\n <input type="file" class="' + swalClasses.file + '">\n <div class="' + swalClasses.range + '">\n <output></output>\n <input type="range">\n </div>\n <select class="' + swalClasses.select + '"></select>\n <div class="' + swalClasses.radio + '"></div>\n <label for="' + swalClasses.checkbox + '" class="' + swalClasses.checkbox + '">\n <input type="checkbox">\n </label>\n <textarea class="' + swalClasses.textarea + '"></textarea>\n <div class="' + swalClasses.validationerror + '"></div>\n <div class="' + swalClasses.buttonswrapper + '">\n <button type="button" class="' + swalClasses.confirm + '">OK</button>\n <button type="button" class="' + swalClasses.cancel + '">Cancel</button>\n </div>\n <button type="button" class="' + swalClasses.close + '" aria-label="Close this dialog">&times;</button>\n </div>\n').replace(/(^|\n)\s*/g, '');
  166. var getContainer = function getContainer() {
  167. return document.body.querySelector('.' + swalClasses.container);
  168. };
  169. var getModal = function getModal() {
  170. return getContainer() ? getContainer().querySelector('.' + swalClasses.modal) : null;
  171. };
  172. var getIcons = function getIcons() {
  173. var modal = getModal();
  174. return modal.querySelectorAll('.' + swalClasses.icon);
  175. };
  176. var elementByClass = function elementByClass(className) {
  177. return getContainer() ? getContainer().querySelector('.' + className) : null;
  178. };
  179. var getTitle = function getTitle() {
  180. return elementByClass(swalClasses.title);
  181. };
  182. var getContent = function getContent() {
  183. return elementByClass(swalClasses.content);
  184. };
  185. var getImage = function getImage() {
  186. return elementByClass(swalClasses.image);
  187. };
  188. var getButtonsWrapper = function getButtonsWrapper() {
  189. return elementByClass(swalClasses.buttonswrapper);
  190. };
  191. var getProgressSteps = function getProgressSteps() {
  192. return elementByClass(swalClasses.progresssteps);
  193. };
  194. var getValidationError = function getValidationError() {
  195. return elementByClass(swalClasses.validationerror);
  196. };
  197. var getConfirmButton = function getConfirmButton() {
  198. return elementByClass(swalClasses.confirm);
  199. };
  200. var getCancelButton = function getCancelButton() {
  201. return elementByClass(swalClasses.cancel);
  202. };
  203. var getCloseButton = function getCloseButton() {
  204. return elementByClass(swalClasses.close);
  205. };
  206. var getFocusableElements = function getFocusableElements(focusCancel) {
  207. var buttons = [getConfirmButton(), getCancelButton()];
  208. if (focusCancel) {
  209. buttons.reverse();
  210. }
  211. var focusableElements = buttons.concat(Array.prototype.slice.call(getModal().querySelectorAll('button, input:not([type=hidden]), textarea, select, a, *[tabindex]:not([tabindex="-1"])')));
  212. return uniqueArray(focusableElements);
  213. };
  214. var hasClass = function hasClass(elem, className) {
  215. if (elem.classList) {
  216. return elem.classList.contains(className);
  217. }
  218. return false;
  219. };
  220. var focusInput = function focusInput(input) {
  221. input.focus();
  222. // place cursor at end of text in text input
  223. if (input.type !== 'file') {
  224. // http://stackoverflow.com/a/2345915/1331425
  225. var val = input.value;
  226. input.value = '';
  227. input.value = val;
  228. }
  229. };
  230. var addClass = function addClass(elem, className) {
  231. if (!elem || !className) {
  232. return;
  233. }
  234. var classes = className.split(/\s+/).filter(Boolean);
  235. classes.forEach(function (className) {
  236. elem.classList.add(className);
  237. });
  238. };
  239. var removeClass = function removeClass(elem, className) {
  240. if (!elem || !className) {
  241. return;
  242. }
  243. var classes = className.split(/\s+/).filter(Boolean);
  244. classes.forEach(function (className) {
  245. elem.classList.remove(className);
  246. });
  247. };
  248. var getChildByClass = function getChildByClass(elem, className) {
  249. for (var i = 0; i < elem.childNodes.length; i++) {
  250. if (hasClass(elem.childNodes[i], className)) {
  251. return elem.childNodes[i];
  252. }
  253. }
  254. };
  255. var show = function show(elem, display) {
  256. if (!display) {
  257. display = 'block';
  258. }
  259. elem.style.opacity = '';
  260. elem.style.display = display;
  261. };
  262. var hide = function hide(elem) {
  263. elem.style.opacity = '';
  264. elem.style.display = 'none';
  265. };
  266. var empty = function empty(elem) {
  267. while (elem.firstChild) {
  268. elem.removeChild(elem.firstChild);
  269. }
  270. };
  271. // borrowed from jqeury $(elem).is(':visible') implementation
  272. var isVisible = function isVisible(elem) {
  273. return elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length;
  274. };
  275. var removeStyleProperty = function removeStyleProperty(elem, property) {
  276. if (elem.style.removeProperty) {
  277. elem.style.removeProperty(property);
  278. } else {
  279. elem.style.removeAttribute(property);
  280. }
  281. };
  282. var fireClick = function fireClick(node) {
  283. if (!isVisible(node)) {
  284. return false;
  285. }
  286. // Taken from http://www.nonobtrusive.com/2011/11/29/programatically-fire-crossbrowser-click-event-with-javascript/
  287. // Then fixed for today's Chrome browser.
  288. if (typeof MouseEvent === 'function') {
  289. // Up-to-date approach
  290. var mevt = new MouseEvent('click', {
  291. view: window,
  292. bubbles: false,
  293. cancelable: true
  294. });
  295. node.dispatchEvent(mevt);
  296. } else if (document.createEvent) {
  297. // Fallback
  298. var evt = document.createEvent('MouseEvents');
  299. evt.initEvent('click', false, false);
  300. node.dispatchEvent(evt);
  301. } else if (document.createEventObject) {
  302. node.fireEvent('onclick');
  303. } else if (typeof node.onclick === 'function') {
  304. node.onclick();
  305. }
  306. };
  307. var animationEndEvent = function () {
  308. var testEl = document.createElement('div');
  309. var transEndEventNames = {
  310. 'WebkitAnimation': 'webkitAnimationEnd',
  311. 'OAnimation': 'oAnimationEnd oanimationend',
  312. 'msAnimation': 'MSAnimationEnd',
  313. 'animation': 'animationend'
  314. };
  315. for (var i in transEndEventNames) {
  316. if (transEndEventNames.hasOwnProperty(i) && testEl.style[i] !== undefined) {
  317. return transEndEventNames[i];
  318. }
  319. }
  320. return false;
  321. }();
  322. // Reset previous window keydown handler and focued element
  323. var resetPrevState = function resetPrevState() {
  324. window.onkeydown = states.previousWindowKeyDown;
  325. if (states.previousActiveElement && states.previousActiveElement.focus) {
  326. var x = window.scrollX;
  327. var y = window.scrollY;
  328. states.previousActiveElement.focus();
  329. if (x && y) {
  330. // IE has no scrollX/scrollY support
  331. window.scrollTo(x, y);
  332. }
  333. }
  334. };
  335. // Measure width of scrollbar
  336. // https://github.com/twbs/bootstrap/blob/master/js/modal.js#L279-L286
  337. var measureScrollbar = function measureScrollbar() {
  338. var supportsTouch = 'ontouchstart' in window || navigator.msMaxTouchPoints;
  339. if (supportsTouch) {
  340. return 0;
  341. }
  342. var scrollDiv = document.createElement('div');
  343. scrollDiv.style.width = '50px';
  344. scrollDiv.style.height = '50px';
  345. scrollDiv.style.overflow = 'scroll';
  346. document.body.appendChild(scrollDiv);
  347. var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
  348. document.body.removeChild(scrollDiv);
  349. return scrollbarWidth;
  350. };
  351. // JavaScript Debounce Function
  352. // Simplivied version of https://davidwalsh.name/javascript-debounce-function
  353. var debounce = function debounce(func, wait) {
  354. var timeout = void 0;
  355. return function () {
  356. var later = function later() {
  357. timeout = null;
  358. func();
  359. };
  360. clearTimeout(timeout);
  361. timeout = setTimeout(later, wait);
  362. };
  363. };
  364. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
  365. return typeof obj;
  366. } : function (obj) {
  367. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  368. };
  369. var _extends = Object.assign || function (target) {
  370. for (var i = 1; i < arguments.length; i++) {
  371. var source = arguments[i];
  372. for (var key in source) {
  373. if (Object.prototype.hasOwnProperty.call(source, key)) {
  374. target[key] = source[key];
  375. }
  376. }
  377. }
  378. return target;
  379. };
  380. var modalParams = _extends({}, defaultParams);
  381. var queue = [];
  382. var swal2Observer = void 0;
  383. /*
  384. * Set type, text and actions on modal
  385. */
  386. var setParameters = function setParameters(params) {
  387. var modal = getModal() || init(params);
  388. for (var param in params) {
  389. if (!defaultParams.hasOwnProperty(param) && param !== 'extraParams') {
  390. console.warn('SweetAlert2: Unknown parameter "' + param + '"');
  391. }
  392. }
  393. // Set modal width
  394. modal.style.width = typeof params.width === 'number' ? params.width + 'px' : params.width;
  395. modal.style.padding = params.padding + 'px';
  396. modal.style.background = params.background;
  397. var successIconParts = modal.querySelectorAll('[class^=swal2-success-circular-line], .swal2-success-fix');
  398. for (var i = 0; i < successIconParts.length; i++) {
  399. successIconParts[i].style.background = params.background;
  400. }
  401. var title = getTitle();
  402. var content = getContent();
  403. var buttonsWrapper = getButtonsWrapper();
  404. var confirmButton = getConfirmButton();
  405. var cancelButton = getCancelButton();
  406. var closeButton = getCloseButton();
  407. // Title
  408. if (params.titleText) {
  409. title.innerText = params.titleText;
  410. } else {
  411. title.innerHTML = params.title.split('\n').join('<br>');
  412. }
  413. // Content
  414. if (params.text || params.html) {
  415. if (_typeof(params.html) === 'object') {
  416. content.innerHTML = '';
  417. if (0 in params.html) {
  418. for (var _i = 0; _i in params.html; _i++) {
  419. content.appendChild(params.html[_i].cloneNode(true));
  420. }
  421. } else {
  422. content.appendChild(params.html.cloneNode(true));
  423. }
  424. } else if (params.html) {
  425. content.innerHTML = params.html;
  426. } else if (params.text) {
  427. content.textContent = params.text;
  428. }
  429. show(content);
  430. } else {
  431. hide(content);
  432. }
  433. // Close button
  434. if (params.showCloseButton) {
  435. show(closeButton);
  436. } else {
  437. hide(closeButton);
  438. }
  439. // Custom Class
  440. modal.className = swalClasses.modal;
  441. if (params.customClass) {
  442. addClass(modal, params.customClass);
  443. }
  444. // Progress steps
  445. var progressStepsContainer = getProgressSteps();
  446. var currentProgressStep = parseInt(params.currentProgressStep === null ? sweetAlert.getQueueStep() : params.currentProgressStep, 10);
  447. if (params.progressSteps.length) {
  448. show(progressStepsContainer);
  449. empty(progressStepsContainer);
  450. if (currentProgressStep >= params.progressSteps.length) {
  451. console.warn('SweetAlert2: Invalid currentProgressStep parameter, it should be less than progressSteps.length ' + '(currentProgressStep like JS arrays starts from 0)');
  452. }
  453. params.progressSteps.forEach(function (step, index) {
  454. var circle = document.createElement('li');
  455. addClass(circle, swalClasses.progresscircle);
  456. circle.innerHTML = step;
  457. if (index === currentProgressStep) {
  458. addClass(circle, swalClasses.activeprogressstep);
  459. }
  460. progressStepsContainer.appendChild(circle);
  461. if (index !== params.progressSteps.length - 1) {
  462. var line = document.createElement('li');
  463. addClass(line, swalClasses.progressline);
  464. line.style.width = params.progressStepsDistance;
  465. progressStepsContainer.appendChild(line);
  466. }
  467. });
  468. } else {
  469. hide(progressStepsContainer);
  470. }
  471. // Icon
  472. var icons = getIcons();
  473. for (var _i2 = 0; _i2 < icons.length; _i2++) {
  474. hide(icons[_i2]);
  475. }
  476. if (params.type) {
  477. var validType = false;
  478. for (var iconType in iconTypes) {
  479. if (params.type === iconType) {
  480. validType = true;
  481. break;
  482. }
  483. }
  484. if (!validType) {
  485. console.error('SweetAlert2: Unknown alert type: ' + params.type);
  486. return false;
  487. }
  488. var icon = modal.querySelector('.' + swalClasses.icon + '.' + iconTypes[params.type]);
  489. show(icon);
  490. // Animate icon
  491. if (params.animation) {
  492. switch (params.type) {
  493. case 'success':
  494. addClass(icon, 'swal2-animate-success-icon');
  495. addClass(icon.querySelector('.swal2-success-line-tip'), 'swal2-animate-success-line-tip');
  496. addClass(icon.querySelector('.swal2-success-line-long'), 'swal2-animate-success-line-long');
  497. break;
  498. case 'error':
  499. addClass(icon, 'swal2-animate-error-icon');
  500. addClass(icon.querySelector('.swal2-x-mark'), 'swal2-animate-x-mark');
  501. break;
  502. default:
  503. break;
  504. }
  505. }
  506. }
  507. // Custom image
  508. var image = getImage();
  509. if (params.imageUrl) {
  510. image.setAttribute('src', params.imageUrl);
  511. show(image);
  512. if (params.imageWidth) {
  513. image.setAttribute('width', params.imageWidth);
  514. } else {
  515. image.removeAttribute('width');
  516. }
  517. if (params.imageHeight) {
  518. image.setAttribute('height', params.imageHeight);
  519. } else {
  520. image.removeAttribute('height');
  521. }
  522. image.className = swalClasses.image;
  523. if (params.imageClass) {
  524. addClass(image, params.imageClass);
  525. }
  526. } else {
  527. hide(image);
  528. }
  529. // Cancel button
  530. if (params.showCancelButton) {
  531. cancelButton.style.display = 'inline-block';
  532. } else {
  533. hide(cancelButton);
  534. }
  535. // Confirm button
  536. if (params.showConfirmButton) {
  537. removeStyleProperty(confirmButton, 'display');
  538. } else {
  539. hide(confirmButton);
  540. }
  541. // Buttons wrapper
  542. if (!params.showConfirmButton && !params.showCancelButton) {
  543. hide(buttonsWrapper);
  544. } else {
  545. show(buttonsWrapper);
  546. }
  547. // Edit text on cancel and confirm buttons
  548. confirmButton.innerHTML = params.confirmButtonText;
  549. cancelButton.innerHTML = params.cancelButtonText;
  550. // Set buttons to selected background colors
  551. if (params.buttonsStyling) {
  552. confirmButton.style.backgroundColor = params.confirmButtonColor;
  553. cancelButton.style.backgroundColor = params.cancelButtonColor;
  554. }
  555. // Add buttons custom classes
  556. confirmButton.className = swalClasses.confirm;
  557. addClass(confirmButton, params.confirmButtonClass);
  558. cancelButton.className = swalClasses.cancel;
  559. addClass(cancelButton, params.cancelButtonClass);
  560. // Buttons styling
  561. if (params.buttonsStyling) {
  562. addClass(confirmButton, swalClasses.styled);
  563. addClass(cancelButton, swalClasses.styled);
  564. } else {
  565. removeClass(confirmButton, swalClasses.styled);
  566. removeClass(cancelButton, swalClasses.styled);
  567. confirmButton.style.backgroundColor = confirmButton.style.borderLeftColor = confirmButton.style.borderRightColor = '';
  568. cancelButton.style.backgroundColor = cancelButton.style.borderLeftColor = cancelButton.style.borderRightColor = '';
  569. }
  570. // CSS animation
  571. if (params.animation === true) {
  572. removeClass(modal, swalClasses.noanimation);
  573. } else {
  574. addClass(modal, swalClasses.noanimation);
  575. }
  576. };
  577. /*
  578. * Animations
  579. */
  580. var openModal = function openModal(animation, onComplete) {
  581. var container = getContainer();
  582. var modal = getModal();
  583. if (animation) {
  584. addClass(modal, swalClasses.show);
  585. addClass(container, swalClasses.fade);
  586. removeClass(modal, swalClasses.hide);
  587. } else {
  588. removeClass(modal, swalClasses.fade);
  589. }
  590. show(modal);
  591. // scrolling is 'hidden' until animation is done, after that 'auto'
  592. container.style.overflowY = 'hidden';
  593. if (animationEndEvent && !hasClass(modal, swalClasses.noanimation)) {
  594. modal.addEventListener(animationEndEvent, function swalCloseEventFinished() {
  595. modal.removeEventListener(animationEndEvent, swalCloseEventFinished);
  596. container.style.overflowY = 'auto';
  597. });
  598. } else {
  599. container.style.overflowY = 'auto';
  600. }
  601. addClass(document.documentElement, swalClasses.shown);
  602. addClass(document.body, swalClasses.shown);
  603. addClass(container, swalClasses.shown);
  604. fixScrollbar();
  605. iOSfix();
  606. states.previousActiveElement = document.activeElement;
  607. if (onComplete !== null && typeof onComplete === 'function') {
  608. setTimeout(function () {
  609. onComplete(modal);
  610. });
  611. }
  612. };
  613. var fixScrollbar = function fixScrollbar() {
  614. // for queues, do not do this more than once
  615. if (states.previousBodyPadding !== null) {
  616. return;
  617. }
  618. // if the body has overflow
  619. if (document.body.scrollHeight > window.innerHeight) {
  620. // add padding so the content doesn't shift after removal of scrollbar
  621. states.previousBodyPadding = document.body.style.paddingRight;
  622. document.body.style.paddingRight = measureScrollbar() + 'px';
  623. }
  624. };
  625. var undoScrollbar = function undoScrollbar() {
  626. if (states.previousBodyPadding !== null) {
  627. document.body.style.paddingRight = states.previousBodyPadding;
  628. states.previousBodyPadding = null;
  629. }
  630. };
  631. // Fix iOS scrolling http://stackoverflow.com/q/39626302/1331425
  632. var iOSfix = function iOSfix() {
  633. var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
  634. if (iOS && !hasClass(document.body, swalClasses.iosfix)) {
  635. var offset = document.body.scrollTop;
  636. document.body.style.top = offset * -1 + 'px';
  637. addClass(document.body, swalClasses.iosfix);
  638. }
  639. };
  640. var undoIOSfix = function undoIOSfix() {
  641. if (hasClass(document.body, swalClasses.iosfix)) {
  642. var offset = parseInt(document.body.style.top, 10);
  643. removeClass(document.body, swalClasses.iosfix);
  644. document.body.style.top = '';
  645. document.body.scrollTop = offset * -1;
  646. }
  647. };
  648. // SweetAlert entry point
  649. var sweetAlert = function sweetAlert() {
  650. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  651. args[_key] = arguments[_key];
  652. }
  653. if (args[0] === undefined) {
  654. console.error('SweetAlert2 expects at least 1 attribute!');
  655. return false;
  656. }
  657. var params = _extends({}, modalParams);
  658. switch (_typeof(args[0])) {
  659. case 'string':
  660. params.title = args[0];
  661. params.html = args[1];
  662. params.type = args[2];
  663. break;
  664. case 'object':
  665. _extends(params, args[0]);
  666. params.extraParams = args[0].extraParams;
  667. if (params.input === 'email' && params.inputValidator === null) {
  668. params.inputValidator = function (email) {
  669. return new Promise(function (resolve, reject) {
  670. var emailRegex = /^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
  671. if (emailRegex.test(email)) {
  672. resolve();
  673. } else {
  674. reject('Invalid email address');
  675. }
  676. });
  677. };
  678. }
  679. if (params.input === 'url' && params.inputValidator === null) {
  680. params.inputValidator = function (url) {
  681. return new Promise(function (resolve, reject) {
  682. var urlRegex = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/;
  683. if (urlRegex.test(url)) {
  684. resolve();
  685. } else {
  686. reject('Invalid URL');
  687. }
  688. });
  689. };
  690. }
  691. break;
  692. default:
  693. console.error('SweetAlert2: Unexpected type of argument! Expected "string" or "object", got ' + _typeof(args[0]));
  694. return false;
  695. }
  696. setParameters(params);
  697. var container = getContainer();
  698. var modal = getModal();
  699. return new Promise(function (resolve, reject) {
  700. // Close on timer
  701. if (params.timer) {
  702. modal.timeout = setTimeout(function () {
  703. sweetAlert.closeModal(params.onClose);
  704. if (params.useRejections) {
  705. reject('timer');
  706. } else {
  707. resolve({ dismiss: 'timer' });
  708. }
  709. }, params.timer);
  710. }
  711. // Get input element by specified type or, if type isn't specified, by params.input
  712. var getInput = function getInput(inputType) {
  713. inputType = inputType || params.input;
  714. if (!inputType) {
  715. return null;
  716. }
  717. switch (inputType) {
  718. case 'select':
  719. case 'textarea':
  720. case 'file':
  721. return getChildByClass(modal, swalClasses[inputType]);
  722. case 'checkbox':
  723. return modal.querySelector('.' + swalClasses.checkbox + ' input');
  724. case 'radio':
  725. return modal.querySelector('.' + swalClasses.radio + ' input:checked') || modal.querySelector('.' + swalClasses.radio + ' input:first-child');
  726. case 'range':
  727. return modal.querySelector('.' + swalClasses.range + ' input');
  728. default:
  729. return getChildByClass(modal, swalClasses.input);
  730. }
  731. };
  732. // Get the value of the modal input
  733. var getInputValue = function getInputValue() {
  734. var input = getInput();
  735. if (!input) {
  736. return null;
  737. }
  738. switch (params.input) {
  739. case 'checkbox':
  740. return input.checked ? 1 : 0;
  741. case 'radio':
  742. return input.checked ? input.value : null;
  743. case 'file':
  744. return input.files.length ? input.files[0] : null;
  745. default:
  746. return params.inputAutoTrim ? input.value.trim() : input.value;
  747. }
  748. };
  749. // input autofocus
  750. if (params.input) {
  751. setTimeout(function () {
  752. var input = getInput();
  753. if (input) {
  754. focusInput(input);
  755. }
  756. }, 0);
  757. }
  758. var confirm = function confirm(value) {
  759. if (params.showLoaderOnConfirm) {
  760. sweetAlert.showLoading();
  761. }
  762. if (params.preConfirm) {
  763. params.preConfirm(value, params.extraParams).then(function (preConfirmValue) {
  764. sweetAlert.closeModal(params.onClose);
  765. resolve(preConfirmValue || value);
  766. }, function (error) {
  767. sweetAlert.hideLoading();
  768. if (error) {
  769. sweetAlert.showValidationError(error);
  770. }
  771. });
  772. } else {
  773. sweetAlert.closeModal(params.onClose);
  774. if (params.useRejections) {
  775. resolve(value);
  776. } else {
  777. resolve({ value: value });
  778. }
  779. }
  780. };
  781. // Mouse interactions
  782. var onButtonEvent = function onButtonEvent(event) {
  783. var e = event || window.event;
  784. var target = e.target || e.srcElement;
  785. var confirmButton = getConfirmButton();
  786. var cancelButton = getCancelButton();
  787. var targetedConfirm = confirmButton && (confirmButton === target || confirmButton.contains(target));
  788. var targetedCancel = cancelButton && (cancelButton === target || cancelButton.contains(target));
  789. switch (e.type) {
  790. case 'mouseover':
  791. case 'mouseup':
  792. if (params.buttonsStyling) {
  793. if (targetedConfirm) {
  794. confirmButton.style.backgroundColor = colorLuminance(params.confirmButtonColor, -0.1);
  795. } else if (targetedCancel) {
  796. cancelButton.style.backgroundColor = colorLuminance(params.cancelButtonColor, -0.1);
  797. }
  798. }
  799. break;
  800. case 'mouseout':
  801. if (params.buttonsStyling) {
  802. if (targetedConfirm) {
  803. confirmButton.style.backgroundColor = params.confirmButtonColor;
  804. } else if (targetedCancel) {
  805. cancelButton.style.backgroundColor = params.cancelButtonColor;
  806. }
  807. }
  808. break;
  809. case 'mousedown':
  810. if (params.buttonsStyling) {
  811. if (targetedConfirm) {
  812. confirmButton.style.backgroundColor = colorLuminance(params.confirmButtonColor, -0.2);
  813. } else if (targetedCancel) {
  814. cancelButton.style.backgroundColor = colorLuminance(params.cancelButtonColor, -0.2);
  815. }
  816. }
  817. break;
  818. case 'click':
  819. // Clicked 'confirm'
  820. if (targetedConfirm && sweetAlert.isVisible()) {
  821. sweetAlert.disableButtons();
  822. if (params.input) {
  823. var inputValue = getInputValue();
  824. if (params.inputValidator) {
  825. sweetAlert.disableInput();
  826. params.inputValidator(inputValue, params.extraParams).then(function () {
  827. sweetAlert.enableButtons();
  828. sweetAlert.enableInput();
  829. confirm(inputValue);
  830. }, function (error) {
  831. sweetAlert.enableButtons();
  832. sweetAlert.enableInput();
  833. if (error) {
  834. sweetAlert.showValidationError(error);
  835. }
  836. });
  837. } else {
  838. confirm(inputValue);
  839. }
  840. } else {
  841. confirm(true);
  842. }
  843. // Clicked 'cancel'
  844. } else if (targetedCancel && sweetAlert.isVisible()) {
  845. sweetAlert.disableButtons();
  846. sweetAlert.closeModal(params.onClose);
  847. if (params.useRejections) {
  848. reject('cancel');
  849. } else {
  850. resolve({ dismiss: 'cancel' });
  851. }
  852. }
  853. break;
  854. default:
  855. }
  856. };
  857. var buttons = modal.querySelectorAll('button');
  858. for (var i = 0; i < buttons.length; i++) {
  859. buttons[i].onclick = onButtonEvent;
  860. buttons[i].onmouseover = onButtonEvent;
  861. buttons[i].onmouseout = onButtonEvent;
  862. buttons[i].onmousedown = onButtonEvent;
  863. }
  864. // Closing modal by close button
  865. getCloseButton().onclick = function () {
  866. sweetAlert.closeModal(params.onClose);
  867. if (params.useRejections) {
  868. reject('close');
  869. } else {
  870. resolve({ dismiss: 'close' });
  871. }
  872. };
  873. // Closing modal by overlay click
  874. container.onclick = function (e) {
  875. if (e.target !== container) {
  876. return;
  877. }
  878. if (params.allowOutsideClick) {
  879. sweetAlert.closeModal(params.onClose);
  880. if (params.useRejections) {
  881. reject('overlay');
  882. } else {
  883. resolve({ dismiss: 'overlay' });
  884. }
  885. }
  886. };
  887. var buttonsWrapper = getButtonsWrapper();
  888. var confirmButton = getConfirmButton();
  889. var cancelButton = getCancelButton();
  890. // Reverse buttons (Confirm on the right side)
  891. if (params.reverseButtons) {
  892. confirmButton.parentNode.insertBefore(cancelButton, confirmButton);
  893. } else {
  894. confirmButton.parentNode.insertBefore(confirmButton, cancelButton);
  895. }
  896. // Focus handling
  897. var setFocus = function setFocus(index, increment) {
  898. var focusableElements = getFocusableElements(params.focusCancel);
  899. // search for visible elements and select the next possible match
  900. for (var _i3 = 0; _i3 < focusableElements.length; _i3++) {
  901. index = index + increment;
  902. // rollover to first item
  903. if (index === focusableElements.length) {
  904. index = 0;
  905. // go to last item
  906. } else if (index === -1) {
  907. index = focusableElements.length - 1;
  908. }
  909. // determine if element is visible
  910. var el = focusableElements[index];
  911. if (isVisible(el)) {
  912. return el.focus();
  913. }
  914. }
  915. };
  916. var handleKeyDown = function handleKeyDown(event) {
  917. var e = event || window.event;
  918. var keyCode = e.keyCode || e.which;
  919. if ([9, 13, 32, 27, 37, 38, 39, 40].indexOf(keyCode) === -1) {
  920. // Don't do work on keys we don't care about.
  921. return;
  922. }
  923. var targetElement = e.target || e.srcElement;
  924. var focusableElements = getFocusableElements(params.focusCancel);
  925. var btnIndex = -1; // Find the button - note, this is a nodelist, not an array.
  926. for (var _i4 = 0; _i4 < focusableElements.length; _i4++) {
  927. if (targetElement === focusableElements[_i4]) {
  928. btnIndex = _i4;
  929. break;
  930. }
  931. }
  932. // TAB
  933. if (keyCode === 9) {
  934. if (!e.shiftKey) {
  935. // Cycle to the next button
  936. setFocus(btnIndex, 1);
  937. } else {
  938. // Cycle to the prev button
  939. setFocus(btnIndex, -1);
  940. }
  941. e.stopPropagation();
  942. e.preventDefault();
  943. // ARROWS - switch focus between buttons
  944. } else if (keyCode === 37 || keyCode === 38 || keyCode === 39 || keyCode === 40) {
  945. // focus Cancel button if Confirm button is currently focused
  946. if (document.activeElement === confirmButton && isVisible(cancelButton)) {
  947. cancelButton.focus();
  948. // and vice versa
  949. } else if (document.activeElement === cancelButton && isVisible(confirmButton)) {
  950. confirmButton.focus();
  951. }
  952. // ENTER/SPACE
  953. } else if (keyCode === 13 || keyCode === 32) {
  954. if (btnIndex === -1 && params.allowEnterKey) {
  955. // ENTER/SPACE clicked outside of a button.
  956. if (params.focusCancel) {
  957. fireClick(cancelButton, e);
  958. } else {
  959. fireClick(confirmButton, e);
  960. }
  961. e.stopPropagation();
  962. e.preventDefault();
  963. }
  964. // ESC
  965. } else if (keyCode === 27 && params.allowEscapeKey === true) {
  966. sweetAlert.closeModal(params.onClose);
  967. if (params.useRejections) {
  968. reject('esc');
  969. } else {
  970. resolve({ dismiss: 'esc' });
  971. }
  972. }
  973. };
  974. if (!window.onkeydown || window.onkeydown.toString() !== handleKeyDown.toString()) {
  975. states.previousWindowKeyDown = window.onkeydown;
  976. window.onkeydown = handleKeyDown;
  977. }
  978. // Loading state
  979. if (params.buttonsStyling) {
  980. confirmButton.style.borderLeftColor = params.confirmButtonColor;
  981. confirmButton.style.borderRightColor = params.confirmButtonColor;
  982. }
  983. /**
  984. * Show spinner instead of Confirm button and disable Cancel button
  985. */
  986. sweetAlert.hideLoading = sweetAlert.disableLoading = function () {
  987. if (!params.showConfirmButton) {
  988. hide(confirmButton);
  989. if (!params.showCancelButton) {
  990. hide(getButtonsWrapper());
  991. }
  992. }
  993. removeClass(buttonsWrapper, swalClasses.loading);
  994. removeClass(modal, swalClasses.loading);
  995. confirmButton.disabled = false;
  996. cancelButton.disabled = false;
  997. };
  998. sweetAlert.getTitle = function () {
  999. return getTitle();
  1000. };
  1001. sweetAlert.getContent = function () {
  1002. return getContent();
  1003. };
  1004. sweetAlert.getInput = function () {
  1005. return getInput();
  1006. };
  1007. sweetAlert.getImage = function () {
  1008. return getImage();
  1009. };
  1010. sweetAlert.getButtonsWrapper = function () {
  1011. return getButtonsWrapper();
  1012. };
  1013. sweetAlert.getConfirmButton = function () {
  1014. return getConfirmButton();
  1015. };
  1016. sweetAlert.getCancelButton = function () {
  1017. return getCancelButton();
  1018. };
  1019. sweetAlert.enableButtons = function () {
  1020. confirmButton.disabled = false;
  1021. cancelButton.disabled = false;
  1022. };
  1023. sweetAlert.disableButtons = function () {
  1024. confirmButton.disabled = true;
  1025. cancelButton.disabled = true;
  1026. };
  1027. sweetAlert.enableConfirmButton = function () {
  1028. confirmButton.disabled = false;
  1029. };
  1030. sweetAlert.disableConfirmButton = function () {
  1031. confirmButton.disabled = true;
  1032. };
  1033. sweetAlert.enableInput = function () {
  1034. var input = getInput();
  1035. if (!input) {
  1036. return false;
  1037. }
  1038. if (input.type === 'radio') {
  1039. var radiosContainer = input.parentNode.parentNode;
  1040. var radios = radiosContainer.querySelectorAll('input');
  1041. for (var _i5 = 0; _i5 < radios.length; _i5++) {
  1042. radios[_i5].disabled = false;
  1043. }
  1044. } else {
  1045. input.disabled = false;
  1046. }
  1047. };
  1048. sweetAlert.disableInput = function () {
  1049. var input = getInput();
  1050. if (!input) {
  1051. return false;
  1052. }
  1053. if (input && input.type === 'radio') {
  1054. var radiosContainer = input.parentNode.parentNode;
  1055. var radios = radiosContainer.querySelectorAll('input');
  1056. for (var _i6 = 0; _i6 < radios.length; _i6++) {
  1057. radios[_i6].disabled = true;
  1058. }
  1059. } else {
  1060. input.disabled = true;
  1061. }
  1062. };
  1063. // Set modal min-height to disable scrolling inside the modal
  1064. sweetAlert.recalculateHeight = debounce(function () {
  1065. var modal = getModal();
  1066. if (!modal) {
  1067. return;
  1068. }
  1069. var prevState = modal.style.display;
  1070. modal.style.minHeight = '';
  1071. show(modal);
  1072. modal.style.minHeight = modal.scrollHeight + 1 + 'px';
  1073. modal.style.display = prevState;
  1074. }, 50);
  1075. // Show block with validation error
  1076. sweetAlert.showValidationError = function (error) {
  1077. var validationError = getValidationError();
  1078. validationError.innerHTML = error;
  1079. show(validationError);
  1080. var input = getInput();
  1081. if (input) {
  1082. focusInput(input);
  1083. addClass(input, swalClasses.inputerror);
  1084. }
  1085. };
  1086. // Hide block with validation error
  1087. sweetAlert.resetValidationError = function () {
  1088. var validationError = getValidationError();
  1089. hide(validationError);
  1090. sweetAlert.recalculateHeight();
  1091. var input = getInput();
  1092. if (input) {
  1093. removeClass(input, swalClasses.inputerror);
  1094. }
  1095. };
  1096. sweetAlert.getProgressSteps = function () {
  1097. return params.progressSteps;
  1098. };
  1099. sweetAlert.setProgressSteps = function (progressSteps) {
  1100. params.progressSteps = progressSteps;
  1101. setParameters(params);
  1102. };
  1103. sweetAlert.showProgressSteps = function () {
  1104. show(getProgressSteps());
  1105. };
  1106. sweetAlert.hideProgressSteps = function () {
  1107. hide(getProgressSteps());
  1108. };
  1109. sweetAlert.enableButtons();
  1110. sweetAlert.hideLoading();
  1111. sweetAlert.resetValidationError();
  1112. // inputs
  1113. var inputTypes = ['input', 'file', 'range', 'select', 'radio', 'checkbox', 'textarea'];
  1114. var input = void 0;
  1115. for (var _i7 = 0; _i7 < inputTypes.length; _i7++) {
  1116. var inputClass = swalClasses[inputTypes[_i7]];
  1117. var inputContainer = getChildByClass(modal, inputClass);
  1118. input = getInput(inputTypes[_i7]);
  1119. // set attributes
  1120. if (input) {
  1121. for (var j in input.attributes) {
  1122. if (input.attributes.hasOwnProperty(j)) {
  1123. var attrName = input.attributes[j].name;
  1124. if (attrName !== 'type' && attrName !== 'value') {
  1125. input.removeAttribute(attrName);
  1126. }
  1127. }
  1128. }
  1129. for (var attr in params.inputAttributes) {
  1130. input.setAttribute(attr, params.inputAttributes[attr]);
  1131. }
  1132. }
  1133. // set class
  1134. inputContainer.className = inputClass;
  1135. if (params.inputClass) {
  1136. addClass(inputContainer, params.inputClass);
  1137. }
  1138. hide(inputContainer);
  1139. }
  1140. var populateInputOptions = void 0;
  1141. switch (params.input) {
  1142. case 'text':
  1143. case 'email':
  1144. case 'password':
  1145. case 'number':
  1146. case 'tel':
  1147. case 'url':
  1148. input = getChildByClass(modal, swalClasses.input);
  1149. input.value = params.inputValue;
  1150. input.placeholder = params.inputPlaceholder;
  1151. input.type = params.input;
  1152. show(input);
  1153. break;
  1154. case 'file':
  1155. input = getChildByClass(modal, swalClasses.file);
  1156. input.placeholder = params.inputPlaceholder;
  1157. input.type = params.input;
  1158. show(input);
  1159. break;
  1160. case 'range':
  1161. var range = getChildByClass(modal, swalClasses.range);
  1162. var rangeInput = range.querySelector('input');
  1163. var rangeOutput = range.querySelector('output');
  1164. rangeInput.value = params.inputValue;
  1165. rangeInput.type = params.input;
  1166. rangeOutput.value = params.inputValue;
  1167. show(range);
  1168. break;
  1169. case 'select':
  1170. var select = getChildByClass(modal, swalClasses.select);
  1171. select.innerHTML = '';
  1172. if (params.inputPlaceholder) {
  1173. var placeholder = document.createElement('option');
  1174. placeholder.innerHTML = params.inputPlaceholder;
  1175. placeholder.value = '';
  1176. placeholder.disabled = true;
  1177. placeholder.selected = true;
  1178. select.appendChild(placeholder);
  1179. }
  1180. populateInputOptions = function populateInputOptions(inputOptions) {
  1181. for (var optionValue in inputOptions) {
  1182. var option = document.createElement('option');
  1183. option.value = optionValue;
  1184. option.innerHTML = inputOptions[optionValue];
  1185. if (params.inputValue === optionValue) {
  1186. option.selected = true;
  1187. }
  1188. select.appendChild(option);
  1189. }
  1190. show(select);
  1191. select.focus();
  1192. };
  1193. break;
  1194. case 'radio':
  1195. var radio = getChildByClass(modal, swalClasses.radio);
  1196. radio.innerHTML = '';
  1197. populateInputOptions = function populateInputOptions(inputOptions) {
  1198. for (var radioValue in inputOptions) {
  1199. var radioInput = document.createElement('input');
  1200. var radioLabel = document.createElement('label');
  1201. var radioLabelSpan = document.createElement('span');
  1202. radioInput.type = 'radio';
  1203. radioInput.name = swalClasses.radio;
  1204. radioInput.value = radioValue;
  1205. if (params.inputValue === radioValue) {
  1206. radioInput.checked = true;
  1207. }
  1208. radioLabelSpan.innerHTML = inputOptions[radioValue];
  1209. radioLabel.appendChild(radioInput);
  1210. radioLabel.appendChild(radioLabelSpan);
  1211. radioLabel.for = radioInput.id;
  1212. radio.appendChild(radioLabel);
  1213. }
  1214. show(radio);
  1215. var radios = radio.querySelectorAll('input');
  1216. if (radios.length) {
  1217. radios[0].focus();
  1218. }
  1219. };
  1220. break;
  1221. case 'checkbox':
  1222. var checkbox = getChildByClass(modal, swalClasses.checkbox);
  1223. var checkboxInput = getInput('checkbox');
  1224. checkboxInput.type = 'checkbox';
  1225. checkboxInput.value = 1;
  1226. checkboxInput.id = swalClasses.checkbox;
  1227. checkboxInput.checked = Boolean(params.inputValue);
  1228. var label = checkbox.getElementsByTagName('span');
  1229. if (label.length) {
  1230. checkbox.removeChild(label[0]);
  1231. }
  1232. label = document.createElement('span');
  1233. label.innerHTML = params.inputPlaceholder;
  1234. checkbox.appendChild(label);
  1235. show(checkbox);
  1236. break;
  1237. case 'textarea':
  1238. var textarea = getChildByClass(modal, swalClasses.textarea);
  1239. textarea.value = params.inputValue;
  1240. textarea.placeholder = params.inputPlaceholder;
  1241. show(textarea);
  1242. break;
  1243. case null:
  1244. break;
  1245. default:
  1246. console.error('SweetAlert2: Unexpected type of input! Expected "text", "email", "password", "number", "tel", "select", "radio", "checkbox", "textarea", "file" or "url", got "' + params.input + '"');
  1247. break;
  1248. }
  1249. if (params.input === 'select' || params.input === 'radio') {
  1250. if (params.inputOptions instanceof Promise) {
  1251. sweetAlert.showLoading();
  1252. params.inputOptions.then(function (inputOptions) {
  1253. sweetAlert.hideLoading();
  1254. populateInputOptions(inputOptions);
  1255. });
  1256. } else if (_typeof(params.inputOptions) === 'object') {
  1257. populateInputOptions(params.inputOptions);
  1258. } else {
  1259. console.error('SweetAlert2: Unexpected type of inputOptions! Expected object or Promise, got ' + _typeof(params.inputOptions));
  1260. }
  1261. }
  1262. openModal(params.animation, params.onOpen);
  1263. // Focus the first element (input or button)
  1264. if (params.allowEnterKey) {
  1265. setFocus(-1, 1);
  1266. } else {
  1267. if (document.activeElement) {
  1268. document.activeElement.blur();
  1269. }
  1270. }
  1271. // fix scroll
  1272. getContainer().scrollTop = 0;
  1273. // Observe changes inside the modal and adjust height
  1274. if (typeof MutationObserver !== 'undefined' && !swal2Observer) {
  1275. swal2Observer = new MutationObserver(sweetAlert.recalculateHeight);
  1276. swal2Observer.observe(modal, { childList: true, characterData: true, subtree: true });
  1277. }
  1278. });
  1279. };
  1280. /*
  1281. * Global function to determine if swal2 modal is shown
  1282. */
  1283. sweetAlert.isVisible = function () {
  1284. return !!getModal();
  1285. };
  1286. /*
  1287. * Global function for chaining sweetAlert modals
  1288. */
  1289. sweetAlert.queue = function (steps) {
  1290. queue = steps;
  1291. var resetQueue = function resetQueue() {
  1292. queue = [];
  1293. document.body.removeAttribute('data-swal2-queue-step');
  1294. };
  1295. var queueResult = [];
  1296. return new Promise(function (resolve, reject) {
  1297. (function step(i, callback) {
  1298. if (i < queue.length) {
  1299. document.body.setAttribute('data-swal2-queue-step', i);
  1300. sweetAlert(queue[i]).then(function (result) {
  1301. queueResult.push(result);
  1302. step(i + 1, callback);
  1303. }, function (dismiss) {
  1304. resetQueue();
  1305. reject(dismiss);
  1306. });
  1307. } else {
  1308. resetQueue();
  1309. resolve(queueResult);
  1310. }
  1311. })(0);
  1312. });
  1313. };
  1314. /*
  1315. * Global function for getting the index of current modal in queue
  1316. */
  1317. sweetAlert.getQueueStep = function () {
  1318. return document.body.getAttribute('data-swal2-queue-step');
  1319. };
  1320. /*
  1321. * Global function for inserting a modal to the queue
  1322. */
  1323. sweetAlert.insertQueueStep = function (step, index) {
  1324. if (index && index < queue.length) {
  1325. return queue.splice(index, 0, step);
  1326. }
  1327. return queue.push(step);
  1328. };
  1329. /*
  1330. * Global function for deleting a modal from the queue
  1331. */
  1332. sweetAlert.deleteQueueStep = function (index) {
  1333. if (typeof queue[index] !== 'undefined') {
  1334. queue.splice(index, 1);
  1335. }
  1336. };
  1337. /*
  1338. * Global function to close sweetAlert
  1339. */
  1340. sweetAlert.close = sweetAlert.closeModal = function (onComplete) {
  1341. var container = getContainer();
  1342. var modal = getModal();
  1343. if (!modal) {
  1344. return;
  1345. }
  1346. removeClass(modal, swalClasses.show);
  1347. addClass(modal, swalClasses.hide);
  1348. clearTimeout(modal.timeout);
  1349. resetPrevState();
  1350. var removeModalAndResetState = function removeModalAndResetState() {
  1351. if (container.parentNode) {
  1352. container.parentNode.removeChild(container);
  1353. }
  1354. removeClass(document.documentElement, swalClasses.shown);
  1355. removeClass(document.body, swalClasses.shown);
  1356. undoScrollbar();
  1357. undoIOSfix();
  1358. };
  1359. // If animation is supported, animate
  1360. if (animationEndEvent && !hasClass(modal, swalClasses.noanimation)) {
  1361. modal.addEventListener(animationEndEvent, function swalCloseEventFinished() {
  1362. modal.removeEventListener(animationEndEvent, swalCloseEventFinished);
  1363. if (hasClass(modal, swalClasses.hide)) {
  1364. removeModalAndResetState();
  1365. }
  1366. });
  1367. } else {
  1368. // Otherwise, remove immediately
  1369. removeModalAndResetState();
  1370. }
  1371. if (onComplete !== null && typeof onComplete === 'function') {
  1372. setTimeout(function () {
  1373. onComplete(modal);
  1374. });
  1375. }
  1376. };
  1377. /*
  1378. * Global function to click 'Confirm' button
  1379. */
  1380. sweetAlert.clickConfirm = function () {
  1381. return getConfirmButton().click();
  1382. };
  1383. /*
  1384. * Global function to click 'Cancel' button
  1385. */
  1386. sweetAlert.clickCancel = function () {
  1387. return getCancelButton().click();
  1388. };
  1389. /**
  1390. * Show spinner instead of Confirm button and disable Cancel button
  1391. */
  1392. sweetAlert.showLoading = sweetAlert.enableLoading = function () {
  1393. var modal = getModal();
  1394. if (!modal) {
  1395. sweetAlert('');
  1396. }
  1397. var buttonsWrapper = getButtonsWrapper();
  1398. var confirmButton = getConfirmButton();
  1399. var cancelButton = getCancelButton();
  1400. show(buttonsWrapper);
  1401. show(confirmButton, 'inline-block');
  1402. addClass(buttonsWrapper, swalClasses.loading);
  1403. addClass(modal, swalClasses.loading);
  1404. confirmButton.disabled = true;
  1405. cancelButton.disabled = true;
  1406. };
  1407. /**
  1408. * Set default params for each popup
  1409. * @param {Object} userParams
  1410. */
  1411. sweetAlert.setDefaults = function (userParams) {
  1412. if (!userParams || (typeof userParams === 'undefined' ? 'undefined' : _typeof(userParams)) !== 'object') {
  1413. return console.error('SweetAlert2: the argument for setDefaults() is required and has to be a object');
  1414. }
  1415. for (var param in userParams) {
  1416. if (!defaultParams.hasOwnProperty(param) && param !== 'extraParams') {
  1417. console.warn('SweetAlert2: Unknown parameter "' + param + '"');
  1418. delete userParams[param];
  1419. }
  1420. }
  1421. _extends(modalParams, userParams);
  1422. };
  1423. /**
  1424. * Reset default params for each popup
  1425. */
  1426. sweetAlert.resetDefaults = function () {
  1427. modalParams = _extends({}, defaul