mediawiki-extensions-Visual.../modules/ve-mw/ui/widgets/ve.ui.MWTocItemWidget.js
Timo Tijhof 2b7ae8c175 Remove use of bind() for inline functions
Follows-up b0e8900, 447e8de, d3f26e65.

Contrary to binding a reference, this-overiddes for inline
functions gets messy and fragile. There's also a minor
performance overhead that can be avoided. No separation of
concerns, eiter, as the object is held by the closure.

Also:
* Remove left-over instances of @method.
* Remove redundant '0' argument to setTimeout.
* Use null instead of $ for apply() on static methods like $.when.
* Use .empty().append() instead of .html(). Does the same but
  without additional overhead and without confusion association
  with "html" which isn't used (it doesn't serialise and re-parse
  or clone, it just appends when given one or more nodes).

Change-Id: I797a3667c67d52568150be9be5d043d149f22077
2014-12-16 23:27:53 +00:00

90 lines
2.3 KiB
JavaScript

/*!
* VisualEditor UserInterface MWTocItemWidget class.
*
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Creates an item an item for the MWTocWidget
*
* @class
* @extends OO.ui.Widget
* @mixins OO.ui.GroupElement
*
* @constructor
* @param {Object} config TOC Item configuration
* @cfg {ve.ce.Node} node ContentEditable node
* @cfg {ve.ui.MWTocItemWidget} parent Parent toc item
* @cfg {string} sectionPrefix TOC item section number
* @cfg {number} tocLevel Depth level of the TOC item
* @cfg {number} tocIndex Running count of TOC items
*
*/
ve.ui.MWTocItemWidget = function VeCeMWTocItemWidget( config ) {
// Parent constructor
OO.ui.Widget.call( this, config );
// Mixin Constructor
OO.ui.GroupElement.call( this, $.extend( {}, config, { $group: this.$( '<ul>' ) } ) );
config = config || {};
// Properties
this.node = config.node || null;
this.parent = config.parent;
this.sectionPrefix = config.sectionPrefix;
this.tocLevel = config.tocLevel;
this.tocIndex = config.tocIndex;
// Allows toc items to be optionally associated to a node.
// For the case of the zero level parent item.
if ( this.node ) {
this.$tocNumber = this.$( '<span>' ).addClass( 'tocnumber' )
.text( this.sectionPrefix );
this.$tocText = this.$( '<span>' ).addClass( 'toctext' )
.text( this.node.$element.text() );
this.$element
.addClass( 'toclevel-' + this.tocLevel )
.addClass( 'tocsection-' + this.tocIndex )
.append( this.$( '<a>' ).append( this.$tocNumber, this.$tocText ) );
// Monitor node events
this.node.model.connect( this, { update: 'onUpdate' } );
}
this.$element.append( this.$group );
};
/* Inheritance */
OO.inheritClass( ve.ui.MWTocItemWidget, OO.ui.Widget );
OO.mixinClass( ve.ui.MWTocItemWidget, OO.ui.GroupElement );
/* Static Properties */
ve.ui.MWTocItemWidget.static.tagName = 'li';
/* Methods */
/**
* Updates the text of the toc item
*
*/
ve.ui.MWTocItemWidget.prototype.onUpdate = function () {
var widget = this;
// Timeout needed to let the dom element actually update
setTimeout( function () {
widget.$tocText.text( widget.node.$element.text() );
} );
};
/**
* Removes this toc item from its parent
*
*/
ve.ui.MWTocItemWidget.prototype.remove = function () {
this.node.model.disconnect( this );
this.parent.removeItems( [this] );
};