handle-swal-dom.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { hexToRgb } from './utils';
  2. import { removeClass, getTopMargin, fadeIn, show, addClass } from './handle-dom';
  3. import defaultParams from './default-params';
  4. var modalClass = '.sweet-alert';
  5. var overlayClass = '.sweet-overlay';
  6. /*
  7. * Add modal + overlay to DOM
  8. */
  9. import injectedHTML from './injected-html';
  10. var sweetAlertInitialize = function() {
  11. var sweetWrap = document.createElement('div');
  12. sweetWrap.innerHTML = injectedHTML;
  13. // Append elements to body
  14. while (sweetWrap.firstChild) {
  15. document.body.appendChild(sweetWrap.firstChild);
  16. }
  17. };
  18. /*
  19. * Get DOM element of modal
  20. */
  21. var getModal = function() {
  22. var $modal = document.querySelector(modalClass);
  23. if (!$modal) {
  24. sweetAlertInitialize();
  25. $modal = getModal();
  26. }
  27. return $modal;
  28. };
  29. /*
  30. * Get DOM element of input (in modal)
  31. */
  32. var getInput = function() {
  33. var $modal = getModal();
  34. if ($modal) {
  35. return $modal.querySelector('input');
  36. }
  37. };
  38. /*
  39. * Get DOM element of overlay
  40. */
  41. var getOverlay = function() {
  42. return document.querySelector(overlayClass);
  43. };
  44. /*
  45. * Add box-shadow style to button (depending on its chosen bg-color)
  46. */
  47. var setFocusStyle = function($button, bgColor) {
  48. var rgbColor = hexToRgb(bgColor);
  49. $button.style.boxShadow = '0 0 2px rgba(' + rgbColor + ', 0.8), inset 0 0 0 1px rgba(0, 0, 0, 0.05)';
  50. };
  51. /*
  52. * Animation when opening modal
  53. */
  54. var openModal = function(callback) {
  55. var $modal = getModal();
  56. fadeIn(getOverlay(), 10);
  57. show($modal);
  58. addClass($modal, 'showSweetAlert');
  59. removeClass($modal, 'hideSweetAlert');
  60. window.previousActiveElement = document.activeElement;
  61. var $okButton = $modal.querySelector('button.confirm');
  62. $okButton.focus();
  63. setTimeout(function () {
  64. addClass($modal, 'visible');
  65. }, 500);
  66. var timer = $modal.getAttribute('data-timer');
  67. if (timer !== 'null' && timer !== '') {
  68. var timerCallback = callback;
  69. $modal.timeout = setTimeout(function() {
  70. var doneFunctionExists = ((timerCallback || null) && $modal.getAttribute('data-has-done-function') === 'true');
  71. if (doneFunctionExists) {
  72. timerCallback(null);
  73. }
  74. else {
  75. sweetAlert.close();
  76. }
  77. }, timer);
  78. }
  79. };
  80. /*
  81. * Reset the styling of the input
  82. * (for example if errors have been shown)
  83. */
  84. var resetInput = function() {
  85. var $modal = getModal();
  86. var $input = getInput();
  87. removeClass($modal, 'show-input');
  88. $input.value = defaultParams.inputValue;
  89. $input.setAttribute('type', defaultParams.inputType);
  90. $input.setAttribute('placeholder', defaultParams.inputPlaceholder);
  91. resetInputError();
  92. };
  93. var resetInputError = function(event) {
  94. // If press enter => ignore
  95. if (event && event.keyCode === 13) {
  96. return false;
  97. }
  98. var $modal = getModal();
  99. var $errorIcon = $modal.querySelector('.sa-input-error');
  100. removeClass($errorIcon, 'show');
  101. var $errorContainer = $modal.querySelector('.sa-error-container');
  102. removeClass($errorContainer, 'show');
  103. };
  104. /*
  105. * Set "margin-top"-property on modal based on its computed height
  106. */
  107. var fixVerticalPosition = function() {
  108. var $modal = getModal();
  109. $modal.style.marginTop = getTopMargin(getModal());
  110. };
  111. export {
  112. sweetAlertInitialize,
  113. getModal,
  114. getOverlay,
  115. getInput,
  116. setFocusStyle,
  117. openModal,
  118. resetInput,
  119. resetInputError,
  120. fixVerticalPosition
  121. };