Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/widgets/ve.ui.MWTocWidget.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWTocWidget class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Creates a ve.ui.MWTocWidget object.
10 *
11 * @class
12 * @extends OO.ui.Widget
13 *
14 * @constructor
15 * @param {ve.ui.Surface} surface
16 * @param {Object} [config] Configuration options
17 */
18 ve.ui.MWTocWidget = function VeUiMWTocWidget( surface, config ) {
19 // Parent constructor
20 ve.ui.MWTocWidget.super.call( this, config );
21
22 // Properties
23 this.surface = surface;
24 this.doc = surface.getModel().getDocument();
25 this.metaList = this.doc.getMetaList();
26 // Topic level 0 lives inside of a toc item
27 this.rootLength = 0;
28 this.initialized = false;
29 // Page settings cache
30 this.mwTOCForce = false;
31 this.mwTOCDisable = false;
32
33 this.$tocList = $( '<ul>' );
34 this.$element.addClass( 'toc ve-ui-mwTocWidget ve-ce-focusableNode' ).append(
35 $( '<div>' ).addClass( 'toctitle' ).append(
36 $( '<h2>' ).text( ve.msg( 'toc' ) )
37 ),
38 this.$tocList
39 ).prop( 'contentEditable', 'false' );
40
41 // Setup toggle link
42 mw.hook( 'wikipage.content' ).fire( this.$element );
43
44 // Events
45 this.metaList.connect( this, {
46 insert: 'onMetaListInsert',
47 remove: 'onMetaListRemove'
48 } );
49
50 this.buildDebounced = ve.debounce( this.build.bind( this ) );
51
52 this.initFromMetaList();
53 this.build();
54 };
55
56 /* Inheritance */
57
58 OO.inheritClass( ve.ui.MWTocWidget, OO.ui.Widget );
59
60 /**
61 * Bound to MetaList insert event to set TOC display options
62 *
63 * @param {ve.dm.MetaItem} metaItem
64 */
65 ve.ui.MWTocWidget.prototype.onMetaListInsert = function ( metaItem ) {
66 // Responsible for adding UI components
67 if ( metaItem instanceof ve.dm.MWTOCMetaItem ) {
68 const property = metaItem.getAttribute( 'property' );
69 if ( property === 'mw:PageProp/forcetoc' ) {
70 this.mwTOCForce = true;
71 } else if ( property === 'mw:PageProp/notoc' ) {
72 this.mwTOCDisable = true;
73 }
74 }
75 this.updateVisibility();
76 };
77
78 /**
79 * Bound to MetaList insert event to set TOC display options
80 *
81 * @param {ve.dm.MetaItem} metaItem
82 */
83 ve.ui.MWTocWidget.prototype.onMetaListRemove = function ( metaItem ) {
84 if ( metaItem instanceof ve.dm.MWTOCMetaItem ) {
85 const property = metaItem.getAttribute( 'property' );
86 if ( property === 'mw:PageProp/forcetoc' ) {
87 this.mwTOCForce = false;
88 } else if ( property === 'mw:PageProp/notoc' ) {
89 this.mwTOCDisable = false;
90 }
91 }
92 this.updateVisibility();
93 };
94
95 /**
96 * Initialize TOC based on the presence of magic words
97 */
98 ve.ui.MWTocWidget.prototype.initFromMetaList = function () {
99 const items = this.metaList.getItemsInGroup( 'mwTOC' );
100 if ( items.length === 0 ) {
101 return;
102 }
103 for ( let i = 0; i < items.length; i++ ) {
104 if ( items[ i ] instanceof ve.dm.MWTOCMetaItem ) {
105 const property = items[ i ].getAttribute( 'property' );
106 if ( property === 'mw:PageProp/forcetoc' ) {
107 this.mwTOCForce = true;
108 }
109 if ( property === 'mw:PageProp/notoc' ) {
110 this.mwTOCDisable = true;
111 }
112 }
113 }
114 this.updateVisibility();
115 };
116
117 /**
118 * Hides or shows the TOC based on page and default settings
119 */
120 ve.ui.MWTocWidget.prototype.updateVisibility = function () {
121 // In MediaWiki if `__FORCETOC__` is anywhere TOC is always displayed
122 // ... Even if there is a `__NOTOC__` in the article
123 this.toggle( !this.mwTOCDisable && ( this.mwTOCForce || this.rootLength >= 3 ) );
124 };
125
126 /**
127 * Rebuild TOC on ve.ce.MWHeadingNode teardown or setup
128 *
129 * Rebuilds on both teardown and setup of a node, so build is debounced
130 */
131 ve.ui.MWTocWidget.prototype.rebuild = function () {
132 if ( this.initialized ) {
133 // Wait for transactions to process
134 this.buildDebounced();
135 }
136 };
137
138 /**
139 * Update the text content of a specific heading node
140 *
141 * @param {ve.ce.MWHeadingNode} viewNode Heading node
142 */
143 ve.ui.MWTocWidget.prototype.updateNode = function ( viewNode ) {
144 if ( viewNode.$tocText ) {
145 viewNode.$tocText.text( viewNode.$element.text() );
146 }
147 };
148
149 /**
150 * Build TOC from mwHeading dm nodes
151 *
152 * Based on generateTOC in Linker.php
153 */
154 ve.ui.MWTocWidget.prototype.build = function () {
155 const $newTocList = $( '<ul>' ),
156 nodes = this.doc.getNodesByType( 'mwHeading', true ),
157 surfaceView = this.surface.getView(),
158 documentView = surfaceView.getDocument(),
159 stack = [],
160 url = new URL( location.href );
161
162 function getItemIndex( $el, n ) {
163 return $el.children( 'li' ).length + ( n === stack.length - 1 ? 1 : 0 );
164 }
165
166 function linkClickHandler( /* heading */ ) {
167 surfaceView.focus();
168 // TODO: Impement heading scroll
169 return false;
170 }
171
172 let lastLevel = 0;
173 for ( let i = 0, l = nodes.length; i < l; i++ ) {
174 const modelNode = nodes[ i ];
175 const level = modelNode.getAttribute( 'level' );
176
177 if ( level > lastLevel ) {
178 let $list;
179 if ( stack.length ) {
180 $list = $( '<ul>' );
181 stack[ stack.length - 1 ].children().last().append( $list );
182 } else {
183 $list = $newTocList;
184 }
185 stack.push( $list );
186 } else if ( level < lastLevel ) {
187 let levelDiff = lastLevel - level;
188 while ( levelDiff > 0 && stack.length > 1 ) {
189 stack.pop();
190 levelDiff--;
191 }
192 }
193
194 const tocNumber = stack.map( getItemIndex ).join( '.' );
195 const viewNode = documentView.getBranchNodeFromOffset( modelNode.getRange().start );
196 url.searchParams.set( 'section', ( i + 1 ).toString() );
197 // The following classes are used here:
198 // * toclevel-1, toclevel-2, ...
199 // * tocsection-1, tocsection-2, ...
200 const $item = $( '<li>' ).addClass( 'toclevel-' + stack.length ).addClass( 'tocsection-' + ( i + 1 ) );
201 const $link = $( '<a>' ).attr( 'href', url.toString() ).append(
202 $( '<span>' ).addClass( 'tocnumber' ).text( tocNumber )
203 );
204 const $text = $( '<span>' ).addClass( 'toctext' );
205
206 viewNode.$tocText = $text;
207 this.updateNode( viewNode );
208
209 stack[ stack.length - 1 ].append( $item.append( $link.append( $text ) ) );
210 $link.on( 'click', linkClickHandler.bind( this, viewNode ) );
211
212 lastLevel = level;
213 }
214
215 this.$tocList.empty().append( $newTocList.children() );
216
217 if ( nodes.length ) {
218 this.rootLength = this.$tocList.children().length;
219 const tocBeforeNode = documentView.getBranchNodeFromOffset( nodes[ 0 ].getRange().start );
220 tocBeforeNode.$element.before( this.$element );
221 } else {
222 this.rootLength = 0;
223 }
224
225 this.initialized = true;
226 this.updateVisibility();
227 };