mediawiki-extensions-Visual.../modules/ve/ui/tools/ve.ui.DropdownTool.js

64 lines
1.4 KiB
JavaScript
Raw Normal View History

2011-12-05 21:10:19 +00:00
/**
* Creates an ve.ui.DropdownTool object.
2011-12-05 21:10:19 +00:00
*
* @class
* @constructor
* @param {ve.ui.Toolbar} toolbar
2011-12-05 21:10:19 +00:00
* @param {String} name
* @param {Object[]} items
*/
ve.ui.DropdownTool = function( toolbar, name, title, items ) {
2011-12-04 02:59:53 +00:00
// Inheritance
ve.ui.Tool.call( this, toolbar, name, title );
2011-12-04 02:59:53 +00:00
if ( !name ) {
return;
}
2011-12-05 21:10:19 +00:00
2011-12-04 02:59:53 +00:00
// Properties
var _this = this;
this.menuView = new ve.ui.Menu( items, function( item ) {
2011-12-04 02:59:53 +00:00
_this.onSelect( item );
_this.$label.text( item.label );
}, this.$ );
this.$label = $( '<div class="es-toolbarDropdownTool-label"></div>' ).appendTo( this.$ );
2011-12-04 02:59:53 +00:00
2011-12-05 21:10:19 +00:00
// Events
$( document )
.add( this.toolbar.surfaceView.$ )
.mousedown( function( e ) {
if ( e.which === 1 ) {
_this.menuView.close();
2011-12-05 21:10:19 +00:00
}
} );
this.$.bind( {
'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( '.es-menuView' );
if ( e.which === 1 && $item.length === 0 ) {
_this.menuView.open();
} else {
_this.menuView.close();
}
}
} );
2011-12-04 02:59:53 +00:00
// DOM Changes
this.$.addClass( 'es-toolbarDropdownTool' ).addClass( 'es-toolbarDropdownTool-' + name );
};
/* Methods */
ve.ui.DropdownTool.prototype.onSelect = function( item ) {
2011-12-04 02:59:53 +00:00
throw 'DropdownTool.onSelect not implemented in this subclass:' + this.constructor;
};
/* Inheritance */
ve.extendClass( ve.ui.DropdownTool, ve.ui.Tool );