| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
ve.ui.MWTransclusionOutlineTemplateWidget = function VeUiMWTransclusionOutlineTemplateWidget( template, replacesPlaceholder ) { |
| 15 |
const spec = template.getSpec(); |
| 16 |
|
| 17 |
|
| 18 |
ve.ui.MWTransclusionOutlineTemplateWidget.super.call( this, template, { |
| 19 |
icon: 'puzzle', |
| 20 |
label: spec.getLabel(), |
| 21 |
ariaDescriptionUnselected: ve.msg( 'visualeditor-dialog-transclusion-template-widget-aria' ), |
| 22 |
ariaDescriptionSelected: ve.msg( 'visualeditor-dialog-transclusion-template-widget-aria-selected' ), |
| 23 |
ariaDescriptionSelectedSingle: ve.msg( 'visualeditor-dialog-transclusion-template-widget-aria-selected-single' ) |
| 24 |
} ); |
| 25 |
|
| 26 |
this.$element.addClass( 've-ui-mwTransclusionOutlineTemplateWidget' ); |
| 27 |
|
| 28 |
|
| 29 |
this.templateModel = template.connect( this, { |
| 30 |
add: 'onParameterAddedToTemplateModel', |
| 31 |
remove: 'onParameterRemovedFromTemplateModel' |
| 32 |
} ); |
| 33 |
|
| 34 |
const canFilter = this.shouldFiltersBeShown(), |
| 35 |
initiallyHideUnused = canFilter && !replacesPlaceholder && !this.transclusionModel.isSingleTemplate(); |
| 36 |
|
| 37 |
const parameterNames = this.getRelevantTemplateParameters( initiallyHideUnused ? 'used' : 'all' ); |
| 38 |
if ( parameterNames.length ) { |
| 39 |
this.initializeParameterList(); |
| 40 |
this.parameterList.addItems( parameterNames.map( this.createCheckbox.bind( this ) ) ); |
| 41 |
} else if ( !canFilter ) { |
| 42 |
this.$noParametersNote = $( '<div>' ) |
| 43 |
.text( ve.msg( 'visualeditor-dialog-transclusion-no-template-parameters' ) ) |
| 44 |
.addClass( 've-ui-mwTransclusionOutlineTemplateWidget-no-template-parameters' ); |
| 45 |
this.$element.append( this.$noParametersNote ); |
| 46 |
} |
| 47 |
|
| 48 |
this.toggleFilters(); |
| 49 |
if ( initiallyHideUnused ) { |
| 50 |
|
| 51 |
this.toggleUnusedWidget.toggleUnusedParameters( false ); |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
OO.inheritClass( ve.ui.MWTransclusionOutlineTemplateWidget, ve.ui.MWTransclusionOutlinePartWidget ); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
ve.ui.MWTransclusionOutlineTemplateWidget.static.searchableParameterCount = 4; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.initializeParameterList = function () { |
| 92 |
if ( this.parameterList ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
const $parametersAriaDescription = $( '<span>' ) |
| 97 |
.text( ve.msg( 'visualeditor-dialog-transclusion-param-selection-aria-description' ) ) |
| 98 |
.addClass( 've-ui-mwTransclusionOutline-ariaHidden' ); |
| 99 |
|
| 100 |
this.parameterList = new ve.ui.MWTransclusionOutlineParameterSelectWidget( { |
| 101 |
ariaLabel: ve.msg( 'visualeditor-dialog-transclusion-param-selection-aria-label', this.templateModel.getSpec().getLabel() ), |
| 102 |
$ariaDescribedBy: $parametersAriaDescription |
| 103 |
} ).connect( this, { |
| 104 |
choose: 'onTemplateParameterChoose', |
| 105 |
templateParameterSpaceDown: 'onTemplateParameterSpaceDown' |
| 106 |
} ); |
| 107 |
|
| 108 |
this.$element.append( |
| 109 |
$parametersAriaDescription, |
| 110 |
this.parameterList.$element |
| 111 |
); |
| 112 |
}; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.createAllParameterCheckboxes = function () { |
| 118 |
if ( !this.parameterListComplete ) { |
| 119 |
this.initializeParameterList(); |
| 120 |
this.getRelevantTemplateParameters().forEach( ( paramName ) => { |
| 121 |
if ( !this.parameterList.findItemFromData( paramName ) ) { |
| 122 |
this.parameterList.addItems( |
| 123 |
[ this.createCheckbox( paramName ) ], |
| 124 |
this.findCanonicalPosition( paramName ) |
| 125 |
); |
| 126 |
} |
| 127 |
} ); |
| 128 |
this.parameterListComplete = true; |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.getRelevantTemplateParameters = function ( filter ) { |
| 138 |
const template = this.templateModel; |
| 139 |
|
| 140 |
let parameterNames; |
| 141 |
switch ( filter ) { |
| 142 |
case 'used': |
| 143 |
parameterNames = template.getOrderedParameterNames(); |
| 144 |
break; |
| 145 |
case 'unused': |
| 146 |
parameterNames = template.getAllParametersOrdered().filter( ( name ) => !( name in template.getParameters() ) ); |
| 147 |
break; |
| 148 |
default: |
| 149 |
parameterNames = template.getAllParametersOrdered(); |
| 150 |
} |
| 151 |
|
| 152 |
return parameterNames.filter( ( name ) => { |
| 153 |
|
| 154 |
if ( template.getSpec().isParameterDeprecated( name ) && !template.hasParameter( name ) ) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
return !!name; |
| 159 |
} ); |
| 160 |
}; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.createCheckbox = function ( paramName ) { |
| 168 |
const spec = this.templateModel.getSpec(), |
| 169 |
parameterModel = this.templateModel.getParameter( paramName ); |
| 170 |
|
| 171 |
return ve.ui.MWTransclusionOutlineParameterSelectWidget.static.createItem( { |
| 172 |
required: spec.isParameterRequired( paramName ), |
| 173 |
label: spec.getParameterLabel( paramName ), |
| 174 |
data: paramName, |
| 175 |
selected: !!parameterModel, |
| 176 |
hasValue: !!parameterModel && !!parameterModel.getValue() |
| 177 |
} ); |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.findCanonicalPosition = function ( paramName ) { |
| 186 |
|
| 187 |
const allParamNames = this.templateModel.getAllParametersOrdered(); |
| 188 |
let insertAt = 0; |
| 189 |
for ( let i = 0; i < allParamNames.length; i++ ) { |
| 190 |
if ( allParamNames[ i ] === paramName || !this.parameterList.items[ insertAt ] ) { |
| 191 |
break; |
| 192 |
} else if ( this.parameterList.items[ insertAt ].getData() === allParamNames[ i ] ) { |
| 193 |
insertAt++; |
| 194 |
} |
| 195 |
} |
| 196 |
return insertAt; |
| 197 |
}; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.setParameter = function ( paramName ) { |
| 203 |
if ( this.parameterList ) { |
| 204 |
this.parameterList.setActiveParameter( paramName ); |
| 205 |
} |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.toggleHasValue = function ( paramName, hasValue ) { |
| 213 |
if ( this.parameterList ) { |
| 214 |
const item = this.parameterList.findItemFromData( paramName ); |
| 215 |
if ( item ) { |
| 216 |
item.toggleHasValue( hasValue ); |
| 217 |
} |
| 218 |
} |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.setSelected = function ( state ) { |
| 225 |
|
| 226 |
if ( !state && this.isSelected() && this.parameterList ) { |
| 227 |
this.parameterList.highlightItem(); |
| 228 |
} |
| 229 |
ve.ui.MWTransclusionOutlineTemplateWidget.super.prototype.setSelected.call( this, state ); |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.onParameterAddedToTemplateModel = function ( param ) { |
| 237 |
const paramName = param.getName(); |
| 238 |
|
| 239 |
if ( !paramName ) { |
| 240 |
return; |
| 241 |
} |
| 242 |
|
| 243 |
if ( this.$noParametersNote ) { |
| 244 |
this.$noParametersNote.remove(); |
| 245 |
delete this.$noParametersNote; |
| 246 |
} |
| 247 |
|
| 248 |
this.initializeParameterList(); |
| 249 |
|
| 250 |
|
| 251 |
let item = this.parameterList.findItemFromData( paramName ); |
| 252 |
if ( item ) { |
| 253 |
|
| 254 |
item.toggle( true ); |
| 255 |
} else { |
| 256 |
item = this.createCheckbox( paramName ); |
| 257 |
this.parameterList.addItems( [ item ], this.findCanonicalPosition( paramName ) ); |
| 258 |
|
| 259 |
this.toggleFilters(); |
| 260 |
|
| 261 |
|
| 262 |
const filter = this.searchWidget && this.searchWidget.getValue(); |
| 263 |
if ( filter ) { |
| 264 |
this.filterParameters( filter ); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
item.setSelected( true, true ); |
| 269 |
|
| 270 |
|
| 271 |
if ( !item.isVisible() ) { |
| 272 |
this.searchWidget.setValue( '' ); |
| 273 |
} |
| 274 |
}; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.onParameterRemovedFromTemplateModel = function ( param ) { |
| 281 |
this.parameterList.markParameterAsUnused( param.getName() ); |
| 282 |
}; |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.onTemplateParameterChoose = function ( item, selected ) { |
| 290 |
this.toggleParameter( item, selected, false ); |
| 291 |
}; |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.onTemplateParameterSpaceDown = function ( item, selected ) { |
| 299 |
this.toggleParameter( item, selected, true ); |
| 300 |
}; |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.toggleParameter = function ( item, selected, soft ) { |
| 310 |
const paramName = item.getData(); |
| 311 |
let param = this.templateModel.getParameter( paramName ); |
| 312 |
if ( !selected ) { |
| 313 |
this.templateModel.removeParameter( param ); |
| 314 |
} else if ( !param ) { |
| 315 |
param = new ve.dm.MWParameterModel( this.templateModel, paramName ); |
| 316 |
this.templateModel.addParameter( param ); |
| 317 |
} |
| 318 |
|
| 319 |
this.updateUnusedParameterToggleState(); |
| 320 |
|
| 321 |
if ( param && selected ) { |
| 322 |
this.emit( 'transclusionOutlineItemSelected', param.getId(), soft ); |
| 323 |
} |
| 324 |
}; |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.updateUnusedParameterToggleState = function () { |
| 330 |
if ( this.toggleUnusedWidget ) { |
| 331 |
this.toggleUnusedWidget.setDisabled( !this.getRelevantTemplateParameters( 'unused' ).length ); |
| 332 |
} |
| 333 |
}; |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.shouldFiltersBeShown = function () { |
| 340 |
const min = this.constructor.static.searchableParameterCount, |
| 341 |
existingParameterWidgets = this.parameterList && this.parameterList.getItemCount(); |
| 342 |
|
| 343 |
return existingParameterWidgets >= min || this.getRelevantTemplateParameters().length >= min; |
| 344 |
}; |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.toggleFilters = function () { |
| 350 |
const visible = this.shouldFiltersBeShown(); |
| 351 |
if ( this.searchWidget ) { |
| 352 |
this.searchWidget.toggle( visible ); |
| 353 |
this.toggleUnusedWidget.toggle( visible ); |
| 354 |
} else if ( visible ) { |
| 355 |
this.initializeFilters(); |
| 356 |
this.updateUnusedParameterToggleState(); |
| 357 |
} |
| 358 |
|
| 359 |
this.recalculateStickyHeaderHeight(); |
| 360 |
}; |
| 361 |
|
| 362 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.recalculateStickyHeaderHeight = function () { |
| 363 |
|
| 364 |
if ( this.$stickyHeader && this.parameterList ) { |
| 365 |
this.parameterList.stickyHeaderHeight = Math.floor( this.$stickyHeader.outerHeight() ); |
| 366 |
} |
| 367 |
}; |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.initializeFilters = function () { |
| 373 |
this.searchWidget = new OO.ui.SearchInputWidget( { |
| 374 |
title: ve.msg( 'visualeditor-dialog-transclusion-filter-title', this.templateModel.getSpec().getLabel() ), |
| 375 |
placeholder: ve.msg( 'visualeditor-dialog-transclusion-filter-placeholder' ), |
| 376 |
classes: [ 've-ui-mwTransclusionOutlineTemplateWidget-searchWidget' ] |
| 377 |
} ).connect( this, { |
| 378 |
change: 'filterParameters' |
| 379 |
} ); |
| 380 |
this.searchWidget.$element.attr( 'role', 'search' ); |
| 381 |
|
| 382 |
this.toggleUnusedWidget = new ve.ui.MWTransclusionOutlineToggleUnusedWidget(); |
| 383 |
this.toggleUnusedWidget.connect( this, { |
| 384 |
toggleUnusedFields: 'onToggleUnusedFields' |
| 385 |
} ); |
| 386 |
|
| 387 |
this.infoWidget = new OO.ui.LabelWidget( { |
| 388 |
label: ve.msg( 'visualeditor-dialog-transclusion-filter-no-match' ), |
| 389 |
classes: [ 've-ui-mwTransclusionOutlineTemplateWidget-no-match' ] |
| 390 |
} ).toggle( false ); |
| 391 |
|
| 392 |
this.$stickyHeader = $( '<div>' ) |
| 393 |
.addClass( 've-ui-mwTransclusionOutlineTemplateWidget-sticky' ) |
| 394 |
.append( |
| 395 |
this.header.$element, |
| 396 |
this.searchWidget.$element, |
| 397 |
this.toggleUnusedWidget.$element |
| 398 |
); |
| 399 |
|
| 400 |
this.$element.prepend( |
| 401 |
this.$stickyHeader, |
| 402 |
this.infoWidget.$element |
| 403 |
); |
| 404 |
}; |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.filterParameters = function ( query ) { |
| 415 |
const template = this.templateModel, |
| 416 |
spec = this.templateModel.getSpec(), |
| 417 |
visibility = {}; |
| 418 |
let nothingFound = true; |
| 419 |
|
| 420 |
query = query.trim().toLowerCase(); |
| 421 |
this.createAllParameterCheckboxes(); |
| 422 |
|
| 423 |
|
| 424 |
this.parameterList.items.forEach( ( item ) => { |
| 425 |
const paramName = item.getData(), |
| 426 |
placesToSearch = [ |
| 427 |
spec.getPrimaryParameterName( paramName ), |
| 428 |
spec.getParameterLabel( paramName ), |
| 429 |
spec.getParameterDescription( paramName ) |
| 430 |
].concat( spec.getParameterAliases( paramName ) ); |
| 431 |
|
| 432 |
const foundSomeMatch = placesToSearch.some( |
| 433 |
|
| 434 |
( term ) => term && typeof term === 'string' && term.toLowerCase().indexOf( query ) !== -1 |
| 435 |
); |
| 436 |
|
| 437 |
item.toggle( foundSomeMatch ); |
| 438 |
|
| 439 |
nothingFound = nothingFound && !foundSomeMatch; |
| 440 |
|
| 441 |
const param = template.getParameter( paramName ); |
| 442 |
if ( param ) { |
| 443 |
visibility[ param.getId() ] = foundSomeMatch; |
| 444 |
} |
| 445 |
} ); |
| 446 |
|
| 447 |
this.toggleUnusedWidget.toggle( !query ); |
| 448 |
this.infoWidget.toggle( nothingFound ); |
| 449 |
this.parameterList.setTabIndex( nothingFound ? -1 : 0 ); |
| 450 |
|
| 451 |
this.recalculateStickyHeaderHeight(); |
| 452 |
this.emit( 'filterParametersById', visibility ); |
| 453 |
}; |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.onToggleUnusedFields = function ( visibility, fromClick ) { |
| 461 |
if ( visibility ) { |
| 462 |
this.createAllParameterCheckboxes(); |
| 463 |
} |
| 464 |
|
| 465 |
if ( this.parameterList ) { |
| 466 |
this.parameterList.items.forEach( ( item ) => { |
| 467 |
item.toggle( visibility || item.isSelected() ); |
| 468 |
} ); |
| 469 |
} |
| 470 |
|
| 471 |
if ( !visibility && fromClick ) { |
| 472 |
this.header.scrollElementIntoView().then( () => { |
| 473 |
if ( this.parameterList ) { |
| 474 |
this.parameterList.ensureVisibilityOfFirstCheckedParameter(); |
| 475 |
} |
| 476 |
} ); |
| 477 |
} |
| 478 |
}; |