mediawiki-extensions-Visual.../modules/ve/ui/tools/ve.ui.DropdownTool.js
Trevor Parscal d208ca9002 Cleaned up uses of jQuery to create and appendTo/prependTo
* Separated DOM changes from creation of elements
* Always using parsing for element creation with known attributes
* Always using attr or addClass for variable attributes

Change-Id: Id101f56594014786892d382d06c658f416224a9c
2012-08-29 18:43:48 +02:00

76 lines
1.8 KiB
JavaScript

/**
* VisualEditor user interface DropdownTool class.
*
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Creates an ve.ui.DropdownTool object.
*
* @class
* @constructor
* @param {ve.ui.Toolbar} toolbar
* @param {String} name
* @param {Object[]} items
*/
ve.ui.DropdownTool = function ( toolbar, name, title, items ) {
// Inheritance
ve.ui.Tool.call( this, toolbar, name, title );
if ( !name ) {
return;
}
// Properties
var tool = this;
this.menuView = new ve.ui.Menu( items, function ( item ) {
tool.onSelect( item );
tool.$labelText.text( item.label );
}, this.$ );
this.$icon = $( '<div class="ve-ui-toolbarDropdownTool-icon"></div>' );
this.$label = $( '<div class="ve-ui-toolbarDropdownTool-label"></div>' );
this.$labelText = $( '<span>&nbsp;</span>' );
// Events
$( document )
.add( this.toolbar.surfaceView.$ )
.mousedown( function ( e ) {
if ( e.which === 1 ) {
tool.menuView.close();
}
} );
this.$.on( {
'mousedown': function ( e ) {
if ( e.which === 1 ) {
e.preventDefault();
return false;
}
},
'mouseup': function ( e ) {
// Don't respond to menu clicks
var $item = $( e.target ).closest( '.ve-ui-menu' );
if ( e.which === 1 && $item.length === 0 ) {
tool.menuView.open();
} else {
tool.menuView.close();
}
}
} );
// DOM Changes
this.$
.append( this.$icon, this.$label )
.addClass( 've-ui-toolbarDropdownTool ve-ui-toolbarDropdownTool-' + name );
this.$label.append( this.$labelText );
};
/* Methods */
ve.ui.DropdownTool.prototype.onSelect = function ( item ) {
throw new Error( 'DropdownTool.onSelect not implemented in this subclass:' + this.constructor );
};
/* Inheritance */
ve.extendClass( ve.ui.DropdownTool, ve.ui.Tool );