stag-popup.js 8.4 KB

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