stag-popup.js 8.5 KB

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