| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
ve.dm.MWTemplateModel = function VeDmMWTemplateModel( transclusion, target ) { |
| 28 |
|
| 29 |
ve.dm.MWTemplateModel.super.call( this, transclusion ); |
| 30 |
|
| 31 |
|
| 32 |
this.target = target; |
| 33 |
|
| 34 |
|
| 35 |
this.title = target.href ? mw.libs.ve.normalizeParsoidResourceName( target.href ) : null; |
| 36 |
this.orderedParameterNames = null; |
| 37 |
this.params = {}; |
| 38 |
this.spec = new ve.dm.MWTemplateSpecModel( this ); |
| 39 |
this.originalData = null; |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
OO.inheritClass( ve.dm.MWTemplateModel, ve.dm.MWTransclusionPartModel ); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
ve.dm.MWTemplateModel.newFromData = function ( transclusion, data ) { |
| 81 |
const template = new ve.dm.MWTemplateModel( transclusion, data.target ); |
| 82 |
|
| 83 |
for ( const key in data.params ) { |
| 84 |
template.addParameter( |
| 85 |
new ve.dm.MWParameterModel( template, key, data.params[ key ].wt ) |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
template.setOriginalData( data ); |
| 90 |
|
| 91 |
return template; |
| 92 |
}; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
ve.dm.MWTemplateModel.newFromName = function ( transclusion, name ) { |
| 103 |
const templateNs = mw.config.get( 'wgNamespaceIds' ).template; |
| 104 |
let title; |
| 105 |
if ( name instanceof mw.Title ) { |
| 106 |
title = name; |
| 107 |
name = title.getRelativeText( templateNs ); |
| 108 |
} else { |
| 109 |
title = mw.Title.newFromText( name, templateNs ); |
| 110 |
} |
| 111 |
if ( title !== null ) { |
| 112 |
const href = title.getPrefixedText(); |
| 113 |
return new ve.dm.MWTemplateModel( transclusion, { href: href, wt: name } ); |
| 114 |
} |
| 115 |
|
| 116 |
return null; |
| 117 |
}; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
ve.dm.MWTemplateModel.prototype.getTarget = function () { |
| 125 |
return this.target; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
ve.dm.MWTemplateModel.prototype.getTitle = function () { |
| 133 |
return this.title; |
| 134 |
}; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
ve.dm.MWTemplateModel.prototype.getTemplateDataQueryTitle = function () { |
| 141 |
|
| 142 |
…}} |
| 143 |
const name = this.target.wt.trim().replace( /^(?:safe)?subst:/i, '' ), |
| 144 |
templateNs = mw.config.get( 'wgNamespaceIds' ).template, |
| 145 |
title = mw.Title.newFromText( name, templateNs ); |
| 146 |
return title ? title.getPrefixedText() : this.getTitle(); |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
ve.dm.MWTemplateModel.prototype.getSpec = function () { |
| 153 |
return this.spec; |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
ve.dm.MWTemplateModel.prototype.getParameters = function () { |
| 164 |
return this.params; |
| 165 |
}; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
ve.dm.MWTemplateModel.prototype.getParameter = function ( name ) { |
| 172 |
return this.params[ name ]; |
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
ve.dm.MWTemplateModel.prototype.hasParameter = function ( name ) { |
| 182 |
return this.getOriginalParameterName( name ) in this.params; |
| 183 |
}; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
ve.dm.MWTemplateModel.prototype.getOriginalParameterName = function ( name ) { |
| 190 |
if ( name in this.params ) { |
| 191 |
return name; |
| 192 |
} |
| 193 |
const aliases = this.spec.getParameterAliases( name ); |
| 194 |
|
| 195 |
for ( let i = 0; i < aliases.length; i++ ) { |
| 196 |
if ( aliases[ i ] in this.params ) { |
| 197 |
return aliases[ i ]; |
| 198 |
} |
| 199 |
} |
| 200 |
return this.spec.getPrimaryParameterName( name ); |
| 201 |
}; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
ve.dm.MWTemplateModel.prototype.getAllParametersOrdered = function () { |
| 213 |
const spec = this.spec, |
| 214 |
usedAliases = {}; |
| 215 |
|
| 216 |
let primaryName; |
| 217 |
for ( const alias in this.params ) { |
| 218 |
if ( spec.isParameterAlias( alias ) ) { |
| 219 |
primaryName = spec.getPrimaryParameterName( alias ); |
| 220 |
if ( !usedAliases[ primaryName ] ) { |
| 221 |
|
| 222 |
usedAliases[ primaryName ] = primaryName in this.params ? [ primaryName ] : []; |
| 223 |
} |
| 224 |
|
| 225 |
usedAliases[ primaryName ].push( alias ); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
let parameters = spec.getCanonicalParameterOrder(); |
| 230 |
|
| 231 |
|
| 232 |
for ( primaryName in usedAliases ) { |
| 233 |
const i = parameters.indexOf( primaryName ); |
| 234 |
|
| 235 |
parameters = parameters.slice( 0, i ).concat( usedAliases[ primaryName ], |
| 236 |
parameters.slice( i + 1 ) ); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
if ( '' in this.params ) { |
| 241 |
parameters.push( '' ); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
return parameters; |
| 246 |
}; |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
ve.dm.MWTemplateModel.prototype.getOrderedParameterNames = function () { |
| 258 |
if ( !this.orderedParameterNames ) { |
| 259 |
const params = this.params; |
| 260 |
this.orderedParameterNames = this.getAllParametersOrdered().filter( ( name ) => name in params ); |
| 261 |
} |
| 262 |
return this.orderedParameterNames; |
| 263 |
}; |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
ve.dm.MWTemplateModel.prototype.addParameter = function ( param ) { |
| 271 |
const name = param.getName(); |
| 272 |
if ( name in this.params ) { |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
this.orderedParameterNames = null; |
| 277 |
this.params[ name ] = param; |
| 278 |
this.spec.fillFromTemplate(); |
| 279 |
|
| 280 |
|
| 281 |
param.connect( this, { change: [ 'emit', 'change' ] } ); |
| 282 |
this.emit( 'add', param ); |
| 283 |
this.emit( 'change' ); |
| 284 |
}; |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
ve.dm.MWTemplateModel.prototype.removeParameter = function ( param ) { |
| 295 |
if ( param ) { |
| 296 |
this.orderedParameterNames = null; |
| 297 |
delete this.params[ param.getName() ]; |
| 298 |
param.disconnect( this ); |
| 299 |
this.emit( 'remove', param ); |
| 300 |
this.emit( 'change' ); |
| 301 |
} |
| 302 |
}; |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
ve.dm.MWTemplateModel.prototype.addPromptedParameters = function () { |
| 308 |
const params = this.params, |
| 309 |
spec = this.spec, |
| 310 |
names = spec.getKnownParameterNames(); |
| 311 |
|
| 312 |
for ( let i = 0; i < names.length; i++ ) { |
| 313 |
const name = names[ i ]; |
| 314 |
const foundAlias = spec.getParameterAliases( name ).some( ( alias ) => alias in params ); |
| 315 |
if ( |
| 316 |
!foundAlias && |
| 317 |
!params[ name ] && |
| 318 |
( |
| 319 |
spec.isParameterRequired( name ) || |
| 320 |
spec.isParameterSuggested( name ) |
| 321 |
) |
| 322 |
) { |
| 323 |
this.addParameter( new ve.dm.MWParameterModel( this, names[ i ] ) ); |
| 324 |
} |
| 325 |
} |
| 326 |
}; |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
ve.dm.MWTemplateModel.prototype.setOriginalData = function ( data ) { |
| 336 |
this.originalData = data; |
| 337 |
}; |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
ve.dm.MWTemplateModel.prototype.serialize = function () { |
| 343 |
const origData = this.originalData || {}, |
| 344 |
origParams = origData.params || {}, |
| 345 |
template = { target: this.target, params: {} }, |
| 346 |
spec = this.spec, |
| 347 |
params = this.params; |
| 348 |
|
| 349 |
for ( const name in params ) { |
| 350 |
if ( name === '' ) { |
| 351 |
continue; |
| 352 |
} |
| 353 |
|
| 354 |
if ( |
| 355 |
|
| 356 |
params[ name ].getValue() === '' && |
| 357 |
it |
| 358 |
!Object.prototype.hasOwnProperty.call( origParams, name ) && |
| 359 |
9) |
| 360 |
!spec.isParameterRequired( name ) |
| 361 |
) { |
| 362 |
continue; |
| 363 |
} |
| 364 |
|
| 365 |
const origName = params[ name ].getOriginalName(); |
| 366 |
template.params[ origName ] = ve.extendObject( |
| 367 |
{}, |
| 368 |
origParams[ origName ], |
| 369 |
{ wt: params[ name ].getValue() } |
| 370 |
); |
| 371 |
|
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
return { template: ve.extendObject( {}, origData, template ) }; |
| 377 |
}; |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
ve.dm.MWTemplateModel.prototype.containsValuableData = function () { |
| 383 |
const params = this.params; |
| 384 |
|
| 385 |
return Object.keys( params ).some( ( name ) => { |
| 386 |
|
| 387 |
if ( !name ) { |
| 388 |
return false; |
| 389 |
} |
| 390 |
|
| 391 |
const param = params[ name ], |
| 392 |
value = param.getValue(); |
| 393 |
return value && |
| 394 |
|
| 395 |
value !== param.getAutoValue() && |
| 396 |
|
| 397 |
value !== param.getDefaultValue(); |
| 398 |
} ); |
| 399 |
}; |