From 821ceb992080aa6920233762287105cd6480f7b3 Mon Sep 17 00:00:00 2001 From: Alex Monk Date: Thu, 10 Sep 2015 17:13:36 +0100 Subject: [PATCH] Order template title results by provided index This only comes back in results from certain modules, so I'm not entirely sure whether we want to do this in here or mw.widgets.TitleInputWidget. Bug: T111598 Change-Id: If2916706db359d520c41c2dec7ded44e0d3a56a9 --- .../ve.ui.MWTemplateTitleInputWidget.js | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/modules/ve-mw/ui/widgets/ve.ui.MWTemplateTitleInputWidget.js b/modules/ve-mw/ui/widgets/ve.ui.MWTemplateTitleInputWidget.js index c8c20abc07..ca7833842c 100644 --- a/modules/ve-mw/ui/widgets/ve.ui.MWTemplateTitleInputWidget.js +++ b/modules/ve-mw/ui/widgets/ve.ui.MWTemplateTitleInputWidget.js @@ -51,18 +51,42 @@ ve.ui.MWTemplateTitleInputWidget.prototype.getLookupRequest = function () { if ( this.showTemplateDescriptions ) { return promise - // Also get descriptions .then( function ( response ) { - var xhr, index, params, - pages = ( response.query && response.query.pages ) || {}, + var xhr, pageId, index, params, indexFound, redirIndex, + redirects = ( response.query && response.query.redirects ) || {}, + origPages = ( response.query && response.query.pages ) || {}, + newPages = [], titles = []; - for ( index in pages ) { - titles.push( pages[ index ].title ); + // Build a new array to replace response.query.pages, ensuring everything goes into + // the order defined by the page's index key, instead of whatever random order the + // browser would let you iterate over the old object in. + for ( pageId in origPages ) { + indexFound = false; + if ( 'index' in origPages[ pageId ] ) { + newPages[ origPages[ pageId ].index - 1 ] = origPages[ pageId ]; + indexFound = true; + } else { + // Watch out for cases where the index is specified on the redirect object + // rather than the page object. + for ( redirIndex in redirects ) { + if ( redirects[ redirIndex ].to === origPages[ pageId ].title ) { + newPages[ redirects[ redirIndex ].index - 1 ] = origPages[ pageId ]; + indexFound = true; + break; + } + } + } } - originalResponse = response; + for ( index in newPages ) { + titles.push( newPages[ index ].title ); + } + response.query.pages = newPages; + originalResponse = response; // lie! + + // Also get descriptions if ( titles.length > 0 ) { params = { action: 'templatedata',