set-params.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. var _isIE8 = require('./utils');
  6. var _getModal$getInput$setFocusStyle = require('./handle-swal-dom');
  7. var _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide = require('./handle-dom');
  8. var alertTypes = ['error', 'warning', 'info', 'success', 'input', 'prompt'];
  9. /*
  10. * Set type, text and actions on modal
  11. */
  12. var setParameters = function setParameters(params) {
  13. var modal = _getModal$getInput$setFocusStyle.getModal();
  14. var $title = modal.querySelector('h2');
  15. var $text = modal.querySelector('p');
  16. var $cancelBtn = modal.querySelector('button.cancel');
  17. var $confirmBtn = modal.querySelector('button.confirm');
  18. /*
  19. * Title
  20. */
  21. $title.innerHTML = params.html ? params.title : _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.escapeHtml(params.title).split('\n').join('<br>');
  22. /*
  23. * Text
  24. */
  25. $text.innerHTML = params.html ? params.text : _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.escapeHtml(params.text || '').split('\n').join('<br>');
  26. if (params.text) _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.show($text);
  27. /*
  28. * Custom class
  29. */
  30. if (params.customClass) {
  31. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.addClass(modal, params.customClass);
  32. modal.setAttribute('data-custom-class', params.customClass);
  33. } else {
  34. // Find previously set classes and remove them
  35. var customClass = modal.getAttribute('data-custom-class');
  36. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.removeClass(modal, customClass);
  37. modal.setAttribute('data-custom-class', '');
  38. }
  39. /*
  40. * Icon
  41. */
  42. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.hide(modal.querySelectorAll('.sa-icon'));
  43. if (params.type && !_isIE8.isIE8()) {
  44. var _ret = (function () {
  45. var validType = false;
  46. for (var i = 0; i < alertTypes.length; i++) {
  47. if (params.type === alertTypes[i]) {
  48. validType = true;
  49. break;
  50. }
  51. }
  52. if (!validType) {
  53. logStr('Unknown alert type: ' + params.type);
  54. return {
  55. v: false
  56. };
  57. }
  58. var typesWithIcons = ['success', 'error', 'warning', 'info'];
  59. var $icon = undefined;
  60. if (typesWithIcons.indexOf(params.type) !== -1) {
  61. $icon = modal.querySelector('.sa-icon.' + 'sa-' + params.type);
  62. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.show($icon);
  63. }
  64. var $input = _getModal$getInput$setFocusStyle.getInput();
  65. // Animate icon
  66. switch (params.type) {
  67. case 'success':
  68. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.addClass($icon, 'animate');
  69. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.addClass($icon.querySelector('.sa-tip'), 'animateSuccessTip');
  70. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.addClass($icon.querySelector('.sa-long'), 'animateSuccessLong');
  71. break;
  72. case 'error':
  73. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.addClass($icon, 'animateErrorIcon');
  74. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.addClass($icon.querySelector('.sa-x-mark'), 'animateXMark');
  75. break;
  76. case 'warning':
  77. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.addClass($icon, 'pulseWarning');
  78. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.addClass($icon.querySelector('.sa-body'), 'pulseWarningIns');
  79. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.addClass($icon.querySelector('.sa-dot'), 'pulseWarningIns');
  80. break;
  81. case 'input':
  82. case 'prompt':
  83. $input.setAttribute('type', params.inputType);
  84. $input.value = params.inputValue;
  85. $input.setAttribute('placeholder', params.inputPlaceholder);
  86. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.addClass(modal, 'show-input');
  87. setTimeout(function () {
  88. $input.focus();
  89. $input.addEventListener('keyup', swal.resetInputError);
  90. }, 400);
  91. break;
  92. }
  93. })();
  94. if (typeof _ret === 'object') {
  95. return _ret.v;
  96. }
  97. }
  98. /*
  99. * Custom image
  100. */
  101. if (params.imageUrl) {
  102. var $customIcon = modal.querySelector('.sa-icon.sa-custom');
  103. $customIcon.style.backgroundImage = 'url(' + params.imageUrl + ')';
  104. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.show($customIcon);
  105. var _imgWidth = 80;
  106. var _imgHeight = 80;
  107. if (params.imageSize) {
  108. var dimensions = params.imageSize.toString().split('x');
  109. var imgWidth = dimensions[0];
  110. var imgHeight = dimensions[1];
  111. if (!imgWidth || !imgHeight) {
  112. logStr('Parameter imageSize expects value with format WIDTHxHEIGHT, got ' + params.imageSize);
  113. } else {
  114. _imgWidth = imgWidth;
  115. _imgHeight = imgHeight;
  116. }
  117. }
  118. $customIcon.setAttribute('style', $customIcon.getAttribute('style') + 'width:' + _imgWidth + 'px; height:' + _imgHeight + 'px');
  119. }
  120. /*
  121. * Show cancel button?
  122. */
  123. modal.setAttribute('data-has-cancel-button', params.showCancelButton);
  124. if (params.showCancelButton) {
  125. $cancelBtn.style.display = 'inline-block';
  126. } else {
  127. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.hide($cancelBtn);
  128. }
  129. /*
  130. * Show confirm button?
  131. */
  132. modal.setAttribute('data-has-confirm-button', params.showConfirmButton);
  133. if (params.showConfirmButton) {
  134. $confirmBtn.style.display = 'inline-block';
  135. } else {
  136. _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.hide($confirmBtn);
  137. }
  138. /*
  139. * Custom text on cancel/confirm buttons
  140. */
  141. if (params.cancelButtonText) {
  142. $cancelBtn.innerHTML = _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.escapeHtml(params.cancelButtonText);
  143. }
  144. if (params.confirmButtonText) {
  145. $confirmBtn.innerHTML = _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide.escapeHtml(params.confirmButtonText);
  146. }
  147. /*
  148. * Custom color on confirm button
  149. */
  150. if (params.confirmButtonColor) {
  151. // Set confirm button to selected background color
  152. $confirmBtn.style.backgroundColor = params.confirmButtonColor;
  153. // Set the confirm button color to the loading ring
  154. $confirmBtn.style.borderLeftColor = params.confirmLoadingButtonColor;
  155. $confirmBtn.style.borderRightColor = params.confirmLoadingButtonColor;
  156. // Set box-shadow to default focused button
  157. _getModal$getInput$setFocusStyle.setFocusStyle($confirmBtn, params.confirmButtonColor);
  158. }
  159. /*
  160. * Allow outside click
  161. */
  162. modal.setAttribute('data-allow-outside-click', params.allowOutsideClick);
  163. /*
  164. * Callback function
  165. */
  166. var hasDoneFunction = params.doneFunction ? true : false;
  167. modal.setAttribute('data-has-done-function', hasDoneFunction);
  168. /*
  169. * Animation
  170. */
  171. if (!params.animation) {
  172. modal.setAttribute('data-animation', 'none');
  173. } else if (typeof params.animation === 'string') {
  174. modal.setAttribute('data-animation', params.animation); // Custom animation
  175. } else {
  176. modal.setAttribute('data-animation', 'pop');
  177. }
  178. /*
  179. * Timer
  180. */
  181. modal.setAttribute('data-timer', params.timer);
  182. };
  183. exports['default'] = setParameters;
  184. module.exports = exports['default'];