sweetalert.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. 'use strict';
  2. var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; };
  3. Object.defineProperty(exports, '__esModule', {
  4. value: true
  5. });
  6. // SweetAlert
  7. // 2014-2015 (c) - Tristan Edwards
  8. // github.com/t4t5/sweetalert
  9. /*
  10. * jQuery-like functions for manipulating the DOM
  11. */
  12. var _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation = require('./modules/handle-dom');
  13. /*
  14. * Handy utilities
  15. */
  16. var _extend$hexToRgb$isIE8$logStr$colorLuminance = require('./modules/utils');
  17. /*
  18. * Handle sweetAlert's DOM elements
  19. */
  20. var _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition = require('./modules/handle-swal-dom');
  21. // Handle button events and keyboard events
  22. var _handleButton$handleConfirm$handleCancel = require('./modules/handle-click');
  23. var _handleKeyDown = require('./modules/handle-key');
  24. var _handleKeyDown2 = _interopRequireWildcard(_handleKeyDown);
  25. // Default values
  26. var _defaultParams = require('./modules/default-params');
  27. var _defaultParams2 = _interopRequireWildcard(_defaultParams);
  28. var _setParameters = require('./modules/set-params');
  29. var _setParameters2 = _interopRequireWildcard(_setParameters);
  30. /*
  31. * Remember state in cases where opening and handling a modal will fiddle with it.
  32. * (We also use window.previousActiveElement as a global variable)
  33. */
  34. var previousWindowKeyDown;
  35. var lastFocusedButton;
  36. /*
  37. * Global sweetAlert function
  38. * (this is what the user calls)
  39. */
  40. var sweetAlert, swal;
  41. exports['default'] = sweetAlert = swal = function () {
  42. var customizations = arguments[0];
  43. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.addClass(document.body, 'stop-scrolling');
  44. _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.resetInput();
  45. /*
  46. * Use argument if defined or default value from params object otherwise.
  47. * Supports the case where a default value is boolean true and should be
  48. * overridden by a corresponding explicit argument which is boolean false.
  49. */
  50. function argumentOrDefault(key) {
  51. var args = customizations;
  52. return args[key] === undefined ? _defaultParams2['default'][key] : args[key];
  53. }
  54. if (customizations === undefined) {
  55. _extend$hexToRgb$isIE8$logStr$colorLuminance.logStr('SweetAlert expects at least 1 attribute!');
  56. return false;
  57. }
  58. var params = _extend$hexToRgb$isIE8$logStr$colorLuminance.extend({}, _defaultParams2['default']);
  59. switch (typeof customizations) {
  60. // Ex: swal("Hello", "Just testing", "info");
  61. case 'string':
  62. params.title = customizations;
  63. params.text = arguments[1] || '';
  64. params.type = arguments[2] || '';
  65. break;
  66. // Ex: swal({ title:"Hello", text: "Just testing", type: "info" });
  67. case 'object':
  68. if (customizations.title === undefined) {
  69. _extend$hexToRgb$isIE8$logStr$colorLuminance.logStr('Missing "title" argument!');
  70. return false;
  71. }
  72. params.title = customizations.title;
  73. for (var customName in _defaultParams2['default']) {
  74. params[customName] = argumentOrDefault(customName);
  75. }
  76. // Show "Confirm" instead of "OK" if cancel button is visible
  77. params.confirmButtonText = params.showCancelButton ? 'Confirm' : _defaultParams2['default'].confirmButtonText;
  78. params.confirmButtonText = argumentOrDefault('confirmButtonText');
  79. // Callback function when clicking on "OK"/"Cancel"
  80. params.doneFunction = arguments[1] || null;
  81. break;
  82. default:
  83. _extend$hexToRgb$isIE8$logStr$colorLuminance.logStr('Unexpected type of argument! Expected "string" or "object", got ' + typeof customizations);
  84. return false;
  85. }
  86. _setParameters2['default'](params);
  87. _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.fixVerticalPosition();
  88. _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.openModal(arguments[1]);
  89. // Modal interactions
  90. var modal = _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getModal();
  91. /*
  92. * Make sure all modal buttons respond to all events
  93. */
  94. var $buttons = modal.querySelectorAll('button');
  95. var buttonEvents = ['onclick', 'onmouseover', 'onmouseout', 'onmousedown', 'onmouseup', 'onfocus'];
  96. var onButtonEvent = function onButtonEvent(e) {
  97. return _handleButton$handleConfirm$handleCancel.handleButton(e, params, modal);
  98. };
  99. for (var btnIndex = 0; btnIndex < $buttons.length; btnIndex++) {
  100. for (var evtIndex = 0; evtIndex < buttonEvents.length; evtIndex++) {
  101. var btnEvt = buttonEvents[evtIndex];
  102. $buttons[btnIndex][btnEvt] = onButtonEvent;
  103. }
  104. }
  105. // Clicking outside the modal dismisses it (if allowed by user)
  106. _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getOverlay().onclick = onButtonEvent;
  107. previousWindowKeyDown = window.onkeydown;
  108. var onKeyEvent = function onKeyEvent(e) {
  109. return _handleKeyDown2['default'](e, params, modal);
  110. };
  111. window.onkeydown = onKeyEvent;
  112. window.onfocus = function () {
  113. // When the user has focused away and focused back from the whole window.
  114. setTimeout(function () {
  115. // Put in a timeout to jump out of the event sequence.
  116. // Calling focus() in the event sequence confuses things.
  117. if (lastFocusedButton !== undefined) {
  118. lastFocusedButton.focus();
  119. lastFocusedButton = undefined;
  120. }
  121. }, 0);
  122. };
  123. // Show alert with enabled buttons always
  124. swal.enableButtons();
  125. };
  126. /*
  127. * Set default params for each popup
  128. * @param {Object} userParams
  129. */
  130. sweetAlert.setDefaults = swal.setDefaults = function (userParams) {
  131. if (!userParams) {
  132. throw new Error('userParams is required');
  133. }
  134. if (typeof userParams !== 'object') {
  135. throw new Error('userParams has to be a object');
  136. }
  137. _extend$hexToRgb$isIE8$logStr$colorLuminance.extend(_defaultParams2['default'], userParams);
  138. };
  139. /*
  140. * Animation when closing modal
  141. */
  142. sweetAlert.close = swal.close = function () {
  143. var modal = _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getModal();
  144. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.fadeOut(_sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getOverlay(), 5);
  145. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.fadeOut(modal, 5);
  146. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass(modal, 'showSweetAlert');
  147. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.addClass(modal, 'hideSweetAlert');
  148. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass(modal, 'visible');
  149. /*
  150. * Reset icon animations
  151. */
  152. var $successIcon = modal.querySelector('.sa-icon.sa-success');
  153. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($successIcon, 'animate');
  154. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($successIcon.querySelector('.sa-tip'), 'animateSuccessTip');
  155. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($successIcon.querySelector('.sa-long'), 'animateSuccessLong');
  156. var $errorIcon = modal.querySelector('.sa-icon.sa-error');
  157. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($errorIcon, 'animateErrorIcon');
  158. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($errorIcon.querySelector('.sa-x-mark'), 'animateXMark');
  159. var $warningIcon = modal.querySelector('.sa-icon.sa-warning');
  160. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($warningIcon, 'pulseWarning');
  161. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($warningIcon.querySelector('.sa-body'), 'pulseWarningIns');
  162. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($warningIcon.querySelector('.sa-dot'), 'pulseWarningIns');
  163. // Reset custom class (delay so that UI changes aren't visible)
  164. setTimeout(function () {
  165. var customClass = modal.getAttribute('data-custom-class');
  166. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass(modal, customClass);
  167. }, 300);
  168. // Make page scrollable again
  169. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass(document.body, 'stop-scrolling');
  170. // Reset the page to its previous state
  171. window.onkeydown = previousWindowKeyDown;
  172. if (window.previousActiveElement) {
  173. window.previousActiveElement.focus();
  174. }
  175. lastFocusedButton = undefined;
  176. clearTimeout(modal.timeout);
  177. return true;
  178. };
  179. /*
  180. * Validation of the input field is done by user
  181. * If something is wrong => call showInputError with errorMessage
  182. */
  183. sweetAlert.showInputError = swal.showInputError = function (errorMessage) {
  184. var modal = _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getModal();
  185. var $errorIcon = modal.querySelector('.sa-input-error');
  186. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.addClass($errorIcon, 'show');
  187. var $errorContainer = modal.querySelector('.sa-error-container');
  188. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.addClass($errorContainer, 'show');
  189. $errorContainer.querySelector('p').innerHTML = errorMessage;
  190. setTimeout(function () {
  191. sweetAlert.enableButtons();
  192. }, 1);
  193. modal.querySelector('input').focus();
  194. };
  195. /*
  196. * Reset input error DOM elements
  197. */
  198. sweetAlert.resetInputError = swal.resetInputError = function (event) {
  199. // If press enter => ignore
  200. if (event && event.keyCode === 13) {
  201. return false;
  202. }
  203. var $modal = _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getModal();
  204. var $errorIcon = $modal.querySelector('.sa-input-error');
  205. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($errorIcon, 'show');
  206. var $errorContainer = $modal.querySelector('.sa-error-container');
  207. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($errorContainer, 'show');
  208. };
  209. /*
  210. * Disable confirm and cancel buttons
  211. */
  212. sweetAlert.disableButtons = swal.disableButtons = function (event) {
  213. var modal = _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getModal();
  214. var $confirmButton = modal.querySelector('button.confirm');
  215. var $cancelButton = modal.querySelector('button.cancel');
  216. $confirmButton.disabled = true;
  217. $cancelButton.disabled = true;
  218. };
  219. /*
  220. * Enable confirm and cancel buttons
  221. */
  222. sweetAlert.enableButtons = swal.enableButtons = function (event) {
  223. var modal = _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getModal();
  224. var $confirmButton = modal.querySelector('button.confirm');
  225. var $cancelButton = modal.querySelector('button.cancel');
  226. $confirmButton.disabled = false;
  227. $cancelButton.disabled = false;
  228. };
  229. if (typeof window !== 'undefined') {
  230. // The 'handle-click' module requires
  231. // that 'sweetAlert' was set as global.
  232. window.sweetAlert = window.swal = sweetAlert;
  233. } else {
  234. _extend$hexToRgb$isIE8$logStr$colorLuminance.logStr('SweetAlert is a frontend module!');
  235. }
  236. module.exports = exports['default'];