Use paramOrder data for ordering template parameters

Parameters are ordered using 2 consecutive buckets, the intersection of
paramOrder and parameters in use ordered using paramOrder followed by the
remaining parameters in alpha-numeric order.

A patch to TemplateData was needed to make this work, but we won't get a
crash without it, just no order specification:
    Ic5b42c4189868412138680654c499b6c8bc8f47e

The paramOrder property needs some help still, as it currently requires
being either omitted or containing an exhaustive list of all known
parameters. It should backfill unmentioned params in JSON specified order
instead.

Bug: 51930
Change-Id: Ic3eb665389380c8e3dd6562b059c2f6655a22588
This commit is contained in:
Trevor Parscal 2014-01-14 12:15:29 -08:00 committed by Trevor Parscal
parent 278bcd9a20
commit 2a40b3b27b
2 changed files with 32 additions and 1 deletions

View file

@ -189,8 +189,23 @@ ve.dm.MWTemplateModel.prototype.hasParameter = function ( name ) {
* @returns {string[]} List of parameter names
*/
ve.dm.MWTemplateModel.prototype.getParameterNames = function () {
var i, len, index, paramOrder, paramNames;
if ( !this.sequence ) {
this.sequence = ve.getObjectKeys( this.params ).sort( function ( a, b ) {
paramOrder = this.spec.getParameterOrder();
paramNames = ve.getObjectKeys( this.params );
this.sequence = [];
// Known parameters first
for ( i = 0, len = paramOrder.length; i < len; i++ ) {
index = paramNames.indexOf( paramOrder[i] );
if ( index !== -1 ) {
this.sequence.push( paramOrder[i] );
paramNames.splice( index, 1 );
}
}
// Unknown parameters in alpha-numeric order second
paramNames.sort( function ( a, b ) {
var aIsNaN = isNaN( a ),
bIsNaN = isNaN( b );
if ( aIsNaN && bIsNaN ) {
@ -208,6 +223,7 @@ ve.dm.MWTemplateModel.prototype.getParameterNames = function () {
// Two numbers
return a - b;
} );
this.sequence.push.apply( this.sequence, paramNames );
}
return this.sequence;
};

View file

@ -23,6 +23,7 @@ ve.dm.MWTemplateSpecModel = function VeDmMWTemplateSpecModel( template ) {
this.template = template;
this.description = null;
this.params = {};
this.paramOrder = [];
this.sets = [];
// Initialization
@ -41,6 +42,7 @@ ve.dm.MWTemplateSpecModel = function VeDmMWTemplateSpecModel( template ) {
* @method
* @param {Object} data Template spec data
* @param {string} [data.description] Template description
* @param {string[]} [data.paramOrder] Canonically ordered parameter names
* @param {Object} [data.params] Template param specs keyed by param name
* @param {string[][]} [data.sets] Lists of param sets
*/
@ -50,6 +52,9 @@ ve.dm.MWTemplateSpecModel.prototype.extend = function ( data ) {
if ( data.description !== null ) {
this.description = data.description;
}
if ( Array.isArray( data.paramOrder ) ) {
this.paramOrder = data.paramOrder.slice();
}
if ( ve.isPlainObject( data.params ) ) {
for ( key in data.params ) {
// Pre-fill spec
@ -149,6 +154,16 @@ ve.dm.MWTemplateSpecModel.prototype.getDescription = function ( lang ) {
return ve.isPlainObject( value ) ? OO.ui.getLocalValue( value, lang ) : value;
};
/**
* Get parameter order.
*
* @method
* @returns {string[]} Canonically ordered parameter names
*/
ve.dm.MWTemplateSpecModel.prototype.getParameterOrder = function () {
return this.paramOrder.slice();
};
/**
* Check if a parameter name is known.
*