1
0

web_swt.js 53 KB

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