| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
ve.ui.MWTocWidget = function VeUiMWTocWidget( surface, config ) { |
| 19 |
|
| 20 |
ve.ui.MWTocWidget.super.call( this, config ); |
| 21 |
|
| 22 |
|
| 23 |
this.surface = surface; |
| 24 |
this.doc = surface.getModel().getDocument(); |
| 25 |
this.metaList = this.doc.getMetaList(); |
| 26 |
|
| 27 |
this.rootLength = 0; |
| 28 |
this.initialized = false; |
| 29 |
|
| 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 |
|
| 42 |
mw.hook( 'wikipage.content' ).fire( this.$element ); |
| 43 |
|
| 44 |
|
| 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 |
|
| 57 |
|
| 58 |
OO.inheritClass( ve.ui.MWTocWidget, OO.ui.Widget ); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
ve.ui.MWTocWidget.prototype.onMetaListInsert = function ( metaItem ) { |
| 66 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 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 |
|
| 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 |
|
| 119 |
|
| 120 |
ve.ui.MWTocWidget.prototype.updateVisibility = function () { |
| 121 |
|
| 122 |
|
| 123 |
this.toggle( !this.mwTOCDisable && ( this.mwTOCForce || this.rootLength >= 3 ) ); |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
ve.ui.MWTocWidget.prototype.rebuild = function () { |
| 132 |
if ( this.initialized ) { |
| 133 |
|
| 134 |
this.buildDebounced(); |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 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 |
|
| 151 |
|
| 152 |
|
| 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( ) { |
| 167 |
surfaceView.focus(); |
| 168 |
|
| 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 |
|
| 198 |
|
| 199 |
|
| 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 |
}; |