2014-01-09 01:32:13 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWTocWidget 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 a ve.ui.MWTocWidget object.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.ui.Widget
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.ui.Surface} surface
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
2014-05-15 16:12:43 +00:00
|
|
|
ve.ui.MWTocWidget = function VeUiMWTocWidget( surface, config ) {
|
2014-12-16 21:14:01 +00:00
|
|
|
var widget = this;
|
|
|
|
|
2014-01-09 01:32:13 +00:00
|
|
|
// Parent constructor
|
|
|
|
OO.ui.Widget.call( this, config );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.surface = surface;
|
|
|
|
this.doc = surface.getModel().getDocument();
|
|
|
|
this.metaList = surface.getModel().metaList;
|
|
|
|
// Topic level 0 lives inside of a toc item
|
|
|
|
this.topics = new ve.ui.MWTocItemWidget();
|
|
|
|
// Place for a cloned previous toc to live while rebuilding.
|
2015-04-09 23:47:15 +00:00
|
|
|
this.$tempTopics = $( '<ul>' );
|
2014-01-09 01:32:13 +00:00
|
|
|
// Section keyed item map
|
|
|
|
this.items = {};
|
|
|
|
this.initialized = false;
|
|
|
|
// Page settings cache
|
|
|
|
this.mwTOCForce = false;
|
|
|
|
this.mwTOCDisable = false;
|
|
|
|
|
|
|
|
// TODO: fix i18n
|
2015-02-06 21:38:00 +00:00
|
|
|
this.tocToggle = {
|
2014-08-22 20:50:48 +00:00
|
|
|
hideMsg: ve.msg( 'hidetoc' ),
|
|
|
|
showMsg: ve.msg( 'showtoc' ),
|
2015-04-09 23:47:15 +00:00
|
|
|
$link: $( '<a class="internal" id="togglelink"></a>' ).text( ve.msg( 'hidetoc' ) ),
|
2014-08-22 20:50:48 +00:00
|
|
|
open: true
|
2014-01-09 01:32:13 +00:00
|
|
|
};
|
|
|
|
this.$element.addClass( 'toc ve-ui-mwTocWidget' ).append(
|
2015-04-09 23:47:15 +00:00
|
|
|
$( '<div>' ).attr( 'id', 'toctitle' ).append(
|
|
|
|
$( '<h2>' ).text( ve.msg( 'toc' ) ),
|
|
|
|
$( '<span>' ).addClass( 'toctoggle' ).append( this.tocToggle.$link )
|
2014-01-09 01:32:13 +00:00
|
|
|
),
|
|
|
|
this.topics.$group, this.$tempTopics
|
|
|
|
);
|
|
|
|
// Place in bodyContent element, which is close to where the TOC normally lives in the dom
|
|
|
|
// Integration ignores hiding the TOC widget, though continues to hide the real page TOC
|
|
|
|
$( '#bodyContent' ).append( this.$element );
|
|
|
|
|
2015-02-06 21:38:00 +00:00
|
|
|
this.tocToggle.$link.on( 'click', function () {
|
|
|
|
if ( widget.tocToggle.open ) {
|
|
|
|
widget.tocToggle.$link.text( widget.tocToggle.showMsg );
|
|
|
|
widget.tocToggle.open = false;
|
2014-01-09 01:32:13 +00:00
|
|
|
} else {
|
2015-02-06 21:38:00 +00:00
|
|
|
widget.tocToggle.$link.text( widget.tocToggle.hideMsg );
|
|
|
|
widget.tocToggle.open = true;
|
2014-01-09 01:32:13 +00:00
|
|
|
}
|
2015-01-29 23:09:47 +00:00
|
|
|
// FIXME: We should really use CSS here
|
2014-12-16 21:14:01 +00:00
|
|
|
widget.topics.$group.add( widget.$tempTopics ).slideToggle();
|
|
|
|
} );
|
2014-01-09 01:32:13 +00:00
|
|
|
|
|
|
|
this.metaList.connect( this, {
|
2014-08-22 20:50:48 +00:00
|
|
|
insert: 'onMetaListInsert',
|
|
|
|
remove: 'onMetaListRemove'
|
2014-01-09 01:32:13 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
this.initFromMetaList();
|
|
|
|
this.build();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWTocWidget, OO.ui.Widget );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bound to MetaList insert event to set TOC display options
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MetaItem} metaItem
|
|
|
|
*/
|
|
|
|
ve.ui.MWTocWidget.prototype.onMetaListInsert = function ( metaItem ) {
|
|
|
|
// Responsible for adding UI components
|
|
|
|
if ( metaItem instanceof ve.dm.MWTOCForceMetaItem ) {
|
|
|
|
// show
|
|
|
|
this.mwTOCForce = true;
|
|
|
|
} else if ( metaItem instanceof ve.dm.MWTOCDisableMetaItem ) {
|
|
|
|
// hide
|
|
|
|
this.mwTOCDisable = true;
|
|
|
|
}
|
|
|
|
this.hideOrShow();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bound to MetaList insert event to set TOC display options
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MetaItem} metaItem
|
|
|
|
*/
|
|
|
|
ve.ui.MWTocWidget.prototype.onMetaListRemove = function ( metaItem ) {
|
|
|
|
if ( metaItem instanceof ve.dm.MWTOCForceMetaItem ) {
|
|
|
|
this.mwTOCForce = false;
|
|
|
|
} else if ( metaItem instanceof ve.dm.MWTOCDisableMetaItem ) {
|
|
|
|
this.mwTOCDisable = false;
|
|
|
|
}
|
|
|
|
this.hideOrShow();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize TOC based on the presense of magic words
|
|
|
|
*/
|
|
|
|
ve.ui.MWTocWidget.prototype.initFromMetaList = function () {
|
|
|
|
var i = 0,
|
|
|
|
items = this.metaList.getItemsInGroup( 'mwTOC' ),
|
|
|
|
len = items.length;
|
|
|
|
if ( len > 0 ) {
|
|
|
|
for ( ; i < len; i++ ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( items[ i ] instanceof ve.dm.MWTOCForceMetaItem ) {
|
2014-01-09 01:32:13 +00:00
|
|
|
this.mwTOCForce = true;
|
|
|
|
}
|
|
|
|
// Needs testing
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( items[ i ] instanceof ve.dm.MWTOCDisableMetaItem ) {
|
2014-01-09 01:32:13 +00:00
|
|
|
this.mwTOCDisable = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.hideOrShow();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hides or shows the TOC based on page and default settings
|
|
|
|
*/
|
|
|
|
ve.ui.MWTocWidget.prototype.hideOrShow = function () {
|
|
|
|
// In MediaWiki if __FORCETOC__ is anywhere TOC is always displayed
|
|
|
|
// ... Even if there is a __NOTOC__ in the article
|
2015-01-29 23:09:47 +00:00
|
|
|
this.toggle( !this.mwTOCDisable && ( this.mwTOCForce || this.topics.items.length >= 3 ) );
|
2014-01-09 01:32:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rebuild TOC on ve.ce.MWHeadingNode teardown or setup
|
|
|
|
* Rebuilds on both teardown and setup of a node, so rebuild is debounced
|
|
|
|
*/
|
|
|
|
ve.ui.MWTocWidget.prototype.rebuild = ve.debounce( function () {
|
2014-12-16 21:14:01 +00:00
|
|
|
var widget = this;
|
2014-01-09 01:32:13 +00:00
|
|
|
// Only rebuild when initialized
|
|
|
|
if ( this.surface.mwTocWidget.initialized ) {
|
|
|
|
this.$tempTopics.append( this.topics.$group.children().clone() );
|
2014-03-19 22:51:23 +00:00
|
|
|
this.teardownItems();
|
2014-01-09 01:32:13 +00:00
|
|
|
// Build after transactions
|
2014-07-08 22:33:32 +00:00
|
|
|
setTimeout( function () {
|
2014-12-16 21:14:01 +00:00
|
|
|
widget.build();
|
|
|
|
widget.$tempTopics.empty();
|
|
|
|
}, 0 );
|
2014-01-09 01:32:13 +00:00
|
|
|
}
|
|
|
|
}, 0 );
|
|
|
|
|
2014-03-19 22:51:23 +00:00
|
|
|
/**
|
|
|
|
* Teardown all of the TOC items
|
|
|
|
*/
|
|
|
|
ve.ui.MWTocWidget.prototype.teardownItems = function () {
|
|
|
|
var item;
|
|
|
|
for ( item in this.items ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
this.items[ item ].remove();
|
|
|
|
delete this.items[ item ];
|
2014-03-19 22:51:23 +00:00
|
|
|
}
|
|
|
|
this.items = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Teardown the widget and remove it from the dom
|
|
|
|
*/
|
|
|
|
ve.ui.MWTocWidget.prototype.teardown = function () {
|
|
|
|
this.teardownItems();
|
|
|
|
this.$element.remove();
|
|
|
|
};
|
|
|
|
|
2014-01-09 01:32:13 +00:00
|
|
|
/**
|
|
|
|
* Build TOC from mwHeading dm nodes
|
|
|
|
*/
|
|
|
|
ve.ui.MWTocWidget.prototype.build = function () {
|
|
|
|
var nodes = this.doc.selectNodes( new ve.Range( 0, this.doc.getDocumentNode().getLength() ), 'leaves' ),
|
|
|
|
i = 0,
|
|
|
|
headingLevel = 0,
|
|
|
|
previousHeadingNode = null,
|
|
|
|
previousHeadingLevel = 0,
|
|
|
|
parentHeadingLevel = 0,
|
|
|
|
levelSkipped = false,
|
|
|
|
tocNumber = 0,
|
|
|
|
tocLevel = 0,
|
|
|
|
tocSection = 0,
|
|
|
|
tocIndex = 0,
|
|
|
|
sectionPrefix = [],
|
|
|
|
parentSectionArray,
|
|
|
|
key,
|
|
|
|
parent,
|
|
|
|
config,
|
|
|
|
headingOuterRange,
|
|
|
|
ceNode;
|
|
|
|
for ( ; i < nodes.length; i++ ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( nodes[ i ].node.parent === previousHeadingNode ) {
|
2014-01-09 01:32:13 +00:00
|
|
|
// Duplicate heading
|
|
|
|
continue;
|
|
|
|
}
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( nodes[ i ].node.parent.getType() === 'mwHeading' ) {
|
2014-01-09 01:32:13 +00:00
|
|
|
tocIndex++;
|
2015-08-19 17:33:02 +00:00
|
|
|
headingLevel = nodes[ i ].node.parent.getAttribute( 'level' );
|
2014-01-09 01:32:13 +00:00
|
|
|
// MW TOC Generation
|
|
|
|
// The first heading will always be be a zero level topic, even heading levels > 2
|
|
|
|
// If heading level is 1 then it is definitely a zero level topic
|
|
|
|
// If heading level is 2 then it is a zero level topic, unless a child of a 1 level
|
|
|
|
// If heading went up and skipped a number, the following headings of the skipped number are in the same level
|
|
|
|
if ( this.topics.items.length === 0 || headingLevel === 1 || ( headingLevel === 2 && parentHeadingLevel !== 1 ) ) {
|
|
|
|
tocSection++;
|
|
|
|
sectionPrefix = [ tocSection ];
|
|
|
|
tocLevel = 0;
|
|
|
|
// reset t
|
|
|
|
levelSkipped = false;
|
|
|
|
parent = this.topics;
|
|
|
|
parentHeadingLevel = headingLevel;
|
|
|
|
} else {
|
|
|
|
// If previously skipped a level, place this heading in the same level as the previous higher one
|
|
|
|
if ( headingLevel === previousHeadingLevel || headingLevel < previousHeadingLevel && levelSkipped ) {
|
|
|
|
tocNumber++;
|
|
|
|
sectionPrefix.pop();
|
|
|
|
sectionPrefix.push( tocNumber );
|
|
|
|
// Only remove the flag if the heading level has dropped but we skipped to a higher number previously
|
|
|
|
if ( headingLevel < previousHeadingLevel ) {
|
|
|
|
levelSkipped = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tocNumber = 1;
|
|
|
|
// Heading not the same as before
|
|
|
|
if ( headingLevel > previousHeadingLevel ) {
|
|
|
|
// Did we skip a level? Flag in case we drop down a number
|
|
|
|
if ( headingLevel - previousHeadingLevel > 1 ) {
|
|
|
|
levelSkipped = true;
|
|
|
|
}
|
|
|
|
tocLevel++;
|
|
|
|
sectionPrefix.push( tocNumber );
|
|
|
|
// Step to lower level unless we are at 1
|
|
|
|
} else if ( headingLevel < previousHeadingLevel && tocLevel !== 1 ) {
|
|
|
|
tocLevel--;
|
|
|
|
sectionPrefix.pop();
|
2015-08-19 17:33:02 +00:00
|
|
|
tocNumber = sectionPrefix[ sectionPrefix.length - 1 ] + 1;
|
2014-01-09 01:32:13 +00:00
|
|
|
sectionPrefix.pop();
|
|
|
|
sectionPrefix.push( tocNumber );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Determine parent
|
|
|
|
parentSectionArray = sectionPrefix.slice( 0 );
|
|
|
|
parentSectionArray.pop();
|
|
|
|
if ( parentSectionArray.length > 0 ) {
|
|
|
|
key = parentSectionArray.join( '.' );
|
2015-08-19 17:33:02 +00:00
|
|
|
parent = this.items[ key ];
|
2014-01-09 01:32:13 +00:00
|
|
|
} else {
|
|
|
|
// Topic level is zero
|
|
|
|
parent = this.topics;
|
|
|
|
}
|
|
|
|
// TODO: Cleanup config generation, merge local vars into config object
|
|
|
|
// Get CE node for the heading
|
2015-08-19 17:33:02 +00:00
|
|
|
headingOuterRange = nodes[ i ].nodeOuterRange;
|
2014-09-24 18:31:55 +00:00
|
|
|
ceNode = this.surface.getView().getDocument().getBranchNodeFromOffset( headingOuterRange.end );
|
2014-01-09 01:32:13 +00:00
|
|
|
config = {
|
2014-08-22 20:50:48 +00:00
|
|
|
node: ceNode,
|
|
|
|
tocIndex: tocIndex,
|
|
|
|
parent: parent,
|
|
|
|
tocLevel: tocLevel,
|
|
|
|
tocSection: tocSection,
|
|
|
|
sectionPrefix: sectionPrefix.join( '.' ),
|
2015-08-19 17:33:02 +00:00
|
|
|
insertIndex: sectionPrefix[ sectionPrefix.length - 1 ]
|
2014-01-09 01:32:13 +00:00
|
|
|
};
|
|
|
|
// Add item
|
2015-08-19 17:33:02 +00:00
|
|
|
this.items[ sectionPrefix.join( '.' ) ] = new ve.ui.MWTocItemWidget( config );
|
|
|
|
config.parent.addItems( [ this.items[ sectionPrefix.join( '.' ) ] ], config.insertIndex );
|
2014-01-09 01:32:13 +00:00
|
|
|
previousHeadingLevel = headingLevel;
|
2015-08-19 17:33:02 +00:00
|
|
|
previousHeadingNode = nodes[ i ].node.parent;
|
2014-01-09 01:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.initialized = true;
|
|
|
|
this.hideOrShow();
|
|
|
|
};
|