2013-04-29 20:56:24 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface LookupInputWidget class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lookup input widget.
|
|
|
|
*
|
|
|
|
* Mixin that adds a menu showing suggested values to a text input. Subclasses must handle `select`
|
|
|
|
* events on #lookupMenu to make use of selections.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.ui.TextInputWidget} input Input widget
|
|
|
|
* @param {Object} [config] Config options
|
The Great ve.ui.Surface refactor of 2013
Prologue:
Farewell ve.Editor my good chap… Oh, hey there HTML frames - I didn't
see you there! In a world where iframes are outlaws, and symbols like
document and window are global, there were more than a few assumptions
about which document or window was being used. But fear not - for this
commit (probably) tracks them all down, leaving a trail of
iframe-compatible awesomeness in its wake. With the great ve.ui.Surface
now able to be used inside of iframes, let the reference editing
commence. But there, lurking in the darkness is a DM issue so fierce it
may take Roan and/or Ed up to 3 whole hours to sort it out.
Note to Roan and/or Ed:
Editing references seems to work fine, but when saving the page there
are "no changes" which is a reasonable indication to the contrary.
Objectives:
* Make it possible to have multiple surfaces be instantiated, get along
nicely, and be embedded inside of iframes if needed.
* Make reference content editable within a dialog
Approach:
* Move what's left of ve.Editor to ve.ui.Surface and essentially
obliterate all use of it
* Make even more stuff inherit from ve.Element (long live this.$$)
* Use the correct document or window anywhere it was being assumed to be
the top level one
* Resolve stacking order issues by removing the excessive use of z-index
and introducing global and local overlay elements for each editor
* Add a surface to the reference dialog, load up the reference contents
and save them back on apply
* Actually destroy what we create in ce and ui surfaces
* Add recursive frame offset calculation method to ve.Element
* Moved ve.ce.Surface's getSelectionRect method to the prototype
Bonus:
* Move ve.ce.DocumentNode.css contents to ve.ce.Node.css (not sure why it
was separate in the first place, but I'm likely the one to blame)
* Fix blatant lies in documentation
* Whitespace cleanup here and there
* Get rid of ve.ui.Window overlays - not used or needed
Change-Id: Iede83e7d24f7cb249b6ba3dc45d770445b862e08
2013-05-20 22:45:50 +00:00
|
|
|
* @cfg {jQuery} [$overlay=this.$$( '.ve-surface-overlay-local:last' )] Overlay layer
|
2013-04-29 20:56:24 +00:00
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget = function VeUiLookupInputWidget( input, config ) {
|
|
|
|
// Config intialization
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.lookupInput = input;
|
The Great ve.ui.Surface refactor of 2013
Prologue:
Farewell ve.Editor my good chap… Oh, hey there HTML frames - I didn't
see you there! In a world where iframes are outlaws, and symbols like
document and window are global, there were more than a few assumptions
about which document or window was being used. But fear not - for this
commit (probably) tracks them all down, leaving a trail of
iframe-compatible awesomeness in its wake. With the great ve.ui.Surface
now able to be used inside of iframes, let the reference editing
commence. But there, lurking in the darkness is a DM issue so fierce it
may take Roan and/or Ed up to 3 whole hours to sort it out.
Note to Roan and/or Ed:
Editing references seems to work fine, but when saving the page there
are "no changes" which is a reasonable indication to the contrary.
Objectives:
* Make it possible to have multiple surfaces be instantiated, get along
nicely, and be embedded inside of iframes if needed.
* Make reference content editable within a dialog
Approach:
* Move what's left of ve.Editor to ve.ui.Surface and essentially
obliterate all use of it
* Make even more stuff inherit from ve.Element (long live this.$$)
* Use the correct document or window anywhere it was being assumed to be
the top level one
* Resolve stacking order issues by removing the excessive use of z-index
and introducing global and local overlay elements for each editor
* Add a surface to the reference dialog, load up the reference contents
and save them back on apply
* Actually destroy what we create in ce and ui surfaces
* Add recursive frame offset calculation method to ve.Element
* Moved ve.ce.Surface's getSelectionRect method to the prototype
Bonus:
* Move ve.ce.DocumentNode.css contents to ve.ce.Node.css (not sure why it
was separate in the first place, but I'm likely the one to blame)
* Fix blatant lies in documentation
* Whitespace cleanup here and there
* Get rid of ve.ui.Window overlays - not used or needed
Change-Id: Iede83e7d24f7cb249b6ba3dc45d770445b862e08
2013-05-20 22:45:50 +00:00
|
|
|
this.$overlay = config.$overlay || this.$$( '.ve-surface-overlay-local:last' );
|
2013-04-29 20:56:24 +00:00
|
|
|
this.lookupMenu = new ve.ui.TextInputMenuWidget( this, {
|
ve.Element refactor
Objectives:
* Move ve.ui.Element to ve.Element
* Make CE nodes inherit from ve.Element
Changes:
ve.ui.Element.js, ve.Element.js
* Move and rename
* Move ve.ui.get$$ to ve.Element.static.get$$
* Add static getDocument and getWindow methods
* Add instance getElementDocument and getElementWindow methods
* Add getTagName method, which by default reads the static tagName property, but when overridden can return a tag name based on other factors
*.php
* Updated file link
ve.ce.*Annotation.js, ve.ce.*Node.js, ve.ce.View.js, ve.ce.Document
* Added config options pass through
* Replaced passing elements through constructor with defining static tag names
* Added getTagName overrides where needed that derive tag name from model
* Refactore dom wrapper methods, now consistently using getTagName
ve.ce.Surface.js
* Removed static initialization (not needed)
ve.dm.Model.js, ve.ui.Window.js
* Added missing docs
ve.ui.GroupElement.js, ve.ui.Layout.js, ve.ui.Widget.js,
* Updated class name for elements
ve.ui.Frame.js, ve.ui.LookupInputWidget.js
* Updated location of get$$
ve.ui.js
* Move get$$ to ve.Element
ve.js
* Add auto-init of static properties to mixinClass
Change-Id: I39ae14966456903728e4d9e53f806ddce9ca2b70
2013-05-13 20:52:59 +00:00
|
|
|
'$$': ve.Element.static.get$$( this.$overlay ),
|
2013-04-29 20:56:24 +00:00
|
|
|
'input': this.lookupInput,
|
|
|
|
'$container': config.$container
|
|
|
|
} );
|
|
|
|
this.lookupCache = {};
|
|
|
|
this.lookupQuery = null;
|
|
|
|
this.lookupRequest = null;
|
|
|
|
|
|
|
|
// Events
|
|
|
|
this.$overlay.append( this.lookupMenu.$ );
|
|
|
|
|
|
|
|
this.lookupInput.$input.on( {
|
|
|
|
'focus': ve.bind( this.onLookupInputFocus, this ),
|
|
|
|
'blur': ve.bind( this.onLookupInputBlur, this ),
|
|
|
|
'mousedown': ve.bind( this.onLookupInputMouseDown, this )
|
|
|
|
} );
|
2013-05-01 22:21:32 +00:00
|
|
|
this.lookupInput.connect( this, { 'change': 'onLookupInputChange' } );
|
2013-04-29 20:56:24 +00:00
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.$.addClass( 've-ui-lookupWidget' );
|
|
|
|
this.lookupMenu.$.addClass( 've-ui-lookupWidget-menu' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle input focus event.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {jQuery.Event} e Input focus event
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.onLookupInputFocus = function () {
|
|
|
|
this.openLookupMenu();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle input blur event.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {jQuery.Event} e Input blur event
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.onLookupInputBlur = function () {
|
|
|
|
this.lookupMenu.hide();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle input mouse down event.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {jQuery.Event} e Input mouse down event
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.onLookupInputMouseDown = function () {
|
|
|
|
this.openLookupMenu();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle input change event.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} value New input value
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.onLookupInputChange = function () {
|
|
|
|
this.openLookupMenu();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the menu.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @chainable
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.openLookupMenu = function () {
|
|
|
|
var value = this.lookupInput.getValue();
|
|
|
|
|
|
|
|
if ( value.length && $.trim( value ) !== '' ) {
|
|
|
|
this.populateLookupMenu();
|
|
|
|
if ( !this.lookupMenu.isVisible() ) {
|
|
|
|
this.lookupMenu.show();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.lookupMenu.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate lookup menu with current information.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @chainable
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.populateLookupMenu = function () {
|
|
|
|
var items = this.getLookupMenuItems();
|
|
|
|
|
|
|
|
this.lookupMenu.clearItems();
|
|
|
|
|
|
|
|
if ( items.length ) {
|
2013-06-18 19:34:01 +00:00
|
|
|
this.lookupMenu.show();
|
2013-04-29 20:56:24 +00:00
|
|
|
this.lookupMenu.addItems( items );
|
|
|
|
this.initializeLookupMenuSelection();
|
2013-06-18 19:34:01 +00:00
|
|
|
} else {
|
|
|
|
this.lookupMenu.hide();
|
2013-04-29 20:56:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set selection in the lookup menu with current information.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @chainable
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.initializeLookupMenuSelection = function () {
|
|
|
|
if ( !this.lookupMenu.getSelectedItem() ) {
|
|
|
|
this.lookupMenu.selectItem( this.lookupMenu.getClosestSelectableItem( 0 ), true );
|
|
|
|
}
|
|
|
|
this.lookupMenu.highlightItem( this.lookupMenu.getSelectedItem() );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get lookup menu items for the current query.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {ve.ui.MenuItemWidget[]} Menu items
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.getLookupMenuItems = function () {
|
|
|
|
var value = this.lookupInput.getValue();
|
|
|
|
|
|
|
|
if ( value && value !== this.lookupQuery ) {
|
|
|
|
// Abort current request if query has changed
|
|
|
|
if ( this.lookupRequest ) {
|
|
|
|
this.lookupRequest.abort();
|
|
|
|
this.lookupQuery = null;
|
|
|
|
this.lookupRequest = null;
|
|
|
|
}
|
|
|
|
if ( value in this.lookupCache ) {
|
|
|
|
return this.getLookupMenuItemsFromData( this.lookupCache[value] );
|
|
|
|
} else {
|
|
|
|
this.lookupQuery = value;
|
|
|
|
this.lookupRequest = this.getLookupRequest()
|
|
|
|
.always( ve.bind( function () {
|
|
|
|
this.lookupQuery = null;
|
|
|
|
this.lookupRequest = null;
|
|
|
|
}, this ) )
|
|
|
|
.done( ve.bind( function ( data ) {
|
|
|
|
this.lookupCache[value] = this.getLookupCacheItemFromData( data );
|
|
|
|
this.openLookupMenu();
|
|
|
|
}, this ) );
|
2013-06-06 00:07:51 +00:00
|
|
|
this.pushPending();
|
|
|
|
this.lookupRequest.always( ve.bind( function () {
|
|
|
|
this.popPending();
|
|
|
|
}, this ) );
|
2013-04-29 20:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a new request object of the current lookup query value.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @abstract
|
2013-06-18 17:56:56 +00:00
|
|
|
* @returns {jqXHR} jQuery AJAX object, or promise object with an .abort() method
|
2013-04-29 20:56:24 +00:00
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.getLookupRequest = function () {
|
|
|
|
// Stub, implemented in subclass
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle successful lookup request.
|
|
|
|
*
|
|
|
|
* Overriding methods should call #populateLookupMenu when results are available and cache results
|
|
|
|
* for future lookups in #lookupCache as an array of #ve.ui.MenuItemWidget objects.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @abstract
|
|
|
|
* @param {Mixed} data Response from server
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.onLookupRequestDone = function () {
|
|
|
|
// Stub, implemented in subclass
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of menu item widgets from the data stored by the lookup request's done handler.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @abstract
|
|
|
|
* @param {Mixed} data Cached result data, usually an array
|
|
|
|
* @returns {ve.ui.MenuItemWidget[]} Menu items
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.getLookupMenuItemsFromData = function () {
|
|
|
|
// Stub, implemented in subclass
|
|
|
|
return [];
|
|
|
|
};
|