| 1 |
'use strict'; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
ve.ui.MWReferenceSearchWidget = function VeUiMWReferenceSearchWidget( config ) { |
| 20 |
|
| 21 |
config = ve.extendObject( { |
| 22 |
placeholder: ve.msg( 'cite-ve-reference-input-placeholder' ) |
| 23 |
}, config ); |
| 24 |
|
| 25 |
|
| 26 |
ve.ui.MWReferenceSearchWidget.super.call( this, config ); |
| 27 |
|
| 28 |
|
| 29 |
this.docRefs = null; |
| 30 |
this.index = null; |
| 31 |
this.wasUsedActively = false; |
| 32 |
this.$overlay = config.$overlay; |
| 33 |
|
| 34 |
|
| 35 |
this.$element.addClass( 've-ui-mwReferenceSearchWidget' ); |
| 36 |
this.$results.on( 'scroll', this.trackActiveUsage.bind( this ) ); |
| 37 |
this.getResults().connect( this, { choose: 'onChoose' } ); |
| 38 |
|
| 39 |
|
| 40 |
if ( mw.config.get( 'wgCiteBookReferencing' ) ) { |
| 41 |
this.$element.addClass( 've-ui-mwReferenceSearchReuseHacks' ); |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
OO.inheritClass( ve.ui.MWReferenceSearchWidget, OO.ui.SearchWidget ); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
ve.ui.MWReferenceSearchWidget.prototype.onQueryChange = function () { |
| 68 |
|
| 69 |
ve.ui.MWReferenceSearchWidget.super.prototype.onQueryChange.call( this ); |
| 70 |
|
| 71 |
|
| 72 |
this.getResults().addItems( this.buildSearchResults( this.getQuery().getValue() ) ); |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
ve.ui.MWReferenceSearchWidget.prototype.onQueryKeydown = function ( e ) { |
| 79 |
|
| 80 |
ve.ui.MWReferenceSearchWidget.super.prototype.onQueryKeydown.call( this, e ); |
| 81 |
|
| 82 |
this.trackActiveUsage(); |
| 83 |
}; |
| 84 |
|
| 85 |
ve.ui.MWReferenceSearchWidget.prototype.clearSearch = function () { |
| 86 |
this.getQuery().setValue( '' ); |
| 87 |
this.wasUsedActively = false; |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
ve.ui.MWReferenceSearchWidget.prototype.trackActiveUsage = function () { |
| 94 |
if ( this.wasUsedActively ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
ve.track( 'activity.reference', { action: 'reuse-dialog-use' } ); |
| 100 |
this.wasUsedActively = true; |
| 101 |
}; |
| 102 |
|
| 103 |
ve.ui.MWReferenceSearchWidget.prototype.onChoose = function ( item ) { |
| 104 |
this.emit( 'reuse', item.getData() ); |
| 105 |
}; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
ve.ui.MWReferenceSearchWidget.prototype.setDocumentRefs = function ( docRefs ) { |
| 113 |
this.results.unselectItem(); |
| 114 |
|
| 115 |
this.docRefs = docRefs; |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
ve.ui.MWReferenceSearchWidget.prototype.setInternalList = function ( internalList ) { |
| 125 |
this.setDocumentRefs( ve.dm.MWDocumentReferences.static.refsForDoc( internalList.getDocument() ) ); |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
ve.ui.MWReferenceSearchWidget.prototype.buildIndex = function () { |
| 132 |
this.index = null; |
| 133 |
this.onQueryChange(); |
| 134 |
}; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
ve.ui.MWReferenceSearchWidget.prototype.buildSearchIndex = function () { |
| 141 |
const groupNames = this.docRefs.getAllGroupNames().sort(); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
const filterExtends = this.$element.hasClass( 've-ui-citoidInspector-extends' ); |
| 146 |
|
| 147 |
let index = []; |
| 148 |
for ( let i = 0; i < groupNames.length; i++ ) { |
| 149 |
const groupName = groupNames[ i ]; |
| 150 |
if ( groupName.indexOf( 'mwReference/' ) !== 0 ) { |
| 151 |
|
| 152 |
continue; |
| 153 |
} |
| 154 |
const groupRefs = this.docRefs.getGroupRefs( groupName ); |
| 155 |
const flatNodes = groupRefs.getAllRefsInDocumentOrder() |
| 156 |
.filter( ( node ) => !filterExtends || !node.getAttribute( 'extendsRef' ) ); |
| 157 |
|
| 158 |
index = index.concat( flatNodes.map( ( node ) => { |
| 159 |
const listKey = node.getAttribute( 'listKey' ); |
| 160 |
|
| 161 |
const group = groupName.slice( 12 ); |
| 162 |
const footnoteNumber = this.docRefs.getIndexLabel( group, listKey ); |
| 163 |
const footnoteLabel = ( group ? group + ' ' : '' ) + footnoteNumber; |
| 164 |
|
| 165 |
|
| 166 |
const matches = listKey.match( /^literal\/([\s\S]*)$/ ); |
| 167 |
const name = matches && matches[ 1 ] || ''; |
| 168 |
|
| 169 |
let $refContent; |
| 170 |
|
| 171 |
let refText = ( footnoteLabel + ' ' + name ).toLowerCase(); |
| 172 |
const itemNode = groupRefs.getInternalModelNode( listKey ); |
| 173 |
if ( itemNode.length ) { |
| 174 |
$refContent = new ve.ui.MWPreviewElement( itemNode, { useView: true } ).$element; |
| 175 |
refText = $refContent.text().toLowerCase() + ' ' + refText; |
| 176 |
|
| 177 |
$refContent.find( 'a[href]' ).each( ( k, element ) => { |
| 178 |
refText += ' ' + element.getAttribute( 'href' ); |
| 179 |
} ); |
| 180 |
} else { |
| 181 |
$refContent = $( '<span>' ) |
| 182 |
.addClass( 've-ce-mwReferencesListNode-muted' ) |
| 183 |
.text( ve.msg( 'cite-ve-referenceslist-missingref-in-list' ) ); |
| 184 |
} |
| 185 |
|
| 186 |
return { |
| 187 |
$refContent: $refContent, |
| 188 |
searchableText: refText, |
| 189 |
|
| 190 |
reference: ve.dm.MWReferenceModel.static.newFromReferenceNode( node ), |
| 191 |
footnoteLabel: footnoteLabel, |
| 192 |
isSubRef: !!node.getAttribute( 'extendsRef' ), |
| 193 |
name: name |
| 194 |
}; |
| 195 |
} ) ); |
| 196 |
} |
| 197 |
|
| 198 |
return index; |
| 199 |
}; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
ve.ui.MWReferenceSearchWidget.prototype.isIndexEmpty = function () { |
| 205 |
return !this.docRefs.hasRefs(); |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
ve.ui.MWReferenceSearchWidget.prototype.buildReuseOptionsMenu = function ( ref ) { |
| 214 |
|
| 215 |
|
| 216 |
const reuseOptionsMenu = new OO.ui.ButtonMenuSelectWidget( { |
| 217 |
classes: [ 've-ui-mwReferenceResultsReuseOptions' ], |
| 218 |
framed: false, |
| 219 |
icon: 'ellipsis', |
| 220 |
53 |
| 221 |
title: '', |
| 222 |
invisibleLabel: true, |
| 223 |
menu: { |
| 224 |
classes: [ 've-ui-mwReferenceResultsReuseOptionsItem' ], |
| 225 |
horizontalPosition: 'end', |
| 226 |
items: [ |
| 227 |
new OO.ui.MenuOptionWidget( { |
| 228 |
data: 'reuse', |
| 229 |
label: ve.msg( 'cite-ve-dialog-reference-useexisting-long-tool' ) |
| 230 |
} ), |
| 231 |
new OO.ui.MenuOptionWidget( { |
| 232 |
data: 'extends', |
| 233 |
label: ve.msg( 'cite-ve-dialog-reference-extend-long-tool' ) |
| 234 |
} ) |
| 235 |
] |
| 236 |
}, |
| 237 |
|
| 238 |
$overlay: this.$overlay |
| 239 |
} ); |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
reuseOptionsMenu.$element.on( 'mousedown', ( e ) => { |
| 244 |
e.stopPropagation(); |
| 245 |
} ); |
| 246 |
|
| 247 |
reuseOptionsMenu.getMenu().on( 'choose', ( menuOption ) => { |
| 248 |
|
| 249 |
this.emit( menuOption.getData(), ref ); |
| 250 |
} ); |
| 251 |
|
| 252 |
return reuseOptionsMenu; |
| 253 |
}; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
ve.ui.MWReferenceSearchWidget.prototype.buildSearchResults = function ( query ) { |
| 261 |
query = query.trim().toLowerCase(); |
| 262 |
const results = []; |
| 263 |
|
| 264 |
if ( !this.index ) { |
| 265 |
this.index = this.buildSearchIndex(); |
| 266 |
} |
| 267 |
|
| 268 |
for ( let i = 0; i < this.index.length; i++ ) { |
| 269 |
const item = this.index[ i ]; |
| 270 |
if ( item.searchableText.indexOf( query ) >= 0 ) { |
| 271 |
results.push( |
| 272 |
new ve.ui.MWReferenceResultWidget( { |
| 273 |
item: item, |
| 274 |
reuseMenu: item.isSubRef ? undefined : this.buildReuseOptionsMenu( item.reference ) |
| 275 |
} ) |
| 276 |
); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
return results; |
| 281 |
}; |