sweetalert.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. ;(function(window, document, undefined) {
  2. "use strict";
  3. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  4. 'use strict';
  5. Object.defineProperty(exports, "__esModule", {
  6. value: true
  7. });
  8. var defaultParams = {
  9. title: '',
  10. text: '',
  11. type: null,
  12. allowOutsideClick: false,
  13. showConfirmButton: true,
  14. showCancelButton: false,
  15. closeOnConfirm: true,
  16. closeOnCancel: true,
  17. confirmButtonText: 'OK',
  18. confirmButtonClass: 'btn-primary',
  19. cancelButtonText: 'Cancel',
  20. cancelButtonClass: 'btn-default',
  21. containerClass: '',
  22. titleClass: '',
  23. textClass: '',
  24. imageUrl: null,
  25. imageSize: null,
  26. timer: null,
  27. customClass: '',
  28. html: false,
  29. animation: true,
  30. allowEscapeKey: true,
  31. inputType: 'text',
  32. inputPlaceholder: '',
  33. inputValue: '',
  34. showLoaderOnConfirm: false
  35. };
  36. exports.default = defaultParams;
  37. },{}],2:[function(require,module,exports){
  38. 'use strict';
  39. Object.defineProperty(exports, "__esModule", {
  40. value: true
  41. });
  42. exports.handleCancel = exports.handleConfirm = exports.handleButton = undefined;
  43. var _handleSwalDom = require('./handle-swal-dom');
  44. var _handleDom = require('./handle-dom');
  45. /*
  46. * User clicked on "Confirm"/"OK" or "Cancel"
  47. */
  48. var handleButton = function handleButton(event, params, modal) {
  49. var e = event || window.event;
  50. var target = e.target || e.srcElement;
  51. var targetedConfirm = target.className.indexOf('confirm') !== -1;
  52. var targetedOverlay = target.className.indexOf('sweet-overlay') !== -1;
  53. var modalIsVisible = (0, _handleDom.hasClass)(modal, 'visible');
  54. var doneFunctionExists = params.doneFunction && modal.getAttribute('data-has-done-function') === 'true';
  55. // Since the user can change the background-color of the confirm button programmatically,
  56. // we must calculate what the color should be on hover/active
  57. var normalColor, hoverColor, activeColor;
  58. if (targetedConfirm && params.confirmButtonColor) {
  59. normalColor = params.confirmButtonColor;
  60. hoverColor = colorLuminance(normalColor, -0.04);
  61. activeColor = colorLuminance(normalColor, -0.14);
  62. }
  63. function shouldSetConfirmButtonColor(color) {
  64. if (targetedConfirm && params.confirmButtonColor) {
  65. target.style.backgroundColor = color;
  66. }
  67. }
  68. switch (e.type) {
  69. case 'click':
  70. var clickedOnModal = modal === target;
  71. var clickedOnModalChild = (0, _handleDom.isDescendant)(modal, target);
  72. // Ignore click outside if allowOutsideClick is false
  73. if (!clickedOnModal && !clickedOnModalChild && modalIsVisible && !params.allowOutsideClick) {
  74. break;
  75. }
  76. if (targetedConfirm && doneFunctionExists && modalIsVisible) {
  77. handleConfirm(modal, params);
  78. } else if (doneFunctionExists && modalIsVisible || targetedOverlay) {
  79. handleCancel(modal, params);
  80. } else if ((0, _handleDom.isDescendant)(modal, target) && target.tagName === 'BUTTON') {
  81. sweetAlert.close();
  82. }
  83. break;
  84. }
  85. };
  86. /*
  87. * User clicked on "Confirm"/"OK"
  88. */
  89. var handleConfirm = function handleConfirm(modal, params) {
  90. var callbackValue = true;
  91. if ((0, _handleDom.hasClass)(modal, 'show-input')) {
  92. callbackValue = modal.querySelector('input').value;
  93. if (!callbackValue) {
  94. callbackValue = '';
  95. }
  96. }
  97. params.doneFunction(callbackValue);
  98. if (params.closeOnConfirm) {
  99. sweetAlert.close();
  100. }
  101. // Disable cancel and confirm button if the parameter is true
  102. if (params.showLoaderOnConfirm) {
  103. sweetAlert.disableButtons();
  104. }
  105. };
  106. /*
  107. * User clicked on "Cancel"
  108. */
  109. var handleCancel = function handleCancel(modal, params) {
  110. // Check if callback function expects a parameter (to track cancel actions)
  111. var functionAsStr = String(params.doneFunction).replace(/\s/g, '');
  112. var functionHandlesCancel = functionAsStr.substring(0, 9) === 'function(' && functionAsStr.substring(9, 10) !== ')';
  113. if (functionHandlesCancel) {
  114. params.doneFunction(false);
  115. }
  116. if (params.closeOnCancel) {
  117. sweetAlert.close();
  118. }
  119. };
  120. exports.handleButton = handleButton;
  121. exports.handleConfirm = handleConfirm;
  122. exports.handleCancel = handleCancel;
  123. },{"./handle-dom":3,"./handle-swal-dom":5}],3:[function(require,module,exports){
  124. 'use strict';
  125. Object.defineProperty(exports, "__esModule", {
  126. value: true
  127. });
  128. var hasClass = function hasClass(elem, className) {
  129. return new RegExp(' ' + className + ' ').test(' ' + elem.className + ' ');
  130. };
  131. var addClass = function addClass(elem, className) {
  132. if (!hasClass(elem, className)) {
  133. elem.className += ' ' + className;
  134. }
  135. };
  136. var removeClass = function removeClass(elem, className) {
  137. var newClass = ' ' + elem.className.replace(/[\t\r\n]/g, ' ') + ' ';
  138. if (hasClass(elem, className)) {
  139. while (newClass.indexOf(' ' + className + ' ') >= 0) {
  140. newClass = newClass.replace(' ' + className + ' ', ' ');
  141. }
  142. elem.className = newClass.replace(/^\s+|\s+$/g, '');
  143. }
  144. };
  145. var escapeHtml = function escapeHtml(str) {
  146. var div = document.createElement('div');
  147. div.appendChild(document.createTextNode(str));
  148. return div.innerHTML;
  149. };
  150. var _show = function _show(elem) {
  151. elem.style.opacity = '';
  152. elem.style.display = 'block';
  153. };
  154. var show = function show(elems) {
  155. if (elems && !elems.length) {
  156. return _show(elems);
  157. }
  158. for (var i = 0; i < elems.length; ++i) {
  159. _show(elems[i]);
  160. }
  161. };
  162. var _hide = function _hide(elem) {
  163. elem.style.opacity = '';
  164. elem.style.display = 'none';
  165. };
  166. var hide = function hide(elems) {
  167. if (elems && !elems.length) {
  168. return _hide(elems);
  169. }
  170. for (var i = 0; i < elems.length; ++i) {
  171. _hide(elems[i]);
  172. }
  173. };
  174. var isDescendant = function isDescendant(parent, child) {
  175. var node = child.parentNode;
  176. while (node !== null) {
  177. if (node === parent) {
  178. return true;
  179. }
  180. node = node.parentNode;
  181. }
  182. return false;
  183. };
  184. var getTopMargin = function getTopMargin(elem) {
  185. elem.style.left = '-9999px';
  186. elem.style.display = 'block';
  187. var height = elem.clientHeight,
  188. padding;
  189. if (typeof getComputedStyle !== "undefined") {
  190. // IE 8
  191. padding = parseInt(getComputedStyle(elem).getPropertyValue('padding-top'), 10);
  192. } else {
  193. padding = parseInt(elem.currentStyle.padding);
  194. }
  195. elem.style.left = '';
  196. elem.style.display = 'none';
  197. return '-' + parseInt((height + padding) / 2) + 'px';
  198. };
  199. var fadeIn = function fadeIn(elem, interval) {
  200. if (+elem.style.opacity < 1) {
  201. interval = interval || 16;
  202. elem.style.opacity = 0;
  203. elem.style.display = 'block';
  204. var last = +new Date();
  205. var tick = function tick() {
  206. elem.style.opacity = +elem.style.opacity + (new Date() - last) / 100;
  207. last = +new Date();
  208. if (+elem.style.opacity < 1) {
  209. setTimeout(tick, interval);
  210. }
  211. };
  212. tick();
  213. }
  214. elem.style.display = 'block'; //fallback IE8
  215. };
  216. var fadeOut = function fadeOut(elem, interval) {
  217. interval = interval || 16;
  218. elem.style.opacity = 1;
  219. var last = +new Date();
  220. var tick = function tick() {
  221. elem.style.opacity = +elem.style.opacity - (new Date() - last) / 100;
  222. last = +new Date();
  223. if (+elem.style.opacity > 0) {
  224. setTimeout(tick, interval);
  225. } else {
  226. elem.style.display = 'none';
  227. }
  228. };
  229. tick();
  230. };
  231. var fireClick = function fireClick(node) {
  232. // Taken from http://www.nonobtrusive.com/2011/11/29/programatically-fire-crossbrowser-click-event-with-javascript/
  233. // Then fixed for today's Chrome browser.
  234. if (typeof MouseEvent === 'function') {
  235. // Up-to-date approach
  236. var mevt = new MouseEvent('click', {
  237. view: window,
  238. bubbles: false,
  239. cancelable: true
  240. });
  241. node.dispatchEvent(mevt);
  242. } else if (document.createEvent) {
  243. // Fallback
  244. var evt = document.createEvent('MouseEvents');
  245. evt.initEvent('click', false, false);
  246. node.dispatchEvent(evt);
  247. } else if (document.createEventObject) {
  248. node.fireEvent('onclick');
  249. } else if (typeof node.onclick === 'function') {
  250. node.onclick();
  251. }
  252. };
  253. var stopEventPropagation = function stopEventPropagation(e) {
  254. // In particular, make sure the space bar doesn't scroll the main window.
  255. if (typeof e.stopPropagation === 'function') {
  256. e.stopPropagation();
  257. e.preventDefault();
  258. } else if (window.event && window.event.hasOwnProperty('cancelBubble')) {
  259. window.event.cancelBubble = true;
  260. }
  261. };
  262. exports.hasClass = hasClass;
  263. exports.addClass = addClass;
  264. exports.removeClass = removeClass;
  265. exports.escapeHtml = escapeHtml;
  266. exports._show = _show;
  267. exports.show = show;
  268. exports._hide = _hide;
  269. exports.hide = hide;
  270. exports.isDescendant = isDescendant;
  271. exports.getTopMargin = getTopMargin;
  272. exports.fadeIn = fadeIn;
  273. exports.fadeOut = fadeOut;
  274. exports.fireClick = fireClick;
  275. exports.stopEventPropagation = stopEventPropagation;
  276. },{}],4:[function(require,module,exports){
  277. 'use strict';
  278. Object.defineProperty(exports, "__esModule", {
  279. value: true
  280. });
  281. var _handleDom = require('./handle-dom');
  282. var _handleSwalDom = require('./handle-swal-dom');
  283. var handleKeyDown = function handleKeyDown(event, params, modal) {
  284. var e = event || window.event;
  285. var keyCode = e.keyCode || e.which;
  286. var $okButton = modal.querySelector('button.confirm');
  287. var $cancelButton = modal.querySelector('button.cancel');
  288. var $modalButtons = modal.querySelectorAll('button[tabindex]');
  289. if ([9, 13, 32, 27].indexOf(keyCode) === -1) {
  290. // Don't do work on keys we don't care about.
  291. return;
  292. }
  293. var $targetElement = e.target || e.srcElement;
  294. var btnIndex = -1; // Find the button - note, this is a nodelist, not an array.
  295. for (var i = 0; i < $modalButtons.length; i++) {
  296. if ($targetElement === $modalButtons[i]) {
  297. btnIndex = i;
  298. break;
  299. }
  300. }
  301. if (keyCode === 9) {
  302. // TAB
  303. if (btnIndex === -1) {
  304. // No button focused. Jump to the confirm button.
  305. $targetElement = $okButton;
  306. } else {
  307. // Cycle to the next button
  308. if (btnIndex === $modalButtons.length - 1) {
  309. $targetElement = $modalButtons[0];
  310. } else {
  311. $targetElement = $modalButtons[btnIndex + 1];
  312. }
  313. }
  314. (0, _handleDom.stopEventPropagation)(e);
  315. $targetElement.focus();
  316. if (params.confirmButtonColor) {
  317. (0, _handleSwalDom.setFocusStyle)($targetElement, params.confirmButtonColor);
  318. }
  319. } else {
  320. if (keyCode === 13) {
  321. if ($targetElement.tagName === 'INPUT') {
  322. $targetElement = $okButton;
  323. $okButton.focus();
  324. }
  325. if (btnIndex === -1) {
  326. // ENTER/SPACE clicked outside of a button.
  327. $targetElement = $okButton;
  328. } else {
  329. // Do nothing - let the browser handle it.
  330. $targetElement = undefined;
  331. }
  332. } else if (keyCode === 27 && params.allowEscapeKey === true) {
  333. $targetElement = $cancelButton;
  334. (0, _handleDom.fireClick)($targetElement, e);
  335. } else {
  336. // Fallback - let the browser handle it.
  337. $targetElement = undefined;
  338. }
  339. }
  340. };
  341. exports.default = handleKeyDown;
  342. },{"./handle-dom":3,"./handle-swal-dom":5}],5:[function(require,module,exports){
  343. 'use strict';
  344. Object.defineProperty(exports, "__esModule", {
  345. value: true
  346. });
  347. exports.fixVerticalPosition = exports.resetInputError = exports.resetInput = exports.openModal = exports.getInput = exports.getOverlay = exports.getModal = exports.sweetAlertInitialize = undefined;
  348. var _handleDom = require('./handle-dom');
  349. var _defaultParams = require('./default-params');
  350. var _defaultParams2 = _interopRequireDefault(_defaultParams);
  351. var _injectedHtml = require('./injected-html');
  352. var _injectedHtml2 = _interopRequireDefault(_injectedHtml);
  353. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  354. var modalClass = '.sweet-alert';
  355. var overlayClass = '.sweet-overlay';
  356. /*
  357. * Add modal + overlay to DOM
  358. */
  359. var sweetAlertInitialize = function sweetAlertInitialize() {
  360. var sweetWrap = document.createElement('div');
  361. sweetWrap.innerHTML = _injectedHtml2.default;
  362. // Append elements to body
  363. while (sweetWrap.firstChild) {
  364. document.body.appendChild(sweetWrap.firstChild);
  365. }
  366. };
  367. /*
  368. * Get DOM element of modal
  369. */
  370. var getModal = function getModal() {
  371. var $modal = document.querySelector(modalClass);
  372. if (!$modal) {
  373. sweetAlertInitialize();
  374. $modal = getModal();
  375. }
  376. return $modal;
  377. };
  378. /*
  379. * Get DOM element of input (in modal)
  380. */
  381. var getInput = function getInput() {
  382. var $modal = getModal();
  383. if ($modal) {
  384. return $modal.querySelector('input');
  385. }
  386. };
  387. /*
  388. * Get DOM element of overlay
  389. */
  390. var getOverlay = function getOverlay() {
  391. return document.querySelector(overlayClass);
  392. };
  393. /*
  394. * Animation when opening modal
  395. */
  396. var openModal = function openModal(callback) {
  397. var $modal = getModal();
  398. (0, _handleDom.fadeIn)(getOverlay(), 10);
  399. (0, _handleDom.show)($modal);
  400. (0, _handleDom.addClass)($modal, 'showSweetAlert');
  401. (0, _handleDom.removeClass)($modal, 'hideSweetAlert');
  402. window.previousActiveElement = document.activeElement;
  403. var $okButton = $modal.querySelector('button.confirm');
  404. $okButton.focus();
  405. setTimeout(function () {
  406. (0, _handleDom.addClass)($modal, 'visible');
  407. }, 500);
  408. var timer = $modal.getAttribute('data-timer');
  409. if (timer !== 'null' && timer !== '') {
  410. var timerCallback = callback;
  411. $modal.timeout = setTimeout(function () {
  412. var doneFunctionExists = (timerCallback || null) && $modal.getAttribute('data-has-done-function') === 'true';
  413. if (doneFunctionExists) {
  414. timerCallback(null);
  415. } else {
  416. sweetAlert.close();
  417. }
  418. }, timer);
  419. }
  420. };
  421. /*
  422. * Reset the styling of the input
  423. * (for example if errors have been shown)
  424. */
  425. var resetInput = function resetInput() {
  426. var $modal = getModal();
  427. var $input = getInput();
  428. (0, _handleDom.removeClass)($modal, 'show-input');
  429. $input.value = _defaultParams2.default.inputValue;
  430. $input.setAttribute('type', _defaultParams2.default.inputType);
  431. $input.setAttribute('placeholder', _defaultParams2.default.inputPlaceholder);
  432. resetInputError();
  433. };
  434. var resetInputError = function resetInputError(event) {
  435. // If press enter => ignore
  436. if (event && event.keyCode === 13) {
  437. return false;
  438. }
  439. var $modal = getModal();
  440. var $errorIcon = $modal.querySelector('.sa-input-error');
  441. (0, _handleDom.removeClass)($errorIcon, 'show');
  442. var $errorContainer = $modal.querySelector('.form-group');
  443. (0, _handleDom.removeClass)($errorContainer, 'has-error');
  444. };
  445. /*
  446. * Set "margin-top"-property on modal based on its computed height
  447. */
  448. var fixVerticalPosition = function fixVerticalPosition() {
  449. var $modal = getModal();
  450. $modal.style.marginTop = (0, _handleDom.getTopMargin)(getModal());
  451. };
  452. exports.sweetAlertInitialize = sweetAlertInitialize;
  453. exports.getModal = getModal;
  454. exports.getOverlay = getOverlay;
  455. exports.getInput = getInput;
  456. exports.openModal = openModal;
  457. exports.resetInput = resetInput;
  458. exports.resetInputError = resetInputError;
  459. exports.fixVerticalPosition = fixVerticalPosition;
  460. },{"./default-params":1,"./handle-dom":3,"./injected-html":6}],6:[function(require,module,exports){
  461. "use strict";
  462. Object.defineProperty(exports, "__esModule", {
  463. value: true
  464. });
  465. var injectedHTML =
  466. // Dark overlay
  467. "<div class=\"sweet-overlay\" tabIndex=\"-1\"></div>" +
  468. // Modal
  469. "<div class=\"sweet-alert\" tabIndex=\"-1\">" +
  470. // Error icon
  471. "<div class=\"sa-icon sa-error\">\n <span class=\"sa-x-mark\">\n <span class=\"sa-line sa-left\"></span>\n <span class=\"sa-line sa-right\"></span>\n </span>\n </div>" +
  472. // Warning icon
  473. "<div class=\"sa-icon sa-warning\">\n <span class=\"sa-body\"></span>\n <span class=\"sa-dot\"></span>\n </div>" +
  474. // Info icon
  475. "<div class=\"sa-icon sa-info\"></div>" +
  476. // Success icon
  477. "<div class=\"sa-icon sa-success\">\n <span class=\"sa-line sa-tip\"></span>\n <span class=\"sa-line sa-long\"></span>\n\n <div class=\"sa-placeholder\"></div>\n <div class=\"sa-fix\"></div>\n </div>" + "<div class=\"sa-icon sa-custom\"></div>" +
  478. // Title, text and input
  479. "<h2>Title</h2>\n <p class=\"lead text-muted\">Text</p>\n <div class=\"form-group\">\n <input type=\"text\" class=\"form-control\" tabIndex=\"3\" />\n <span class=\"sa-input-error help-block\">\n <span class=\"glyphicon glyphicon-exclamation-sign\"></span> <span class=\"sa-help-text\">Not valid</span>\n </span>\n </div>" +
  480. // Cancel and confirm buttons
  481. "<div class=\"sa-button-container\">\n <button class=\"cancel btn btn-lg\" tabIndex=\"2\">Cancel</button>\n <div class=\"sa-confirm-button-container\">\n <button class=\"confirm btn btn-lg\" tabIndex=\"1\">OK</button>" +
  482. // Loading animation
  483. "<div class=\"la-ball-fall\">\n <div></div>\n <div></div>\n <div></div>\n </div>\n </div>\n </div>" +
  484. // End of modal
  485. "</div>";
  486. exports.default = injectedHTML;
  487. },{}],7:[function(require,module,exports){
  488. 'use strict';
  489. Object.defineProperty(exports, "__esModule", {
  490. value: true
  491. });
  492. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
  493. var _utils = require('./utils');
  494. var _handleSwalDom = require('./handle-swal-dom');
  495. var _handleDom = require('./handle-dom');
  496. var alertTypes = ['error', 'warning', 'info', 'success', 'input', 'prompt'];
  497. /*
  498. * Set type, text and actions on modal
  499. */
  500. var setParameters = function setParameters(params) {
  501. var modal = (0, _handleSwalDom.getModal)();
  502. var $title = modal.querySelector('h2');
  503. var $text = modal.querySelector('p');
  504. var $cancelBtn = modal.querySelector('button.cancel');
  505. var $confirmBtn = modal.querySelector('button.confirm');
  506. /*
  507. * Title
  508. */
  509. $title.innerHTML = params.html ? params.title : (0, _handleDom.escapeHtml)(params.title).split('\n').join('<br>');
  510. /*
  511. * Text
  512. */
  513. $text.innerHTML = params.html ? params.text : (0, _handleDom.escapeHtml)(params.text || '').split('\n').join('<br>');
  514. if (params.text) (0, _handleDom.show)($text);
  515. /*
  516. * Custom class
  517. */
  518. if (params.customClass) {
  519. (0, _handleDom.addClass)(modal, params.customClass);
  520. modal.setAttribute('data-custom-class', params.customClass);
  521. } else {
  522. // Find previously set classes and remove them
  523. var customClass = modal.getAttribute('data-custom-class');
  524. (0, _handleDom.removeClass)(modal, customClass);
  525. modal.setAttribute('data-custom-class', '');
  526. }
  527. /*
  528. * Icon
  529. */
  530. (0, _handleDom.hide)(modal.querySelectorAll('.sa-icon'));
  531. if (params.type && !(0, _utils.isIE8)()) {
  532. var _ret = function () {
  533. var validType = false;
  534. for (var i = 0; i < alertTypes.length; i++) {
  535. if (params.type === alertTypes[i]) {
  536. validType = true;
  537. break;
  538. }
  539. }
  540. if (!validType) {
  541. logStr('Unknown alert type: ' + params.type);
  542. return {
  543. v: false
  544. };
  545. }
  546. var typesWithIcons = ['success', 'error', 'warning', 'info'];
  547. var $icon = void 0;
  548. if (typesWithIcons.indexOf(params.type) !== -1) {
  549. $icon = modal.querySelector('.sa-icon.' + 'sa-' + params.type);
  550. (0, _handleDom.show)($icon);
  551. }
  552. var $input = (0, _handleSwalDom.getInput)();
  553. // Animate icon
  554. switch (params.type) {
  555. case 'success':
  556. (0, _handleDom.addClass)($icon, 'animate');
  557. (0, _handleDom.addClass)($icon.querySelector('.sa-tip'), 'animateSuccessTip');
  558. (0, _handleDom.addClass)($icon.querySelector('.sa-long'), 'animateSuccessLong');
  559. break;
  560. case 'error':
  561. (0, _handleDom.addClass)($icon, 'animateErrorIcon');
  562. (0, _handleDom.addClass)($icon.querySelector('.sa-x-mark'), 'animateXMark');
  563. break;
  564. case 'warning':
  565. (0, _handleDom.addClass)($icon, 'pulseWarning');
  566. (0, _handleDom.addClass)($icon.querySelector('.sa-body'), 'pulseWarningIns');
  567. (0, _handleDom.addClass)($icon.querySelector('.sa-dot'), 'pulseWarningIns');
  568. break;
  569. case 'input':
  570. case 'prompt':
  571. $input.setAttribute('type', params.inputType);
  572. $input.value = params.inputValue;
  573. $input.setAttribute('placeholder', params.inputPlaceholder);
  574. (0, _handleDom.addClass)(modal, 'show-input');
  575. setTimeout(function () {
  576. $input.focus();
  577. $input.addEventListener('keyup', swal.resetInputError);
  578. }, 400);
  579. break;
  580. }
  581. }();
  582. if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
  583. }
  584. /*
  585. * Custom image
  586. */
  587. if (params.imageUrl) {
  588. var $customIcon = modal.querySelector('.sa-icon.sa-custom');
  589. $customIcon.style.backgroundImage = 'url(' + params.imageUrl + ')';
  590. (0, _handleDom.show)($customIcon);
  591. var _imgWidth = 80;
  592. var _imgHeight = 80;
  593. if (params.imageSize) {
  594. var dimensions = params.imageSize.toString().split('x');
  595. var imgWidth = dimensions[0];
  596. var imgHeight = dimensions[1];
  597. if (!imgWidth || !imgHeight) {
  598. logStr('Parameter imageSize expects value with format WIDTHxHEIGHT, got ' + params.imageSize);
  599. } else {
  600. _imgWidth = imgWidth;
  601. _imgHeight = imgHeight;
  602. }
  603. }
  604. $customIcon.setAttribute('style', $customIcon.getAttribute('style') + 'width:' + _imgWidth + 'px; height:' + _imgHeight + 'px');
  605. }
  606. /*
  607. * Show cancel button?
  608. */
  609. modal.setAttribute('data-has-cancel-button', params.showCancelButton);
  610. if (params.showCancelButton) {
  611. $cancelBtn.style.display = 'inline-block';
  612. } else {
  613. (0, _handleDom.hide)($cancelBtn);
  614. }
  615. /*
  616. * Show confirm button?
  617. */
  618. modal.setAttribute('data-has-confirm-button', params.showConfirmButton);
  619. if (params.showConfirmButton) {
  620. $confirmBtn.style.display = 'inline-block';
  621. } else {
  622. (0, _handleDom.hide)($confirmBtn);
  623. }
  624. /*
  625. * Custom text on cancel/confirm buttons
  626. */
  627. if (params.cancelButtonText) {
  628. $cancelBtn.innerHTML = (0, _handleDom.escapeHtml)(params.cancelButtonText);
  629. }
  630. if (params.confirmButtonText) {
  631. $confirmBtn.innerHTML = (0, _handleDom.escapeHtml)(params.confirmButtonText);
  632. }
  633. /*
  634. * Reset confirm buttons to default class (Ugly fix)
  635. */
  636. $confirmBtn.className = 'confirm btn btn-lg';
  637. /*
  638. * Attach selected class to the sweet alert modal
  639. */
  640. (0, _handleDom.addClass)(modal, params.containerClass);
  641. /*
  642. * Set confirm button to selected class
  643. */
  644. (0, _handleDom.addClass)($confirmBtn, params.confirmButtonClass);
  645. /*
  646. * Set cancel button to selected class
  647. */
  648. (0, _handleDom.addClass)($cancelBtn, params.cancelButtonClass);
  649. /*
  650. * Set title to selected class
  651. */
  652. (0, _handleDom.addClass)($title, params.titleClass);
  653. /*
  654. * Set text to selected class
  655. */
  656. (0, _handleDom.addClass)($text, params.textClass);
  657. /*
  658. * Allow outside click
  659. */
  660. modal.setAttribute('data-allow-outside-click', params.allowOutsideClick);
  661. /*
  662. * Callback function
  663. */
  664. var hasDoneFunction = params.doneFunction ? true : false;
  665. modal.setAttribute('data-has-done-function', hasDoneFunction);
  666. /*
  667. * Animation
  668. */
  669. if (!params.animation) {
  670. modal.setAttribute('data-animation', 'none');
  671. } else if (typeof params.animation === 'string') {
  672. modal.setAttribute('data-animation', params.animation); // Custom animation
  673. } else {
  674. modal.setAttribute('data-animation', 'pop');
  675. }
  676. /*
  677. * Timer
  678. */
  679. modal.setAttribute('data-timer', params.timer);
  680. };
  681. exports.default = setParameters;
  682. },{"./handle-dom":3,"./handle-swal-dom":5,"./utils":8}],8:[function(require,module,exports){
  683. 'use strict';
  684. Object.defineProperty(exports, "__esModule", {
  685. value: true
  686. });
  687. /*
  688. * Allow user to pass their own params
  689. */
  690. var extend = function extend(a, b) {
  691. for (var key in b) {
  692. if (b.hasOwnProperty(key)) {
  693. a[key] = b[key];
  694. }
  695. }
  696. return a;
  697. };
  698. /*
  699. * Check if the user is using Internet Explorer 8 (for fallbacks)
  700. */
  701. var isIE8 = function isIE8() {
  702. return window.attachEvent && !window.addEventListener;
  703. };
  704. /*
  705. * IE compatible logging for developers
  706. */
  707. var logStr = function logStr(string) {
  708. if (window.console) {
  709. // IE...
  710. window.console.log('SweetAlert: ' + string);
  711. }
  712. };
  713. exports.extend = extend;
  714. exports.isIE8 = isIE8;
  715. exports.logStr = logStr;
  716. },{}],9:[function(require,module,exports){
  717. 'use strict';
  718. Object.defineProperty(exports, "__esModule", {
  719. value: true
  720. });
  721. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; // SweetAlert
  722. // 2014-2015 (c) - Tristan Edwards
  723. // github.com/t4t5/sweetalert
  724. /*
  725. * jQuery-like functions for manipulating the DOM
  726. */
  727. /*
  728. * Handy utilities
  729. */
  730. /*
  731. * Handle sweetAlert's DOM elements
  732. */
  733. // Handle button events and keyboard events
  734. // Default values
  735. var _handleDom = require('./modules/handle-dom');
  736. var _utils = require('./modules/utils');
  737. var _handleSwalDom = require('./modules/handle-swal-dom');
  738. var _handleClick = require('./modules/handle-click');
  739. var _handleKey = require('./modules/handle-key');
  740. var _handleKey2 = _interopRequireDefault(_handleKey);
  741. var _defaultParams = require('./modules/default-params');
  742. var _defaultParams2 = _interopRequireDefault(_defaultParams);
  743. var _setParams = require('./modules/set-params');
  744. var _setParams2 = _interopRequireDefault(_setParams);
  745. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  746. /*
  747. * Remember state in cases where opening and handling a modal will fiddle with it.
  748. * (We also use window.previousActiveElement as a global variable)
  749. */
  750. var previousWindowKeyDown;
  751. var lastFocusedButton;
  752. /*
  753. * Global sweetAlert function
  754. * (this is what the user calls)
  755. */
  756. var sweetAlert, _swal;
  757. exports.default = sweetAlert = _swal = function swal() {
  758. var customizations = arguments[0];
  759. (0, _handleDom.addClass)(document.body, 'stop-scrolling');
  760. (0, _handleSwalDom.resetInput)();
  761. /*
  762. * Use argument if defined or default value from params object otherwise.
  763. * Supports the case where a default value is boolean true and should be
  764. * overridden by a corresponding explicit argument which is boolean false.
  765. */
  766. function argumentOrDefault(key) {
  767. var args = customizations;
  768. return args[key] === undefined ? _defaultParams2.default[key] : args[key];
  769. }
  770. if (customizations === undefined) {
  771. (0, _utils.logStr)('SweetAlert expects at least 1 attribute!');
  772. return false;
  773. }
  774. var params = (0, _utils.extend)({}, _defaultParams2.default);
  775. switch (typeof customizations === 'undefined' ? 'undefined' : _typeof(customizations)) {
  776. // Ex: swal("Hello", "Just testing", "info");
  777. case 'string':
  778. params.title = customizations;
  779. params.text = arguments[1] || '';
  780. params.type = arguments[2] || '';
  781. break;
  782. // Ex: swal({ title:"Hello", text: "Just testing", type: "info" });
  783. case 'object':
  784. if (customizations.title === undefined) {
  785. (0, _utils.logStr)('Missing "title" argument!');
  786. return false;
  787. }
  788. params.title = customizations.title;
  789. for (var customName in _defaultParams2.default) {
  790. params[customName] = argumentOrDefault(customName);
  791. }
  792. // Show "Confirm" instead of "OK" if cancel button is visible
  793. params.confirmButtonText = params.showCancelButton ? 'Confirm' : _defaultParams2.default.confirmButtonText;
  794. params.confirmButtonText = argumentOrDefault('confirmButtonText');
  795. // Callback function when clicking on "OK"/"Cancel"
  796. params.doneFunction = arguments[1] || null;
  797. break;
  798. default:
  799. (0, _utils.logStr)('Unexpected type of argument! Expected "string" or "object", got ' + (typeof customizations === 'undefined' ? 'undefined' : _typeof(customizations)));
  800. return false;
  801. }
  802. (0, _setParams2.default)(params);
  803. (0, _handleSwalDom.fixVerticalPosition)();
  804. (0, _handleSwalDom.openModal)(arguments[1]);
  805. // Modal interactions
  806. var modal = (0, _handleSwalDom.getModal)();
  807. /*
  808. * Make sure all modal buttons respond to all events
  809. */
  810. var $buttons = modal.querySelectorAll('button');
  811. var buttonEvents = ['onclick'];
  812. var onButtonEvent = function onButtonEvent(e) {
  813. return (0, _handleClick.handleButton)(e, params, modal);
  814. };
  815. for (var btnIndex = 0; btnIndex < $buttons.length; btnIndex++) {
  816. for (var evtIndex = 0; evtIndex < buttonEvents.length; evtIndex++) {
  817. var btnEvt = buttonEvents[evtIndex];
  818. $buttons[btnIndex][btnEvt] = onButtonEvent;
  819. }
  820. }
  821. // Clicking outside the modal dismisses it (if allowed by user)
  822. (0, _handleSwalDom.getOverlay)().onclick = onButtonEvent;
  823. previousWindowKeyDown = window.onkeydown;
  824. var onKeyEvent = function onKeyEvent(e) {
  825. return (0, _handleKey2.default)(e, params, modal);
  826. };
  827. window.onkeydown = onKeyEvent;
  828. window.onfocus = function () {
  829. // When the user has focused away and focused back from the whole window.
  830. setTimeout(function () {
  831. // Put in a timeout to jump out of the event sequence.
  832. // Calling focus() in the event sequence confuses things.
  833. if (lastFocusedButton !== undefined) {
  834. lastFocusedButton.focus();
  835. lastFocusedButton = undefined;
  836. }
  837. }, 0);
  838. };
  839. // Show alert with enabled buttons always
  840. _swal.enableButtons();
  841. };
  842. /*
  843. * Set default params for each popup
  844. * @param {Object} userParams
  845. */
  846. sweetAlert.setDefaults = _swal.setDefaults = function (userParams) {
  847. if (!userParams) {
  848. throw new Error('userParams is required');
  849. }
  850. if ((typeof userParams === 'undefined' ? 'undefined' : _typeof(userParams)) !== 'object') {
  851. throw new Error('userParams has to be a object');
  852. }
  853. (0, _utils.extend)(_defaultParams2.default, userParams);
  854. };
  855. /*
  856. * Animation when closing modal
  857. */
  858. sweetAlert.close = _swal.close = function () {
  859. var modal = (0, _handleSwalDom.getModal)();
  860. (0, _handleDom.fadeOut)((0, _handleSwalDom.getOverlay)(), 5);
  861. (0, _handleDom.fadeOut)(modal, 5);
  862. (0, _handleDom.removeClass)(modal, 'showSweetAlert');
  863. (0, _handleDom.addClass)(modal, 'hideSweetAlert');
  864. (0, _handleDom.removeClass)(modal, 'visible');
  865. /*
  866. * Reset icon animations
  867. */
  868. var $successIcon = modal.querySelector('.sa-icon.sa-success');
  869. (0, _handleDom.removeClass)($successIcon, 'animate');
  870. (0, _handleDom.removeClass)($successIcon.querySelector('.sa-tip'), 'animateSuccessTip');
  871. (0, _handleDom.removeClass)($successIcon.querySelector('.sa-long'), 'animateSuccessLong');
  872. var $errorIcon = modal.querySelector('.sa-icon.sa-error');
  873. (0, _handleDom.removeClass)($errorIcon, 'animateErrorIcon');
  874. (0, _handleDom.removeClass)($errorIcon.querySelector('.sa-x-mark'), 'animateXMark');
  875. var $warningIcon = modal.querySelector('.sa-icon.sa-warning');
  876. (0, _handleDom.removeClass)($warningIcon, 'pulseWarning');
  877. (0, _handleDom.removeClass)($warningIcon.querySelector('.sa-body'), 'pulseWarningIns');
  878. (0, _handleDom.removeClass)($warningIcon.querySelector('.sa-dot'), 'pulseWarningIns');
  879. // Reset custom class (delay so that UI changes aren't visible)
  880. setTimeout(function () {
  881. var customClass = modal.getAttribute('data-custom-class');
  882. (0, _handleDom.removeClass)(modal, customClass);
  883. }, 300);
  884. // Make page scrollable again
  885. (0, _handleDom.removeClass)(document.body, 'stop-scrolling');
  886. // Reset the page to its previous state
  887. window.onkeydown = previousWindowKeyDown;
  888. if (window.previousActiveElement) {
  889. window.previousActiveElement.focus();
  890. }
  891. lastFocusedButton = undefined;
  892. clearTimeout(modal.timeout);
  893. return true;
  894. };
  895. /*
  896. * Validation of the input field is done by user
  897. * If something is wrong => call showInputError with errorMessage
  898. */
  899. sweetAlert.showInputError = _swal.showInputError = function (errorMessage) {
  900. var modal = (0, _handleSwalDom.getModal)();
  901. var $errorIcon = modal.querySelector('.sa-input-error');
  902. (0, _handleDom.addClass)($errorIcon, 'show');
  903. var $errorContainer = modal.querySelector('.form-group');
  904. (0, _handleDom.addClass)($errorContainer, 'has-error');
  905. $errorContainer.querySelector('.sa-help-text').innerHTML = errorMessage;
  906. setTimeout(function () {
  907. sweetAlert.enableButtons();
  908. }, 1);
  909. modal.querySelector('input').focus();
  910. };
  911. /*
  912. * Reset input error DOM elements
  913. */
  914. sweetAlert.resetInputError = _swal.resetInputError = function (event) {
  915. // If press enter => ignore
  916. if (event && event.keyCode === 13) {
  917. return false;
  918. }
  919. var $modal = (0, _handleSwalDom.getModal)();
  920. var $errorIcon = $modal.querySelector('.sa-input-error');
  921. (0, _handleDom.removeClass)($errorIcon, 'show');
  922. var $errorContainer = $modal.querySelector('.form-group');
  923. (0, _handleDom.removeClass)($errorContainer, 'has-error');
  924. };
  925. /*
  926. * Disable confirm and cancel buttons
  927. */
  928. sweetAlert.disableButtons = _swal.disableButtons = function (event) {
  929. var modal = (0, _handleSwalDom.getModal)();
  930. var $confirmButton = modal.querySelector('button.confirm');
  931. var $cancelButton = modal.querySelector('button.cancel');
  932. $confirmButton.disabled = true;
  933. $cancelButton.disabled = true;
  934. };
  935. /*
  936. * Enable confirm and cancel buttons
  937. */
  938. sweetAlert.enableButtons = _swal.enableButtons = function (event) {
  939. var modal = (0, _handleSwalDom.getModal)();
  940. var $confirmButton = modal.querySelector('button.confirm');
  941. var $cancelButton = modal.querySelector('button.cancel');
  942. $confirmButton.disabled = false;
  943. $cancelButton.disabled = false;
  944. };
  945. if (typeof window !== 'undefined') {
  946. // The 'handle-click' module requires
  947. // that 'sweetAlert' was set as global.
  948. window.sweetAlert = window.swal = sweetAlert;
  949. } else {
  950. (0, _utils.logStr)('SweetAlert is a frontend module!');
  951. }
  952. },{"./modules/default-params":1,"./modules/handle-click":2,"./modules/handle-dom":3,"./modules/handle-key":4,"./modules/handle-swal-dom":5,"./modules/set-params":7,"./modules/utils":8}]},{},[9]);
  953. /*
  954. * Use SweetAlert with RequireJS
  955. */
  956. if (typeof define === 'function' && define.amd) {
  957. define(function () {
  958. return sweetAlert;
  959. });
  960. } else if (typeof module !== 'undefined' && module.exports) {
  961. module.exports = sweetAlert;
  962. }
  963. })(window, document);