2014-01-09 01:32:13 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWTocItemWidget class.
|
|
|
|
*
|
2015-01-08 23:54:03 +00:00
|
|
|
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
|
2014-01-09 01:32:13 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an item an item for the MWTocWidget
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.ui.Widget
|
2015-06-05 17:59:00 +00:00
|
|
|
* @mixins OO.ui.mixin.GroupElement
|
2014-01-09 01:32:13 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
*/
|
2014-05-15 16:12:43 +00:00
|
|
|
ve.ui.MWTocItemWidget = function VeCeMWTocItemWidget( config ) {
|
2014-01-09 01:32:13 +00:00
|
|
|
// Parent constructor
|
|
|
|
OO.ui.Widget.call( this, config );
|
|
|
|
|
|
|
|
// Mixin Constructor
|
2015-06-05 17:59:00 +00:00
|
|
|
OO.ui.mixin.GroupElement.call( this, $.extend( {}, config, { $group: $( '<ul>' ) } ) );
|
2014-01-09 01:32:13 +00:00
|
|
|
|
|
|
|
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 ) {
|
2015-04-09 23:47:15 +00:00
|
|
|
this.$tocNumber = $( '<span>' ).addClass( 'tocnumber' )
|
2014-01-09 01:32:13 +00:00
|
|
|
.text( this.sectionPrefix );
|
2015-04-09 23:47:15 +00:00
|
|
|
this.$tocText = $( '<span>' ).addClass( 'toctext' )
|
2014-01-09 01:32:13 +00:00
|
|
|
.text( this.node.$element.text() );
|
|
|
|
this.$element
|
|
|
|
.addClass( 'toclevel-' + this.tocLevel )
|
|
|
|
.addClass( 'tocsection-' + this.tocIndex )
|
2015-04-09 23:47:15 +00:00
|
|
|
.append( $( '<a>' ).append( this.$tocNumber, this.$tocText ) );
|
2014-01-09 01:32:13 +00:00
|
|
|
|
|
|
|
// Monitor node events
|
2014-08-22 20:50:48 +00:00
|
|
|
this.node.model.connect( this, { update: 'onUpdate' } );
|
2014-01-09 01:32:13 +00:00
|
|
|
}
|
|
|
|
this.$element.append( this.$group );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWTocItemWidget, OO.ui.Widget );
|
|
|
|
|
2015-06-05 17:59:00 +00:00
|
|
|
OO.mixinClass( ve.ui.MWTocItemWidget, OO.ui.mixin.GroupElement );
|
2014-01-09 01:32:13 +00:00
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
ve.ui.MWTocItemWidget.static.tagName = 'li';
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the text of the toc item
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
ve.ui.MWTocItemWidget.prototype.onUpdate = function () {
|
2014-12-16 21:14:01 +00:00
|
|
|
var widget = this;
|
2014-01-09 01:32:13 +00:00
|
|
|
// Timeout needed to let the dom element actually update
|
2014-07-08 22:33:32 +00:00
|
|
|
setTimeout( function () {
|
2014-12-16 21:14:01 +00:00
|
|
|
widget.$tocText.text( widget.node.$element.text() );
|
|
|
|
} );
|
2014-01-09 01:32:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes this toc item from its parent
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
ve.ui.MWTocItemWidget.prototype.remove = function () {
|
|
|
|
this.node.model.disconnect( this );
|
|
|
|
this.parent.removeItems( [this] );
|
|
|
|
};
|