2013-03-26 23:55:41 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface PopupWidget class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an ve.ui.PopupWidget object.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.ui.Widget
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [config] Config options
|
2013-05-16 19:34:18 +00:00
|
|
|
* @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
|
2013-03-26 23:55:41 +00:00
|
|
|
*/
|
|
|
|
ve.ui.PopupWidget = function VeUiPopupWidget( config ) {
|
2013-04-03 00:36:54 +00:00
|
|
|
// Config intialization
|
|
|
|
config = config || {};
|
|
|
|
|
2013-03-26 23:55:41 +00:00
|
|
|
// Parent constructor
|
|
|
|
ve.ui.Widget.call( this, config );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.visible = false;
|
2013-04-24 19:40:56 +00:00
|
|
|
this.$callout = this.$$( '<div>' );
|
2013-05-16 19:34:18 +00:00
|
|
|
this.$body = this.$$( '<div>' );
|
2013-03-26 23:55:41 +00:00
|
|
|
this.transitionTimeout = null;
|
2013-04-03 00:36:54 +00:00
|
|
|
this.align = config.align || 'center';
|
2013-05-16 19:34:18 +00:00
|
|
|
this.autoClose = !!config.autoClose;
|
2013-03-26 23:55:41 +00:00
|
|
|
|
2013-05-15 23:31:02 +00:00
|
|
|
// Events
|
|
|
|
this.$.add( this.$body ).add( this.$callout )
|
2013-05-16 19:34:18 +00:00
|
|
|
.on( 'mousedown', function ( e ) {
|
|
|
|
// Cancel only local mousedown events
|
|
|
|
return e.target !== this;
|
|
|
|
} );
|
2013-05-15 23:31:02 +00:00
|
|
|
|
2013-03-26 23:55:41 +00:00
|
|
|
// Initialization
|
2013-05-16 19:34:18 +00:00
|
|
|
if ( this.autoClose ) {
|
|
|
|
// Tab index on body so that it may blur
|
|
|
|
this.$body.attr( 'tabindex', 1 );
|
|
|
|
// Listen for blur events
|
|
|
|
this.$body.on( 'blur', ve.bind( this.onPopupBlur, this ) );
|
|
|
|
}
|
2013-03-26 23:55:41 +00:00
|
|
|
this.$
|
|
|
|
.addClass( 've-ui-popupWidget' )
|
|
|
|
.append(
|
|
|
|
this.$callout.addClass( 've-ui-popupWidget-callout' ),
|
|
|
|
this.$body.addClass( 've-ui-popupWidget-body' )
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.inheritClass( ve.ui.PopupWidget, ve.ui.Widget );
|
|
|
|
|
2013-04-24 19:40:56 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event hide
|
|
|
|
*/
|
|
|
|
|
2013-03-26 23:55:41 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2013-05-16 19:34:18 +00:00
|
|
|
/**
|
|
|
|
* Handle blur events.
|
|
|
|
*
|
|
|
|
* @param {jQuery.Event} e Blur event
|
|
|
|
*/
|
|
|
|
ve.ui.PopupWidget.prototype.onPopupBlur = function () {
|
|
|
|
var $body = this.$body;
|
|
|
|
|
|
|
|
// Find out what is focused after blur
|
|
|
|
setTimeout( ve.bind( function () {
|
|
|
|
var $focused = $body.find( ':focus' );
|
|
|
|
// Is there a focused child element?
|
|
|
|
if ( $focused.length > 0 ) {
|
|
|
|
// Bind a one off blur event to that focused child element
|
|
|
|
$focused.one( 'blur', ve.bind( function () {
|
|
|
|
setTimeout( ve.bind( function () {
|
|
|
|
if ( $body.find( ':focus' ).length === 0 ) {
|
|
|
|
// Be sure focus is not the popup itself.
|
|
|
|
if ( $body.is( ':focus' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Not a child and not the popup itself, so hide.
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
}, this ), 0 );
|
|
|
|
}, this ) );
|
|
|
|
} else {
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
}, this ), 0 );
|
|
|
|
};
|
|
|
|
|
2013-03-26 23:55:41 +00:00
|
|
|
/**
|
|
|
|
* Show the context.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @chainable
|
|
|
|
*/
|
|
|
|
ve.ui.PopupWidget.prototype.show = function () {
|
|
|
|
this.$.show();
|
|
|
|
this.visible = true;
|
2013-05-16 19:34:18 +00:00
|
|
|
if ( this.autoClose ) {
|
|
|
|
// Focus body so that it may blur
|
|
|
|
this.$body.focus();
|
|
|
|
}
|
2013-03-26 23:55:41 +00:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hide the context.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @chainable
|
|
|
|
*/
|
|
|
|
ve.ui.PopupWidget.prototype.hide = function () {
|
|
|
|
this.$.hide();
|
|
|
|
this.visible = false;
|
2013-04-24 19:40:56 +00:00
|
|
|
this.emit( 'hide' );
|
2013-03-26 23:55:41 +00:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2013-04-24 19:40:56 +00:00
|
|
|
|
2013-03-26 23:55:41 +00:00
|
|
|
/**
|
|
|
|
* Updates the position and size.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @chainable
|
|
|
|
*/
|
|
|
|
ve.ui.PopupWidget.prototype.display = function ( x, y, width, height, transition ) {
|
2013-04-03 00:36:54 +00:00
|
|
|
var left, overlapLeft, overlapRight,
|
|
|
|
padding = 15;
|
|
|
|
|
|
|
|
switch ( this.align ) {
|
|
|
|
case 'left':
|
|
|
|
// Inset callout from left
|
|
|
|
left = -padding;
|
|
|
|
break;
|
|
|
|
case 'right':
|
|
|
|
// Inset callout from right
|
|
|
|
left = -width + padding;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Place callout in center
|
|
|
|
left = -width / 2;
|
|
|
|
break;
|
|
|
|
}
|
2013-03-26 23:55:41 +00:00
|
|
|
|
2013-04-03 00:36:54 +00:00
|
|
|
// Prevent viewport clipping, using padding between body and popup edges
|
|
|
|
overlapRight = this.$$( 'body' ).width() - ( x + ( width + left + ( padding * 2 ) ) );
|
|
|
|
overlapLeft = x + ( left - ( padding * 2 ) );
|
2013-03-26 23:55:41 +00:00
|
|
|
if ( overlapRight < 0 ) {
|
2013-04-03 00:36:54 +00:00
|
|
|
left += overlapRight;
|
2013-03-26 23:55:41 +00:00
|
|
|
} else if ( overlapLeft < 0 ) {
|
2013-04-03 00:36:54 +00:00
|
|
|
left -= overlapLeft;
|
2013-03-26 23:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent transition from being interrupted
|
|
|
|
clearTimeout( this.transitionTimeout );
|
|
|
|
if ( transition ) {
|
|
|
|
// Enable transition
|
|
|
|
this.$.addClass( 've-ui-popupWidget-transitioning' );
|
|
|
|
// Prevent transitioning after transition is complete
|
|
|
|
this.transitionTimeout = setTimeout( ve.bind( function () {
|
|
|
|
this.$.removeClass( 've-ui-popupWidget-transitioning' );
|
|
|
|
}, this ), 200 );
|
|
|
|
} else {
|
|
|
|
// Prevent transitioning immediately
|
|
|
|
this.$.removeClass( 've-ui-popupWidget-transitioning' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Position body relative to anchor and adjust size
|
|
|
|
this.$body.css( {
|
2013-04-03 00:36:54 +00:00
|
|
|
'left': left, 'width': width, 'height': height === undefined ? 'auto' : height
|
2013-03-26 23:55:41 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|