2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor user interface DropdownTool class.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2011-12-05 21:10:19 +00:00
|
|
|
/**
|
2012-02-06 23:50:56 +00:00
|
|
|
* Creates an ve.ui.DropdownTool object.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-12-05 21:10:19 +00:00
|
|
|
* @class
|
|
|
|
* @constructor
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.ui.Toolbar} toolbar
|
2011-12-05 21:10:19 +00:00
|
|
|
* @param {String} name
|
|
|
|
* @param {Object[]} items
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.DropdownTool = function( toolbar, name, title, items ) {
|
2011-12-04 02:59:53 +00:00
|
|
|
// Inheritance
|
2012-02-06 23:50:56 +00:00
|
|
|
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;
|
2012-02-06 23:50:56 +00:00
|
|
|
this.menuView = new ve.ui.Menu( items, function( item ) {
|
2011-12-04 02:59:53 +00:00
|
|
|
_this.onSelect( item );
|
2011-12-06 00:10:30 +00:00
|
|
|
_this.$label.text( item.label );
|
|
|
|
}, this.$ );
|
2012-07-25 00:47:40 +00:00
|
|
|
this.$icon = $( '<div class="es-toolbarDropdownTool-icon"></div>' ).appendTo( this.$ );
|
2012-04-19 22:31:09 +00:00
|
|
|
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 ) {
|
2011-12-06 00:10:30 +00:00
|
|
|
if ( e.which === 1 ) {
|
2011-12-09 01:28:44 +00:00
|
|
|
_this.menuView.close();
|
2011-12-05 21:10:19 +00:00
|
|
|
}
|
|
|
|
} );
|
2011-12-06 00:10:30 +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 ) {
|
2011-12-09 01:28:44 +00:00
|
|
|
_this.menuView.open();
|
2011-12-09 19:07:06 +00:00
|
|
|
} else {
|
|
|
|
_this.menuView.close();
|
2011-12-06 00:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
2011-12-04 02:59:53 +00:00
|
|
|
|
|
|
|
// DOM Changes
|
|
|
|
this.$.addClass( 'es-toolbarDropdownTool' ).addClass( 'es-toolbarDropdownTool-' + name );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
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 */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.extendClass( ve.ui.DropdownTool, ve.ui.Tool );
|