stag-popup.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. // remove from the DOM
  58. if(popup.is('.dynamic-popup')) popup.remove();
  59. // if all closed
  60. if(!stagPopupsQueue.length) {
  61. $('html, body').removeClass('no-scroll');
  62. $(window.top.document.body).find('#stag_mcp_lhs').removeClass('no-scroll');
  63. if(!_noEvent) {
  64. $('body').trigger('stag-popup-closed');
  65. }
  66. }
  67. return false;
  68. }
  69. function convertContentLinksForInPopupNavigation(popup) {
  70. popup.find('.stag-popup-content-inner').find('a').each(function() {
  71. let a = $(this);
  72. if(!(!a.attr('href') ||
  73. a.is('[open-in-stag-popup]') ||
  74. a.is('[native]') ||
  75. a.attr('href') === '#' ||
  76. a[0].onclick)) {
  77. a.attr('replace-in-stag-popup', 1);
  78. }
  79. });
  80. }
  81. function openDynamicStagPopup(url, initer, title, updateParent, style = '', replace = false) {
  82. if(url.indexOf('popupmode') === -1) {
  83. url += (url.indexOf('?') !== -1 ? '&' : '?') + 'popupmode=1';
  84. }
  85. showMask();
  86. window.noMc = true;
  87. $.get(url, (_data) => {
  88. let popup = null;
  89. if(replace) {
  90. if(!stagPopupsQueue.length) {
  91. console.error('No stag-popup currently visible!');
  92. return false;
  93. }
  94. popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  95. if(!popup.is('.dynamic-popup')) {
  96. console.error('Topmost stag-popup is not dynamic!');
  97. return false;
  98. }
  99. popup.attr('stag-popup-key', url);
  100. }
  101. else {
  102. popup = $('.dynamic-popup[stag-popup-key="' + url + '"]');
  103. if(!popup.length) {
  104. $('main.stag-content').append(
  105. '<div class="stag-popup ' + (style ? style : 'stag-popup-lg') + ' dynamic-popup mcp-theme-1" stag-popup-key="' + url + '">' +
  106. '<div class="stag-popup-content p-0">' +
  107. '<h3 class="stag-popup-title mb-0 mt-3 mx-3 pb-0 border-bottom-0"><span></span>' +
  108. '<a href="#" class="ml-auto text-secondary" onclick="return closeStagPopup()"><i class="fa fa-times-circle"></i></a>\n' +
  109. '</h3>' +
  110. '<div class="stag-popup-content-inner"></div>' +
  111. '</div>' +
  112. '</div>'
  113. );
  114. popup = $('.dynamic-popup[stag-popup-key="' + url + '"]');
  115. }
  116. }
  117. popup.attr('mc-initer', initer);
  118. popup.find('.stag-popup-title>span').text(title);
  119. popup.find('.stag-popup-content-inner').html(_data);
  120. convertContentLinksForInPopupNavigation(popup);
  121. if(!replace) {
  122. if(updateParent) {
  123. popup.attr('update-parent', 1);
  124. }
  125. else {
  126. popup.removeAttr('update-parent');
  127. }
  128. showStagPopup(url, true);
  129. }
  130. if(initer) runMCInitializer(initer);
  131. runMCInitializer('pro-suggest'); // not the place for this! Move to better place.
  132. initMoes();
  133. hideMask();
  134. });
  135. }
  136. function isDynamicStagPopupPresent() {
  137. if(!stagPopupsQueue.length) return false;
  138. let popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  139. if(popup.is('.dynamic-popup')) return true;
  140. return false;
  141. }
  142. function refreshDynamicStagPopup(_url = false) {
  143. if(!stagPopupsQueue.length) return false;
  144. let popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  145. if(popup.is('.dynamic-popup')) {
  146. showMask();
  147. window.noMc = true;
  148. if(_url) {
  149. popup.attr('stag-popup-key', _url);
  150. }
  151. let url = popup.attr('stag-popup-key'),
  152. initer = popup.attr('mc-initer');
  153. $.get(url, (_data) => {
  154. popup.find('.stag-popup-content-inner').html(_data);
  155. convertContentLinksForInPopupNavigation(popup);
  156. if(initer) runMCInitializer(initer);
  157. runMCInitializer('pro-suggest');
  158. initMoes();
  159. hideMask();
  160. });
  161. }
  162. return false;
  163. }
  164. function hasResponseError(_data) {
  165. let msg = 'Unknown error!';
  166. if (_data) {
  167. if (_data.success) return false;
  168. else if (_data.message) msg = _data.message;
  169. }
  170. toastr.error(msg);
  171. return true;
  172. }
  173. (function() {
  174. window.initStagPopupEvents = function () {
  175. $(document)
  176. .off('click.stag-popup-discard', '.stag-popup')
  177. .on('click.stag-popup-discard', '.stag-popup', function(_e) {
  178. if($(_e.target).is('.stag-popup')) {
  179. closeStagPopup();
  180. return false;
  181. }
  182. });
  183. // catch ESC and discard any visible popups
  184. $(document)
  185. .off('keydown.stag-popup-escape')
  186. .on('keydown.stag-popup-escape', function (e) {
  187. if(e.which === 27) {
  188. if(!isEventConsumed(e)) {
  189. if(stagPopupsQueue.length) {
  190. closeStagPopup();
  191. markEventAsConsumed(e);
  192. return false;
  193. }
  194. }
  195. }
  196. });
  197. $(document)
  198. .off('click.open-in-stag-popup', '[open-in-stag-popup]')
  199. .on('click.open-in-stag-popup', '[open-in-stag-popup]', function() {
  200. let trig = $(this);
  201. openDynamicStagPopup(
  202. trig.attr('href'),
  203. trig.attr('mc-initer'),
  204. trig.attr('title'),
  205. trig.is('[update-parent]'),
  206. trig.attr('popup-style')
  207. );
  208. return false;
  209. });
  210. $(document)
  211. .off('click.replace-in-stag-popup', '[replace-in-stag-popup]')
  212. .on('click.replace-in-stag-popup', '[replace-in-stag-popup]', function() {
  213. let trig = $(this);
  214. openDynamicStagPopup(
  215. trig.attr('href'),
  216. trig.attr('mc-initer'),
  217. trig.attr('title'),
  218. null, // will be inherited when replacing
  219. null, // will be inherited when replacing
  220. true
  221. );
  222. return false;
  223. });
  224. $(document)
  225. .off('click.close-stag-popup', '.btn-close-stag-popup')
  226. .on('click.close-stag-popup', '.btn-close-stag-popup', function() {
  227. closeStagPopup();
  228. return false;
  229. });
  230. }
  231. addMCInitializer('stag-popups', window.initStagPopupEvents);
  232. })();