From a08febb0afa2da284d52a5b429c306e33cdd73a8 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Sat, 12 May 2018 16:40:34 +0100 Subject: [PATCH] Simplify toolbar registration VE-MW toolbars now define a placeholder for reference toolbars. Depends-On: I0bace5e5fe05f9c214d57a74c478b48a7dcaec3b Change-Id: I7cb0f1f1affbfd7a4013f396b1423b6f84da52ab --- modules/ve-cite/ve.ui.MWReference.init.js | 92 ++++++++++------------- 1 file changed, 39 insertions(+), 53 deletions(-) diff --git a/modules/ve-cite/ve.ui.MWReference.init.js b/modules/ve-cite/ve.ui.MWReference.init.js index c8a6be197..819ecfdc8 100644 --- a/modules/ve-cite/ve.ui.MWReference.init.js +++ b/modules/ve-cite/ve.ui.MWReference.init.js @@ -4,71 +4,57 @@ * @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt * @license MIT */ + ( function () { - var i, j, jLen, toolGroup, toolGroups, linkIndex, target, label, group; + var name; - // HACK: Find the position of the current citation toolbar definition - // and manipulate it. - - targetLoop: - for ( i in ve.init.mw ) { - target = ve.init.mw[ i ]; - if ( !target || !( target.prototype instanceof ve.init.Target ) ) { - continue; - } - toolGroups = target.static.toolbarGroups; - linkIndex = toolGroups.length; + function fixTarget( target ) { + var i, iLen, toolGroup, label, group, + toolGroups = target.static.toolbarGroups; if ( mw.config.get( 'wgCiteVisualEditorOtherGroup' ) ) { - for ( j = 0; j < linkIndex; j++ ) { - toolGroup = toolGroups[ j ]; - if ( toolGroup.include === '*' && ( !toolGroup.demote || toolGroup.demote.indexOf( 'reference' ) === -1 ) ) { + for ( i = 0, iLen = toolGroups.length; i < iLen; i++ ) { + toolGroup = toolGroups[ i ]; + if ( toolGroup.name === 'insert' && ( !toolGroup.demote || toolGroup.demote.indexOf( 'reference' ) === -1 ) ) { toolGroup.demote = toolGroup.demote || []; toolGroup.demote.push( { group: 'cite' }, 'reference', 'reference/existing' ); } } - continue; - } - - for ( j = 0, jLen = toolGroups.length; j < jLen; j++ ) { - if ( ve.getProp( toolGroups[ j ], 'include', 0, 'group' ) === 'cite' ) { - // Skip if the cite group exists already - linkIndex = -1; - continue targetLoop; - } - } - // Looking through the object to find what we actually need - // to replace. This way, if toolbarGroups are changed in VE code - // we won't have to manually change the index here. - for ( j = 0, jLen = toolGroups.length; j < jLen; j++ ) { - if ( ve.getProp( toolGroups[ j ], 'include', 0 ) === 'link' ) { - linkIndex = j; - break; - } - } - - label = OO.ui.deferMsg( 'cite-ve-toolbar-group-label' ); - group = { - classes: [ 've-test-toolbar-cite' ], - type: 'list', - indicator: 'down', - include: [ { group: 'cite' }, 'reference', 'reference/existing' ], - demote: [ 'reference', 'reference/existing' ] - }; - - // Treat mobile targets differently - if ( ve.init.mw.MobileArticleTarget && target.prototype instanceof ve.init.mw.MobileArticleTarget ) { - group.header = label; - group.title = label; - group.icon = 'reference'; } else { - group.label = label; + // Find the reference placeholder group and replace it + for ( i = 0, iLen = toolGroups.length; i < iLen; i++ ) { + if ( toolGroups[ i ].name === 'reference' ) { + toolGroups[ i ] = group = { + // Change the name so it isn't replaced twice + name: 'cite', + type: 'list', + indicator: 'down', + include: [ { group: 'cite' }, 'reference', 'reference/existing' ], + demote: [ 'reference', 'reference/existing' ] + }; + label = OO.ui.deferMsg( 'cite-ve-toolbar-group-label' ); + // Treat mobile targets differently + if ( ve.init.mw.MobileArticleTarget && target.prototype instanceof ve.init.mw.MobileArticleTarget ) { + group.header = label; + group.title = label; + group.icon = 'reference'; + } else { + group.label = label; + } + break; + } + } } - - // Insert a new group for references after the link group (or at the end). - toolGroups.splice( linkIndex + 1, 0, group ); } + for ( name in ve.init.mw.targetFactory.registry ) { + fixTarget( ve.init.mw.targetFactory.lookup( name ) ); + } + + ve.init.mw.targetFactory.on( 'register', function ( name, target ) { + fixTarget( target ); + } ); + /** * Add reference insertion tools from on-wiki data. *