mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-25 23:05:35 +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
193 lines
4.1 KiB
JavaScript
193 lines
4.1 KiB
JavaScript
/*!
|
|
* VisualEditor UserInterface FormatDropdownTool class.
|
|
*
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* UserInterface format dropdown tool.
|
|
*
|
|
* @class
|
|
* @extends ve.ui.DropdownTool
|
|
* @constructor
|
|
* @param {ve.ui.Toolbar} toolbar
|
|
* @param {Object} [config] Config options
|
|
*/
|
|
ve.ui.FormatDropdownTool = function VeUiFormatDropdownTool( toolbar, config ) {
|
|
var i, len, item,
|
|
items = this.constructor.static.items.slice( 0 );
|
|
|
|
// Parent constructor
|
|
ve.ui.DropdownTool.call( this, toolbar, config );
|
|
|
|
// Initialization
|
|
for ( i = 0, len = items.length; i < len; i++ ) {
|
|
item = items[i];
|
|
items[i] = new ve.ui.MenuItemWidget( ve.msg( item.label ), item.data, item.options );
|
|
}
|
|
this.menu.addItems( items );
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
ve.inheritClass( ve.ui.FormatDropdownTool, ve.ui.DropdownTool );
|
|
|
|
/* Static Properties */
|
|
|
|
ve.ui.FormatDropdownTool.static.name = 'format';
|
|
|
|
ve.ui.FormatDropdownTool.static.titleMessage = 'visualeditor-formatdropdown-title';
|
|
|
|
/**
|
|
* Options given to ve.ui.DropdownTool.
|
|
*
|
|
* @static
|
|
* @property
|
|
* @type {Object[]}
|
|
*/
|
|
ve.ui.FormatDropdownTool.static.items = [
|
|
{
|
|
'label': 'visualeditor-formatdropdown-format-paragraph',
|
|
'data': {
|
|
'type' : 'paragraph'
|
|
},
|
|
'options': {
|
|
'rel': 'paragraph'
|
|
}
|
|
},
|
|
{
|
|
'label': 'visualeditor-formatdropdown-format-heading1',
|
|
'data': {
|
|
'type' : 'heading',
|
|
'attributes': { 'level': 1 }
|
|
},
|
|
'options': {
|
|
'rel': 'heading-1'
|
|
}
|
|
},
|
|
{
|
|
'label': 'visualeditor-formatdropdown-format-heading2',
|
|
'data': {
|
|
'type' : 'heading',
|
|
'attributes': { 'level': 2 }
|
|
},
|
|
'options': {
|
|
'rel': 'heading-2'
|
|
}
|
|
},
|
|
{
|
|
'label': 'visualeditor-formatdropdown-format-heading3',
|
|
'data': {
|
|
'type' : 'heading',
|
|
'attributes': { 'level': 3 }
|
|
},
|
|
'options': {
|
|
'rel': 'heading-3'
|
|
}
|
|
},
|
|
{
|
|
'label': 'visualeditor-formatdropdown-format-heading4',
|
|
'data': {
|
|
'type' : 'heading',
|
|
'attributes': { 'level': 4 }
|
|
},
|
|
'options': {
|
|
'rel': 'heading-4'
|
|
}
|
|
},
|
|
{
|
|
'label': 'visualeditor-formatdropdown-format-heading5',
|
|
'data': {
|
|
'type' : 'heading',
|
|
'attributes': { 'level': 5 }
|
|
},
|
|
'options': {
|
|
'rel': 'heading-5'
|
|
}
|
|
},
|
|
{
|
|
'label': 'visualeditor-formatdropdown-format-heading6',
|
|
'data': {
|
|
'type' : 'heading',
|
|
'attributes': { 'level': 6 }
|
|
},
|
|
'options': {
|
|
'rel': 'heading-6'
|
|
}
|
|
},
|
|
{
|
|
'label': 'visualeditor-formatdropdown-format-preformatted',
|
|
'data': {
|
|
'type' : 'preformatted'
|
|
},
|
|
'options': {
|
|
'rel': 'preformatted'
|
|
}
|
|
}
|
|
];
|
|
|
|
/* Methods */
|
|
|
|
/**
|
|
* Handle dropdown option being selected.
|
|
*
|
|
* @method
|
|
* @param {ve.ui.MenuItemWidget} item Menu item
|
|
*/
|
|
ve.ui.FormatDropdownTool.prototype.onSelect = function ( item ) {
|
|
var data;
|
|
|
|
if ( item ) {
|
|
data = item.getData();
|
|
this.toolbar.getSurface().execute( 'format', 'convert', data.type, data.attributes );
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Handle the toolbar state being updated.
|
|
*
|
|
* @method
|
|
* @param {ve.dm.Node[]} nodes List of nodes covered by the current selection
|
|
* @param {ve.AnnotationSet} full Annotations that cover all of the current selection
|
|
* @param {ve.AnnotationSet} partial Annotations that cover some or all of the current selection
|
|
*/
|
|
ve.ui.FormatDropdownTool.prototype.onUpdateState = function ( nodes ) {
|
|
var i, nodesLength, node, j, itemsLength, item, match,
|
|
items = this.menu.getItems();
|
|
|
|
nodeLoop:
|
|
for ( i = 0, nodesLength = nodes.length; i < nodesLength; i++ ) {
|
|
node = nodes[i];
|
|
if ( !node.canHaveChildren() ) {
|
|
node = node.getParent();
|
|
}
|
|
if ( node ) {
|
|
for ( j = 0, itemsLength = items.length; j < itemsLength; j++ ) {
|
|
item = items[j];
|
|
if ( item.data.type === node.getType() ) {
|
|
if ( item.data.attributes && !node.hasAttributes( item.data.attributes ) ) {
|
|
continue;
|
|
}
|
|
if ( match ) {
|
|
match = null;
|
|
break nodeLoop;
|
|
}
|
|
match = item;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if ( match ) {
|
|
this.setLabel( match.label );
|
|
this.menu.setSelectedItem( match );
|
|
} else {
|
|
this.setLabel();
|
|
this.menu.setSelectedItem( null );
|
|
}
|
|
};
|
|
|
|
/* Registration */
|
|
|
|
ve.ui.toolFactory.register( 'format', ve.ui.FormatDropdownTool );
|