MDL-67513 core: Only move modal to body if currently attached
[moodle.git] / lib / amd / build / modal.min.js
... / ...
CommitLineData
1function _typeof(a){"@babel/helpers - typeof";if("function"==typeof Symbol&&"symbol"==typeof Symbol.iterator){_typeof=function(a){return typeof a}}else{_typeof=function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a}}return _typeof(a)}define ("core/modal",["jquery","core/templates","core/notification","core/key_codes","core/custom_interaction_events","core/modal_backdrop","core/event","core/modal_events","core/local/aria/focuslock","core/pending","core/aria","core/fullscreen"],function(a,b,c,d,e,f,g,h,i,j,k,l){var m={CONTAINER:"[data-region=\"modal-container\"]",MODAL:"[data-region=\"modal\"]",HEADER:"[data-region=\"header\"]",TITLE:"[data-region=\"title\"]",BODY:"[data-region=\"body\"]",FOOTER:"[data-region=\"footer\"]",HIDE:"[data-action=\"hide\"]",DIALOG:"[role=dialog]",FORM:"form",MENU_BAR:"[role=menubar]",HAS_Z_INDEX:".moodle-has-zindex",CAN_RECEIVE_FOCUS:"input:not([type=\"hidden\"]), a[href], button, textarea, select, [tabindex]"},n={LOADING:"core/loading",BACKDROP:"core/modal_backdrop"},o,p=0,q=function(b){this.root=a(b);this.modal=this.root.find(m.MODAL);this.header=this.modal.find(m.HEADER);this.headerPromise=a.Deferred();this.title=this.header.find(m.TITLE);this.titlePromise=a.Deferred();this.body=this.modal.find(m.BODY);this.bodyPromise=a.Deferred();this.footer=this.modal.find(m.FOOTER);this.footerPromise=a.Deferred();this.hiddenSiblings=[];this.isAttached=!1;this.bodyJS=null;this.footerJS=null;this.modalCount=p++;if(!this.root.is(m.CONTAINER)){c.exception({message:"Element is not a modal container"})}if(!this.modal.length){c.exception({message:"Container does not contain a modal"})}if(!this.header.length){c.exception({message:"Modal is missing a header region"})}if(!this.title.length){c.exception({message:"Modal header is missing a title region"})}if(!this.body.length){c.exception({message:"Modal is missing a body region"})}if(!this.footer.length){c.exception({message:"Modal is missing a footer region"})}this.registerEventListeners()};q.prototype.attachToDOM=function(){this.getAttachmentPoint().append(this.root);if(this.isAttached){return}i.trapFocus(this.root[0]);if(this.bodyJS){b.runTemplateJS(this.bodyJS);this.bodyJS=null}if(this.footerJS){b.runTemplateJS(this.footerJS);this.footerJS=null}this.isAttached=!0};q.prototype.countOtherVisibleModals=function(){var b=0;a("body").find(m.CONTAINER).each(function(c,d){d=a(d);if(!this.root.is(d)&&d.hasClass("show")){b++}}.bind(this));return b};q.prototype.getBackdrop=function(){if(!o){o=b.render(n.BACKDROP,{}).then(function(b){var c=a(b);return new f(c)}).fail(c.exception)}return o};q.prototype.getRoot=function(){return this.root};q.prototype.getModal=function(){return this.modal};q.prototype.getTitle=function(){return this.title};q.prototype.getBody=function(){return this.body};q.prototype.getFooter=function(){return this.footer};q.prototype.getTitlePromise=function(){return this.titlePromise};q.prototype.getBodyPromise=function(){return this.bodyPromise};q.prototype.getFooterPromise=function(){return this.footerPromise};q.prototype.getModalCount=function(){return this.modalCount};q.prototype.setTitle=function(b){var d=this.getTitle();this.titlePromise=a.Deferred();this.asyncSet(b,d.html.bind(d)).then(function(){this.titlePromise.resolve(d)}.bind(this)).catch(c.exception)};q.prototype.setBody=function(d){this.bodyPromise=a.Deferred();var e=this.getBody();if("string"==typeof d){e.html(d);g.notifyFilterContentUpdated(e);this.getRoot().trigger(h.bodyRendered,this);this.bodyPromise.resolve(e)}else{var f="amd-modal-js-pending-id-"+this.getModalCount();M.util.js_pending(f);var i=null;e.css("overflow","hidden");d=a.when(d);if("pending"==d.state()){var j=e.innerHeight();if(100>j){j=100}e.animate({height:j+"px"},150);e.html("");i=b.render(n.LOADING,{}).then(function(b){var c=a(b).hide();e.html(c);c.fadeIn(150);return a.when(c.promise(),d)}).then(function(a){return a.fadeOut(100).promise()}).then(function(){return d})}else{i=d}i.then(function(a,c){var d=null;if(this.isVisible()){e.css("opacity",0);var f=e.innerHeight();e.html(a);e.css("height","");var g=e.innerHeight();e.css("height",f+"px");d=e.animate({height:g+"px",opacity:1},{duration:150,queue:!1}).promise()}else{e.html(a)}if(c){if(this.isAttached){b.runTemplateJS(c)}else{this.bodyJS=c}}return d}.bind(this)).then(function(a){g.notifyFilterContentUpdated(e);this.getRoot().trigger(h.bodyRendered,this);return a}.bind(this)).then(function(){this.bodyPromise.resolve(e)}.bind(this)).fail(c.exception).always(function(){e.css("height","");e.css("overflow","");e.css("opacity","");M.util.js_complete(f)}).fail(c.exception)}};q.prototype.setFooter=function(d){this.showFooter();this.footerPromise=a.Deferred();var e=this.getFooter();if("string"==typeof d){e.html(d);this.footerPromise.resolve(e)}else{b.render(n.LOADING,{}).then(function(a){e.html(a);return d}).then(function(a,c){e.html(a);if(c){if(this.isAttached){b.runTemplateJS(c)}else{this.footerJS=c}}return e}.bind(this)).then(function(a){this.footerPromise.resolve(a)}.bind(this)).catch(c.exception)}};q.prototype.hasFooterContent=function(){return this.getFooter().children().length?!0:!1};q.prototype.hideFooter=function(){this.getFooter().addClass("hidden")};q.prototype.showFooter=function(){this.getFooter().removeClass("hidden")};q.prototype.setLarge=function(){if(this.isLarge()){return}this.getModal().addClass("modal-lg")};q.prototype.isLarge=function(){return this.getModal().hasClass("modal-lg")};q.prototype.setSmall=function(){if(this.isSmall()){return}this.getModal().removeClass("modal-lg")};q.prototype.isSmall=function(){return!this.getModal().hasClass("modal-lg")};q.prototype.setScrollable=function(a){if(!a){this.getModal()[0].classList.remove("modal-dialog-scrollable");return}this.getModal()[0].classList.add("modal-dialog-scrollable")};q.prototype.calculateZIndex=function(){var b=a(m.DIALOG+", "+m.MENU_BAR+", "+m.HAS_Z_INDEX),c=parseInt(this.root.css("z-index"));b.each(function(b,d){d=a(d);var e=d.css("z-index")?parseInt(d.css("z-index")):0;if(e>c){c=e}});return c};q.prototype.isVisible=function(){return this.root.hasClass("show")};q.prototype.hasFocus=function(){var b=a(document.activeElement);return this.root.is(b)||this.root.has(b).length};q.prototype.hasTransitions=function(){return this.getRoot().hasClass("fade")};q.prototype.getAttachmentPoint=function(){return a(l.getElement()||document.body)};q.prototype.show=function(){if(this.isVisible()){return a.Deferred().resolve()}var b=new j("core/modal:show");if(this.hasFooterContent()){this.showFooter()}else{this.hideFooter()}this.attachToDOM();return this.getBackdrop().then(function(b){var c=this.calculateZIndex(),d=c+2;this.root.css("z-index",d);b.setZIndex(d-1);b.show();this.root.removeClass("hide").addClass("show");this.accessibilityShow();this.getModal().focus();a("body").addClass("modal-open");this.root.trigger(h.shown,this)}.bind(this)).then(b.resolve)};q.prototype.hideIfNotForm=function(){var a=this.modal.find(m.FORM);if(0==a.length){this.hide()}};q.prototype.hide=function(){this.getBackdrop().done(function(b){i.untrapFocus();if(!this.countOtherVisibleModals()){b.hide();a("body").removeClass("modal-open")}var c=parseInt(this.root.css("z-index"));this.root.css("z-index","");b.setZIndex(c-3);this.accessibilityHide();if(this.hasTransitions()){this.getRoot().one("transitionend webkitTransitionEnd oTransitionEnd",function(){this.getRoot().removeClass("show").addClass("hide")}.bind(this))}else{this.getRoot().removeClass("show").addClass("hide")}if(a(document.body).find(this.getRoot()).length){a(document.body).append(this.getRoot())}this.root.trigger(h.hidden,this)}.bind(this))};q.prototype.destroy=function(){this.hide();this.root.remove();this.root.trigger(h.destroyed,this)};q.prototype.accessibilityShow=function(){k.unhide(this.root.get());k.hideSiblings(this.root.get()[0])};q.prototype.accessibilityHide=function(){k.unhideSiblings(this.root.get()[0]);k.hide(this.root.get())};q.prototype.registerEventListeners=function(){this.getRoot().on("keydown",function(a){if(!this.isVisible()){return}if(a.keyCode==d.escape){this.hide()}}.bind(this));this.getRoot().click(function(b){if(!a(b.target).closest(m.MODAL).length){if(a(b.target).closest(m.CONTAINER).length){var c=a.Event(h.outsideClick);this.getRoot().trigger(c,this);if(!c.isDefaultPrevented()){this.hideIfNotForm()}}}}.bind(this));e.define(this.getModal(),[e.events.activate]);this.getModal().on(e.events.activate,m.HIDE,function(a,b){this.hide();b.originalEvent.preventDefault()}.bind(this))};q.prototype.registerCloseOnCancel=function(){this.getModal().on(e.events.activate,this.getActionSelector("cancel"),function(b,c){var d=a.Event(h.cancel);this.getRoot().trigger(d,this);if(!d.isDefaultPrevented()){c.originalEvent.preventDefault();if(this.removeOnClose){this.destroy()}else{this.hide()}}}.bind(this))};q.prototype.registerCloseOnSave=function(){this.getModal().on(e.events.activate,this.getActionSelector("save"),function(b,c){var d=a.Event(h.save);this.getRoot().trigger(d,this);if(!d.isDefaultPrevented()){c.originalEvent.preventDefault();if(this.removeOnClose){this.destroy()}else{this.hide()}}}.bind(this))};q.prototype.asyncSet=function(b,d){var e=b;if("object"!==_typeof(b)||!b.hasOwnProperty("then")){e=a.Deferred();e.resolve(b)}e.then(function(a){d(a)}).fail(c.exception);return e};q.prototype.setButtonText=function(a,b){var c=this.getFooter().find(this.getActionSelector(a));if(!c){throw new Error("Unable to find the '"+a+"' button")}return this.asyncSet(b,c.text.bind(c))};q.prototype.getActionSelector=function(a){return"[data-action='"+a+"']"};q.prototype.setRemoveOnClose=function(a){this.removeOnClose=a};return q});
2//# sourceMappingURL=modal.min.js.map