Skip to content

Commit

Permalink
MDL-75781 javascript: Centering when resize for dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuvu committed Nov 8, 2022
1 parent dca920d commit 497c7ee
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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.
*
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 497c7ee

Please sign in to comment.