stag-popup.js 5.7 KB

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