Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

1714 строки
49 KiB

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