# all files / src/widgets/ PopupWidget.js

12.44% Statements 24/193
0% Branches 0/152
0% Functions 0/21
12.44% Lines 24/193

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598

    1×

    1×
    1×
    1×
    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     /**
     * PopupWidget is a container for content. The popup is overlaid and positioned absolutely.
     * By default, each popup has an anchor that points toward its origin.
     * Please see the [OOjs UI documentation on Mediawiki] [1] for more information and examples.
     *
     * Unlike most widgets, PopupWidget is initially hidden and must be shown by calling #toggle.
     *
     *     @example
     *     // A popup widget.
     *     var popup = new OO.ui.PopupWidget( {
     *         $content: $( '<p>Hi there!</p>' ),
     *         padded: true,
     *         width: 300
     *     } );
     *
     *     $( 'body' ).append( popup.$element );
     *     // To display the popup, toggle the visibility to 'true'.
     *     popup.toggle( true );
     *
     * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Popups
     *
     * @class
     * @extends OO.ui.Widget
     * @mixins OO.ui.mixin.LabelElement
     * @mixins OO.ui.mixin.ClippableElement
     * @mixins OO.ui.mixin.FloatableElement
     *
     * @constructor
     * @param {Object} [config] Configuration options
     * @cfg {number} [width=320] Width of popup in pixels
     * @cfg {number} [height] Height of popup in pixels. Omit to use the automatic height.
     * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
     * @cfg {string} [position='below'] Where to position the popup relative to $floatableContainer
     *  'above': Put popup above $floatableContainer; anchor points down to the horizontal center
     *           of $floatableContainer
     *  'below': Put popup below $floatableContainer; anchor points up to the horizontal center
     *           of $floatableContainer
     *  'before': Put popup to the left (LTR) / right (RTL) of $floatableContainer; anchor points
     *            endwards (right/left) to the vertical center of $floatableContainer
     *  'after': Put popup to the right (LTR) / left (RTL) of $floatableContainer; anchor points
     *            startwards (left/right) to the vertical center of $floatableContainer
     * @cfg {string} [align='center'] How to align the popup to $floatableContainer
     *  'forwards': If position is above/below, move the popup as far endwards (right in LTR, left in RTL)
     *              as possible while still keeping the anchor within the popup;
     *              if position is before/after, move the popup as far downwards as possible.
     *  'backwards': If position is above/below, move the popup as far startwards (left in LTR, right in RTL)
     *               as possible while still keeping the anchor within the popup;
     *               if position in before/after, move the popup as far upwards as possible.
     *  'center': Horizontally (if position is above/below) or vertically (before/after) align the center
     *            of the popup with the center of $floatableContainer.
     * 'force-left': Alias for 'forwards' in LTR and 'backwards' in RTL
     * 'force-right': Alias for 'backwards' in RTL and 'forwards' in LTR
     * @cfg {jQuery} [$container] Constrain the popup to the boundaries of the specified container.
     *  See the [OOjs UI docs on MediaWiki][3] for an example.
     *  [3]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Popups#containerExample
     * @cfg {number} [containerPadding=10] Padding between the popup and its container, specified as a number of pixels.
     * @cfg {jQuery} [$content] Content to append to the popup's body
     * @cfg {jQuery} [$footer] Content to append to the popup's footer
     * @cfg {boolean} [autoClose=false] Automatically close the popup when it loses focus.
     * @cfg {jQuery} [$autoCloseIgnore] Elements that will not close the popup when clicked.
     *  This config option is only relevant if #autoClose is set to `true`. See the [OOjs UI docs on MediaWiki][2]
     *  for an example.
     *  [2]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Popups#autocloseExample
     * @cfg {boolean} [head=false] Show a popup header that contains a #label (if specified) and close
     *  button.
     * @cfg {boolean} [padded=false] Add padding to the popup's body
     */
    OO.ui.PopupWidget = function OoUiPopupWidget( config ) {
    	// Configuration initialization
    	config = config || {};

    	// Parent constructor
    	OO.ui.PopupWidget.parent.call( this, config );

    	// Properties (must be set before ClippableElement constructor call)
    	this.$body = $( '<div>' );
    	this.$popup = $( '<div>' );

    	// Mixin constructors
    	OO.ui.mixin.LabelElement.call( this, config );
    	OO.ui.mixin.ClippableElement.call( this, $.extend( {}, config, {
    		$clippable: this.$body,
    		$clippableContainer: this.$popup
    	} ) );
    	OO.ui.mixin.FloatableElement.call( this, config );

    	// Properties
    	this.$anchor = $( '<div>' );
    	// If undefined, will be computed lazily in computePosition()
    	this.$container = config.$container;
    	this.containerPadding = config.containerPadding !== undefined ? config.containerPadding : 10;
    	this.autoClose = !!config.autoClose;
    	this.$autoCloseIgnore = config.$autoCloseIgnore;
    	this.transitionTimeout = null;
    	this.anchored = false;
    	this.width = config.width !== undefined ? config.width : 320;
    	this.height = config.height !== undefined ? config.height : null;
    	this.onMouseDownHandler = this.onMouseDown.bind( this );
    	this.onDocumentKeyDownHandler = this.onDocumentKeyDown.bind( this );

    	// Initialization
    	this.toggleAnchor( config.anchor === undefined || config.anchor );
    	this.setAlignment( config.align || 'center' );
    	this.setPosition( config.position || 'below' );
    	this.$body.addClass( 'oo-ui-popupWidget-body' );
    	this.$anchor.addClass( 'oo-ui-popupWidget-anchor' );
    	this.$popup
    		.addClass( 'oo-ui-popupWidget-popup' )
    		.append( this.$body );
    	this.$element
    		.addClass( 'oo-ui-popupWidget' )
    		.append( this.$popup, this.$anchor );
    	// Move content, which was added to #$element by OO.ui.Widget, to the body
    	// FIXME This is gross, we should use '$body' or something for the config
    	if ( config.$content instanceof jQuery ) {
    		this.$body.append( config.$content );
    	}

    	if ( config.padded ) {
    		this.$body.addClass( 'oo-ui-popupWidget-body-padded' );
    	}

    	if ( config.head ) {
    		this.closeButton = new OO.ui.ButtonWidget( { framed: false, icon: 'close' } );
    		this.closeButton.connect( this, { click: 'onCloseButtonClick' } );
    		this.$head = $( '<div>' )
    			.addClass( 'oo-ui-popupWidget-head' )
    			.append( this.$label, this.closeButton.$element );
    		this.$popup.prepend( this.$head );
    	}

    	if ( config.$footer ) {
    		this.$footer = $( '<div>' )
    			.addClass( 'oo-ui-popupWidget-footer' )
    			.append( config.$footer );
    		this.$popup.append( this.$footer );
    	}

    	// Initially hidden - using #toggle may cause errors if subclasses override toggle with methods
    	// that reference properties not initialized at that time of parent class construction
    	// TODO: Find a better way to handle post-constructor setup
    	this.visible = false;
    	this.$element.addClass( 'oo-ui-element-hidden' );
    };

    /* Setup */

    OO.inheritClass( OO.ui.PopupWidget, OO.ui.Widget );
    OO.mixinClass( OO.ui.PopupWidget, OO.ui.mixin.LabelElement );
    OO.mixinClass( OO.ui.PopupWidget, OO.ui.mixin.ClippableElement );
    OO.mixinClass( OO.ui.PopupWidget, OO.ui.mixin.FloatableElement );

    /* Events */

    /**
     * @event ready
     *
     * The popup is ready: it is visible and has been positioned and clipped.
     */

    /* Methods */

    /**
     * Handles mouse down events.
     *
     * @private
     * @param {MouseEvent} e Mouse down event
     */
    OO.ui.PopupWidget.prototype.onMouseDown = function ( e ) {
    	if (
    		this.isVisible() &&
    		!OO.ui.contains( this.$element.add( this.$autoCloseIgnore ).get(), e.target, true )
    	) {
    		this.toggle( false );
    	}
    };

    /**
     * Bind mouse down listener.
     *
     * @private
     */
    OO.ui.PopupWidget.prototype.bindMouseDownListener = function () {
    	// Capture clicks outside popup
    	this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler, true );
    };

    /**
     * Handles close button click events.
     *
     * @private
     */
    OO.ui.PopupWidget.prototype.onCloseButtonClick = function () {
    	if ( this.isVisible() ) {
    		this.toggle( false );
    	}
    };

    /**
     * Unbind mouse down listener.
     *
     * @private
     */
    OO.ui.PopupWidget.prototype.unbindMouseDownListener = function () {
    	this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler, true );
    };

    /**
     * Handles key down events.
     *
     * @private
     * @param {KeyboardEvent} e Key down event
     */
    OO.ui.PopupWidget.prototype.onDocumentKeyDown = function ( e ) {
    	if (
    		e.which === OO.ui.Keys.ESCAPE &&
    		this.isVisible()
    	) {
    		this.toggle( false );
    		e.preventDefault();
    		e.stopPropagation();
    	}
    };

    /**
     * Bind key down listener.
     *
     * @private
     */
    OO.ui.PopupWidget.prototype.bindKeyDownListener = function () {
    	this.getElementWindow().addEventListener( 'keydown', this.onDocumentKeyDownHandler, true );
    };

    /**
     * Unbind key down listener.
     *
     * @private
     */
    OO.ui.PopupWidget.prototype.unbindKeyDownListener = function () {
    	this.getElementWindow().removeEventListener( 'keydown', this.onDocumentKeyDownHandler, true );
    };

    /**
     * Show, hide, or toggle the visibility of the anchor.
     *
     * @param {boolean} [show] Show anchor, omit to toggle
     */
    OO.ui.PopupWidget.prototype.toggleAnchor = function ( show ) {
    	show = show === undefined ? !this.anchored : !!show;

    	if ( this.anchored !== show ) {
    		if ( show ) {
    			this.$element.addClass( 'oo-ui-popupWidget-anchored' );
    			this.$element.addClass( 'oo-ui-popupWidget-anchored-' + this.anchorEdge );
    		} else {
    			this.$element.removeClass( 'oo-ui-popupWidget-anchored' );
    			this.$element.removeClass( 'oo-ui-popupWidget-anchored-' + this.anchorEdge );
    		}
    		this.anchored = show;
    	}
    };
    /**
     * Change which edge the anchor appears on.
     *
     * @param {string} edge 'top', 'bottom', 'start' or 'end'
     */
    OO.ui.PopupWidget.prototype.setAnchorEdge = function ( edge ) {
    	if ( [ 'top', 'bottom', 'start', 'end' ].indexOf( edge ) === -1 ) {
    		throw new Error( 'Invalid value for edge: ' + edge );
    	}
    	if ( this.anchorEdge !== null ) {
    		this.$element.removeClass( 'oo-ui-popupWidget-anchored-' + this.anchorEdge );
    	}
    	this.anchorEdge = edge;
    	if ( this.anchored ) {
    		this.$element.addClass( 'oo-ui-popupWidget-anchored-' + edge );
    	}
    };

    /**
     * Check if the anchor is visible.
     *
     * @return {boolean} Anchor is visible
     */
    OO.ui.PopupWidget.prototype.hasAnchor = function () {
    	return this.anchored;
    };

    /**
     * Toggle visibility of the popup. The popup is initially hidden and must be shown by calling
     * `.toggle( true )` after its #$element is attached to the DOM.
     *
     * Do not show the popup while it is not attached to the DOM. The calculations required to display
     * it in the right place and with the right dimensions only work correctly while it is attached.
     * Side-effects may include broken interface and exceptions being thrown. This wasn't always
     * strictly enforced, so currently it only generates a warning in the browser console.
     *
     * @fires ready
     * @inheritdoc
     */
    OO.ui.PopupWidget.prototype.toggle = function ( show ) {
    	var change;
    	show = show === undefined ? !this.isVisible() : !!show;

    	change = show !== this.isVisible();

    	if ( show && !this.warnedUnattached && !this.isElementAttached() ) {
    		OO.ui.warnDeprecation( 'PopupWidget#toggle: Before calling this method, the popup must be attached to the DOM.' );
    		this.warnedUnattached = true;
    	}
    	if ( show && !this.$floatableContainer && this.isElementAttached() ) {
    		// Fall back to the parent node if the floatableContainer is not set
    		this.setFloatableContainer( this.$element.parent() );
    	}

    	// Parent method
    	OO.ui.PopupWidget.parent.prototype.toggle.call( this, show );

    	if ( change ) {
    		this.togglePositioning( show && !!this.$floatableContainer );

    		if ( show ) {
    			if ( this.autoClose ) {
    				this.bindMouseDownListener();
    				this.bindKeyDownListener();
    			}
    			this.updateDimensions();
    			this.toggleClipping( true );
    			this.emit( 'ready' );
    		} else {
    			this.toggleClipping( false );
    			if ( this.autoClose ) {
    				this.unbindMouseDownListener();
    				this.unbindKeyDownListener();
    			}
    		}
    	}

    	return this;
    };

    /**
     * Set the size of the popup.
     *
     * Changing the size may also change the popup's position depending on the alignment.
     *
     * @param {number} width Width in pixels
     * @param {number} height Height in pixels
     * @param {boolean} [transition=false] Use a smooth transition
     * @chainable
     */
    OO.ui.PopupWidget.prototype.setSize = function ( width, height, transition ) {
    	this.width = width;
    	this.height = height !== undefined ? height : null;
    	if ( this.isVisible() ) {
    		this.updateDimensions( transition );
    	}
    };

    /**
     * Update the size and position.
     *
     * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
     * be called automatically.
     *
     * @param {boolean} [transition=false] Use a smooth transition
     * @chainable
     */
    OO.ui.PopupWidget.prototype.updateDimensions = function ( transition ) {
    	var widget = this;

    	// Prevent transition from being interrupted
    	clearTimeout( this.transitionTimeout );
    	if ( transition ) {
    		// Enable transition
    		this.$element.addClass( 'oo-ui-popupWidget-transitioning' );
    	}

    	this.position();

    	if ( transition ) {
    		// Prevent transitioning after transition is complete
    		this.transitionTimeout = setTimeout( function () {
    			widget.$element.removeClass( 'oo-ui-popupWidget-transitioning' );
    		}, 200 );
    	} else {
    		// Prevent transitioning immediately
    		this.$element.removeClass( 'oo-ui-popupWidget-transitioning' );
    	}
    };

    /**
     * @inheritdoc
     */
    OO.ui.PopupWidget.prototype.computePosition = function () {
    	var direction, align, vertical, start, end, near, far, sizeProp, popupSize, anchorSize, anchorPos,
    		anchorOffset, anchorMargin, parentPosition, positionProp, positionAdjustment, floatablePos,
    		offsetParentPos, containerPos,
    		popupPos = {},
    		anchorCss = { left: '', right: '', top: '', bottom: '' },
    		alignMap = {
    			ltr: {
    				'force-left': 'backwards',
    				'force-right': 'forwards'
    			},
    			rtl: {
    				'force-left': 'forwards',
    				'force-right': 'backwards'
    			}
    		},
    		anchorEdgeMap = {
    			above: 'bottom',
    			below: 'top',
    			before: 'end',
    			after: 'start'
    		},
    		hPosMap = {
    			forwards: 'start',
    			center: 'center',
    			backwards: this.anchored ? 'before' : 'end'
    		},
    		vPosMap = {
    			forwards: 'top',
    			center: 'center',
    			backwards: 'bottom'
    		};

    	if ( !this.$container ) {
    		// Lazy-initialize $container if not specified in constructor
    		this.$container = $( this.getClosestScrollableElementContainer() );
    	}
    	direction = this.$container.css( 'direction' );

    	// Set height and width before we do anything else, since it might cause our measurements
    	// to change (e.g. due to scrollbars appearing or disappearing), and it also affects centering
    	this.$popup.css( {
    		width: this.width,
    		height: this.height !== null ? this.height : 'auto'
    	} );

    	align = alignMap[ direction ][ this.align ] || this.align;
    	// If the popup is positioned before or after, then the anchor positioning is vertical, otherwise horizontal
    	vertical = this.popupPosition === 'before' || this.popupPosition === 'after';
    	start = vertical ? 'top' : ( direction === 'rtl' ? 'right' : 'left' );
    	end = vertical ? 'bottom' : ( direction === 'rtl' ? 'left' : 'right' );
    	near = vertical ? 'top' : 'left';
    	far = vertical ? 'bottom' : 'right';
    	sizeProp = vertical ? 'Height' : 'Width';
    	popupSize = vertical ? ( this.height || this.$popup.height() ) : this.width;

    	this.setAnchorEdge( anchorEdgeMap[ this.popupPosition ] );
    	this.horizontalPosition = vertical ? this.popupPosition : hPosMap[ align ];
    	this.verticalPosition = vertical ? vPosMap[ align ] : this.popupPosition;

    	// Parent method
    	parentPosition = OO.ui.mixin.FloatableElement.prototype.computePosition.call( this );
    	// Find out which property FloatableElement used for positioning, and adjust that value
    	positionProp = vertical ?
    		( parentPosition.top !== '' ? 'top' : 'bottom' ) :
    		( parentPosition.left !== '' ? 'left' : 'right' );

    	// Figure out where the near and far edges of the popup and $floatableContainer are
    	floatablePos = this.$floatableContainer.offset();
    	floatablePos[ far ] = floatablePos[ near ] + this.$floatableContainer[ 'outer' + sizeProp ]();
    	// Measure where the offsetParent is and compute our position based on that and parentPosition
    	offsetParentPos = this.$element.offsetParent()[ 0 ] === document.documentElement ?
    		{ top: 0, left: 0 } :
    		this.$element.offsetParent().offset();

    	if ( positionProp === near ) {
    		popupPos[ near ] = offsetParentPos[ near ] + parentPosition[ near ];
    		popupPos[ far ] = popupPos[ near ] + popupSize;
    	} else {
    		popupPos[ far ] = offsetParentPos[ near ] +
    			this.$element.offsetParent()[ 'inner' + sizeProp ]() - parentPosition[ far ];
    		popupPos[ near ] = popupPos[ far ] - popupSize;
    	}

    	if ( this.anchored ) {
    		// Position the anchor (which is positioned relative to the popup) to point to $floatableContainer
    		anchorPos = ( floatablePos[ start ] + floatablePos[ end ] ) / 2;
    		anchorOffset = ( start === far ? -1 : 1 ) * ( anchorPos - popupPos[ start ] );

    		// If the anchor is less than 2*anchorSize from either edge, move the popup to make more space
    		// this.$anchor.width()/height() returns 0 because of the CSS trickery we use, so use scrollWidth/Height
    		anchorSize = this.$anchor[ 0 ][ 'scroll' + sizeProp ];
    		anchorMargin = parseFloat( this.$anchor.css( 'margin-' + start ) );
    		if ( anchorOffset + anchorMargin < 2 * anchorSize ) {
    			// Not enough space for the anchor on the start side; pull the popup startwards
    			positionAdjustment = ( positionProp === start ? -1 : 1 ) *
    				( 2 * anchorSize - ( anchorOffset + anchorMargin ) );
    		} else if ( anchorOffset + anchorMargin > popupSize - 2 * anchorSize ) {
    			// Not enough space for the anchor on the end side; pull the popup endwards
    			positionAdjustment = ( positionProp === end ? -1 : 1 ) *
    				( anchorOffset + anchorMargin - ( popupSize - 2 * anchorSize ) );
    		} else {
    			positionAdjustment = 0;
    		}
    	} else {
    		positionAdjustment = 0;
    	}

    	// Check if the popup will go beyond the edge of this.$container
    	containerPos = this.$container[ 0 ] === document.documentElement ?
    		{ top: 0, left: 0 } :
    		this.$container.offset();
    	containerPos[ far ] = containerPos[ near ] + this.$container[ 'inner' + sizeProp ]();
    	// Take into account how much the popup will move because of the adjustments we're going to make
    	popupPos[ near ] += ( positionProp === near ? 1 : -1 ) * positionAdjustment;
    	popupPos[ far ] += ( positionProp === near ? 1 : -1 ) * positionAdjustment;
    	if ( containerPos[ near ] + this.containerPadding > popupPos[ near ] ) {
    		// Popup goes beyond the near (left/top) edge, move it to the right/bottom
    		positionAdjustment += ( positionProp === near ? 1 : -1 ) *
    			( containerPos[ near ] + this.containerPadding - popupPos[ near ] );
    	} else if ( containerPos[ far ] - this.containerPadding < popupPos[ far ] ) {
    		// Popup goes beyond the far (right/bottom) edge, move it to the left/top
    		positionAdjustment += ( positionProp === far ? 1 : -1 ) *
    			( popupPos[ far ] - ( containerPos[ far ] - this.containerPadding ) );
    	}

    	if ( this.anchored ) {
    		// Adjust anchorOffset for positionAdjustment
    		anchorOffset += ( positionProp === start ? -1 : 1 ) * positionAdjustment;

    		// Position the anchor
    		anchorCss[ start ] = anchorOffset;
    		this.$anchor.css( anchorCss );
    	}

    	// Move the popup if needed
    	parentPosition[ positionProp ] += positionAdjustment;

    	return parentPosition;
    };

    /**
     * Set popup alignment
     *
     * @param {string} [align=center] Alignment of the popup, `center`, `force-left`, `force-right`,
     *  `backwards` or `forwards`.
     */
    OO.ui.PopupWidget.prototype.setAlignment = function ( align ) {
    	// Validate alignment
    	if ( [ 'force-left', 'force-right', 'backwards', 'forwards', 'center' ].indexOf( align ) > -1 ) {
    		this.align = align;
    	} else {
    		this.align = 'center';
    	}
    	this.position();
    };

    /**
     * Get popup alignment
     *
     * @return {string} Alignment of the popup, `center`, `force-left`, `force-right`,
     *  `backwards` or `forwards`.
     */
    OO.ui.PopupWidget.prototype.getAlignment = function () {
    	return this.align;
    };

    /**
     * Change the positioning of the popup.
     *
     * @param {string} position 'above', 'below', 'before' or 'after'
     */
    OO.ui.PopupWidget.prototype.setPosition = function ( position ) {
    	if ( [ 'above', 'below', 'before', 'after' ].indexOf( position ) === -1 ) {
    		position = 'below';
    	}
    	this.popupPosition = position;
    	this.position();
    };

    /**
     * Get popup positioning.
     *
     * @return {string} 'above', 'below', 'before' or 'after'
     */
    OO.ui.PopupWidget.prototype.getPosition = function () {
    	return this.popupPosition;
    };

    /**
     * Get an ID of the body element, this can be used as the
     * `aria-describedby` attribute for an input field.
     *
     * @return {string} The ID of the body element
     */
    OO.ui.PopupWidget.prototype.getBodyId = function () {
    	var id = this.$body.attr( 'id' );
    	if ( id === undefined ) {
    		id = OO.ui.generateElementId();
    		this.$body.attr( 'id', id );
    	}
    	return id;
    };