| 1 |
'use strict'; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
ve.ce.MWReferencesListNode = function VeCeMWReferencesListNode() { |
| 20 |
|
| 21 |
ve.ce.MWReferencesListNode.super.apply( this, arguments ); |
| 22 |
|
| 23 |
|
| 24 |
ve.ce.FocusableNode.call( this ); |
| 25 |
|
| 26 |
|
| 27 |
this.modified = false; |
| 28 |
|
| 29 |
|
| 30 |
this.$element.addClass( 've-ce-mwReferencesListNode' ); |
| 31 |
this.$reflist = $( '<ol>' ).addClass( 'mw-references references' ); |
| 32 |
this.$originalRefList = null; |
| 33 |
this.$refmsg = $( '<p>' ) |
| 34 |
.addClass( 've-ce-mwReferencesListNode-muted' ); |
| 35 |
|
| 36 |
|
| 37 |
this.getModel().connect( this, { attributeChange: 'onAttributeChange' } ); |
| 38 |
|
| 39 |
this.updateDebounced = ve.debounce( this.update.bind( this ) ); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
mw.requestIdleCallback( this.update.bind( this ) ); |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
OO.inheritClass( ve.ce.MWReferencesListNode, ve.ce.LeafNode ); |
| 53 |
|
| 54 |
OO.mixinClass( ve.ce.MWReferencesListNode, ve.ce.FocusableNode ); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
ve.ce.MWReferencesListNode.static.name = 'mwReferencesList'; |
| 59 |
|
| 60 |
ve.ce.MWReferencesListNode.static.tagName = 'div'; |
| 61 |
|
| 62 |
ve.ce.MWReferencesListNode.static.primaryCommandName = 'referencesList'; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
ve.ce.MWReferencesListNode.static.getDescription = function ( model ) { |
| 71 |
return model.getAttribute( 'refGroup' ); |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
ve.ce.MWReferencesListNode.prototype.getExtraHighlightClasses = function () { |
| 79 |
return ve.ce.FocusableNode.prototype |
| 80 |
.getExtraHighlightClasses.apply( this, arguments ) |
| 81 |
.concat( [ 've-ce-mwReferencesListNode-highlight' ] ); |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
ve.ce.MWReferencesListNode.prototype.onSetup = function () { |
| 90 |
const internalList = this.getModel().getDocument().getInternalList(); |
| 91 |
const listNode = internalList.getListNode(); |
| 92 |
|
| 93 |
internalList.connect( this, { update: 'onInternalListUpdate' } ); |
| 94 |
listNode.connect( this, { update: 'onListNodeUpdate' } ); |
| 95 |
|
| 96 |
|
| 97 |
ve.ce.MWReferencesListNode.super.prototype.onSetup.call( this ); |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
ve.ce.MWReferencesListNode.prototype.onTeardown = function () { |
| 104 |
|
| 105 |
ve.ce.MWReferencesListNode.super.prototype.onTeardown.call( this ); |
| 106 |
|
| 107 |
if ( !this.getModel() || !this.getModel().getDocument() ) { |
| 108 |
return; |
| 109 |
} |
| 110 |
const internalList = this.getModel().getDocument().getInternalList(); |
| 111 |
const listNode = internalList.getListNode(); |
| 112 |
|
| 113 |
internalList.disconnect( this, { update: 'onInternalListUpdate' } ); |
| 114 |
listNode.disconnect( this, { update: 'onListNodeUpdate' } ); |
| 115 |
}; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
ve.ce.MWReferencesListNode.prototype.onInternalListUpdate = function ( groupsChanged ) { |
| 125 |
if ( !this.getModel() ) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
if ( groupsChanged.indexOf( this.getModel().getAttribute( 'listGroup' ) ) !== -1 ) { |
| 130 |
this.modified = true; |
| 131 |
this.updateDebounced(); |
| 132 |
} |
| 133 |
}; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
ve.ce.MWReferencesListNode.prototype.onAttributeChange = function ( key ) { |
| 143 |
switch ( key ) { |
| 144 |
case 'listGroup': |
| 145 |
this.updateDebounced(); |
| 146 |
this.modified = true; |
| 147 |
break; |
| 148 |
case 'isResponsive': |
| 149 |
this.updateClasses(); |
| 150 |
break; |
| 151 |
} |
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
ve.ce.MWReferencesListNode.prototype.onListNodeUpdate = function () { |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
this.updateDebounced(); |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
ve.ce.MWReferencesListNode.prototype.update = function () { |
| 170 |
const model = this.getModel(); |
| 171 |
|
| 172 |
|
| 173 |
if ( !model ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
const refGroup = model.getAttribute( 'refGroup' ); |
| 178 |
|
| 179 |
const docRefs = ve.dm.MWDocumentReferences.static.refsForDoc( model.getDocument() ); |
| 180 |
const groupRefs = docRefs.getGroupRefs( refGroup ); |
| 181 |
const hasModelReferences = !groupRefs.isEmpty(); |
| 182 |
|
| 183 |
let emptyText; |
| 184 |
if ( refGroup !== '' ) { |
| 185 |
emptyText = ve.msg( 'cite-ve-referenceslist-isempty', refGroup ); |
| 186 |
} else { |
| 187 |
emptyText = ve.msg( 'cite-ve-referenceslist-isempty-default' ); |
| 188 |
} |
| 189 |
|
| 190 |
let originalDomElements; |
| 191 |
if ( model.getElement().originalDomElementsHash ) { |
| 192 |
originalDomElements = model.getStore().value( |
| 193 |
model.getElement().originalDomElementsHash |
| 194 |
); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
if ( |
| 203 |
!hasModelReferences && |
| 204 |
!this.modified && |
| 205 |
originalDomElements |
| 206 |
) { |
| 207 |
|
| 208 |
this.$originalRefList = $( ve.copyDomElements( originalDomElements, document ) ); |
| 209 |
|
| 210 |
if ( this.$originalRefList.find( 'li' ).length ) { |
| 211 |
this.$element.append( this.$originalRefList ); |
| 212 |
} else { |
| 213 |
this.$refmsg.text( emptyText ); |
| 214 |
this.$element.append( this.$refmsg ); |
| 215 |
} |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
if ( this.$originalRefList ) { |
| 220 |
this.$originalRefList.remove(); |
| 221 |
this.$originalRefList = null; |
| 222 |
} |
| 223 |
|
| 224 |
if ( originalDomElements ) { |
| 225 |
|
| 226 |
const divs = originalDomElements.filter( ( element ) => element.tagName === 'DIV' ); |
| 227 |
if ( divs.length ) { |
| 228 |
|
| 229 |
this.$element.addClass( divs[ 0 ].getAttribute( 'class' ) ); |
| 230 |
this.$element.attr( 'style', divs[ 0 ].getAttribute( 'style' ) ); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
this.$reflist.detach().empty().attr( 'data-mw-group', refGroup || null ); |
| 235 |
this.$refmsg.detach(); |
| 236 |
|
| 237 |
if ( !hasModelReferences ) { |
| 238 |
this.$refmsg.text( emptyText ); |
| 239 |
this.$element.append( this.$refmsg ); |
| 240 |
} else { |
| 241 |
|
| 242 |
this.$reflist.append( |
| 243 |
groupRefs.getTopLevelKeysInReflistOrder() |
| 244 |
.map( ( listKey ) => this.renderListItem( |
| 245 |
groupRefs, refGroup, listKey |
| 246 |
) ) |
| 247 |
); |
| 248 |
|
| 249 |
this.updateClasses(); |
| 250 |
this.$element.append( this.$reflist ); |
| 251 |
} |
| 252 |
}; |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
ve.ce.MWReferencesListNode.prototype.renderListItem = function ( groupRefs, refGroup, key ) { |
| 264 |
const ref = groupRefs.getInternalModelNode( key ); |
| 265 |
const backlinkNodes = groupRefs.getRefUsages( key ); |
| 266 |
const subrefs = groupRefs.getSubrefs( key ); |
| 267 |
|
| 268 |
const $li = $( '<li>' ) |
| 269 |
.css( '--footnote-number', `"${ groupRefs.getIndexLabel( key ) }."` ) |
| 270 |
.append( this.renderBacklinks( backlinkNodes, refGroup ), ' ' ); |
| 271 |
|
| 272 |
if ( ref && ref.length ) { |
| 273 |
const refPreview = new ve.ui.MWPreviewElement( ref, { useView: true } ); |
| 274 |
$li.append( |
| 275 |
$( '<span>' ) |
| 276 |
.addClass( 'reference-text' ) |
| 277 |
.append( refPreview.$element ) |
| 278 |
); |
| 279 |
|
| 280 |
if ( this.getRoot() ) { |
| 281 |
const surface = this.getRoot().getSurface().getSurface(); |
| 282 |
|
| 283 |
$li.on( 'mousedown', ( e ) => { |
| 284 |
if ( ve.isUnmodifiedLeftClick( e ) ) { |
| 285 |
const node = groupRefs.getRefNode( key ); |
| 286 |
const items = ve.ui.contextItemFactory.getRelatedItems( [ node ] ) |
| 287 |
.filter( ( item ) => item.name !== 'mobileActions' ); |
| 288 |
if ( items.length ) { |
| 289 |
const contextItem = ve.ui.contextItemFactory.lookup( items[ 0 ].name ); |
| 290 |
if ( contextItem ) { |
| 291 |
const command = surface.commandRegistry |
| 292 |
.lookup( contextItem.static.commandName ); |
| 293 |
if ( command ) { |
| 294 |
const fragmentArgs = { |
| 295 |
fragment: surface.getModel() |
| 296 |
.getLinearFragment( node.getOuterRange(), true ), |
| 297 |
selectFragmentOnClose: false |
| 298 |
}; |
| 299 |
const newArgs = ve.copy( command.args ); |
| 300 |
if ( command.name === 'reference' ) { |
| 301 |
newArgs[ 1 ] = fragmentArgs; |
| 302 |
} else { |
| 303 |
ve.extendObject( newArgs[ 0 ], fragmentArgs ); |
| 304 |
} |
| 305 |
command.execute( surface, newArgs ); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
e.preventDefault(); |
| 311 |
} ); |
| 312 |
} |
| 313 |
} else { |
| 314 |
$li.append( |
| 315 |
$( '<span>' ) |
| 316 |
.addClass( 've-ce-mwReferencesListNode-muted' ) |
| 317 |
.text( subrefs.length ? ve.msg( 'cite-ve-referenceslist-missing-parent' ) : |
| 318 |
ve.msg( 'cite-ve-referenceslist-missingref-in-list' ) ) |
| 319 |
).addClass( 've-ce-mwReferencesListNode-missingRef' ); |
| 320 |
} |
| 321 |
|
| 322 |
if ( subrefs.length ) { |
| 323 |
$li.append( |
| 324 |
$( '<ol>' ).append( |
| 325 |
subrefs.map( ( subNode ) => this.renderListItem( |
| 326 |
groupRefs, refGroup, subNode.getAttribute( 'listKey' ) |
| 327 |
) ) |
| 328 |
) |
| 329 |
); |
| 330 |
} |
| 331 |
|
| 332 |
return $li; |
| 333 |
}; |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
ve.ce.MWReferencesListNode.prototype.updateClasses = function () { |
| 341 |
const isResponsive = this.getModel().getAttribute( 'isResponsive' ); |
| 342 |
|
| 343 |
this.$element |
| 344 |
.toggleClass( 'mw-references-wrap', isResponsive ) |
| 345 |
.toggleClass( 'mw-references-columns', isResponsive && this.$reflist.children().length > 10 ); |
| 346 |
}; |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
ve.ce.MWReferencesListNode.prototype.renderBacklinks = function ( keyedNodes, refGroup ) { |
| 356 |
if ( keyedNodes.length === 1 ) { |
| 357 |
return $( '<a>' ) |
| 358 |
.attr( 'rel', 'mw:referencedBy' ) |
| 359 |
.attr( 'data-mw-group', refGroup || null ) |
| 360 |
.append( $( '<span>' ).addClass( 'mw-linkback-text' ).text( '↑ ' ) ); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
const $refSpan = $( '<span>' ).attr( 'rel', 'mw:referencedBy' ); |
| 365 |
for ( let i = 0; i < keyedNodes.length; i++ ) { |
| 366 |
$( '<a>' ) |
| 367 |
.attr( 'data-mw-group', refGroup || null ) |
| 368 |
|
| 369 |
.append( $( '<span>' ).addClass( 'mw-linkback-text' ).text( ( i + 1 ) + ' ' ) ) |
| 370 |
.appendTo( $refSpan ); |
| 371 |
} |
| 372 |
return $refSpan; |
| 373 |
}; |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
ve.ce.nodeFactory.register( ve.ce.MWReferencesListNode ); |