stag-popup.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. hideMoeFormMask();
  38. if(!stagPopupsQueue.length) return false;
  39. let popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  40. let closeAll = !!popup.attr('close-all-with-self');
  41. popup.removeClass('show');
  42. stagPopupsQueue.splice(stagPopupsQueue.length - 1, 1);
  43. if(closeAll) {
  44. while(stagPopupsQueue.length) {
  45. closeStagPopup(true);
  46. }
  47. }
  48. else {
  49. if(popup.is('[update-parent]') && !_noEvent) {
  50. let hook = popup.attr('update-parent');
  51. if(!!$.trim(hook) && hasMCHook(hook)) {
  52. runMCHook(hook)
  53. }
  54. else {
  55. if(stagPopupsQueue.length) {
  56. refreshDynamicStagPopup();
  57. }
  58. else {
  59. fastReload(popup.is('[update-target]') ? popup.attr('update-target') : '');
  60. return;
  61. }
  62. }
  63. }
  64. }
  65. // remove from the DOM
  66. if(popup.is('.dynamic-popup')) popup.remove();
  67. // if all closed
  68. if(!stagPopupsQueue.length) {
  69. $('html, body').removeClass('no-scroll');
  70. $(window.top.document.body).find('#stag_mcp_lhs').removeClass('no-scroll');
  71. if(!_noEvent) {
  72. $('body').trigger('stag-popup-closed');
  73. }
  74. }
  75. return false;
  76. }
  77. function convertContentLinksForInPopupNavigation(popup) {
  78. popup.find('.stag-popup-content-inner').find('a').each(function() {
  79. let a = $(this);
  80. if(!(!a.attr('href') ||
  81. a.is('[open-in-stag-popup]') ||
  82. a.is('[native]') ||
  83. a.attr('href') === '#' ||
  84. a[0].onclick)) {
  85. a.attr('replace-in-stag-popup', 1);
  86. }
  87. });
  88. }
  89. function openDynamicStagPopup(_url, initer, title, updateParent, style = '', replace = false, updateTarget = null) {
  90. let url = _url;
  91. if(url.indexOf('popupmode') === -1) {
  92. url += (url.indexOf('?') !== -1 ? '&' : '?') + 'popupmode=1';
  93. }
  94. showMask();
  95. window.noMc = true;
  96. $.get(url, (_data) => {
  97. let popup = null;
  98. if(replace) {
  99. if(!stagPopupsQueue.length) {
  100. console.error('No stag-popup currently visible!');
  101. return false;
  102. }
  103. popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  104. if(!popup.is('.dynamic-popup')) {
  105. console.error('Topmost stag-popup is not dynamic!');
  106. return false;
  107. }
  108. popup.attr('stag-popup-key', url);
  109. }
  110. else {
  111. popup = $('.dynamic-popup[stag-popup-key="' + url + '"]');
  112. if(!popup.length) {
  113. $('main.stag-content').append(
  114. '<div class="stag-popup ' + (style ? style : 'stag-popup-lg') + ' dynamic-popup mcp-theme-1" stag-popup-key="' + url + '">' +
  115. '<div class="stag-popup-content p-0">' +
  116. '<h3 class="stag-popup-title mb-0 mt-3 mx-3 pb-0 border-bottom-0"><span></span>' +
  117. '<a href="#" class="ml-auto text-secondary" onclick="return closeStagPopup()"><i class="fa fa-times-circle"></i></a>\n' +
  118. '</h3>' +
  119. '<div class="stag-popup-content-inner"></div>' +
  120. '</div>' +
  121. '</div>'
  122. );
  123. popup = $('.dynamic-popup[stag-popup-key="' + url + '"]');
  124. }
  125. }
  126. popup.attr('mc-initer', initer);
  127. popup.find('.stag-popup-title>span').html(title);
  128. popup.find('.stag-popup-content-inner').html(_data);
  129. convertContentLinksForInPopupNavigation(popup);
  130. if(!replace) {
  131. if(updateParent) {
  132. popup.attr('update-parent', updateParent);
  133. }
  134. else {
  135. popup.removeAttr('update-parent');
  136. }
  137. showStagPopup(url, true);
  138. if(updateTarget) {
  139. popup.attr('update-target', updateTarget);
  140. }
  141. }
  142. if(initer) runMCInitializer(initer);
  143. runMCInitializer('pro-suggest'); // not the place for this! Move to better place.
  144. initMoes();
  145. hideMask();
  146. initFileInputs();
  147. }).fail(() => {
  148. toastr.error('Unable to open ' + _url);
  149. hideMask();
  150. });
  151. }
  152. function isDynamicStagPopupPresent() {
  153. if(!stagPopupsQueue.length) return false;
  154. let popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  155. if(popup.is('.dynamic-popup')) return true;
  156. return false;
  157. }
  158. function refreshDynamicStagPopup(_url = false, _target = null) {
  159. if(!stagPopupsQueue.length) return false;
  160. let popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  161. if(popup.is('.dynamic-popup')) {
  162. showMask();
  163. window.noMc = true;
  164. if(_url) {
  165. popup.attr('stag-popup-key', _url);
  166. }
  167. let url = popup.attr('stag-popup-key'),
  168. initer = popup.attr('mc-initer');
  169. $.get(url, (_data) => {
  170. if(_target) {
  171. _data = '<div>' + _data + '</div>';
  172. _target = _target.split(',').map(_x => $.trim(_x));
  173. for (let i = 0; i < _target.length; i++) {
  174. let t = _target[i];
  175. let targetElement = $(t).first();
  176. if(targetElement.length) {
  177. let sourceElement = $(_data).find(t).first();
  178. if (sourceElement && sourceElement.length) {
  179. targetElement.html(sourceElement.html());
  180. initFastLoad(targetElement);
  181. console.log('Replaced ' + t);
  182. }
  183. else {
  184. console.warn(t + ' not found in returned content');
  185. }
  186. }
  187. else {
  188. console.warn(t + ' not found in existing content');
  189. }
  190. }
  191. }
  192. else {
  193. popup.find('.stag-popup-content-inner').html(_data);
  194. }
  195. convertContentLinksForInPopupNavigation(popup);
  196. if(initer) runMCInitializer(initer);
  197. runMCInitializer('pro-suggest');
  198. initMoes();
  199. hideMask();
  200. initFileInputs();
  201. });
  202. }
  203. return false;
  204. }
  205. function hasResponseError(_data) {
  206. let msg = 'Unknown error!';
  207. if (_data) {
  208. if (_data.success) return false;
  209. else if (_data.message) msg = _data.message;
  210. }
  211. toastr.error(msg);
  212. return true;
  213. }
  214. (function() {
  215. window.initStagPopupEvents = function () {
  216. $(document)
  217. .off('mousedown.stag-popup-discard', '.stag-popup')
  218. .on('mousedown.stag-popup-discard', '.stag-popup', function(_e) {
  219. if($(_e.target).is('.stag-popup') && !isEventConsumed(_e)) {
  220. closeStagPopup();
  221. return false;
  222. }
  223. });
  224. // catch ESC and discard any visible popups
  225. $(document)
  226. .off('keydown.stag-popup-escape')
  227. .on('keydown.stag-popup-escape', function (e) {
  228. if(e.which === 27) {
  229. if(!isEventConsumed(e)) {
  230. if(stagPopupsQueue.length) {
  231. if($('.stag-popup.show [moe] [url]:not([show]):visible').length || $('.stag-popup.show [visit-moe] [url]:not([show]):visible').length) {
  232. return;
  233. }
  234. closeStagPopup();
  235. markEventAsConsumed(e);
  236. return false;
  237. }
  238. }
  239. }
  240. });
  241. $(document)
  242. .off('click.open-in-stag-popup', '[open-in-stag-popup]')
  243. .on('click.open-in-stag-popup', '[open-in-stag-popup]', function() {
  244. let trig = $(this);
  245. let updateParent = trig.is('[update-parent]');
  246. if(updateParent) {
  247. if(!!$.trim(trig.attr('update-parent'))) {
  248. updateParent = trig.attr('update-parent');
  249. }
  250. else {
  251. updateParent = true;
  252. }
  253. }
  254. openDynamicStagPopup(
  255. trig.attr('href'),
  256. trig.attr('mc-initer'),
  257. trig.attr('title'),
  258. updateParent,
  259. trig.attr('popup-style'),
  260. false,
  261. trig.is('[update-target]') ? trig.attr('update-target') : null
  262. );
  263. return false;
  264. });
  265. $(document)
  266. .off('click.replace-in-stag-popup', '[replace-in-stag-popup]')
  267. .on('click.replace-in-stag-popup', '[replace-in-stag-popup]', function() {
  268. let trig = $(this);
  269. openDynamicStagPopup(
  270. trig.attr('href'),
  271. trig.attr('mc-initer'),
  272. trig.attr('title'),
  273. null, // will be inherited when replacing
  274. null, // will be inherited when replacing
  275. true,
  276. null
  277. );
  278. return false;
  279. });
  280. $(document)
  281. .off('click.close-stag-popup', '.btn-close-stag-popup')
  282. .on('click.close-stag-popup', '.btn-close-stag-popup', function() {
  283. closeStagPopup();
  284. return false;
  285. });
  286. }
  287. addMCInitializer('stag-popups', window.initStagPopupEvents);
  288. })();