stag-popup.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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(popup.is('[update-target]') ? popup.attr('update-target') : '');
  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, updateTarget = null) {
  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').html(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. if(updateTarget) {
  132. popup.attr('update-target', updateTarget);
  133. }
  134. }
  135. if(initer) runMCInitializer(initer);
  136. runMCInitializer('pro-suggest'); // not the place for this! Move to better place.
  137. initMoes();
  138. hideMask();
  139. initFileInputs();
  140. }).fail(() => {
  141. toastr.error('Unable to open ' + _url);
  142. hideMask();
  143. });
  144. }
  145. function isDynamicStagPopupPresent() {
  146. if(!stagPopupsQueue.length) return false;
  147. let popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  148. if(popup.is('.dynamic-popup')) return true;
  149. return false;
  150. }
  151. function refreshDynamicStagPopup(_url = false) {
  152. if(!stagPopupsQueue.length) return false;
  153. let popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  154. if(popup.is('.dynamic-popup')) {
  155. showMask();
  156. window.noMc = true;
  157. if(_url) {
  158. popup.attr('stag-popup-key', _url);
  159. }
  160. let url = popup.attr('stag-popup-key'),
  161. initer = popup.attr('mc-initer');
  162. $.get(url, (_data) => {
  163. popup.find('.stag-popup-content-inner').html(_data);
  164. convertContentLinksForInPopupNavigation(popup);
  165. if(initer) runMCInitializer(initer);
  166. runMCInitializer('pro-suggest');
  167. initMoes();
  168. hideMask();
  169. initFileInputs();
  170. });
  171. }
  172. return false;
  173. }
  174. function hasResponseError(_data) {
  175. let msg = 'Unknown error!';
  176. if (_data) {
  177. if (_data.success) return false;
  178. else if (_data.message) msg = _data.message;
  179. }
  180. toastr.error(msg);
  181. return true;
  182. }
  183. (function() {
  184. window.initStagPopupEvents = function () {
  185. $(document)
  186. .off('mousedown.stag-popup-discard', '.stag-popup')
  187. .on('mousedown.stag-popup-discard', '.stag-popup', function(_e) {
  188. if($(_e.target).is('.stag-popup') && !isEventConsumed(_e)) {
  189. closeStagPopup();
  190. return false;
  191. }
  192. });
  193. // catch ESC and discard any visible popups
  194. $(document)
  195. .off('keydown.stag-popup-escape')
  196. .on('keydown.stag-popup-escape', function (e) {
  197. if(e.which === 27) {
  198. if(!isEventConsumed(e)) {
  199. if(stagPopupsQueue.length) {
  200. if($('.stag-popup.show [moe] [url]:visible').length || $('.stag-popup.show [visit-moe] [url]:visible').length) {
  201. return;
  202. }
  203. closeStagPopup();
  204. markEventAsConsumed(e);
  205. return false;
  206. }
  207. }
  208. }
  209. });
  210. $(document)
  211. .off('click.open-in-stag-popup', '[open-in-stag-popup]')
  212. .on('click.open-in-stag-popup', '[open-in-stag-popup]', function() {
  213. let trig = $(this);
  214. openDynamicStagPopup(
  215. trig.attr('href'),
  216. trig.attr('mc-initer'),
  217. trig.attr('title'),
  218. trig.is('[update-parent]'),
  219. trig.attr('popup-style'),
  220. false,
  221. trig.is('[update-target]') ? trig.attr('update-target') : null
  222. );
  223. return false;
  224. });
  225. $(document)
  226. .off('click.replace-in-stag-popup', '[replace-in-stag-popup]')
  227. .on('click.replace-in-stag-popup', '[replace-in-stag-popup]', function() {
  228. let trig = $(this);
  229. openDynamicStagPopup(
  230. trig.attr('href'),
  231. trig.attr('mc-initer'),
  232. trig.attr('title'),
  233. null, // will be inherited when replacing
  234. null, // will be inherited when replacing
  235. true,
  236. null
  237. );
  238. return false;
  239. });
  240. $(document)
  241. .off('click.close-stag-popup', '.btn-close-stag-popup')
  242. .on('click.close-stag-popup', '.btn-close-stag-popup', function() {
  243. closeStagPopup();
  244. return false;
  245. });
  246. }
  247. addMCInitializer('stag-popups', window.initStagPopupEvents);
  248. })();