/* global mediaWiki, ve */
(function(mw) {
/**
* Override for template editor serialization.
*
* Adding suggested params for infoboxes.
*
* Based on code from ]
* https://github.com/wikimedia/mediawiki-extensions-VisualEditor/blob/master/modules/ve-mw/dm/models/ve.dm.MWTemplateModel.js#L345
* More permanent solution:
* https://phabricator.wikimedia.org/T280078#7490484
*/
function override() {
if (!(typeof ve === 'object' && 'dm' in ve && 'MWTemplateModel' in ve.dm)) {
//console.log(' override ve.dm.MWTemplateModel skipped');
return;
}
//console.log(' override ve.dm.MWTemplateModel');
/**
* @inheritdoc
*/
ve.dm.MWTemplateModel.prototype.serialize = function () {
var origData = this.originalData || {},
origParams = origData.params || {},
template = { target: this.target, params: {} },
spec = this.spec,
params = this.params;
//console.log('', {template:template, spec:spec});
var keepSuggested = false;
if (typeof spec == 'object'
&& typeof spec.template == 'object'
&& typeof spec.template.title == 'string'
&& spec.template.title.indexOf('infobox')>=0) {
keepSuggested = true;
}
for ( var name in params ) {
if ( name === '' ) {
continue;
}
// removing empty parameters
try {
if (
// Don't add empty parameters (T101075)
params.getValue() === '' &&
// …unless they were present before the edit
!Object.prototype.hasOwnProperty.call( origParams, name ) &&
// …unless they are required (T276989)
!spec.isParameterRequired( name ) &&
// …unless keeping suggested
!(spec.isParameterSuggested( name ) && keepSuggested)
) {
continue;
}
} catch(e) {
console.error('', 'override failed this needs to be fixed', e)
}
var origName = params.getOriginalName();
template.params = ve.extendObject(
{},
origParams,
{ wt: params.getValue() }
);
}
// Performs a non-deep extend, so this won't reintroduce
// deleted parameters (T75134)
template = ve.extendObject( {}, origData, template );
return { template: template };
};
}
//console.log(' loaded, adding hook');
mw.hook('ve.activationComplete').add(function () {
//console.log(' hooked');
override();
});
})(mediaWiki);