2015-02-17 23:56:44 +00:00
|
|
|
/**
|
2015-03-07 00:17:21 +00:00
|
|
|
* TemplateData Model
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-03-07 01:20:26 +00:00
|
|
|
* @class
|
|
|
|
* @mixins OO.EventEmitter
|
|
|
|
*
|
|
|
|
* @constructor
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {Object} config Dialog configuration object
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model = function mwTemplateDataModel( config ) {
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
OO.EventEmitter.call( this );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.params = {};
|
|
|
|
this.description = {};
|
|
|
|
this.paramOrder = [];
|
2015-10-02 10:34:57 +00:00
|
|
|
this.format = 'inline';
|
2015-02-17 23:56:44 +00:00
|
|
|
this.paramOrderChanged = false;
|
|
|
|
this.paramIdentifierCounter = 0;
|
|
|
|
this.originalTemplateDataObject = null;
|
|
|
|
this.sourceCodeParameters = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Setup */
|
|
|
|
OO.initClass( mw.TemplateData.Model );
|
|
|
|
OO.mixinClass( mw.TemplateData.Model, OO.EventEmitter );
|
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event add-param
|
|
|
|
* @param {string} key Parameter key
|
|
|
|
* @param {Object} data Parameter data
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event change-description
|
|
|
|
* @param {string} description New template description
|
|
|
|
* @param {Object} [language] Description language, if supplied
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event change-paramOrder
|
|
|
|
* @param {string[]} orderArray Parameter key array in order
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event change-property
|
|
|
|
* @param {string} paramKey Parameter key
|
|
|
|
* @param {string} prop Property name
|
2015-09-17 20:10:33 +00:00
|
|
|
* @param {...Mixed} value Property value
|
2015-02-17 23:56:44 +00:00
|
|
|
*/
|
|
|
|
|
2016-01-11 23:26:54 +00:00
|
|
|
/**
|
|
|
|
* @event change
|
|
|
|
*/
|
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
/* Static Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare two objects or strings
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {Object|string} obj1 Base object
|
|
|
|
* @param {Object|string} obj2 Compared object
|
|
|
|
* @param {boolean} [allowSubset] Allow the second object to be a
|
|
|
|
* partial object (or a subset) of the first.
|
|
|
|
* @return {boolean} Objects have equal values
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.static.compare = function ( obj1, obj2, allowSubset ) {
|
|
|
|
if ( allowSubset && obj2 === undefined ) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-10-11 05:22:21 +00:00
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
// Make sure the objects are of the same type
|
|
|
|
if ( $.type( obj1 ) !== $.type( obj2 ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparing objects or arrays
|
|
|
|
if ( typeof obj1 === 'object' ) {
|
|
|
|
return OO.compare( obj2, obj1, allowSubset );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything else (primitive types, functions, etc)
|
|
|
|
return obj1 === obj2;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translate obsolete parameter types into the new types
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} paramType Given type
|
|
|
|
* @return {string} Normalized non-obsolete type
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.static.translateObsoleteParamTypes = function ( paramType ) {
|
|
|
|
switch ( paramType ) {
|
|
|
|
case 'string/wiki-page-name':
|
|
|
|
return 'wiki-page-name';
|
|
|
|
case 'string/wiki-file-name':
|
|
|
|
return 'wiki-file-name';
|
|
|
|
case 'string/wiki-user-name':
|
|
|
|
return 'wiki-user-name';
|
|
|
|
default:
|
|
|
|
return paramType;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve information about all legal properties for a parameter.
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {boolean} getFullData Retrieve full information about each
|
|
|
|
* parameter. If false, the method will return an array of property
|
|
|
|
* names only.
|
|
|
|
* @return {Object|string[]} Legal property names with or without their
|
|
|
|
* definition data
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.static.getAllProperties = function ( getFullData ) {
|
|
|
|
var properties = {
|
|
|
|
name: {
|
|
|
|
type: 'string',
|
|
|
|
// Validation regex
|
|
|
|
restrict: /[\|=]|}}/
|
|
|
|
},
|
|
|
|
aliases: {
|
|
|
|
type: 'array',
|
|
|
|
delimiter: mw.msg( 'comma-separator' )
|
|
|
|
},
|
|
|
|
label: {
|
|
|
|
type: 'string',
|
|
|
|
allowLanguages: true
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
type: 'string',
|
|
|
|
allowLanguages: true
|
|
|
|
},
|
2015-03-04 20:32:38 +00:00
|
|
|
example: {
|
|
|
|
type: 'string',
|
|
|
|
allowLanguages: true
|
|
|
|
},
|
2015-02-17 23:56:44 +00:00
|
|
|
type: {
|
|
|
|
type: 'select',
|
|
|
|
children: [
|
|
|
|
'boolean',
|
|
|
|
'content',
|
|
|
|
'wiki-file-name',
|
|
|
|
'line',
|
|
|
|
'number',
|
|
|
|
'date',
|
|
|
|
'wiki-page-name',
|
|
|
|
'string',
|
2015-09-17 19:28:07 +00:00
|
|
|
'wiki-template-name',
|
2015-02-17 23:56:44 +00:00
|
|
|
'unbalanced-wikitext',
|
|
|
|
'undefined',
|
2015-09-17 19:39:18 +00:00
|
|
|
'url',
|
2015-02-17 23:56:44 +00:00
|
|
|
'wiki-user-name'
|
|
|
|
],
|
|
|
|
'default': 'undefined'
|
|
|
|
},
|
|
|
|
'default': {
|
|
|
|
type: 'string',
|
2015-03-09 21:45:04 +00:00
|
|
|
multiline: true,
|
|
|
|
allowLanguages: true
|
2015-02-17 23:56:44 +00:00
|
|
|
},
|
|
|
|
autovalue: {
|
|
|
|
type: 'string'
|
|
|
|
},
|
|
|
|
deprecated: {
|
2015-03-05 23:33:12 +00:00
|
|
|
type: 'boolean',
|
|
|
|
// This should only be defined for boolean properties.
|
|
|
|
// Define the property that represents the text value.
|
|
|
|
textValue: 'deprecatedValue'
|
|
|
|
},
|
|
|
|
deprecatedValue: {
|
|
|
|
type: 'string',
|
|
|
|
changesBooleanValue: 'deprecated'
|
2015-02-17 23:56:44 +00:00
|
|
|
},
|
|
|
|
required: {
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
suggested: {
|
|
|
|
type: 'boolean'
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
if ( !getFullData ) {
|
|
|
|
return Object.keys( properties );
|
|
|
|
} else {
|
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the list of property names that allow for multiple languages.
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {string[]} Property names
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.static.getPropertiesWithLanguage = function () {
|
|
|
|
var prop,
|
|
|
|
result = [],
|
|
|
|
propDefinitions = this.getAllProperties( true );
|
|
|
|
|
|
|
|
for ( prop in propDefinitions ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( propDefinitions[ prop ].allowLanguages ) {
|
2015-02-17 23:56:44 +00:00
|
|
|
result.push( prop );
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Split a string into an array and clean/trim the values
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} str String to split
|
|
|
|
* @param {string} [delim] Delimeter
|
|
|
|
* @return {string[]} Clean array
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.static.splitAndTrimArray = function ( str, delim ) {
|
|
|
|
var arr = [];
|
2015-06-06 01:21:18 +00:00
|
|
|
delim = delim || mw.msg( 'comma-separator' );
|
2015-02-17 23:56:44 +00:00
|
|
|
|
|
|
|
$.each( str.split( delim ), function () {
|
|
|
|
var trimmed = $.trim( this );
|
|
|
|
if ( trimmed ) {
|
|
|
|
arr.push( trimmed );
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is an adjustment of OO.simpleArrayUnion that ignores
|
|
|
|
* empty values when inserting into the unified array.
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
|
|
|
* @param {...Array} arrays Arrays to union
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {Array} Union of the arrays
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.static.arrayUnionWithoutEmpty = function () {
|
|
|
|
var result = OO.simpleArrayUnion.apply( this, arguments );
|
|
|
|
|
|
|
|
// Trim and filter empty strings
|
|
|
|
return result.filter( function ( i ) {
|
|
|
|
return $.trim( i );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-03-07 00:17:21 +00:00
|
|
|
* Create a new mwTemplateData.Model from templatedata object.
|
2015-02-17 23:56:44 +00:00
|
|
|
*
|
2015-03-07 00:17:21 +00:00
|
|
|
* @param {Object} tdObject TemplateData parsed object
|
|
|
|
* @param {string[]} paramsInSource Parameter names found in template source
|
|
|
|
* @return {mw.TemplateData.Model} New model
|
2015-02-17 23:56:44 +00:00
|
|
|
*/
|
2015-03-07 00:17:21 +00:00
|
|
|
mw.TemplateData.Model.static.newFromObject = function ( tdObject, paramsInSource ) {
|
|
|
|
var param,
|
|
|
|
model = new mw.TemplateData.Model();
|
2015-02-17 23:56:44 +00:00
|
|
|
|
2015-03-07 00:17:21 +00:00
|
|
|
model.setSourceCodeParameters( paramsInSource || [] );
|
2015-02-17 23:56:44 +00:00
|
|
|
|
2015-03-07 00:17:21 +00:00
|
|
|
// Store the original templatedata object for comparison later
|
|
|
|
model.setOriginalTemplateDataObject( tdObject );
|
2015-02-17 23:56:44 +00:00
|
|
|
|
2015-03-07 00:17:21 +00:00
|
|
|
// Initialize the model
|
|
|
|
model.params = {};
|
2015-02-17 23:56:44 +00:00
|
|
|
|
2015-03-07 00:17:21 +00:00
|
|
|
// Add params
|
|
|
|
if ( tdObject.params ) {
|
|
|
|
for ( param in tdObject.params ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
model.addParam( param, tdObject.params[ param ] );
|
2015-03-07 00:17:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
model.setTemplateDescription( tdObject.description );
|
2014-10-11 05:22:21 +00:00
|
|
|
|
2015-03-07 00:17:21 +00:00
|
|
|
// Override the param order if it exists in the templatedata string
|
|
|
|
if ( tdObject.paramOrder && tdObject.paramOrder.length > 0 ) {
|
|
|
|
model.setTemplateParamOrder( tdObject.paramOrder );
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
2015-03-07 00:17:21 +00:00
|
|
|
|
2015-10-02 10:34:57 +00:00
|
|
|
if ( tdObject.format ) {
|
|
|
|
model.setTemplateFormat( tdObject.format );
|
|
|
|
}
|
|
|
|
|
2015-03-07 00:17:21 +00:00
|
|
|
return model;
|
2015-02-17 23:56:44 +00:00
|
|
|
};
|
|
|
|
|
2015-03-07 00:17:21 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
/**
|
|
|
|
* Go over the importable parameters and check if they are
|
|
|
|
* included in the parameter model. Return the parameter names
|
|
|
|
* that are not included yet.
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {string[]} Parameters that are not yet included in
|
|
|
|
* the model
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getMissingParams = function () {
|
|
|
|
var i,
|
|
|
|
result = [],
|
|
|
|
allParamNames = this.getAllParamNames();
|
|
|
|
|
|
|
|
// Check source code params
|
|
|
|
for ( i = 0; i < this.sourceCodeParameters.length; i++ ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( $.inArray( this.sourceCodeParameters[ i ], allParamNames ) === -1 ) {
|
|
|
|
result.push( this.sourceCodeParameters[ i ] );
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add imported parameters into the model
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {Object} Parameters added. -1 for failure.
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.importSourceCodeParameters = function () {
|
|
|
|
var i, paramKey,
|
|
|
|
allParamNames = this.getAllParamNames(),
|
|
|
|
existingArray = [],
|
|
|
|
importedArray = [],
|
|
|
|
skippedArray = [];
|
|
|
|
|
|
|
|
// Check existing params
|
|
|
|
for ( i = 0; i < allParamNames.length; i++ ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
paramKey = allParamNames[ i ];
|
2015-02-17 23:56:44 +00:00
|
|
|
if ( $.inArray( paramKey, this.sourceCodeParameters ) !== -1 ) {
|
|
|
|
existingArray.push( paramKey );
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
2014-10-11 05:22:21 +00:00
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
// Add sourceCodeParameters to the model
|
|
|
|
for ( i = 0; i < this.sourceCodeParameters.length; i++ ) {
|
|
|
|
if (
|
2015-09-17 20:10:33 +00:00
|
|
|
$.inArray( this.sourceCodeParameters[ i ], existingArray ) === -1 &&
|
|
|
|
this.addParam( this.sourceCodeParameters[ i ] )
|
2015-02-17 23:56:44 +00:00
|
|
|
) {
|
2015-09-17 20:10:33 +00:00
|
|
|
importedArray.push( this.sourceCodeParameters[ i ] );
|
2015-02-17 23:56:44 +00:00
|
|
|
} else {
|
2015-09-17 20:10:33 +00:00
|
|
|
skippedArray.push( this.sourceCodeParameters[ i ] );
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
2014-10-11 05:22:21 +00:00
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
return {
|
|
|
|
imported: importedArray,
|
|
|
|
skipped: skippedArray,
|
|
|
|
existing: existingArray
|
2014-10-11 05:22:21 +00:00
|
|
|
};
|
2015-02-17 23:56:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve all existing language codes in the current templatedata model
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {string[]} Language codes in use
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getExistingLanguageCodes = function () {
|
|
|
|
var param, prop, lang,
|
|
|
|
result = [],
|
|
|
|
languageProps = this.constructor.static.getPropertiesWithLanguage();
|
|
|
|
|
|
|
|
// Take languages from the template description
|
|
|
|
if ( $.isPlainObject( this.description ) ) {
|
|
|
|
result.concat( Object.keys( this.description ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go over description
|
|
|
|
if ( $.type( this.description ) ) {
|
|
|
|
for ( lang in this.description ) {
|
|
|
|
result.push( lang );
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Go over the parameters
|
|
|
|
for ( param in this.params ) {
|
|
|
|
// Go over the properties
|
2015-09-17 20:10:33 +00:00
|
|
|
for ( prop in this.params[ param ] ) {
|
2015-02-17 23:56:44 +00:00
|
|
|
if ( $.inArray( prop, languageProps ) !== -1 ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
result = this.constructor.static.arrayUnionWithoutEmpty( result, Object.keys( this.params[ param ][ prop ] ) );
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add parameter to the model
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} key Parameter key
|
2015-09-17 20:10:33 +00:00
|
|
|
* @param {Object} [paramData] Parameter data
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {boolean} Parameter was added successfully
|
2016-01-11 23:26:54 +00:00
|
|
|
* @fires add-param
|
|
|
|
* @fires change
|
2015-02-17 23:56:44 +00:00
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.addParam = function ( key, paramData ) {
|
2015-03-05 23:33:12 +00:00
|
|
|
var prop, name, lang, propToSet,
|
2015-02-17 23:56:44 +00:00
|
|
|
existingNames = this.getAllParamNames(),
|
|
|
|
data = $.extend( true, {}, paramData ),
|
|
|
|
language = this.getDefaultLanguage(),
|
2015-03-05 23:33:12 +00:00
|
|
|
propertiesWithLanguage = this.constructor.static.getPropertiesWithLanguage(),
|
|
|
|
allProps = this.constructor.static.getAllProperties( true );
|
2015-02-17 23:56:44 +00:00
|
|
|
|
|
|
|
name = key;
|
|
|
|
// Check that the parameter is not already in the model
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( this.params[ key ] || $.inArray( key, existingNames ) !== -1 ) {
|
2015-02-17 23:56:44 +00:00
|
|
|
// Change parameter key
|
|
|
|
key = this.getNewValidParameterKey( key );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize
|
2015-09-17 20:10:33 +00:00
|
|
|
this.params[ key ] = {};
|
2015-02-17 23:56:44 +00:00
|
|
|
|
|
|
|
// Store the key
|
2015-09-17 20:10:33 +00:00
|
|
|
this.params[ key ].name = name;
|
2015-02-17 23:56:44 +00:00
|
|
|
|
|
|
|
// Mark the parameter if it is in the template source
|
|
|
|
if ( $.inArray( key, this.sourceCodeParameters ) !== -1 ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
this.params[ key ].inSource = true;
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Translate types
|
2015-03-05 23:33:12 +00:00
|
|
|
if ( data.type !== undefined ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
this.params[ key ].normalizedType = this.constructor.static.translateObsoleteParamTypes( this.params[ key ].type );
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 23:33:12 +00:00
|
|
|
// Get the deprecated value
|
|
|
|
if ( $.type( data.deprecated ) === 'string' ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
this.params[ key ].deprecatedValue = data.deprecated;
|
2015-03-05 23:33:12 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
// Go over the rest of the data
|
|
|
|
if ( data ) {
|
|
|
|
for ( prop in data ) {
|
2015-03-05 23:33:12 +00:00
|
|
|
propToSet = prop;
|
|
|
|
if (
|
|
|
|
// This is to make sure that forwards compatibility is achieved
|
|
|
|
// and the code doesn't die on properties that aren't valid
|
|
|
|
allProps[ prop ] &&
|
|
|
|
// Check if property should have its text represented in another internal property
|
|
|
|
// (for example, deprecated and deprecatedValue)
|
|
|
|
allProps[ prop ].textValue
|
|
|
|
) {
|
|
|
|
// Set the textValue property
|
|
|
|
propToSet = allProps[ prop ].textValue;
|
|
|
|
// Set the boolean value in the current property
|
2015-09-17 20:10:33 +00:00
|
|
|
this.setParamProperty( key, prop, !!data[ prop ], language );
|
|
|
|
if ( $.type( data[ prop ] ) === 'boolean' ) {
|
2015-03-07 00:17:21 +00:00
|
|
|
// Only set the value of the dependent if the value is a string or
|
|
|
|
// language. Otherwise, if the value is boolean, keep the dependent
|
|
|
|
// empty.
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-05 23:33:12 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
if (
|
2015-03-05 23:33:12 +00:00
|
|
|
$.inArray( propToSet, propertiesWithLanguage ) !== -1 &&
|
2015-09-17 20:10:33 +00:00
|
|
|
$.isPlainObject( data[ prop ] )
|
2015-02-17 23:56:44 +00:00
|
|
|
) {
|
|
|
|
// Add all language properties
|
2015-09-17 20:10:33 +00:00
|
|
|
for ( lang in data[ prop ] ) {
|
|
|
|
this.setParamProperty( key, propToSet, data[ prop ], lang );
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-09-17 20:10:33 +00:00
|
|
|
this.setParamProperty( key, propToSet, data[ prop ], language );
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add to paramOrder
|
|
|
|
this.addKeyTemplateParamOrder( key );
|
|
|
|
|
|
|
|
// Trigger the add parameter event
|
2015-09-17 20:10:33 +00:00
|
|
|
this.emit( 'add-param', key, this.params[ key ] );
|
2016-01-11 23:26:54 +00:00
|
|
|
this.emit( 'change' );
|
2015-02-17 23:56:44 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve an array of all used parameter names. Note that parameter
|
|
|
|
* names can be different than their stored keys.
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {string[]} Used parameter names
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getAllParamNames = function () {
|
|
|
|
var param,
|
|
|
|
result = [];
|
|
|
|
for ( param in this.params ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
result.push( this.params[ param ].name );
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the template description
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
|
|
|
* @param {string} desc New template description
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {Object} [language] Description language, if supplied. If not given,
|
|
|
|
* will default to the wiki language.
|
|
|
|
* @fires change-description
|
2016-01-11 23:26:54 +00:00
|
|
|
* @fires change
|
2015-02-17 23:56:44 +00:00
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.setTemplateDescription = function ( desc, language ) {
|
|
|
|
language = language || this.getDefaultLanguage();
|
|
|
|
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( !this.constructor.static.compare( this.description[ language ], desc ) ) {
|
2015-02-17 23:56:44 +00:00
|
|
|
if ( $.type( desc ) === 'object' ) {
|
|
|
|
$.extend( this.description, desc );
|
2015-09-17 20:10:33 +00:00
|
|
|
this.emit( 'change-description', desc[ language ], language );
|
2014-10-11 05:22:21 +00:00
|
|
|
} else {
|
2015-09-17 20:10:33 +00:00
|
|
|
this.description[ language ] = desc;
|
2015-02-17 23:56:44 +00:00
|
|
|
this.emit( 'change-description', desc, language );
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
2016-01-11 23:26:54 +00:00
|
|
|
this.emit( 'change' );
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the template description.
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} [language] Optional language key
|
|
|
|
* @return {string|Object} The template description. If it is set
|
|
|
|
* as multilanguage object and no language is set, the whole object
|
|
|
|
* will be returned.
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getTemplateDescription = function ( language ) {
|
|
|
|
language = language || this.getDefaultLanguage();
|
2015-09-17 20:10:33 +00:00
|
|
|
return this.description[ language ];
|
2015-02-17 23:56:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific parameter's description
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} paramKey Parameter key
|
|
|
|
* @param {string} [language] Optional language key
|
|
|
|
* @return {string} Parameter description in given language.
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getParamDescription = function ( paramKey, language ) {
|
|
|
|
language = language || this.getDefaultLanguage();
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( this.params[ paramKey ] && this.params[ paramKey ].description ) {
|
2015-02-17 23:56:44 +00:00
|
|
|
// Return description in this language or fall back
|
2015-09-17 20:10:33 +00:00
|
|
|
return this.params[ paramKey ].description[ language ] || '';
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current wiki language code. Defaults on 'en'.
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {string} Wiki language
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getDefaultLanguage = function () {
|
|
|
|
return mw.config.get( 'wgContentLanguage' ) || 'en';
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set template param order array.
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string[]} orderArray Parameter key array in order
|
|
|
|
* @fires change-paramOrder
|
2016-01-11 23:26:54 +00:00
|
|
|
* @fires change
|
2015-02-17 23:56:44 +00:00
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.setTemplateParamOrder = function ( orderArray ) {
|
|
|
|
orderArray = orderArray || [];
|
|
|
|
// TODO: Make the compare method verify order of array?
|
|
|
|
// Copy the array
|
|
|
|
this.paramOrder = orderArray.slice();
|
|
|
|
this.emit( 'change-paramOrder', orderArray );
|
2016-01-11 23:26:54 +00:00
|
|
|
this.emit( 'change' );
|
2015-02-17 23:56:44 +00:00
|
|
|
};
|
|
|
|
|
2015-10-02 10:34:57 +00:00
|
|
|
/**
|
|
|
|
* Set template format.
|
|
|
|
*
|
|
|
|
* @param {string} [format='inline'] Preferred format
|
|
|
|
* @fires change-format
|
2016-01-11 23:26:54 +00:00
|
|
|
* @fires change
|
2015-10-02 10:34:57 +00:00
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.setTemplateFormat = function ( format ) {
|
|
|
|
format = format || 'inline';
|
|
|
|
if ( this.format !== format ) {
|
|
|
|
this.format = format;
|
|
|
|
this.emit( 'change-format', format );
|
2016-01-11 23:26:54 +00:00
|
|
|
this.emit( 'change' );
|
2015-10-02 10:34:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
/**
|
|
|
|
* Add a key to the end of the paramOrder
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} key New key the add into the paramOrder
|
|
|
|
* @fires add-paramOrder
|
2016-01-11 23:26:54 +00:00
|
|
|
* @fires change
|
2015-02-17 23:56:44 +00:00
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.addKeyTemplateParamOrder = function ( key ) {
|
|
|
|
if ( $.inArray( key, this.paramOrder ) === -1 ) {
|
|
|
|
this.paramOrder.push( key );
|
|
|
|
this.emit( 'add-paramOrder', key );
|
2016-01-11 23:26:54 +00:00
|
|
|
this.emit( 'change' );
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-11 23:26:54 +00:00
|
|
|
/**
|
|
|
|
* TODO: document
|
|
|
|
*
|
|
|
|
* @fires change-paramOrder
|
|
|
|
* @fires change
|
|
|
|
*/
|
2015-02-17 23:56:44 +00:00
|
|
|
mw.TemplateData.Model.prototype.reorderParamOrderKey = function ( key, newIndex ) {
|
|
|
|
var keyIndex = this.paramOrder.indexOf( key );
|
|
|
|
// Move the parameter
|
|
|
|
this.paramOrder.splice(
|
|
|
|
newIndex,
|
|
|
|
0,
|
2015-09-17 20:10:33 +00:00
|
|
|
this.paramOrder.splice( keyIndex, 1 )[ 0 ]
|
2015-02-17 23:56:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
this.paramOrderChanged = true;
|
|
|
|
|
|
|
|
// Emit event
|
|
|
|
this.emit( 'change-paramOrder', this.paramOrder );
|
2016-01-11 23:26:54 +00:00
|
|
|
this.emit( 'change' );
|
2015-02-17 23:56:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a key to the end of the paramOrder
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} key New key the add into the paramOrder
|
2016-01-11 23:26:54 +00:00
|
|
|
* @fires change-paramOrder
|
|
|
|
* @fires change
|
2015-02-17 23:56:44 +00:00
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.removeKeyTemplateParamOrder = function ( key ) {
|
|
|
|
var keyPos = $.inArray( key, this.paramOrder );
|
|
|
|
if ( keyPos > -1 ) {
|
|
|
|
this.paramOrder.splice( keyPos, 1 );
|
|
|
|
this.emit( 'change-paramOrder', this.paramOrder );
|
2016-01-11 23:26:54 +00:00
|
|
|
this.emit( 'change' );
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the template paramOrder array
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {string[]} orderArray Parameter keys in order
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getTemplateParamOrder = function () {
|
|
|
|
return this.paramOrder;
|
|
|
|
};
|
|
|
|
|
2015-10-02 10:34:57 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the template preferred format
|
|
|
|
*
|
|
|
|
* @return {string} Preferred format
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getTemplateFormat = function () {
|
|
|
|
return this.format;
|
|
|
|
};
|
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
/**
|
|
|
|
* Set a specific parameter's property
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} paramKey Parameter key
|
|
|
|
* @param {string} prop Property name
|
2015-09-17 20:10:33 +00:00
|
|
|
* @param {...Mixed} value Property value
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} [language] Value language
|
2015-09-17 20:10:33 +00:00
|
|
|
* @return {boolean} Operation was successful
|
2015-02-17 23:56:44 +00:00
|
|
|
* @fires change-property
|
2016-01-11 23:26:54 +00:00
|
|
|
* @fires change
|
2015-02-17 23:56:44 +00:00
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.setParamProperty = function ( paramKey, prop, value, language ) {
|
|
|
|
var propertiesWithLanguage = this.constructor.static.getPropertiesWithLanguage(),
|
2015-03-05 23:33:12 +00:00
|
|
|
allProps = this.constructor.static.getAllProperties( true ),
|
|
|
|
status = false;
|
2015-02-17 23:56:44 +00:00
|
|
|
|
|
|
|
language = language || this.getDefaultLanguage();
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( !allProps[ prop ] ) {
|
2015-02-17 23:56:44 +00:00
|
|
|
// The property isn't supported yet
|
2015-03-05 23:33:12 +00:00
|
|
|
return status;
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
2014-10-11 05:22:21 +00:00
|
|
|
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( allProps[ prop ].type === 'array' && $.type( value ) === 'string' ) {
|
2015-03-07 00:17:21 +00:00
|
|
|
// When we split the string, we want to use a trimmed delimiter
|
2015-09-17 20:10:33 +00:00
|
|
|
value = this.constructor.static.splitAndTrimArray( value, $.trim( allProps[ prop ].delimiter ) );
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
2014-10-11 05:22:21 +00:00
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
// Check if the property is split by language code
|
|
|
|
if ( $.inArray( prop, propertiesWithLanguage ) !== -1 ) {
|
|
|
|
// Initialize property if necessary
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( !$.isPlainObject( this.params[ paramKey ][ prop ] ) ) {
|
|
|
|
this.params[ paramKey ][ prop ] = {};
|
2015-01-06 01:14:51 +00:00
|
|
|
}
|
2015-09-17 20:10:33 +00:00
|
|
|
value = $.isPlainObject( value ) ? value[ language ] : value;
|
2015-02-17 23:56:44 +00:00
|
|
|
// Compare with language
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( !this.constructor.static.compare( this.params[ paramKey ][ prop ][ language ], value ) ) {
|
|
|
|
this.params[ paramKey ][ prop ][ language ] = value;
|
2015-02-17 23:56:44 +00:00
|
|
|
this.emit( 'change-property', paramKey, prop, value, language );
|
2016-01-11 23:26:54 +00:00
|
|
|
this.emit( 'change' );
|
2015-03-05 23:33:12 +00:00
|
|
|
status = true;
|
2015-01-06 01:14:51 +00:00
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
} else {
|
|
|
|
// Compare without language
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( !this.constructor.static.compare( this.params[ paramKey ][ prop ], value ) ) {
|
|
|
|
this.params[ paramKey ][ prop ] = value;
|
2015-02-17 23:56:44 +00:00
|
|
|
this.emit( 'change-property', paramKey, prop, value, language );
|
2016-01-11 23:26:54 +00:00
|
|
|
this.emit( 'change' );
|
2015-03-05 23:33:12 +00:00
|
|
|
status = true;
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
2015-03-05 23:33:12 +00:00
|
|
|
|
2015-03-12 20:51:59 +00:00
|
|
|
if ( allProps[ prop ].textValue && value === false ) {
|
|
|
|
// Unset the text value if the boolean it depends on is false
|
2015-09-17 20:10:33 +00:00
|
|
|
status = this.setParamProperty( paramKey, allProps[ prop ].textValue, '', language );
|
2015-03-05 23:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2015-02-17 23:56:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark a parameter for deletion.
|
|
|
|
* Don't actually delete the parameter so we can make sure it is removed
|
|
|
|
* from the final output.
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} paramKey Parameter key
|
|
|
|
* @fires delete-param
|
2016-01-11 23:26:54 +00:00
|
|
|
* @fires change
|
2015-02-17 23:56:44 +00:00
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.deleteParam = function ( paramKey ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
this.params[ paramKey ].deleted = true;
|
2015-02-17 23:56:44 +00:00
|
|
|
// Remove from paramOrder
|
|
|
|
this.removeKeyTemplateParamOrder( paramKey );
|
|
|
|
this.emit( 'delete-param', paramKey );
|
2016-01-11 23:26:54 +00:00
|
|
|
this.emit( 'change' );
|
2015-02-17 23:56:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore parameter by unmarking it as deleted.
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} paramKey Parameter key
|
|
|
|
* @fires add-param
|
2016-01-11 23:26:54 +00:00
|
|
|
* @fires change
|
2015-02-17 23:56:44 +00:00
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.restoreParam = function ( paramKey ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( this.params[ paramKey ] ) {
|
|
|
|
this.params[ paramKey ].deleted = false;
|
2015-02-17 23:56:44 +00:00
|
|
|
// Add back to paramOrder
|
|
|
|
this.addKeyTemplateParamOrder( paramKey );
|
2015-09-17 20:10:33 +00:00
|
|
|
this.emit( 'add-param', paramKey, this.params[ paramKey ] );
|
2016-01-11 23:26:54 +00:00
|
|
|
this.emit( 'change' );
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all data attached to a parameter
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} paramKey Parameter key
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.emptyParamData = function ( paramKey ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( this.params[ paramKey ] ) {
|
2015-02-17 23:56:44 +00:00
|
|
|
// Delete all data and readd the parameter
|
2015-09-17 20:10:33 +00:00
|
|
|
delete this.params[ paramKey ];
|
2015-02-17 23:56:44 +00:00
|
|
|
this.addParam( paramKey );
|
|
|
|
// Mark this parameter as intentionally emptied
|
2015-09-17 20:10:33 +00:00
|
|
|
this.params[ paramKey ].emptied = true;
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a parameter property.
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} paramKey Parameter key
|
|
|
|
* @param {string} prop Parameter property
|
2015-09-17 20:10:33 +00:00
|
|
|
* @return {...Mixed|null} Property value if it exists. Returns null if the
|
2015-02-17 23:56:44 +00:00
|
|
|
* parameter key itself doesn't exist.
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getParamProperty = function ( paramKey, prop ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( this.params[ paramKey ] ) {
|
|
|
|
return this.params[ paramKey ][ prop ];
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a specific parameter data
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} key Parameter key
|
|
|
|
* @return {Object} Parameter data
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getParamData = function ( key ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
return this.params[ key ];
|
2015-02-17 23:56:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the complete object of all parameters.
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {Object} All parameters and their data
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getParams = function () {
|
|
|
|
return this.params;
|
|
|
|
};
|
|
|
|
|
|
|
|
mw.TemplateData.Model.prototype.isParamDeleted = function ( key ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
return this.params[ key ] && this.params[ key ].deleted === true;
|
2015-02-17 23:56:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
mw.TemplateData.Model.prototype.isParamExists = function ( key ) {
|
|
|
|
return $.inArray( key, Object.keys( this.params ) ) > -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the original templatedata object
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {Object} templatedataObj TemplateData object
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.setOriginalTemplateDataObject = function ( templatedataObj ) {
|
|
|
|
this.originalTemplateDataObject = $.extend( true, {}, templatedataObj );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get full page name
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} pageName Page name
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.setFullPageName = function ( pageName ) {
|
|
|
|
this.fullPageName = pageName;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set parent page
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
|
|
|
* @param {string} parent Parent page
|
2015-02-17 23:56:44 +00:00
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.setParentPage = function ( parent ) {
|
|
|
|
this.parentPage = parent;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get page full name
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {string} Page full name
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getFullPageName = function () {
|
|
|
|
return this.fullPageName;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parent page
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {string} Parent page
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getParentPage = function () {
|
|
|
|
return this.parentPage;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get original templatedata object
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {Object} Templatedata object
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getOriginalTemplateDataObject = function () {
|
|
|
|
return this.originalTemplateDataObject;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the current model and output it as a complete templatedata string
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {string} Templatedata String
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.outputTemplateDataString = function () {
|
|
|
|
var param, paramKey, key, prop, oldKey, name, compareOrig, normalizedValue,
|
|
|
|
allProps = this.constructor.static.getAllProperties( true ),
|
|
|
|
original = this.getOriginalTemplateDataObject(),
|
|
|
|
result = $.extend( true, {}, this.getOriginalTemplateDataObject() ),
|
|
|
|
defaultLang = this.getDefaultLanguage();
|
|
|
|
|
|
|
|
// Template description
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( this.description[ defaultLang ] !== undefined ) {
|
2015-02-17 23:56:44 +00:00
|
|
|
normalizedValue = this.propRemoveUnusedLanguages( this.description );
|
|
|
|
if ( this.isOutputInLanguageObject( result.description, normalizedValue ) ) {
|
|
|
|
result.description = normalizedValue;
|
2014-10-11 05:22:21 +00:00
|
|
|
} else {
|
2015-02-17 23:56:44 +00:00
|
|
|
// Store only one language as a string
|
2015-09-17 20:10:33 +00:00
|
|
|
result.description = normalizedValue[ defaultLang ];
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
} else {
|
|
|
|
// Delete description
|
|
|
|
delete result.description;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Param order
|
|
|
|
if ( original.paramOrder || this.paramOrderChanged ) {
|
|
|
|
result.paramOrder = this.paramOrder;
|
|
|
|
} else {
|
|
|
|
delete result.paramOrder;
|
|
|
|
}
|
|
|
|
|
2015-10-02 10:34:57 +00:00
|
|
|
// Format
|
|
|
|
result.format = this.format;
|
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
// Attach sets as-is for now
|
|
|
|
// TODO: Work properly with sets
|
|
|
|
if ( original.sets ) {
|
|
|
|
result.sets = original.sets;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go over parameters in data
|
|
|
|
for ( paramKey in this.params ) {
|
|
|
|
key = paramKey;
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( this.params[ key ].deleted ) {
|
|
|
|
delete result.params[ key ];
|
2015-02-17 23:56:44 +00:00
|
|
|
continue;
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
// If the user intentionally empties a parameter, delete it from
|
|
|
|
// the result and treat it as a new parameter
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( this.params[ key ].emptied ) {
|
|
|
|
delete result.params[ key ];
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
// Check if name was changed and change the key accordingly
|
2015-09-17 20:10:33 +00:00
|
|
|
name = this.params[ key ].name;
|
2015-02-17 23:56:44 +00:00
|
|
|
oldKey = key;
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( key !== this.params[ key ].name ) {
|
|
|
|
key = this.params[ key ].name;
|
2015-02-17 23:56:44 +00:00
|
|
|
// See if the parameters already has something with this new key
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( this.params[ key ] ) {
|
2015-02-17 23:56:44 +00:00
|
|
|
// Change the key to be something else
|
|
|
|
key += this.getNewValidParameterKey( key );
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
// Copy param details to new name in the model
|
2015-09-17 20:10:33 +00:00
|
|
|
this.params[ key ] = this.params[ oldKey ];
|
2015-02-17 23:56:44 +00:00
|
|
|
// Update the references to the key and param data
|
2015-09-17 20:10:33 +00:00
|
|
|
param = result.params[ name ];
|
2015-02-17 23:56:44 +00:00
|
|
|
// Delete the old param in both the result and model param
|
2015-09-17 20:10:33 +00:00
|
|
|
delete result.params[ oldKey ];
|
|
|
|
delete this.params[ oldKey ];
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
2015-01-06 01:14:51 +00:00
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
// Notice for clarity:
|
|
|
|
// Whether the parameter name was changed or not the following
|
|
|
|
// consistency with object keys will be observed:
|
|
|
|
// * oldKey: original will use oldKey (for comparison to the old value)
|
|
|
|
// * key: this.params will use key (for storing without conflict)
|
|
|
|
// * name: result will use name (for valid output)
|
|
|
|
|
|
|
|
// Check if param is new
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( !result.params[ name ] ) {
|
2015-02-17 23:56:44 +00:00
|
|
|
// New param. Initialize it
|
2015-09-17 20:10:33 +00:00
|
|
|
result.params[ name ] = {};
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
2014-10-11 05:22:21 +00:00
|
|
|
|
2015-02-17 23:56:44 +00:00
|
|
|
// Go over all properties
|
|
|
|
for ( prop in allProps ) {
|
|
|
|
switch ( prop ) {
|
2015-03-05 23:33:12 +00:00
|
|
|
case 'deprecatedValue':
|
2015-02-17 23:56:44 +00:00
|
|
|
case 'name':
|
|
|
|
continue;
|
|
|
|
case 'type':
|
|
|
|
// Only include type if the original included type
|
|
|
|
// or if the current type is not undefined
|
|
|
|
if (
|
|
|
|
original.type !== undefined ||
|
2015-09-17 20:10:33 +00:00
|
|
|
this.params[ key ].type !== 'undefined'
|
2015-02-17 23:56:44 +00:00
|
|
|
) {
|
2015-09-17 20:10:33 +00:00
|
|
|
result.params[ name ][ prop ] = this.params[ key ].type;
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'deprecated':
|
|
|
|
case 'required':
|
|
|
|
case 'suggested':
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( !this.params[ key ][ prop ] ) {
|
2015-02-17 23:56:44 +00:00
|
|
|
// Only add a literal false value if there was a false
|
|
|
|
// value before
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( original.params[ oldKey ] && original.params[ oldKey ][ prop ] === false ) {
|
|
|
|
result.params[ name ][ prop ] = false;
|
2015-02-26 00:26:32 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, delete this value
|
2015-09-17 20:10:33 +00:00
|
|
|
delete result.params[ name ][ prop ];
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
} else {
|
2015-03-05 23:33:12 +00:00
|
|
|
if ( prop === 'deprecated' ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
result.params[ name ][ prop ] = this.params[ key ].deprecatedValue || true;
|
2015-03-05 23:33:12 +00:00
|
|
|
// Remove deprecatedValue
|
2015-09-17 20:10:33 +00:00
|
|
|
delete result.params[ name ].deprecatedValue;
|
2015-03-05 23:33:12 +00:00
|
|
|
} else {
|
2015-09-17 20:10:33 +00:00
|
|
|
result.params[ name ][ prop ] = this.params[ key ][ prop ];
|
2015-03-05 23:33:12 +00:00
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'aliases':
|
|
|
|
// Only update the aliases in if the new templatedata has an
|
|
|
|
// aliases array that isn't empty
|
|
|
|
if (
|
2015-09-17 20:10:33 +00:00
|
|
|
$.type( this.params[ key ][ prop ] ) === 'array' &&
|
|
|
|
this.params[ key ][ prop ].length > 0
|
2015-02-17 23:56:44 +00:00
|
|
|
) {
|
2015-09-17 20:10:33 +00:00
|
|
|
result.params[ name ][ prop ] = this.params[ key ][ prop ];
|
2015-02-17 23:56:44 +00:00
|
|
|
} else {
|
|
|
|
// If the new aliases array is empty, delete it from the original
|
2015-09-17 20:10:33 +00:00
|
|
|
delete result.params[ name ][ prop ];
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Check if there's a value in the model
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( this.params[ key ][ prop ] !== undefined ) {
|
|
|
|
if ( allProps[ prop ].allowLanguages ) {
|
|
|
|
normalizedValue = this.propRemoveUnusedLanguages( this.params[ key ][ prop ] );
|
2015-02-17 23:56:44 +00:00
|
|
|
// Check if this should be displayed with language object or directly as string
|
2015-09-17 20:10:33 +00:00
|
|
|
compareOrig = original.params[ oldKey ] ? original.params[ oldKey ][ prop ] : {};
|
2015-02-17 23:56:44 +00:00
|
|
|
if ( this.isOutputInLanguageObject( compareOrig, normalizedValue ) ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
result.params[ name ][ prop ] = normalizedValue;
|
2015-02-17 23:56:44 +00:00
|
|
|
} else {
|
|
|
|
// Store only one language as a string
|
2015-09-17 20:10:33 +00:00
|
|
|
result.params[ name ][ prop ] = normalizedValue[ defaultLang ];
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-03-05 23:33:12 +00:00
|
|
|
// Set up the result
|
2015-09-17 20:10:33 +00:00
|
|
|
result.params[ name ][ prop ] = this.params[ key ][ prop ];
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
break;
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
return JSON.stringify( result, null, '\t' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the key if it already exists in the parameter list. If it does,
|
|
|
|
* find a new key that doesn't, and return it.
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @param {string} key New parameter key
|
|
|
|
* @return {string} Valid new parameter key
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getNewValidParameterKey = function ( key ) {
|
|
|
|
var allParamNames = this.getAllParamNames();
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( this.params[ key ] || $.inArray( key, allParamNames ) !== -1 ) {
|
2015-02-17 23:56:44 +00:00
|
|
|
// Change the key to be something else
|
|
|
|
key += this.paramIdentifierCounter;
|
|
|
|
this.paramIdentifierCounter++;
|
|
|
|
this.getNewValidParameterKey( key );
|
|
|
|
} else {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Go over a language property and remove empty language key values
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {Object} Property data with only used language keys
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.propRemoveUnusedLanguages = function ( propData ) {
|
|
|
|
var key,
|
|
|
|
result = {};
|
|
|
|
if ( $.isPlainObject( propData ) ) {
|
|
|
|
for ( key in propData ) {
|
2015-09-17 20:10:33 +00:00
|
|
|
if ( propData[ key ] ) {
|
|
|
|
result[ key ] = propData[ key ];
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-17 23:56:44 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the output of the current parameter property should be
|
|
|
|
* outputted in full language mode (object) or a simple string.
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
|
|
|
* @param {string} originalPropValue Original property value
|
|
|
|
* @param {string} newPropValue New property value
|
2015-02-17 23:56:44 +00:00
|
|
|
* @return {boolean} Output should be a full language object
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.isOutputInLanguageObject = function ( originalPropValue, newPropValue ) {
|
|
|
|
if (
|
|
|
|
(
|
|
|
|
// The original was already split to languages
|
|
|
|
$.type( originalPropValue ) === 'object' &&
|
|
|
|
// Original was not an empty object
|
|
|
|
!$.isEmptyObject( originalPropValue )
|
|
|
|
) ||
|
|
|
|
(
|
|
|
|
// The new value is split to languages
|
|
|
|
$.type( newPropValue ) === 'object' &&
|
|
|
|
// New object is not empty
|
|
|
|
!$.isEmptyObject( newPropValue ) &&
|
2014-10-11 05:22:21 +00:00
|
|
|
(
|
2015-02-17 23:56:44 +00:00
|
|
|
// The new value doesn't have the default language
|
2015-09-17 20:10:33 +00:00
|
|
|
newPropValue[ this.getDefaultLanguage() ] === undefined ||
|
2015-02-17 23:56:44 +00:00
|
|
|
// There is more than just one language in the new property
|
|
|
|
Object.keys( newPropValue ).length > 1
|
2014-10-11 05:22:21 +00:00
|
|
|
)
|
2015-02-17 23:56:44 +00:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2015-03-07 00:17:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the parameters that are available in the template source code
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
2015-03-07 00:17:21 +00:00
|
|
|
* @param {string[]} sourceParams Parameters available in template source
|
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.setSourceCodeParameters = function ( sourceParams ) {
|
|
|
|
this.sourceCodeParameters = sourceParams;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the parameters that are available in the template source code
|
2015-09-17 20:10:33 +00:00
|
|
|
*
|
|
|
|
* @return {string[]} Parameters available in template source
|
2015-03-07 00:17:21 +00:00
|
|
|
*/
|
|
|
|
mw.TemplateData.Model.prototype.getSourceCodeParameters = function () {
|
|
|
|
return this.sourceCodeParameters;
|
|
|
|
};
|