From 497c7ee517d74416fbcbf98a66c470296ea8bed7 Mon Sep 17 00:00:00 2001 From: hieuvu Date: Tue, 25 Oct 2022 15:43:43 +0700 Subject: [PATCH] MDL-75781 javascript: Centering when resize for dialog --- ...moodle-core-notification-dialogue-debug.js | 49 +++++++++++++++++++ .../moodle-core-notification-dialogue-min.js | 4 +- .../moodle-core-notification-dialogue.js | 49 +++++++++++++++++++ lib/yui/src/notification/js/dialogue.js | 49 +++++++++++++++++++ 4 files changed, 149 insertions(+), 2 deletions(-) diff --git a/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-debug.js b/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-debug.js index 2bce1751750..8ffa449efee 100644 --- a/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-debug.js +++ b/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-debug.js @@ -167,6 +167,18 @@ Y.extend(DIALOGUE, Y.Panel, { return node.eventName !== 'focusoutside'; })); + Y.one('document').on('orientationchange', function() { + // This will detect a change in orientation and re-trigger centering. + this.centerDialogOnVisible(); + }, this); + + Y.one('window').on('resize', function() { + // Detect window resize (most browsers). + this.centerDialogOnVisible(); + }, this); + // Observe dialog on size change. + this.centerDialogOnDialogSizeChange(this); + // Workaround upstream YUI bug http://yuilibrary.com/projects/yui3/ticket/2532507 // and allow setting of z-index in theme. bb = this.get('boundingBox'); @@ -409,6 +421,43 @@ Y.extend(DIALOGUE, Y.Panel, { } this.makeResponsive(); }, + + /** + * Automatic re-center dialog when dialog size is changed. + * + * @method centerDialogOnDialogSizeChange + * @param {M.core.dialogue} dialog object to apply centering. + */ + centerDialogOnDialogSizeChange: function(dialog) { + // ResizeObserver doesn't get recognized in JSHint. + // So we need to suppress the false warning. + var observer = new ResizeObserver(function() { // jshint ignore:line + dialog.centerDialogOnVisible(); + }); + var bb = dialog.get('boundingBox'); + observer.observe(bb._node, {attributes: true, attributeFilter: ['class']}); + }, + + /** + * Centering dialog when dialog is visible. + * + * @method centerDialogOnVisible + */ + centerDialogOnVisible: function() { + if (!this.get('visible')) { + return; // Only centre visible dialogue. + } + + if (this.name !== DIALOGUE_NAME) { + return; // Only centre Moodle dialogues. + } + + if (this.shouldResizeFullscreen()) { + this.makeResponsive(); + } + this.centerDialogue(); + }, + /** * Return whether this dialogue should be fullscreen or not. * diff --git a/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-min.js b/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-min.js index bdfcf83655d..7863a89be0e 100644 --- a/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-min.js +++ b/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-min.js @@ -1,2 +1,2 @@ -YUI.add("moodle-core-notification-dialogue",function(s,h){var i,t,n,a,l,e,o="moodle-dialogue",d="notificationBase",r={BASE:"moodle-dialogue-base",WRAP:"moodle-dialogue-wrap",HEADER:"moodle-dialogue-hd",BODY:"moodle-dialogue-bd",CONTENT:"moodle-dialogue-content",FOOTER:"moodle-dialogue-ft",HIDDEN:"hidden",LIGHTBOX:"moodle-dialogue-lightbox"};M.core=M.core||{},t=o+"-fullscreen",n=o+"-hidden",a="moodle-has-zindex",s.extend(i=function(e){var t="moodle-dialogue-"+s.stamp(this)+"-wrap";e.notificationBase=s.Node.create('
').append(s.Node.create('').append(s.Node.create('
')).append(s.Node.create('
')).append(s.Node.create('
'))),e.attachmentPoint=e.attachmentPoint||document.body,s.one(e.attachmentPoint).append(e.notificationBase),e.srcNode="#"+t,delete e.buttons,i.superclass.constructor.apply(this,[e])},s.Panel,{_resizeevent:null,_orientationevent:null,_calculatedzindex:!"form",_currentMaskNodeId:null,_originalPosition:null,_hiddenSiblings:null,hideIfNotForm:function(){null===this.get("boundingBox").one("form")&&this.hide()},initializer:function(){var e,t;!1!==this.get("closeButton")&&(e=this.get("closeButtonTitle"),this.get("buttons").header[0].setAttribute("title",e),this.get("buttons").header[0].setAttribute("aria-label",e)),this.setStdModContent(s.WidgetStdMod.HEADER,'
'+this.get("headerContent")+"
",s.WidgetStdMod.REPLACE),this._hiddenSiblings=[],this.get("render")&&this.render(),this.after("visibleChange",this.visibilityChanged,this),this.get("center")&&this.centerDialogue(),this.get("modal")&&(this.get(d).set("aria-hidden","true"),this.plug(s.M.core.LockScroll)),this.set("focusOn",s.Array(this.get("focusOn")).filter(function(e){return"focusoutside"!==e.eventName})),(e=this.get("boundingBox")).addClass(a),s.Array.each(this.get("extraClasses"),e.addClass,e),this.get("visible")&&(this.applyZIndex(),this.applyAndTrapFocus(),this.get("modal")&&this.setAccessibilityVisible()),this.on("maskShow",this.applyZIndex),this.on("maskShow",function(){var e,t=s.one(s.config.win),i=this.get("boundingBox");this.get("center")||(this._originalPosition=i.getXY()),e=this.get("maskNode"),this._currentMaskNodeId!==e.get("_yuid")&&(this._currentMaskNodeId=e.get("_yuid"),e.on("click",this.hideIfNotForm,this)),"fixed"!==i.getStyle("position")&&i.setStyles({top:t.get("scrollTop"),left:t.get("scrollLeft")})},this),e=this.get("notificationBase"),""!==(t=this.get("additionalBaseClass"))&&e.addClass(t),this.after("destroyedChange",function(){this.get(d).remove(!0)},this)},applyZIndex:function(){var e,t=1040,i=this.get("boundingBox"),o=this.get("maskNode"),n=this.get("zIndex");0===n||this._calculatedzindex?(s.all(" [role=dialog], [role=menubar], ."+a).each(function(e){e=this.findZIndex(e);t').append(s.Node.create('').append(s.Node.create('
')).append(s.Node.create('
')).append(s.Node.create('
'))),e.attachmentPoint=e.attachmentPoint||document.body,s.one(e.attachmentPoint).append(e.notificationBase),e.srcNode="#"+t,delete e.buttons,i.superclass.constructor.apply(this,[e])},s.Panel,{_resizeevent:null,_orientationevent:null,_calculatedzindex:!"form",_currentMaskNodeId:null,_originalPosition:null,_hiddenSiblings:null,hideIfNotForm:function(){null===this.get("boundingBox").one("form")&&this.hide()},initializer:function(){var e,t;!1!==this.get("closeButton")&&(e=this.get("closeButtonTitle"),this.get("buttons").header[0].setAttribute("title",e),this.get("buttons").header[0].setAttribute("aria-label",e)),this.setStdModContent(s.WidgetStdMod.HEADER,'
'+this.get("headerContent")+"
",s.WidgetStdMod.REPLACE),this._hiddenSiblings=[],this.get("render")&&this.render(),this.after("visibleChange",this.visibilityChanged,this),this.get("center")&&this.centerDialogue(),this.get("modal")&&(this.get(d).set("aria-hidden","true"),this.plug(s.M.core.LockScroll)),this.set("focusOn",s.Array(this.get("focusOn")).filter(function(e){return"focusoutside"!==e.eventName})),s.one("document").on("orientationchange",function(){this.centerDialogOnVisible()},this),s.one("window").on("resize",function(){this.centerDialogOnVisible()},this),this.centerDialogOnDialogSizeChange(this),(e=this.get("boundingBox")).addClass(a),s.Array.each(this.get("extraClasses"),e.addClass,e),this.get("visible")&&(this.applyZIndex(),this.applyAndTrapFocus(),this.get("modal")&&this.setAccessibilityVisible()),this.on("maskShow",this.applyZIndex),this.on("maskShow",function(){var e,t=s.one(s.config.win),i=this.get("boundingBox");this.get("center")||(this._originalPosition=i.getXY()),e=this.get("maskNode"),this._currentMaskNodeId!==e.get("_yuid")&&(this._currentMaskNodeId=e.get("_yuid"),e.on("click",this.hideIfNotForm,this)),"fixed"!==i.getStyle("position")&&i.setStyles({top:t.get("scrollTop"),left:t.get("scrollLeft")})},this),e=this.get("notificationBase"),""!==(t=this.get("additionalBaseClass"))&&e.addClass(t),this.after("destroyedChange",function(){this.get(d).remove(!0)},this)},applyZIndex:function(){var e,t=1040,i=this.get("boundingBox"),o=this.get("maskNode"),n=this.get("zIndex");0===n||this._calculatedzindex?(s.all(" [role=dialog], [role=menubar], ."+a).each(function(e){e=this.findZIndex(e);t