mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-16 02:51:50 +00:00
a5aeca3ff5
Objective: Refactor UI widgets, improve usability and accessibility of menus, general cleanup and style improvements. Extras: Fixed documentation in a few other files to make descriptions of jQuery event arguments more consistent, classes inherit correctly, and made use of the @cfg functionality in jsduck. Changes: .docs/config.json * Added window, HTMLDocument, HTMLElement, DocumentFragment and XMLHttpRequest to externals, so jsduck doesn't throw warnings when they are used demos/ve/index.php, modules/ve/test/index.php, VisualEditor.php * Moved widgets above tools (since tools use widgets) demos/ve/index.php * Refactored widget initialization to use options * Renamed variables to match widget names ve.init.mw.ViewPageTarget.css * Adjusted text sizes to make widgets work normally * Added margins for buttons in toolbar (since button widgets don't have any) * Removed styles for init buttons (button widgets now) ve.init.mw.ViewPageTarget.js * Switched to using button widgets (involved moving things around a bit) ve.ui.LinkInspector.js, ve.ui.MWLinkInspector.js * Renamed static property "inputWidget" to "linkTargetInputWidget" to better reflect the required base class for the properties value icons.ai, check.png, check.svg * Added "check" icon, used in menu right now to show which item is selected ve.ui.Icons-raster.css, ve.ui.Icons-vector.css * Added check icon * Removed :before pseudo selectors from most of the icon classes (not need by button tool anymore, makes them more reusable now) ve.ui.Tool.css * Adjusted drop down tool styles so menu appears below, instead of on top, of the label * Adjusted paragraph font size to better match actual content * Updated class names to still work with menu widget changes (items are their own widgets now) * Updated selectors as per changes in the structure of button tools ve.ui.Widget.css * Added styles for buttons and menu items * Adjusted menu styles ve.ui.*ButtonTool.js * Added config options argument passthrough ve.ui.ButtonTool.js * Moved var statement to the top inside constructor * Switched to using "a" tag to get cross-browser :active support * Added icon to inside of button to make icon styles more reusable * Removed disabled support (now provided by widget parent class) ve.ui.FormatDropDownTool.js * Updated options initialization to construct menu item objects * Modified handling of items to account for changes in menu and item classes * Optimized onUpdateState method a bit, adding early exit to inner loop ve.ui.ButtonTool.js, ve.ui.DropdownTool.js, ve.ui.Context.js, ve.ui.Frame, ve.ui.Tool.js, ve.ui.Widget.js * Added chain ability to non-getter methods ve.ui.DropdownTool.js * Removed items argument to constructor * Updated code as per changes in menu class * Fixed inconsistent naming of event handler methods * Removed item event handling (now handled by items directly) * Made use of this.$$ to ensure tool works in other frames ve.ui.Tool.js * Made tools inherit from widget * Moved trigger registry event handler to a method ve.ui.Context.js * Switched from using menu to contain toolbar to a simple wrapper ve.ui.js * Added get$$ method, a convenience function for binding jQuery to a specific document context ve.ui.*Widget.js * Switched to using a config options object instead of individual arguments * Added options * Factored out flags and labels into their own classes * Refactored value setting methods for inputs ve.ui.MenuWidget.js, ve.ui.MenuItemWidget.js * Broke items out into their own classes * Redesigned API * Updated code that uses these classes * Added support for keyboard interaction * Made items flash when selected (delaying the hiding of the menu for 200ms) ve.ui.LinkTargetInputWidget.js, ve.ui.MWLinkTargetInputWidget * Refactored annotation setting methods Change-Id: I7769bd5a5b79f1ab36f258ef9f2be583ca503ce6
156 lines
3.7 KiB
JavaScript
156 lines
3.7 KiB
JavaScript
/*!
|
|
* VisualEditor UserInterface DropdownTool class.
|
|
*
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* UserInterface dropdown tool.
|
|
*
|
|
* @abstract
|
|
* @class
|
|
* @extends ve.ui.Tool
|
|
* @constructor
|
|
* @param {ve.ui.Toolbar} toolbar
|
|
* @param {Object} [config] Config options
|
|
*/
|
|
ve.ui.DropdownTool = function VeUiDropdownTool( toolbar, config ) {
|
|
// Parent constructor
|
|
ve.ui.Tool.call( this, toolbar, config );
|
|
|
|
// Properties
|
|
this.menu = new ve.ui.MenuWidget();
|
|
this.$icon = this.$$( '<div class="ve-ui-dropdownTool-icon ve-ui-icon-down"></div>' );
|
|
this.$label = this.$$( '<div class="ve-ui-dropdownTool-label"></div>' );
|
|
this.$labelText = this.$$( '<span> </span>' );
|
|
|
|
// Events
|
|
this.$$( document )
|
|
.add( this.toolbar.getSurface().getView().$ )
|
|
.mousedown( ve.bind( this.onBlur, this ) );
|
|
this.$.on( {
|
|
'mousedown': ve.bind( this.onMouseDown, this ),
|
|
'mouseup': ve.bind( this.onMouseUp, this )
|
|
} );
|
|
this.menu.on( 'select', ve.bind( this.onMenuItemSelect, this ) );
|
|
|
|
// Initialization
|
|
this.$
|
|
.append( this.$icon, this.$label, this.menu.$ )
|
|
.addClass( 've-ui-dropdownTool ve-ui-dropdownTool-' + this.constructor.static.name )
|
|
.attr( 'title', ve.msg( this.constructor.static.titleMessage ) );
|
|
this.$label.append( this.$labelText );
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
ve.inheritClass( ve.ui.DropdownTool, ve.ui.Tool );
|
|
|
|
/* Methods */
|
|
|
|
/**
|
|
* Handle the mouse button being pressed.
|
|
*
|
|
* @method
|
|
* @param {jQuery.Event} e Mouse down event
|
|
*/
|
|
ve.ui.DropdownTool.prototype.onMouseDown = function ( e ) {
|
|
if ( e.which === 1 ) {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Handle the mouse button being released.
|
|
*
|
|
* @method
|
|
* @param {jQuery.Event} e Mouse up event
|
|
*/
|
|
ve.ui.DropdownTool.prototype.onMouseUp = function ( e ) {
|
|
if ( e.which === 1 ) {
|
|
// Toggle menu
|
|
if ( this.menu.isVisible() ) {
|
|
this.menu.hide();
|
|
} else {
|
|
this.menu.show();
|
|
}
|
|
}
|
|
return false;
|
|
};
|
|
|
|
/**
|
|
* Handle focus being lost.
|
|
*
|
|
* The event is actually generated from a mousedown on an element outside the menu, so it is not
|
|
* a normal blur event object.
|
|
*
|
|
* @method
|
|
* @param {jQuery.Event} e Mouse down event
|
|
*/
|
|
ve.ui.DropdownTool.prototype.onBlur = function ( e ) {
|
|
if ( e.which === 1 ) {
|
|
this.menu.hide();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Handle one of the items in the menu being selected.
|
|
*
|
|
* This should not be overridden in subclasses, it simple connects events from the internal menu
|
|
* to the onSelect method.
|
|
*
|
|
* @method
|
|
* @param {ve.ui.MenuItemWidget|null} item Selected menu item, null if none is selected
|
|
*/
|
|
ve.ui.DropdownTool.prototype.onMenuItemSelect = function ( item ) {
|
|
if ( this.toolbar.getSurface().isEnabled() ) {
|
|
this.setLabel( item && item.label );
|
|
this.onSelect( item );
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Handle dropdown option being selected.
|
|
*
|
|
* This is an abstract method that must be overridden in a concrete subclass.
|
|
*
|
|
* @abstract
|
|
* @method
|
|
* @param {ve.ui.MenuItemWidget} item Menu item
|
|
*/
|
|
ve.ui.DropdownTool.prototype.onSelect = function () {
|
|
throw new Error(
|
|
've.ui.DropdownTool.onSelect not implemented in this subclass:' + this.constructor
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Handle toolbar state being cleared.
|
|
*
|
|
* @method
|
|
*/
|
|
ve.ui.DropdownTool.prototype.onClearState = function () {
|
|
this.setLabel();
|
|
};
|
|
|
|
/**
|
|
* Set the label.
|
|
*
|
|
* If the label value is empty, undefined or only contains whitespace an empty label will be used.
|
|
*
|
|
* @method
|
|
* @param {jQuery|string} [value] Label text
|
|
* @chainable
|
|
*/
|
|
ve.ui.DropdownTool.prototype.setLabel = function ( value ) {
|
|
if ( typeof value === 'string' && value.length && /[^\s]*/.test( value ) ) {
|
|
this.$labelText.text( value );
|
|
} else if ( value instanceof jQuery ) {
|
|
this.$labelText.empty().append( value );
|
|
} else {
|
|
this.$labelText.html( ' ' );
|
|
}
|
|
return this;
|
|
};
|