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

81 lines
1.9 KiB
JavaScript
Raw Normal View History

/**
* VisualEditor user interface HistoryButtonTool class.
*
* @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
/**
* Creates an ve.ui.HistoryButtonTool object.
*
2011-12-05 21:10:19 +00:00
* @class
* @constructor
* @extends {ve.ui.ButtonTool}
* @param {ve.ui.Toolbar} toolbar
2011-12-05 21:10:19 +00:00
* @param {String} name
*/
ve.ui.HistoryButtonTool = function ( toolbar, name, title, data ) {
2011-12-05 21:10:19 +00:00
// Inheritance
ve.ui.ButtonTool.call( this, toolbar, name, title );
2011-12-05 21:10:19 +00:00
// Properties
2011-12-04 02:59:53 +00:00
this.data = data;
this.enabled = false;
Refactor ve.js utilities and improve documentation Refactor: * ve.indexOf Renamed from ve.inArray. This was named after the jQuery method which in turn has a longer story about why it is so unfortunately named. It doesn't return a boolean, but an index. Hence the native method being called indexOf as well. * ve.bind Renamed from ve.proxy. I considered making it use Function.prototype.bind if available. As it performs better than $.proxy (which doesn't use to the native bind if available). However since bind needs to be bound itself in order to use it detached, it turns out with the "call()" and "bind()" it is slower than the $.proxy shim: http://jsperf.com/function-bind-shim-perf It would've been like this: ve.bind = Function.prototype.bind ? Function.prototype.call.bind( Function.prototype.bind ) : $.proxy; But instead sticking to ve.bind = $.proxy; * ve.extendObject Documented the parts of jQuery.extend that we use. This makes it easier to replace in the future. Documentation: * Added function documentation blocks. * Added annotations to functions that we will be able to remove in the future in favour of the native methods. With "@until + when/how". In this case "ES5". Meaning, whenever we drop support for browsers that don't support ES5. Although in the developer community ES5 is still fairly fresh, browsers have been aware for it long enough that thee moment we're able to drop it may be sooner than we think. The only blocker so far is IE8. The rest of the browsers have had it long enough that the traffic we need to support of non-IE supports it. Misc.: * Removed 'node: true' from .jshintrc since Parsoid is no longer in this repo and thus no more nodejs files. - This unraveled two lint errors: Usage of 'module' and 'console'. (both were considered 'safe globals' due to nodejs, but not in browser code). * Replaced usage (before renaming): - $.inArray -> ve.inArray - Function.prototype.bind -> ve.proxy - Array.isArray -> ve.isArray - [].indexOf -> ve.inArray - $.fn.bind/live/delegate/unbind/die/delegate -> $.fn.on/off Change-Id: Idcf1fa6a685b6ed3d7c99ffe17bd57a7bc586a2c
2012-08-11 08:14:56 +00:00
this.toolbar.getSurfaceView().model.on( 'history', ve.bind( this.updateState, this ) );
2011-12-04 02:59:53 +00:00
};
2011-12-05 21:10:19 +00:00
/* Methods */
ve.ui.HistoryButtonTool.prototype.onClick = function () {
2011-12-04 02:59:53 +00:00
switch ( this.name ) {
case 'undo':
case 'redo':
if ( this.isButtonEnabled( this.name ) ) {
var surfaceView = this.toolbar.getSurfaceView();
surfaceView.stopPolling();
surfaceView.showSelection(
surfaceView.getModel()[this.name]( 1 ) || surfaceView.model.selection
);
surfaceView.clearPollData();
surfaceView.startPolling();
}
2011-12-04 02:59:53 +00:00
break;
}
};
ve.ui.HistoryButtonTool.prototype.updateState = function ( annotations ) {
this.enabled = this.isButtonEnabled( this.name );
this.updateEnabled();
};
ve.ui.HistoryButtonTool.prototype.isButtonEnabled = function ( name ) {
var surfaceModel = this.toolbar.getSurfaceView().getModel();
switch( name ) {
case 'undo':
return surfaceModel.bigStack.length - surfaceModel.undoIndex > 0;
case 'redo':
return surfaceModel.undoIndex > 0;
default:
return false;
}
};
2011-12-05 21:10:19 +00:00
/* Registration */
ve.ui.Tool.tools.undo = {
'constructor': ve.ui.HistoryButtonTool,
'name': 'undo',
'title': ve.msg( 'visualeditor-historybutton-undo-tooltip' )
2011-12-04 02:59:53 +00:00
};
ve.ui.Tool.tools.redo = {
'constructor': ve.ui.HistoryButtonTool,
'name': 'redo',
'title': ve.msg( 'visualeditor-historybutton-redo-tooltip' )
2011-12-04 02:59:53 +00:00
};
2011-12-05 21:10:19 +00:00
/* Inhertiance */
2011-12-04 02:59:53 +00:00
ve.extendClass( ve.ui.HistoryButtonTool, ve.ui.ButtonTool );