mediawiki-extensions-Visual.../modules/ve/ui/widgets/ve.ui.TextInputMenuWidget.js

329 lines
7.8 KiB
JavaScript
Raw Normal View History

(bug 43841) Major ve.ui rewrite, especially ve.ui.LinkInspector Objectives: * Make the link inspector easier to use * Try to resolve a few bugs (bug 43841, bug 43063, bug 42986) * Stop using jquery.multiSuggest (which didn't really understand annotations) * Better divide MediaWiki specifics from generic implementations Changes: VisualEditor.php, modules/ve/test/index.php, demos/ve/index.php * Updated links to files ve.Registry * Fixed mistake where registry was initialized as an array - this didn't cause any errors because you can add arbitrary properties to an array and use it like any other object ve.Factory * Removed duplicate initialization of registry property * Added entries property, which is an array that's appended to for tracking the order of registrations ve.CommandRegistry * Added mwLink command which opens the mwLink inspector ve.ui.TextInputWidget * Added basic widget class for text inputs ve.ui.TextInputMenuWidget * Added widget that provides a menu of options for a text input widget ve.ui.MWLinkTargetInputWidget * Added MediaWiki specific link target widget ve.ui.MenuWidget * Converted ve.ui.Menu into a widget * Moved the body of onSelect to onMouseUp ve.ui.LinkTargetInputWidget * Added link target widget which adds link annotation functionality to a normal text input ve.ui.InputWidget * Added generic input widget which emits reliable and instant change events and synchronizes a value property with the DOM value ve.ui.Widget * Added base widget class * Widgets can be used in any frame ve.ui.Tool * Fixed line length issues ve.ui.InspectorFactory * Made use of new entries property for factories to select the most recently added inspector if more than one match a given annotation ve.ui.Inspector * Added auto-focus on the first visible input element on open * Moved afterClose event to after re-focus on document on close * Added documentation ve.ui.Frame * Adjusted documentation * Added binding of $$ to the frame context so it can be passed around * Added documentation ve.ui.Context * Added ve.ui.Widget.css to iframes * Updated code as per moving of ve.ui.Menu to ve.ui.MenuWidget * Removed unused positionBelowOverlay method * Added CSS settings to set overlay left and width properties according to context size * Added documentation ve.ui.DropdownTool * Updated code as per moving of ve.ui.Menu to ve.ui.MenuWidget ve.ui.FormatDropdownTool * Added documentation ve.ui.MWLinkButtonTool * Added MediaWiki specific version of ve.ui.LinkButtonTool, which opens the mwLink inspector ve.ui.Widget.css * Added styles for all widgets ve.ui.Tool.css, ve.init.sa.css, ve.init.mw.ViewPageTarget.css, ve.init.mw.ViewPageTarget-apex.css * Updated code as per moving of ve.ui.Menu to ve.ui.MenuWidget ve.ui.Menu.css * Deleted (merged into ve.ui.Widget.css) ve.ui.Menu.css * Deleted suggest styles (no longer used) pending.gif, pending.psd * Added diagonal stripe animation to indicate a pending request to the API ve.ui.MWLinkInspector * Added MediaWiki specific inspector which uses MediaWiki specific annotations and widgets ve.ui.LinkInspector * Removed mw global hint (not needed anymore) * Switched from comparing targets to annotations (since the target text is ambiguous in some situations) * Switched to using input widget, which is configured using a static property * Removed use of jquery.multiSuggest * Moved MediaWiki specifics to their own class (ve.ui.MWLinkInspector) ve.init.mw.ViewPageTarget * Added MediaWiki specific toolbar and command options Change-Id: I859b5871a9d2f17d970c002067c8ff24f3513e9f
2013-01-09 21:34:23 +00:00
/**
* VisualEditor user interface TextInputMenuWidget class.
*
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Creates an ve.ui.TextInputMenuWidget object.
*
* @class
* @constructor
* @extends ve.ui.Widget
* @param {Function} $$ jQuery for the frame the widget is in
* @param {jQuery} $container Container to render menu into
*/
ve.ui.TextInputMenuWidget = function VeUiTextInputMenuWidget( $$, $container ) {
// Parent constructor
ve.ui.Widget.call( this, $$ );
// Properties
this.$container = $container;
this.groups = {};
this.items = {};
this.sequence = [];
this.$groups = this.$$( '<ul>' ).addClass( 've-ui-textInputMenuWidget-groups' );
this.visible = false;
this.selectedItem = null;
this.newItems = [];
// Events
this.$.on( {
'mousedown': ve.bind( this.onMouseDown, this ),
'mouseup': ve.bind( this.onMouseUp, this ),
} );
// Initialization
this.$
.hide()
.addClass( 've-ui-textInputMenuWidget' )
.append( this.$groups );
this.$container.append( this.$ );
};
/* Inheritance */
ve.inheritClass( ve.ui.TextInputMenuWidget, ve.ui.Widget );
/* Methods */
/**
* Handles mouse down events.
*
* @method
*/
ve.ui.TextInputMenuWidget.prototype.onMouseDown = function ( e ) {
this.$groups.find( '.ve-ui-textInputMenuWidget-item-selected' )
.removeClass( 've-ui-textInputMenuWidget-item-selected' );
$( e.target ).closest( '.ve-ui-textInputMenuWidget-item' )
.addClass( 've-ui-textInputMenuWidget-item-selected' );
e.preventDefault();
return false;
};
/**
* Handles mouse up events.
*
* @method
*/
ve.ui.TextInputMenuWidget.prototype.onMouseUp = function ( e ) {
var $item = $( e.target ).closest( '.ve-ui-textInputMenuWidget-item' );
if ( $item.length ) {
this.hide();
this.emit( 'select', $item.data( 'item' ).data );
}
e.preventDefault();
return false;
};
/**
* Checks if the menu is visible.
*
* @method
*/
ve.ui.TextInputMenuWidget.prototype.isVisible = function () {
return this.visible;
};
/**
* Shows the menu.
*
* @method
*/
ve.ui.TextInputMenuWidget.prototype.show = function () {
var i, len;
if ( !this.sequence.length ) {
return;
}
this.$.show();
this.visible = true;
if ( this.newItems.length ) {
for ( i = 0, len = this.newItems.length; i < len; i++ ) {
this.newItems[i].$.autoEllipsis( { 'hasSpan': true, 'tooltip': true } );
}
this.newItems = [];
}
};
/**
* Hides the menu.
*
* @method
*/
ve.ui.TextInputMenuWidget.prototype.hide = function () {
this.$.hide();
this.visible = false;
};
/**
* Places the menu underneeth a given element.
*
* @method
*/
ve.ui.TextInputMenuWidget.prototype.setPosition = function ( $element ) {
this.$.css( {
'left': $element.offset().left,
'top': $element.offset().top + $element.outerHeight( true )
} );
};
/**
* Gets the currently selected item.
*
* @method
*/
ve.ui.TextInputMenuWidget.prototype.getSelectedItem = function () {
return this.selectedItem;
};
/**
* Adds a group.
*
* @method
* @param {string} name Name of group
* @param {string} label Group label
* @throws {Error} Name must be unique for each group
*/
ve.ui.TextInputMenuWidget.prototype.addGroup = function ( name, label ) {
if ( name in this.groups ) {
throw new Error( 'Name must be unique for each group' );
}
var group = {
'name': name,
'items': [],
'$': this.$$( '<li>' ),
'$label': this.$$( '<span>' ),
'$items': this.$$( '<ul>' )
};
group.$label.text( label ).addClass( 've-ui-textInputMenuWidget-group-label' );
group.$items.addClass( 've-ui-textInputMenuWidget-items' );
group.$
.hide()
.attr( 'rel', name )
.addClass( 've-ui-textInputMenuWidget-group' )
.append( group.$label )
.append( group.$items );
this.$groups.append( group.$ );
this.groups[name] = group;
// Group names and item objects are collected in a sequence for consistent ordering
this.sequence.push( name );
};
/**
* Adds an item.
*
* @method
* @param {string} groupName Name of group
* @param {string} label Item label
* @param {Object} data Item data
* @throws {Error} Group does not exist
* @throws {Error} Data must be unique for each item
*/
ve.ui.TextInputMenuWidget.prototype.addItem = function ( groupName, label, data ) {
if ( !( groupName in this.groups ) ) {
throw new Error( 'Group does not exist:' + groupName );
}
var group = this.groups[groupName],
hash = ve.getHash( data ),
item = {
'group': group,
'label': label,
'data': data,
'$': this.$$( '<li>' ),
'$label': this.$$( '<span>' )
};
if ( hash in this.items ) {
throw new Error( 'Data must be unique for each item' );
}
item.$label.text( label ).addClass( 've-ui-textInputMenuWidget-item-label' );
item.$
.data( 'item', item )
.addClass( 've-ui-textInputMenuWidget-item' )
.append( item.$label );
group.$.show();
group.$items.append( item.$ );
group.items.push( item );
this.items[hash] = item;
// Items are inserted before their group name, for quick splicing
this.sequence.splice( this.sequence.indexOf( groupName ), 0, item );
if ( this.visible ) {
item.$.autoEllipsis( { 'hasSpan': true, 'tooltip': true } );
} else {
this.newItems.push( item );
}
};
/**
* Removes all items.
*
* @method
*/
ve.ui.TextInputMenuWidget.prototype.clearItems = function () {
this.groups = {};
this.items = {};
this.sequence = [];
this.selectedItem = null;
this.$groups.empty();
};
/**
* Gets an item from it's data.
*
* This performs a hash comparison, not an identity comparison.
*
* @method
* @param {Object} data Item data
*/
ve.ui.TextInputMenuWidget.prototype.getItemFromData = function ( data ) {
var hash = ve.getHash( data );
if ( hash in this.items ) {
return this.items[hash];
}
};
/**
* Selects an item.
*
* @method
* @param {Object} data Item data
* @returns {boolean} Item was selected
*/
ve.ui.TextInputMenuWidget.prototype.selectItemByData = function ( data ) {
var hash = ve.getHash( data ),
item = this.items[hash];
if ( item && item !== this.selectedItem ) {
this.selectedItem = item;
this.$groups.find( '.ve-ui-textInputMenuWidget-item-selected' )
.removeClass( 've-ui-textInputMenuWidget-item-selected' );
item.$.addClass( 've-ui-textInputMenuWidget-item-selected' );
this.emit( 'select', item.data );
return true;
}
return false;
};
/**
* Selects the next item in the menu.
*
* @method
* @param {number} direction Direction to move selection in
* @returns {boolean} A different item was selected
*/
ve.ui.TextInputMenuWidget.prototype.selectRelativeItem = function ( direction ) {
var item,
i = direction > 0 ?
// Default to 0 instead of -1, if nothing is selected let's start at the beginning
Math.max( 0, this.sequence.indexOf( this.selectedItem ) + direction ) :
// Default to n-1 instead of -1, if nothing is selected let's start at the end
Math.min( this.sequence.indexOf( this.selectedItem ) + direction, this.sequence.length - 1 ),
len = this.sequence.length,
inc = direction > 0 ? 1 : -1,
stopAt = i;
// Iterate to the next item in the sequence
while ( i < len ) {
item = this.sequence[i];
if ( ve.isPlainObject( item ) ) {
return this.selectItemByData( item.data );
}
// Wrap around
i = ( i + inc + len ) % len;
if ( i === stopAt ) {
// We've looped around, I guess we're all alone
return this.selectItemByData( item.data );
}
}
return false;
};
/**
* Selects the next item in the menu.
*
* @method
* @param {number} index Item index
* @returns {boolean} A different item was selected
*/
ve.ui.TextInputMenuWidget.prototype.selectItemByIndex = function ( index ) {
var item, itemAtIndex,
i = 0,
len = this.sequence.length,
at = 0;
while ( i < len ) {
item = this.sequence[i];
if ( ve.isPlainObject( item ) ) {
itemAtIndex = item;
if ( at === index ) {
break;
}
at++;
}
i++;
}
if ( itemAtIndex ) {
return this.selectItemByData( itemAtIndex.data );
}
return false;
};