stag-popup.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. window.stagPopupsQueue = [];
  2. function showStagPopup(_key, _noAutoFocus) {
  3. /*$('html, body').addClass('no-scroll');
  4. $(window.top.document.body).find('#stag_mcp_lhs').addClass('no-scroll');*/
  5. let stagPopup = $('[stag-popup-key="' + _key + '"]');
  6. stagPopup.addClass('show');
  7. stagPopup.find('[moe][initialized]').removeAttr('initialized');
  8. initMoes();
  9. if(!_noAutoFocus) {
  10. window.setTimeout(function() {
  11. stagPopup.find('input[type="text"]:not([readonly]):visible,select:visible').first().focus();
  12. }, 150);
  13. }
  14. stagPopupsQueue.push(stagPopup);
  15. return false;
  16. }
  17. function submitStagPopup(_form) {
  18. if(!_form[0].checkValidity()) {
  19. _form[0].reportValidity();
  20. return false;
  21. }
  22. showMask();
  23. $.post(_form.attr('action'), _form.serialize(), function(_data) {
  24. stagPopupsQueue = [];
  25. fastReload();
  26. });
  27. return false;
  28. }
  29. function closeStagPopup(_noEvent = false) {
  30. if(!stagPopupsQueue.length) return false;
  31. let popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  32. let closeAll = !!popup.attr('close-all-with-self');
  33. popup.removeClass('show');
  34. stagPopupsQueue.splice(stagPopupsQueue.length - 1, 1);
  35. if(closeAll) {
  36. while(stagPopupsQueue.length) {
  37. closeStagPopup(true);
  38. }
  39. }
  40. else {
  41. if(popup.is('[update-parent]')) {
  42. if(stagPopupsQueue.length) {
  43. refreshDynamicStagPopup();
  44. }
  45. else {
  46. fastReload()
  47. }
  48. }
  49. }
  50. // if all closed
  51. if(!stagPopupsQueue.length) {
  52. $('html, body').removeClass('no-scroll');
  53. $(window.top.document.body).find('#stag_mcp_lhs').removeClass('no-scroll');
  54. if(!_noEvent) {
  55. $('body').trigger('stag-popup-closed');
  56. }
  57. }
  58. return false;
  59. }
  60. function openDynamicStagPopup(url, initer, title, updateParent) {
  61. url += (url.indexOf('?') !== -1 ? '&' : '?') + 'popupmode=1';
  62. showMask();
  63. window.noMc = true;
  64. $.get(url, (_data) => {
  65. let popup = $('.dynamic-popup[stag-popup-key="' + url + '"]');
  66. if(!popup.length) {
  67. $('main.stag-content').append(
  68. '<div class="stag-popup stag-popup-lg dynamic-popup mcp-theme-1" stag-popup-key="' + url + '">' +
  69. '<div class="stag-popup-content p-0">' +
  70. '<h3 class="stag-popup-title mb-0 mt-3 mx-3 pb-0 border-bottom-0"><span></span>' +
  71. '<a href="#" class="ml-auto text-secondary" onclick="return closeStagPopup()"><i class="fa fa-times-circle"></i></a>\n' +
  72. '</h3>' +
  73. '<div class="stag-popup-content-inner"></div>' +
  74. '</div>' +
  75. '</div>'
  76. );
  77. popup = $('.dynamic-popup[stag-popup-key="' + url + '"]');
  78. }
  79. popup.attr('mc-initer', initer);
  80. popup.find('.stag-popup-title>span').text(title);
  81. popup.find('.stag-popup-content-inner').html(_data);
  82. if(updateParent) {
  83. popup.attr('update-parent', 1);
  84. }
  85. else {
  86. popup.removeAttr('update-parent');
  87. }
  88. showStagPopup(url);
  89. if(initer) runMCInitializer(initer);
  90. runMCInitializer('pro-suggest');
  91. initMoes();
  92. hideMask();
  93. });
  94. }
  95. function isDynamicStagPopupPresent() {
  96. if(!stagPopupsQueue.length) return false;
  97. let popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  98. if(popup.is('.dynamic-popup')) return true;
  99. return false;
  100. }
  101. function refreshDynamicStagPopup() {
  102. if(!stagPopupsQueue.length) return;
  103. let popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  104. if(popup.is('.dynamic-popup')) {
  105. showMask();
  106. window.noMc = true;
  107. let url = popup.attr('stag-popup-key'),
  108. initer = popup.attr('mc-initer');
  109. $.get(url, (_data) => {
  110. popup.find('.stag-popup-content-inner').html(_data);
  111. if(initer) runMCInitializer(initer);
  112. runMCInitializer('pro-suggest');
  113. initMoes();
  114. hideMask();
  115. });
  116. }
  117. }
  118. (function() {
  119. window.initStagPopupEvents = function () {
  120. $(document)
  121. .off('click.stag-popup-discard', '.stag-popup')
  122. .on('click.stag-popup-discard', '.stag-popup', function(_e) {
  123. if($(_e.target).is('.stag-popup')) {
  124. closeStagPopup();
  125. return false;
  126. }
  127. });
  128. // catch ESC and discard any visible popups
  129. $(document)
  130. .off('keydown.stag-popup-escape')
  131. .on('keydown.stag-popup-escape', function (e) {
  132. if(e.which === 27) {
  133. if(!window.moeClosedAt || (new Date()).getTime() - window.moeClosedAt > 1000) {
  134. if(stagPopupsQueue.length) {
  135. closeStagPopup();
  136. return false;
  137. }
  138. }
  139. }
  140. });
  141. $(document)
  142. .off('click.open-in-stag-popup', 'a[open-in-stag-popup]')
  143. .on('click.open-in-stag-popup', 'a[open-in-stag-popup]', function() {
  144. openDynamicStagPopup(this.href, $(this).attr('mc-initer'), $(this).attr('title'), $(this).is('[update-parent]'));
  145. return false;
  146. });
  147. }
  148. addMCInitializer('stag-popups', window.initStagPopupEvents);
  149. })();