stag-popup.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 false;
  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. // close out any open inline visit-segment
  95. $('.note-section.visit-segment.edit').removeClass('edit');
  96. showMask();
  97. window.noMc = true;
  98. $.get(url, (_data) => {
  99. let popup = null;
  100. if(replace) {
  101. if(!stagPopupsQueue.length) {
  102. console.error('No stag-popup currently visible!');
  103. return false;
  104. }
  105. popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  106. if(!popup.is('.dynamic-popup')) {
  107. console.error('Topmost stag-popup is not dynamic!');
  108. return false;
  109. }
  110. popup.attr('stag-popup-key', url);
  111. }
  112. else {
  113. popup = $('.dynamic-popup[stag-popup-key="' + url + '"]');
  114. if(!popup.length) {
  115. $('main.stag-content').append(
  116. '<div class="stag-popup ' + (style ? style : 'stag-popup-lg') + ' dynamic-popup draggable resizable mcp-theme-1" stag-popup-key="' + url + '">' +
  117. '<div class="stag-popup-content p-0">' +
  118. '<h3 class="stag-popup-title mb-0 mt-3 mx-3 pb-0 border-bottom-0"><span></span>' +
  119. '<a href="#" class="ml-auto text-secondary" onclick="return closeStagPopup()"><i class="fa fa-times-circle"></i></a>\n' +
  120. '</h3>' +
  121. '<div class="stag-popup-content-inner"></div>' +
  122. '</div>' +
  123. '</div>'
  124. );
  125. popup = $('.dynamic-popup[stag-popup-key="' + url + '"]');
  126. }
  127. }
  128. popup.attr('mc-initer', initer);
  129. popup.find('.stag-popup-title>span').html(title);
  130. popup.find('.stag-popup-content-inner').html(_data);
  131. convertContentLinksForInPopupNavigation(popup);
  132. if(!replace) {
  133. if(updateParent) {
  134. popup.attr('update-parent', updateParent);
  135. }
  136. else {
  137. popup.removeAttr('update-parent');
  138. }
  139. showStagPopup(url, true);
  140. if(updateTarget) {
  141. popup.attr('update-target', updateTarget);
  142. }
  143. }
  144. if(initer) runMCInitializer(initer);
  145. runMCInitializer('pro-suggest'); // not the place for this! Move to better place.
  146. initMoes();
  147. hideMask();
  148. initFileInputs();
  149. }).fail(() => {
  150. toastr.error('Unable to open ' + _url);
  151. hideMask();
  152. });
  153. }
  154. function isDynamicStagPopupPresent() {
  155. if(!stagPopupsQueue.length) return false;
  156. let popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  157. if(popup.is('.dynamic-popup')) return true;
  158. return false;
  159. }
  160. function refreshDynamicStagPopup(_url = false, _target = null) {
  161. if(!stagPopupsQueue.length) return false;
  162. let popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  163. if(popup.is('.dynamic-popup')) {
  164. showMask();
  165. window.noMc = true;
  166. if(_url) {
  167. popup.attr('stag-popup-key', _url);
  168. }
  169. let url = popup.attr('stag-popup-key'),
  170. initer = popup.attr('mc-initer');
  171. $.get(url, (_data) => {
  172. if(_target) {
  173. _data = '<div>' + _data + '</div>';
  174. _target = _target.split(',').map(_x => $.trim(_x));
  175. for (let i = 0; i < _target.length; i++) {
  176. let t = _target[i];
  177. let targetElement = $(t).first();
  178. if(targetElement.length) {
  179. let sourceElement = $(_data).find(t).first();
  180. if (sourceElement && sourceElement.length) {
  181. targetElement.html(sourceElement.html());
  182. initFastLoad(targetElement);
  183. console.log('Replaced ' + t);
  184. }
  185. else {
  186. console.warn(t + ' not found in returned content');
  187. }
  188. }
  189. else {
  190. console.warn(t + ' not found in existing content');
  191. }
  192. }
  193. }
  194. else {
  195. popup.find('.stag-popup-content-inner').html(_data);
  196. }
  197. convertContentLinksForInPopupNavigation(popup);
  198. if(initer) runMCInitializer(initer);
  199. runMCInitializer('pro-suggest');
  200. initMoes();
  201. hideMask();
  202. initFileInputs();
  203. });
  204. }
  205. return false;
  206. }
  207. function hasResponseError(_data) {
  208. let msg = 'Unknown error!';
  209. if (_data) {
  210. if (_data.success) return false;
  211. else if (_data.message) msg = _data.message;
  212. }
  213. toastr.error(msg);
  214. return true;
  215. }
  216. (function() {
  217. window.initStagPopupEvents = function () {
  218. $(document)
  219. .off('mousedown.stag-popup-discard', '.stag-popup')
  220. .on('mousedown.stag-popup-discard', '.stag-popup', function(_e) {
  221. if($(_e.target).is('.stag-popup') && !isEventConsumed(_e)) {
  222. closeStagPopup();
  223. return false;
  224. }
  225. });
  226. // catch ESC and discard any visible popups
  227. $(document)
  228. .off('keydown.stag-popup-escape')
  229. .on('keydown.stag-popup-escape', function (e) {
  230. if(e.which === 27) {
  231. if(!isEventConsumed(e)) {
  232. if(stagPopupsQueue.length) {
  233. if($('.stag-popup.show [moe] [url]:not([show]):visible').length || $('.stag-popup.show [visit-moe] [url]:not([show]):visible').length) {
  234. return;
  235. }
  236. closeStagPopup();
  237. markEventAsConsumed(e);
  238. return false;
  239. }
  240. }
  241. }
  242. });
  243. $(document)
  244. .off('click.open-in-stag-popup', '[open-in-stag-popup]')
  245. .on('click.open-in-stag-popup', '[open-in-stag-popup]', function() {
  246. let trig = $(this);
  247. let updateParent = trig.is('[update-parent]');
  248. if(updateParent) {
  249. if(!!$.trim(trig.attr('update-parent'))) {
  250. updateParent = trig.attr('update-parent');
  251. }
  252. else {
  253. updateParent = true;
  254. }
  255. }
  256. openDynamicStagPopup(
  257. trig.attr('href'),
  258. trig.attr('mc-initer'),
  259. trig.attr('title'),
  260. updateParent,
  261. trig.attr('popup-style'),
  262. false,
  263. trig.is('[update-target]') ? trig.attr('update-target') : null
  264. );
  265. return false;
  266. });
  267. $(document)
  268. .off('click.replace-in-stag-popup', '[replace-in-stag-popup]')
  269. .on('click.replace-in-stag-popup', '[replace-in-stag-popup]', function() {
  270. let trig = $(this);
  271. openDynamicStagPopup(
  272. trig.attr('href'),
  273. trig.attr('mc-initer'),
  274. trig.attr('title'),
  275. null, // will be inherited when replacing
  276. null, // will be inherited when replacing
  277. true,
  278. null
  279. );
  280. return false;
  281. });
  282. $(document)
  283. .off('click.close-stag-popup', '.btn-close-stag-popup')
  284. .on('click.close-stag-popup', '.btn-close-stag-popup', function() {
  285. closeStagPopup();
  286. return false;
  287. });
  288. // draggable
  289. let dragging = false, dragX, dragY;
  290. $(document)
  291. .off('mousedown.start-drag', '.stag-popup.dynamic-popup.draggable .stag-popup-title')
  292. .on('mousedown.start-drag', '.stag-popup.dynamic-popup.draggable .stag-popup-title', function(_e) {
  293. if(dragging && dragging.length) {
  294. dragging = false;
  295. }
  296. dragging = $(this).closest('.stag-popup-content');
  297. dragX = _e.screenX;
  298. dragY = _e.screenY;
  299. });
  300. $(document)
  301. .off('mousemove.drag')
  302. .on('mousemove.drag', function(_e) {
  303. if(dragging && dragging.length) {
  304. let targetX = dragging.position().left + (_e.screenX - dragX),
  305. targetY = dragging.position().top + (_e.screenY - dragY);
  306. if(targetX >= 0) dragX = _e.screenX;
  307. if(targetY >= 0) dragY = _e.screenY;
  308. if(targetX < 0) targetX = 0;
  309. if(targetY < 0) targetY = 0;
  310. dragging[0].style.left = targetX + 'px';
  311. dragging[0].style.top = targetY + 'px';
  312. dragging[0].style.transform = 'none';
  313. return false;
  314. }
  315. });
  316. // resizable
  317. let resizeHighlighting = false, resizeDirection = false, resizeAnchor = false, resizing = false, resizeGutter = 8, resizeX, resizeY;
  318. $(document)
  319. .off('mousemove.highlight-resize', '.stag-popup.dynamic-popup.resizable .stag-popup-content')
  320. .on('mousemove.highlight-resize', '.stag-popup.dynamic-popup.resizable .stag-popup-content', function(_e) {
  321. if(dragging && dragging.length) return;
  322. if(resizing && resizing.length) return;
  323. let mouseX = _e.pageX - $(this).offset().left,
  324. mouseY = _e.pageY - $(this).offset().top;
  325. if((mouseX >= 0 && mouseX <= resizeGutter) || (mouseX >= $(this).outerWidth() - resizeGutter && mouseX <= $(this).outerWidth())) {
  326. $(this).addClass('resizing-x');
  327. resizeHighlighting = $(this);
  328. resizeDirection = 'x';
  329. resizeAnchor = (mouseX >= 0 && mouseX <= resizeGutter) ? 'left' : 'right';
  330. }
  331. else if((mouseY >= 0 && mouseY <= resizeGutter) || (mouseY >= $(this).outerHeight() - resizeGutter && mouseY <= $(this).outerHeight())) {
  332. $(this).addClass('resizing-y');
  333. resizeHighlighting = $(this);
  334. resizeDirection = 'y';
  335. resizeAnchor = (mouseY >= 0 && mouseY <= resizeGutter) ? 'top' : 'bottom';
  336. }
  337. else {
  338. $(this).removeClass('resizing-x');
  339. $(this).removeClass('resizing-y');
  340. resizeHighlighting = false;
  341. }
  342. });
  343. $(document)
  344. .off('mousedown.start-resize', '.stag-popup.dynamic-popup.resizable .stag-popup-content')
  345. .on('mousedown.start-resize', '.stag-popup.dynamic-popup.resizable .stag-popup-content', function(_e) {
  346. if(resizeHighlighting && resizeHighlighting.length) {
  347. if(resizing && resizing.length) {
  348. resizing.removeClass('resizing-x resizing-y');
  349. resizing = false;
  350. }
  351. resizing = $(this);
  352. resizeX = _e.screenX;
  353. resizeY = _e.screenY;
  354. }
  355. });
  356. $(document)
  357. .off('mousemove.resize')
  358. .on('mousemove.resize', function(_e) {
  359. if(resizing && resizing.length) {
  360. console.log(resizeDirection);
  361. console.log(resizeAnchor);
  362. // x
  363. if(resizeDirection === 'x') {
  364. let deltaX = _e.screenX - resizeX;
  365. // anchor: left => update left and width of the popup
  366. if(resizeAnchor === 'left') {
  367. resizing[0].style.left = resizing.position().left + deltaX + 'px';
  368. resizing[0].style.width = resizing.outerWidth() - deltaX + 'px';
  369. resizing[0].style.transform = 'none';
  370. }
  371. // anchor: right => update width of the popup
  372. else if(resizeAnchor === 'right') {
  373. resizing[0].style.left = resizing.position().left + 'px'
  374. resizing[0].style.width = resizing.outerWidth() + deltaX + 'px';
  375. resizing[0].style.transform = 'none';
  376. }
  377. }
  378. // x
  379. if(resizeDirection === 'y') {
  380. let deltaY = _e.screenY - resizeY;
  381. // anchor: top => update top and height of the popup
  382. if(resizeAnchor === 'top') {
  383. resizing[0].style.top = resizing.position().top + deltaY + 'px';
  384. resizing[0].style.height = resizing.outerHeight() - deltaY + 'px';
  385. }
  386. // anchor: bottom => update height of the popup
  387. else if(resizeAnchor === 'bottom') {
  388. resizing[0].style.height = resizing.outerHeight() + deltaY + 'px';
  389. }
  390. }
  391. resizeX = _e.screenX;
  392. resizeY = _e.screenY;
  393. return false;
  394. }
  395. });
  396. $(document)
  397. .off('mouseleave.stop-resize', '.stag-popup.dynamic-popup.resizable .stag-popup-content')
  398. .on('mouseleave.stop-resize', '.stag-popup.dynamic-popup.resizable .stag-popup-content', function(_e) {
  399. if(resizing && resizing.length) return;
  400. if(resizeHighlighting && resizeHighlighting.length) {
  401. resizeHighlighting.removeClass('resizing-x resizing-y');
  402. resizeHighlighting = false;
  403. return false;
  404. }
  405. });
  406. // common
  407. $(document)
  408. .off('mouseleave.stop-drag-resize')
  409. .on('mouseleave.stop-drag-resize', function() {
  410. if(dragging && dragging.length) {
  411. dragging = false;
  412. }
  413. if(resizeHighlighting && resizeHighlighting.length) {
  414. resizeHighlighting.removeClass('resizing-x resizing-y');
  415. resizeHighlighting = false;
  416. }
  417. if(resizing && resizing.length) {
  418. resizing.removeClass('resizing-x resizing-y');
  419. resizing = false;
  420. }
  421. });
  422. $(document)
  423. .off('mouseup.stop-drag-resize')
  424. .on('mouseup.stop-drag-resize', function() {
  425. if(dragging && dragging.length) {
  426. dragging = false;
  427. }
  428. if(resizeHighlighting && resizeHighlighting.length) {
  429. resizeHighlighting.removeClass('resizing-x resizing-y');
  430. resizeHighlighting = false;
  431. }
  432. if(resizing && resizing.length) {
  433. resizing.removeClass('resizing-x resizing-y');
  434. resizing = false;
  435. }
  436. });
  437. }
  438. addMCInitializer('stag-popups', window.initStagPopupEvents);
  439. })();