stag-popup.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. stagPopup.addClass('show');
  7. stagPopup.find('[moe][initialized]').removeAttr('initialized');
  8. initMoes();
  9. if(!_noAutoFocus) {
  10. window.setTimeout(function() {
  11. stagPopup.find('input[type="text"]:not([readonly]):visible,select:visible').first().focus();
  12. }, 150);
  13. }
  14. stagPopupsQueue.push(stagPopup);
  15. return false;
  16. }
  17. function submitStagPopup(_form) {
  18. if(!_form[0].checkValidity()) {
  19. _form[0].reportValidity();
  20. return false;
  21. }
  22. showMask();
  23. $.post(_form.attr('action'), _form.serialize(), function(_data) {
  24. stagPopupsQueue = [];
  25. fastReload();
  26. });
  27. return false;
  28. }
  29. function closeStagPopup(_noEvent = false) {
  30. if(!stagPopupsQueue.length) return false;
  31. let popup = stagPopupsQueue[stagPopupsQueue.length - 1];
  32. let closeAll = !!popup.attr('close-all-with-self');
  33. popup.removeClass('show');
  34. stagPopupsQueue.splice(stagPopupsQueue.length - 1, 1);
  35. if(closeAll) {
  36. while(stagPopupsQueue.length) {
  37. closeStagPopup(true);
  38. }
  39. }
  40. // if all closed
  41. if(!stagPopupsQueue.length) {
  42. $('html, body').removeClass('no-scroll');
  43. $(window.top.document.body).find('#stag_mcp_lhs').removeClass('no-scroll');
  44. if(!_noEvent) {
  45. $('body').trigger('stag-popup-closed');
  46. }
  47. }
  48. return false;
  49. }
  50. (function() {
  51. window.initStagPopupEvents = function () {
  52. $(document)
  53. .off('click.stag-popup-discard', '.stag-popup')
  54. .on('click.stag-popup-discard', '.stag-popup', function(_e) {
  55. if($(_e.target).is('.stag-popup')) {
  56. closeStagPopup();
  57. return false;
  58. }
  59. });
  60. // catch ESC and discard any visible popups
  61. $(document)
  62. .off('keydown.stag-popup-escape')
  63. .on('keydown.stag-popup-escape', function (e) {
  64. if(e.which === 27) {
  65. if(stagPopupsQueue.length) {
  66. closeStagPopup();
  67. return false;
  68. }
  69. }
  70. });
  71. }
  72. addMCInitializer('stag-popups', window.initStagPopupEvents);
  73. })();