mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-15 18:39:52 +00:00
71020f7175
[jshint] ce/ve.ce.Surface.js: line 670, col 9, Too many var statements. ce/ve.ce.Surface.js: line 695, col 6, Missing semicolon. ce/ve.ce.Surface.js: line 726, col 22, Expected '===' and instead saw '=='. ce/ve.ce.Surface.js: line 726, col 41, Expected '===' and instead saw '=='. ce/ve.ce.Surface.js: line 733, col 13, Too many var statements. ce/ve.ce.Surface.js: line 734, col 24, Expected '===' and instead saw '=='. ce/ve.ce.Surface.js: line 1013, col 13, Too many var statements. ce/ve.ce.Surface.js: line 1019, col 17, Too many var statements. ce/ve.ce.Surface.js: line 1023, col 18, Too many ar statements. ce/ve.ce.Surface.js: line 1027, col 13, Too many var statements. dm/annotations/ve.dm.LinkAnnotation.js: line 70, col 52, Insecure '.'. dm/ve.dm.Converter.js: line 383, col 29, Empty block. dm/ve.dm.Converter.js: line 423, col 33, Empty block. Commands: * jshint . * ack '(if|else|function|switch|for|while)\(' * Sublime Text 2: Find(*): (if|else|function|switch|for|while)\( Replace: $1 ( * ack ' ' -Q # double spaces, except in certain comments Change-Id: I8e34bf2924bc8688fdf8acef08bbc4f6707e93be
142 lines
3.4 KiB
JavaScript
142 lines
3.4 KiB
JavaScript
/**
|
|
* VisualEditor user interface Toolbar class.
|
|
*
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* Editing toolbar.
|
|
*
|
|
* @class
|
|
* @constructor
|
|
*/
|
|
ve.ui.Toolbar = function ( $container, surfaceView, config ) {
|
|
// Inheritance TODO: Do we still need it?
|
|
ve.EventEmitter.call( this );
|
|
if ( !surfaceView ) {
|
|
return;
|
|
}
|
|
|
|
// Properties
|
|
this.surfaceView = surfaceView;
|
|
this.$ = $container;
|
|
this.$groups = $( '<div class="ve-ui-toolbarGroups"></div>' );
|
|
this.tools = [];
|
|
this.config = config || [
|
|
{ 'name': 'history', 'items' : ['undo', 'redo'] },
|
|
{ 'name': 'textStyle', 'items' : ['format'] },
|
|
{ 'name': 'textStyle', 'items' : ['bold', 'italic', 'link', 'clear'] },
|
|
{ 'name': 'list', 'items' : ['number', 'bullet', 'outdent', 'indent'] }
|
|
];
|
|
|
|
// DOM Changes
|
|
this.$.prepend( this.$groups );
|
|
|
|
// Events
|
|
this.surfaceView.model.on( 'change', ve.bind( this.updateTools, this ) );
|
|
|
|
// Initialization
|
|
this.setup();
|
|
};
|
|
|
|
/* Methods */
|
|
|
|
/**
|
|
* Triggers update events on all tools.
|
|
*
|
|
* @method
|
|
*/
|
|
ve.ui.Toolbar.prototype.updateTools = function () {
|
|
var model = this.surfaceView.getModel(),
|
|
doc = model.getDocument(),
|
|
annotations,
|
|
nodes = [],
|
|
range = model.getSelection(),
|
|
startNode,
|
|
endNode,
|
|
tool = this,
|
|
i;
|
|
|
|
if ( range !== null ) {
|
|
if ( range.from === range.to ) {
|
|
nodes.push( doc.getNodeFromOffset( range.from ) );
|
|
} else {
|
|
startNode = doc.getNodeFromOffset( range.from );
|
|
endNode = doc.getNodeFromOffset ( range.end );
|
|
|
|
if ( startNode.type === 'document' || endNode.type === 'document' ) {
|
|
// Clear state
|
|
for ( i = 0; i < this.tools.length; i++ ) {
|
|
this.tools[i].clearState();
|
|
}
|
|
return;
|
|
}
|
|
|
|
// These should be different, alas just in case.
|
|
if ( startNode === endNode ) {
|
|
nodes.push( startNode );
|
|
} else {
|
|
model.getDocument().getDocumentNode().traverseLeafNodes( function ( node ) {
|
|
nodes.push( node );
|
|
if ( node === endNode ) {
|
|
return false;
|
|
}
|
|
}, startNode );
|
|
}
|
|
}
|
|
|
|
if ( range.getLength() > 0 ) {
|
|
annotations = doc.getAnnotationsFromRange( range );
|
|
} else {
|
|
// Clear context
|
|
tool.surfaceView.contextView.clear();
|
|
annotations = doc.getAnnotationsFromOffset(
|
|
doc.getNearestContentOffset( range.start - 1 )
|
|
);
|
|
}
|
|
// Update state
|
|
for ( i = 0; i < this.tools.length; i++ ) {
|
|
this.tools[i].updateState( annotations, nodes );
|
|
}
|
|
} else {
|
|
// Clear state
|
|
for ( i = 0; i < this.tools.length; i++ ) {
|
|
this.tools[i].clearState();
|
|
}
|
|
}
|
|
};
|
|
|
|
ve.ui.Toolbar.prototype.getSurfaceView = function () {
|
|
return this.surfaceView;
|
|
};
|
|
|
|
ve.ui.Toolbar.prototype.setup = function () {
|
|
var i, j, $group, tool, toolDefintion;
|
|
for ( i = 0; i < this.config.length; i++ ) {
|
|
$group = $( '<div class="ve-ui-toolbarGroup"></div>' )
|
|
.addClass( 've-ui-toolbarGroup-' + this.config[i].name );
|
|
if ( this.config[i].label ) {
|
|
$group.append(
|
|
$( '<div class="ve-ui-toolbarLabel"></div>' ).html( this.config[i].label )
|
|
);
|
|
}
|
|
|
|
for ( j = 0; j < this.config[i].items.length; j++ ) {
|
|
toolDefintion = ve.ui.Tool.tools[ this.config[i].items[j] ];
|
|
if ( toolDefintion ) {
|
|
tool = new toolDefintion.constructor(
|
|
this, toolDefintion.name, toolDefintion.title, toolDefintion.data
|
|
);
|
|
this.tools.push( tool );
|
|
$group.append( tool.$ );
|
|
}
|
|
}
|
|
|
|
this.$groups.append( $group );
|
|
}
|
|
|
|
};
|
|
|
|
ve.extendClass( ve.ui.Toolbar, ve.EventEmitter );
|