Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit c6bd23b

Browse files
author
Gabriel Schulhof
committed
Popup: New method: reposition() - emits "popupbeforeposition" and repositions the popup based on coordinates passed in.
1 parent 76663b0 commit c6bd23b

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

js/widgets/popup.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,8 @@ define( [
126126
if ( !this._expectResizeEvent() ) {
127127
if ( this._ui.container.hasClass( "ui-popup-hidden" ) ) {
128128
// effectively rapid-open the popup while leaving the screen intact
129-
this._trigger( "beforeposition" );
130-
this._ui.container
131-
.removeClass( "ui-popup-hidden" )
132-
.offset( this._placementCoords( this._desiredCoords( undefined, undefined, "window" ) ) );
129+
this._ui.container.removeClass( "ui-popup-hidden" );
130+
this.reposition( { positionTo: "window" } );
133131
}
134132

135133
this._resizeScreen();
@@ -494,17 +492,18 @@ define( [
494492
// The desired coordinates passed in will be returned untouched if no reference element can be identified via
495493
// desiredPosition.positionTo. Nevertheless, this function ensures that its return value always contains valid
496494
// x and y coordinates by specifying the center middle of the window if the coordinates are absent.
497-
_desiredCoords: function( x, y, positionTo ) {
498-
var dst = null, offset, winCoords = windowCoords();
495+
// options: { x: coordinate, y: coordinate, positionTo: string: "origin", "window", or jQuery selector
496+
_desiredCoords: function( o ) {
497+
var dst = null, offset, winCoords = windowCoords(), x = o.x, y = o.y, pTo = o.positionTo;
499498

500499
// Establish which element will serve as the reference
501-
if ( positionTo && positionTo !== "origin" ) {
502-
if ( positionTo === "window" ) {
500+
if ( pTo && pTo !== "origin" ) {
501+
if ( pTo === "window" ) {
503502
x = winCoords.cx / 2 + winCoords.x;
504503
y = winCoords.cy / 2 + winCoords.y;
505504
} else {
506505
try {
507-
dst = $( positionTo );
506+
dst = $( pTo );
508507
} catch( e ) {
509508
dst = null;
510509
}
@@ -535,6 +534,13 @@ define( [
535534
return { x: x, y: y };
536535
},
537536

537+
reposition: function( o ) {
538+
if ( this._isOpen ) {
539+
this._trigger( "beforeposition" );
540+
this._ui.container.offset( this._placementCoords( this._desiredCoords( o ) ) );
541+
}
542+
},
543+
538544
_openPrereqsComplete: function() {
539545
this._ui.container.addClass( "ui-popup-active" );
540546
this._isOpen = true;
@@ -568,7 +574,7 @@ define( [
568574
// Give applications a chance to modify the contents of the container before it appears
569575
this._trigger( "beforeposition" );
570576

571-
coords = this._placementCoords( this._desiredCoords( o.x, o.y, o.positionTo ) );
577+
coords = this._placementCoords( this._desiredCoords( o ) );
572578

573579
// Count down to triggering "popupafteropen" - we have two prerequisites:
574580
// 1. The popup window animation completes (container())

0 commit comments

Comments
 (0)