| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
function Model() { |
| 10 |
|
| 11 |
OO.EventEmitter.call( this ); |
| 12 |
|
| 13 |
|
| 14 |
this.description = {}; |
| 15 |
|
| 16 |
this.maps = undefined; |
| 17 |
this.mapsChanged = false; |
| 18 |
this.originalMaps = undefined; |
| 19 |
|
| 20 |
this.format = null; |
| 21 |
|
| 22 |
this.params = {}; |
| 23 |
this.paramIdentifierCounter = 2; |
| 24 |
this.sourceCodeParameters = []; |
| 25 |
this.paramOrder = []; |
| 26 |
this.paramOrderChanged = false; |
| 27 |
|
| 28 |
this.originalTemplateDataObject = null; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
OO.mixinClass( Model, OO.EventEmitter ); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 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 |
Model.static.compare = function ( obj1, obj2, allowSubset ) { |
| 78 |
if ( obj1 === obj2 || ( allowSubset && obj2 === undefined ) ) { |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
if ( typeof obj1 !== typeof obj2 ) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
return typeof obj1 === 'object' && OO.compare( obj2, obj1, allowSubset ); |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
Model.static.translateObsoleteParamTypes = function ( paramType ) { |
| 96 |
return paramType.replace( /^string\//, '' ); |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
Model.static.getAllProperties = function ( getFullData ) { |
| 109 |
const properties = { |
| 110 |
name: { |
| 111 |
type: 'string', |
| 112 |
|
| 113 |
restrict: /[|=]|}}/ |
| 114 |
}, |
| 115 |
aliases: { |
| 116 |
type: 'array' |
| 117 |
}, |
| 118 |
label: { |
| 119 |
type: 'string', |
| 120 |
allowLanguages: true |
| 121 |
}, |
| 122 |
description: { |
| 123 |
type: 'string', |
| 124 |
allowLanguages: true |
| 125 |
}, |
| 126 |
example: { |
| 127 |
type: 'string', |
| 128 |
allowLanguages: true |
| 129 |
}, |
| 130 |
type: { |
| 131 |
type: 'select', |
| 132 |
children: [ |
| 133 |
'unknown', |
| 134 |
'boolean', |
| 135 |
'content', |
| 136 |
'wiki-file-name', |
| 137 |
'line', |
| 138 |
'number', |
| 139 |
'date', |
| 140 |
'wiki-page-name', |
| 141 |
'string', |
| 142 |
'wiki-template-name', |
| 143 |
'unbalanced-wikitext', |
| 144 |
'url', |
| 145 |
'wiki-user-name' |
| 146 |
], |
| 147 |
default: 'unknown' |
| 148 |
}, |
| 149 |
suggestedvalues: { |
| 150 |
type: 'array' |
| 151 |
}, |
| 152 |
default: { |
| 153 |
type: 'string', |
| 154 |
multiline: true, |
| 155 |
allowLanguages: true |
| 156 |
}, |
| 157 |
autovalue: { |
| 158 |
type: 'string' |
| 159 |
}, |
| 160 |
status: { |
| 161 |
type: 'select', |
| 162 |
children: [ |
| 163 |
'optional', |
| 164 |
'deprecated', |
| 165 |
'required', |
| 166 |
'suggested' |
| 167 |
], |
| 168 |
default: 'optional' |
| 169 |
}, |
| 170 |
deprecated: { |
| 171 |
type: 'boolean', |
| 172 |
|
| 173 |
|
| 174 |
textValue: 'deprecatedValue' |
| 175 |
}, |
| 176 |
deprecatedValue: { |
| 177 |
type: 'string' |
| 178 |
}, |
| 179 |
required: { |
| 180 |
type: 'boolean' |
| 181 |
}, |
| 182 |
suggested: { |
| 183 |
type: 'boolean' |
| 184 |
} |
| 185 |
}; |
| 186 |
|
| 187 |
if ( !getFullData ) { |
| 188 |
return Object.keys( properties ); |
| 189 |
} else { |
| 190 |
return properties; |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
Model.static.getPropertiesWithLanguage = function () { |
| 200 |
const result = [], |
| 201 |
propDefinitions = this.getAllProperties( true ); |
| 202 |
|
| 203 |
for ( const prop in propDefinitions ) { |
| 204 |
if ( propDefinitions[ prop ].allowLanguages ) { |
| 205 |
result.push( prop ); |
| 206 |
} |
| 207 |
} |
| 208 |
return result; |
| 209 |
}; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
Model.static.splitAndTrimArray = function ( str, delim ) { |
| 219 |
delim = delim || mw.msg( 'comma-separator' ); |
| 220 |
|
| 221 |
const arr = []; |
| 222 |
str.split( delim ).forEach( ( part ) => { |
| 223 |
const trimmed = part.trim(); |
| 224 |
if ( trimmed ) { |
| 225 |
arr.push( trimmed ); |
| 226 |
} |
| 227 |
} ); |
| 228 |
|
| 229 |
return arr; |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
Model.static.arrayUnionWithoutEmpty = function () { |
| 240 |
const result = OO.simpleArrayUnion.apply( this, arguments ); |
| 241 |
|
| 242 |
|
| 243 |
return result.filter( ( i ) => i.trim() ); |
| 244 |
}; |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
Model.static.newFromObject = function ( tdObject, paramsInSource ) { |
| 254 |
const model = new Model(); |
| 255 |
|
| 256 |
model.setSourceCodeParameters( paramsInSource || [] ); |
| 257 |
|
| 258 |
|
| 259 |
model.setOriginalTemplateDataObject( tdObject ); |
| 260 |
|
| 261 |
tdObject = tdObject || { params: {} }; |
| 262 |
|
| 263 |
|
| 264 |
model.params = {}; |
| 265 |
|
| 266 |
|
| 267 |
if ( tdObject.params ) { |
| 268 |
for ( const param in tdObject.params ) { |
| 269 |
model.addParam( param, tdObject.params[ param ] ); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
if ( tdObject.maps ) { |
| 275 |
model.setMapInfo( tdObject.maps ); |
| 276 |
} |
| 277 |
|
| 278 |
model.setTemplateDescription( tdObject.description ); |
| 279 |
|
| 280 |
|
| 281 |
if ( tdObject.paramOrder && tdObject.paramOrder.length > 0 ) { |
| 282 |
model.setTemplateParamOrder( tdObject.paramOrder ); |
| 283 |
} |
| 284 |
|
| 285 |
if ( tdObject.format !== undefined ) { |
| 286 |
model.setTemplateFormat( tdObject.format ); |
| 287 |
} |
| 288 |
|
| 289 |
return model; |
| 290 |
}; |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
Model.prototype.getMissingParams = function () { |
| 303 |
const allParamNames = this.getAllParamNames(), |
| 304 |
sourceCodeParameters = this.sourceCodeParameters; |
| 305 |
|
| 306 |
return sourceCodeParameters.filter( ( sourceCodeParameter ) => allParamNames.indexOf( sourceCodeParameter ) === -1 ); |
| 307 |
}; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
Model.prototype.importSourceCodeParameters = function () { |
| 315 |
const allParamNames = this.getAllParamNames(), |
| 316 |
existingArray = [], |
| 317 |
importedArray = [], |
| 318 |
skippedArray = []; |
| 319 |
|
| 320 |
|
| 321 |
allParamNames.forEach( ( paramKey ) => { |
| 322 |
if ( this.sourceCodeParameters.indexOf( paramKey ) !== -1 ) { |
| 323 |
existingArray.push( paramKey ); |
| 324 |
} |
| 325 |
} ); |
| 326 |
|
| 327 |
|
| 328 |
this.sourceCodeParameters.forEach( ( sourceCodeParameter ) => { |
| 329 |
if ( existingArray.indexOf( sourceCodeParameter ) === -1 ) { |
| 330 |
this.addParam( sourceCodeParameter ); |
| 331 |
importedArray.push( sourceCodeParameter ); |
| 332 |
} else { |
| 333 |
skippedArray.push( sourceCodeParameter ); |
| 334 |
} |
| 335 |
} ); |
| 336 |
|
| 337 |
return { |
| 338 |
imported: importedArray, |
| 339 |
skipped: skippedArray, |
| 340 |
existing: existingArray |
| 341 |
}; |
| 342 |
}; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
Model.prototype.getExistingLanguageCodes = function () { |
| 350 |
let result = []; |
| 351 |
|
| 352 |
|
| 353 |
if ( $.isPlainObject( this.description ) ) { |
| 354 |
result = Object.keys( this.description ); |
| 355 |
} |
| 356 |
|
| 357 |
const languageProps = this.constructor.static.getPropertiesWithLanguage(); |
| 358 |
|
| 359 |
for ( const param in this.params ) { |
| 360 |
|
| 361 |
for ( const prop in this.params[ param ] ) { |
| 362 |
if ( languageProps.indexOf( prop ) !== -1 ) { |
| 363 |
result = this.constructor.static.arrayUnionWithoutEmpty( result, Object.keys( this.params[ param ][ prop ] ) ); |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
return result; |
| 369 |
}; |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
Model.prototype.addParam = function ( name, paramData ) { |
| 380 |
const data = $.extend( true, {}, paramData ); |
| 381 |
const key = this.getNewValidParameterKey( name ); |
| 382 |
|
| 383 |
|
| 384 |
this.params[ key ] = { name: name }; |
| 385 |
|
| 386 |
|
| 387 |
if ( this.sourceCodeParameters.indexOf( key ) !== -1 ) { |
| 388 |
this.params[ key ].inSource = true; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
if ( data.type !== undefined ) { |
| 393 |
data.type = this.constructor.static.translateObsoleteParamTypes( data.type ); |
| 394 |
this.params[ key ].type = data.type; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
if ( typeof data.deprecated === 'string' ) { |
| 399 |
this.params[ key ].deprecatedValue = data.deprecated; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
if ( data ) { |
| 404 |
const language = this.getDefaultLanguage(); |
| 405 |
const propertiesWithLanguage = this.constructor.static.getPropertiesWithLanguage(); |
| 406 |
const allProps = this.constructor.static.getAllProperties( true ); |
| 407 |
for ( const prop in data ) { |
| 408 |
let propToSet = prop; |
| 409 |
if ( |
| 410 |
|
| 411 |
|
| 412 |
allProps[ prop ] && |
| 413 |
|
| 414 |
|
| 415 |
allProps[ prop ].textValue |
| 416 |
) { |
| 417 |
|
| 418 |
propToSet = allProps[ prop ].textValue; |
| 419 |
|
| 420 |
this.setParamProperty( key, prop, !!data[ prop ], language ); |
| 421 |
if ( typeof data[ prop ] === 'boolean' ) { |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
continue; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
if ( |
| 430 |
propertiesWithLanguage.indexOf( propToSet ) !== -1 && |
| 431 |
$.isPlainObject( data[ prop ] ) |
| 432 |
) { |
| 433 |
|
| 434 |
for ( const lang in data[ prop ] ) { |
| 435 |
this.setParamProperty( key, propToSet, data[ prop ], lang ); |
| 436 |
} |
| 437 |
} else { |
| 438 |
this.setParamProperty( key, propToSet, data[ prop ], language ); |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
this.addKeyTemplateParamOrder( key ); |
| 445 |
|
| 446 |
|
| 447 |
this.emit( 'add-param', key, this.params[ key ] ); |
| 448 |
this.emit( 'change' ); |
| 449 |
}; |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
Model.prototype.getAllParamNames = function () { |
| 458 |
let result = []; |
| 459 |
for ( const key in this.params ) { |
| 460 |
const param = this.params[ key ]; |
| 461 |
result.push( param.name ); |
| 462 |
if ( param.aliases ) { |
| 463 |
result = result.concat( param.aliases ); |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
return result; |
| 468 |
}; |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
Model.prototype.setTemplateDescription = function ( desc, language ) { |
| 480 |
language = language || this.getDefaultLanguage(); |
| 481 |
|
| 482 |
if ( !this.constructor.static.compare( this.description[ language ], desc ) ) { |
| 483 |
if ( typeof desc === 'object' ) { |
| 484 |
Object.assign( this.description, desc ); |
| 485 |
this.emit( 'change-description', desc[ language ], language ); |
| 486 |
} else { |
| 487 |
this.description[ language ] = desc; |
| 488 |
this.emit( 'change-description', desc, language ); |
| 489 |
} |
| 490 |
this.emit( 'change' ); |
| 491 |
} |
| 492 |
}; |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
Model.prototype.getTemplateDescription = function ( language ) { |
| 501 |
language = language || this.getDefaultLanguage(); |
| 502 |
return this.description[ language ]; |
| 503 |
}; |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
Model.prototype.setMapInfo = function ( map ) { |
| 511 |
if ( map !== undefined ) { |
| 512 |
if ( !this.constructor.static.compare( this.maps, map ) ) { |
| 513 |
if ( this.mapsChanged === false ) { |
| 514 |
this.originalMaps = OO.copy( map ); |
| 515 |
this.mapsChanged = true; |
| 516 |
} |
| 517 |
this.maps = map; |
| 518 |
this.emit( 'change-map', map ); |
| 519 |
this.emit( 'change' ); |
| 520 |
} |
| 521 |
} |
| 522 |
}; |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
Model.prototype.getMapInfo = function () { |
| 530 |
return this.maps; |
| 531 |
}; |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
Model.prototype.getOriginalMapsInfo = function () { |
| 539 |
return this.originalMaps; |
| 540 |
}; |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
Model.prototype.getParamValue = function ( paramKey, property, language ) { |
| 551 |
language = language || this.getDefaultLanguage(); |
| 552 |
return OO.getProp( this.params, paramKey, property, language ) || ''; |
| 553 |
}; |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
Model.prototype.getDefaultLanguage = function () { |
| 561 |
return mw.config.get( 'wgContentLanguage' ) || 'en'; |
| 562 |
}; |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
Model.prototype.setTemplateParamOrder = function ( orderArray ) { |
| 572 |
orderArray = orderArray || []; |
| 573 |
|
| 574 |
|
| 575 |
this.paramOrder = orderArray.slice(); |
| 576 |
this.emit( 'change-paramOrder', orderArray ); |
| 577 |
this.emit( 'change' ); |
| 578 |
}; |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
Model.prototype.setTemplateFormat = function ( format ) { |
| 588 |
format = format || null; |
| 589 |
if ( this.format !== format ) { |
| 590 |
this.format = format; |
| 591 |
this.emit( 'change-format', format ); |
| 592 |
this.emit( 'change' ); |
| 593 |
} |
| 594 |
}; |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
Model.prototype.addKeyTemplateParamOrder = function ( key ) { |
| 604 |
if ( this.paramOrder.indexOf( key ) === -1 ) { |
| 605 |
this.paramOrder.push( key ); |
| 606 |
this.emit( 'add-paramOrder', key ); |
| 607 |
this.emit( 'change' ); |
| 608 |
} |
| 609 |
}; |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
Model.prototype.reorderParamOrderKey = function ( key, newIndex ) { |
| 620 |
const keyIndex = this.paramOrder.indexOf( key ); |
| 621 |
|
| 622 |
this.paramOrder.splice( |
| 623 |
newIndex - ( newIndex > keyIndex ? 1 : 0 ), |
| 624 |
0, |
| 625 |
this.paramOrder.splice( keyIndex, 1 )[ 0 ] |
| 626 |
); |
| 627 |
|
| 628 |
this.paramOrderChanged = true; |
| 629 |
|
| 630 |
|
| 631 |
this.emit( 'change-paramOrder', this.paramOrder ); |
| 632 |
this.emit( 'change' ); |
| 633 |
}; |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
Model.prototype.removeKeyTemplateParamOrder = function ( key ) { |
| 643 |
const keyPos = this.paramOrder.indexOf( key ); |
| 644 |
if ( keyPos > -1 ) { |
| 645 |
this.paramOrder.splice( keyPos, 1 ); |
| 646 |
this.emit( 'change-paramOrder', this.paramOrder ); |
| 647 |
this.emit( 'change' ); |
| 648 |
} |
| 649 |
}; |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
Model.prototype.getTemplateParamOrder = function () { |
| 657 |
return this.paramOrder; |
| 658 |
}; |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
Model.prototype.getTemplateFormat = function () { |
| 666 |
return this.format; |
| 667 |
}; |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
Model.prototype.setParamProperty = function ( paramKey, prop, value, language ) { |
| 681 |
const allProps = this.constructor.static.getAllProperties( true ); |
| 682 |
let status = false; |
| 683 |
|
| 684 |
language = language || this.getDefaultLanguage(); |
| 685 |
if ( !allProps[ prop ] ) { |
| 686 |
|
| 687 |
return status; |
| 688 |
} |
| 689 |
|
| 690 |
const propertiesWithLanguage = this.constructor.static.getPropertiesWithLanguage(); |
| 691 |
|
| 692 |
if ( propertiesWithLanguage.indexOf( prop ) !== -1 ) { |
| 693 |
|
| 694 |
if ( !$.isPlainObject( this.params[ paramKey ][ prop ] ) ) { |
| 695 |
this.params[ paramKey ][ prop ] = {}; |
| 696 |
} |
| 697 |
value = $.isPlainObject( value ) ? value[ language ] : value; |
| 698 |
|
| 699 |
if ( !this.constructor.static.compare( this.params[ paramKey ][ prop ][ language ], value ) ) { |
| 700 |
this.params[ paramKey ][ prop ][ language ] = value; |
| 701 |
this.emit( 'change-property', paramKey, prop, value, language ); |
| 702 |
this.emit( 'change' ); |
| 703 |
status = true; |
| 704 |
} |
| 705 |
} else { |
| 706 |
|
| 707 |
if ( !this.constructor.static.compare( this.params[ paramKey ][ prop ], value ) ) { |
| 708 |
const oldValue = this.params[ paramKey ][ prop ]; |
| 709 |
this.params[ paramKey ][ prop ] = value; |
| 710 |
|
| 711 |
let newKey = value; |
| 712 |
if ( prop === 'name' && oldValue !== value ) { |
| 713 |
|
| 714 |
if ( this.params[ newKey ] && !this.params[ newKey ].deleted ) { |
| 715 |
|
| 716 |
newKey = this.getNewValidParameterKey( newKey ); |
| 717 |
} |
| 718 |
|
| 719 |
this.params[ newKey ] = this.params[ paramKey ]; |
| 720 |
|
| 721 |
this.params[ paramKey ] = { deleted: true }; |
| 722 |
} |
| 723 |
|
| 724 |
this.emit( 'change-property', paramKey, prop, value, language ); |
| 725 |
this.emit( 'change' ); |
| 726 |
|
| 727 |
if ( prop === 'name' ) { |
| 728 |
this.paramOrder[ this.paramOrder.indexOf( paramKey ) ] = newKey; |
| 729 |
this.paramOrderChanged = true; |
| 730 |
this.emit( 'change-paramOrder', this.paramOrder ); |
| 731 |
this.emit( 'change' ); |
| 732 |
} |
| 733 |
|
| 734 |
status = true; |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
if ( allProps[ prop ].textValue && value === false ) { |
| 739 |
|
| 740 |
status = this.setParamProperty( paramKey, allProps[ prop ].textValue, '', language ); |
| 741 |
} |
| 742 |
|
| 743 |
return status; |
| 744 |
}; |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
Model.prototype.deleteParam = function ( paramKey ) { |
| 756 |
this.params[ paramKey ].deleted = true; |
| 757 |
|
| 758 |
this.removeKeyTemplateParamOrder( paramKey ); |
| 759 |
this.emit( 'delete-param', paramKey ); |
| 760 |
this.emit( 'change' ); |
| 761 |
}; |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
Model.prototype.emptyParamData = function ( paramKey ) { |
| 769 |
if ( this.params[ paramKey ] ) { |
| 770 |
|
| 771 |
delete this.params[ paramKey ]; |
| 772 |
this.addParam( paramKey ); |
| 773 |
|
| 774 |
this.params[ paramKey ].emptied = true; |
| 775 |
} |
| 776 |
}; |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
Model.prototype.getParamProperty = function ( paramKey, prop ) { |
| 787 |
if ( this.params[ paramKey ] ) { |
| 788 |
return this.params[ paramKey ][ prop ]; |
| 789 |
} |
| 790 |
return null; |
| 791 |
}; |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
Model.prototype.getParamData = function ( key ) { |
| 800 |
return this.params[ key ]; |
| 801 |
}; |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
Model.prototype.getParams = function () { |
| 809 |
return this.params; |
| 810 |
}; |
| 811 |
|
| 812 |
Model.prototype.isParamDeleted = function ( key ) { |
| 813 |
return this.params[ key ] && this.params[ key ].deleted === true; |
| 814 |
}; |
| 815 |
|
| 816 |
Model.prototype.isParamExists = function ( key ) { |
| 817 |
return Object.prototype.hasOwnProperty.call( this.params, key ); |
| 818 |
}; |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
Model.prototype.setOriginalTemplateDataObject = function ( templatedataObj ) { |
| 826 |
this.originalTemplateDataObject = templatedataObj ? $.extend( true, {}, templatedataObj ) : null; |
| 827 |
}; |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
Model.prototype.setFullPageName = function ( pageName ) { |
| 835 |
this.fullPageName = pageName; |
| 836 |
}; |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
Model.prototype.setParentPage = function ( parent ) { |
| 844 |
this.parentPage = parent; |
| 845 |
}; |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
Model.prototype.getFullPageName = function () { |
| 853 |
return this.fullPageName; |
| 854 |
}; |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
Model.prototype.getParentPage = function () { |
| 862 |
return this.parentPage; |
| 863 |
}; |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
Model.prototype.restoreOriginalMaps = function () { |
| 869 |
this.setMapInfo( this.getOriginalMapsInfo() ); |
| 870 |
}; |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
Model.prototype.getOriginalTemplateDataObject = function () { |
| 879 |
return this.originalTemplateDataObject; |
| 880 |
}; |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
Model.prototype.outputTemplateData = function () { |
| 888 |
const allProps = this.constructor.static.getAllProperties( true ), |
| 889 |
original = this.getOriginalTemplateDataObject() || {}; |
| 890 |
original.params = original.params || {}; |
| 891 |
const result = $.extend( true, {}, original ), |
| 892 |
defaultLang = this.getDefaultLanguage(); |
| 893 |
|
| 894 |
let normalizedValue; |
| 895 |
|
| 896 |
if ( this.description[ defaultLang ] !== undefined ) { |
| 897 |
normalizedValue = this.propRemoveUnusedLanguages( this.description ); |
| 898 |
if ( this.isOutputInLanguageObject( result.description, normalizedValue ) ) { |
| 899 |
result.description = normalizedValue; |
| 900 |
} else { |
| 901 |
|
| 902 |
result.description = normalizedValue[ defaultLang ]; |
| 903 |
} |
| 904 |
} else { |
| 905 |
|
| 906 |
delete result.description; |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
if ( this.maps === undefined || Object.keys( this.maps ).length === 0 ) { |
| 911 |
delete result.maps; |
| 912 |
} else { |
| 913 |
result.maps = this.maps; |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
if ( original.paramOrder || this.paramOrderChanged ) { |
| 918 |
result.paramOrder = this.paramOrder; |
| 919 |
} else { |
| 920 |
delete result.paramOrder; |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
if ( this.format === null ) { |
| 925 |
delete result.format; |
| 926 |
} else { |
| 927 |
result.format = this.format; |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
if ( original.sets ) { |
| 933 |
result.sets = original.sets; |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
for ( const paramKey in this.params ) { |
| 938 |
const key = paramKey; |
| 939 |
if ( this.params[ key ].deleted ) { |
| 940 |
delete result.params[ key ]; |
| 941 |
continue; |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
if ( this.params[ key ].emptied ) { |
| 947 |
delete result.params[ key ]; |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
const name = this.params[ key ].name; |
| 952 |
const oldKey = key; |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
if ( !result.params[ name ] ) { |
| 963 |
|
| 964 |
result.params[ name ] = {}; |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
for ( const prop in allProps ) { |
| 969 |
if ( prop === 'status' || prop === 'deprecatedValue' || prop === 'name' ) { |
| 970 |
continue; |
| 971 |
} |
| 972 |
|
| 973 |
switch ( allProps[ prop ].type ) { |
| 974 |
case 'select': |
| 975 |
|
| 976 |
|
| 977 |
if ( |
| 978 |
original.params[ key ] && |
| 979 |
original.params[ key ][ prop ] !== 'unknown' && |
| 980 |
this.params[ key ][ prop ] === 'unknown' |
| 981 |
) { |
| 982 |
result.params[ name ][ prop ] = undefined; |
| 983 |
} else { |
| 984 |
result.params[ name ][ prop ] = this.params[ key ][ prop ]; |
| 985 |
} |
| 986 |
break; |
| 987 |
case 'boolean': |
| 988 |
if ( !this.params[ key ][ prop ] ) { |
| 989 |
|
| 990 |
|
| 991 |
if ( original.params[ oldKey ] && original.params[ oldKey ][ prop ] === false ) { |
| 992 |
result.params[ name ][ prop ] = false; |
| 993 |
} else { |
| 994 |
|
| 995 |
delete result.params[ name ][ prop ]; |
| 996 |
} |
| 997 |
} else { |
| 998 |
if ( prop === 'deprecated' ) { |
| 999 |
result.params[ name ][ prop ] = this.params[ key ].deprecatedValue || true; |
| 1000 |
|
| 1001 |
delete result.params[ name ].deprecatedValue; |
| 1002 |
} else { |
| 1003 |
result.params[ name ][ prop ] = this.params[ key ][ prop ]; |
| 1004 |
} |
| 1005 |
} |
| 1006 |
break; |
| 1007 |
case 'array': |
| 1008 |
|
| 1009 |
|
| 1010 |
if ( |
| 1011 |
Array.isArray( this.params[ key ][ prop ] ) && |
| 1012 |
this.params[ key ][ prop ].length > 0 |
| 1013 |
) { |
| 1014 |
result.params[ name ][ prop ] = this.params[ key ][ prop ]; |
| 1015 |
} else { |
| 1016 |
|
| 1017 |
delete result.params[ name ][ prop ]; |
| 1018 |
} |
| 1019 |
break; |
| 1020 |
default: |
| 1021 |
|
| 1022 |
if ( this.params[ key ][ prop ] !== undefined ) { |
| 1023 |
const compareOrig = original.params[ oldKey ] && original.params[ oldKey ][ prop ]; |
| 1024 |
if ( allProps[ prop ].allowLanguages ) { |
| 1025 |
normalizedValue = this.propRemoveUnusedLanguages( this.params[ key ][ prop ] ); |
| 1026 |
|
| 1027 |
if ( this.isOutputInLanguageObject( compareOrig || {}, normalizedValue ) ) { |
| 1028 |
result.params[ name ][ prop ] = normalizedValue; |
| 1029 |
} else { |
| 1030 |
|
| 1031 |
result.params[ name ][ prop ] = normalizedValue[ defaultLang ]; |
| 1032 |
} |
| 1033 |
} else if ( this.params[ key ][ prop ] || |
| 1034 |
|
| 1035 |
compareOrig !== undefined |
| 1036 |
) { |
| 1037 |
|
| 1038 |
result.params[ name ][ prop ] = this.params[ key ][ prop ]; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
break; |
| 1042 |
} |
| 1043 |
} |
| 1044 |
} |
| 1045 |
return result; |
| 1046 |
}; |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
Model.prototype.getNewValidParameterKey = function ( key ) { |
| 1056 |
if ( this.params[ key ] || this.getAllParamNames().indexOf( key ) !== -1 ) { |
| 1057 |
|
| 1058 |
if ( /\d$/.test( key ) ) { |
| 1059 |
key += '-'; |
| 1060 |
} |
| 1061 |
key += this.paramIdentifierCounter; |
| 1062 |
this.paramIdentifierCounter++; |
| 1063 |
return this.getNewValidParameterKey( key ); |
| 1064 |
} |
| 1065 |
|
| 1066 |
return key; |
| 1067 |
}; |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
Model.prototype.propRemoveUnusedLanguages = function ( propData ) { |
| 1075 |
const result = {}; |
| 1076 |
if ( $.isPlainObject( propData ) ) { |
| 1077 |
for ( const key in propData ) { |
| 1078 |
if ( propData[ key ] ) { |
| 1079 |
result[ key ] = propData[ key ]; |
| 1080 |
} |
| 1081 |
} |
| 1082 |
} |
| 1083 |
return result; |
| 1084 |
}; |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
Model.prototype.isOutputInLanguageObject = function ( originalPropValue, newPropValue ) { |
| 1095 |
if ( |
| 1096 |
( |
| 1097 |
|
| 1098 |
typeof originalPropValue === 'object' && |
| 1099 |
|
| 1100 |
!$.isEmptyObject( originalPropValue ) |
| 1101 |
) || |
| 1102 |
( |
| 1103 |
|
| 1104 |
typeof newPropValue === 'object' && |
| 1105 |
|
| 1106 |
!$.isEmptyObject( newPropValue ) && |
| 1107 |
( |
| 1108 |
|
| 1109 |
newPropValue[ this.getDefaultLanguage() ] === undefined || |
| 1110 |
|
| 1111 |
Object.keys( newPropValue ).length > 1 |
| 1112 |
) |
| 1113 |
) |
| 1114 |
) { |
| 1115 |
return true; |
| 1116 |
} |
| 1117 |
return false; |
| 1118 |
}; |
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
Model.prototype.setSourceCodeParameters = function ( sourceParams ) { |
| 1126 |
this.sourceCodeParameters = sourceParams; |
| 1127 |
}; |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
Model.prototype.getSourceCodeParameters = function () { |
| 1135 |
return this.sourceCodeParameters; |
| 1136 |
}; |
| 1137 |
|
| 1138 |
module.exports = Model; |