stag-popup.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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]') && !_noEvent) {
  48. if(stagPopupsQueue.length) {
  49. refreshDynamicStagPopup();
  50. }
  51. else {
  52. fastReload()
  53. return;
  54. }
  55. }
  56. }
  57. // if all closed
  58. if(!stagPopupsQueue.length) {
  59. $('html, body').removeClass('no-scroll');
  60. $(window.top.document.body).find('#stag_mcp_lhs').removeClass('no-scroll');
  61. if(!_noEvent) {
  62. $('body').trigger('stag-popup-closed');
  63. }
  64. }
  65. return false;
  66. }
  67. function openDynamicStagPopup(url, initer, title, updateParent, style = '') {
  68. url += (url.indexOf('?') !== -1 ? '&' : '?') + 'popupmode=1';
  69. showMask();
  70. window.noMc = true;
  71. $.get(url, (_data) => {
  72. let popup = $('.dynamic-popup[stag-popup-key="' + url + '"]');
  73. if(!popup.length) {
  74. $('main.stag-content').append(
  75. '<div class="stag-popup ' + (style ? style : 'stag-popup-lg') + ' dynamic-popup mcp-theme-1" stag-popup-key="' + url + '">' +
  76. '<div class="stag-popup-content p-0">' +
  77. '<h3 class="stag-popup-title mb-0 mt-3 mx-3 pb-0 border-bottom-0"><span></span>' +
  78. '<a href="#" class="ml-auto text-secondary" onclick="return closeStagPopup()"><i class="fa fa-times-circle"></i></a>\n' +
  79. '</h3>' +
  80. '<div class="stag-popup-content-inner"></div>' +
  81. '</div>' +
  82. '</div>'
  83. );
  84. popup = $('.dynamic-popup[stag-popup-key="' + url + '"]');
  85. }
  86. popup.attr('mc-initer', initer);
  87. popup.find('.stag-popup-title>span').text(title);
  88. popup.find('.stag-popup-content-inner').html(_data);
  89. if(updateParent) {
  90. popup.attr('update-parent', 1);
  91. }
  92. else {
  93. popup.removeAttr('update-parent');
  94. }
  95. showStagPopup(url);
  96. if(initer) runMCInitializer(initer);
  97. runMCInitializer('pro-suggest');
  98. initMoes();
  99. hideMask();
  100. });
  101. }
  102. function isDynamicStagPopupPresent() {
  103. if(!stagPopupsQueue.length) return false;
  104. let popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  105. if(popup.is('.dynamic-popup')) return true;
  106. return false;
  107. }
  108. function refreshDynamicStagPopup(_url = false) {
  109. if(!stagPopupsQueue.length) return false;
  110. let popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  111. if(popup.is('.dynamic-popup')) {
  112. showMask();
  113. window.noMc = true;
  114. if(_url) {
  115. popup.attr('stag-popup-key', _url);
  116. }
  117. let url = popup.attr('stag-popup-key'),
  118. initer = popup.attr('mc-initer');
  119. $.get(url, (_data) => {
  120. popup.find('.stag-popup-content-inner').html(_data);
  121. if(initer) runMCInitializer(initer);
  122. runMCInitializer('pro-suggest');
  123. initMoes();
  124. hideMask();
  125. });
  126. }
  127. return false;
  128. }
  129. function hasResponseError(_data) {
  130. let msg = 'Unknown error!';
  131. if (_data) {
  132. if (_data.success) return false;
  133. else if (_data.message) msg = _data.message;
  134. }
  135. toastr.error(msg);
  136. return true;
  137. }
  138. (function() {
  139. window.initStagPopupEvents = function () {
  140. $(document)
  141. .off('click.stag-popup-discard', '.stag-popup')
  142. .on('click.stag-popup-discard', '.stag-popup', function(_e) {
  143. if($(_e.target).is('.stag-popup')) {
  144. closeStagPopup();
  145. return false;
  146. }
  147. });
  148. // catch ESC and discard any visible popups
  149. $(document)
  150. .off('keydown.stag-popup-escape')
  151. .on('keydown.stag-popup-escape', function (e) {
  152. if(e.which === 27) {
  153. if(!window.moeClosedAt || (new Date()).getTime() - window.moeClosedAt > 1000) {
  154. if(stagPopupsQueue.length) {
  155. closeStagPopup();
  156. return false;
  157. }
  158. }
  159. }
  160. });
  161. $(document)
  162. .off('click.open-in-stag-popup', '[open-in-stag-popup]')
  163. .on('click.open-in-stag-popup', '[open-in-stag-popup]', function() {
  164. let trig = $(this);
  165. openDynamicStagPopup(
  166. trig.attr('href'),
  167. trig.attr('mc-initer'),
  168. trig.attr('title'),
  169. trig.is('[update-parent]'),
  170. trig.attr('popup-style')
  171. );
  172. return false;
  173. });
  174. $(document)
  175. .off('click.close-stag-popup', '.btn-close-stag-popup')
  176. .on('click.close-stag-popup', '.btn-close-stag-popup', function() {
  177. closeStagPopup();
  178. return false;
  179. });
  180. }
  181. addMCInitializer('stag-popups', window.initStagPopupEvents);
  182. })();