2 * VisualEditor MediaWiki Cite initialisation code.
4 * @copyright 2011-2017 Cite VisualEditor Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
8 var i, j, jLen, toolGroup, toolGroups, linkIndex, target, label, group;
10 // HACK: Find the position of the current citation toolbar definition
14 for ( i in ve.init.mw ) {
15 target = ve.init.mw[ i ];
16 if ( !target || !( target.prototype instanceof ve.init.Target ) ) {
19 toolGroups = target.static.toolbarGroups;
20 linkIndex = toolGroups.length;
22 if ( mw.config.get( 'wgCiteVisualEditorOtherGroup' ) ) {
23 for ( j = 0; j < linkIndex; j++ ) {
24 toolGroup = toolGroups[ j ];
25 if ( toolGroup.include === '*' && ( !toolGroup.demote || toolGroup.demote.indexOf( 'reference' ) === -1 ) ) {
26 toolGroup.demote = toolGroup.demote || [];
27 toolGroup.demote.push( { group: 'cite' }, 'reference', 'reference/existing' );
33 for ( j = 0, jLen = toolGroups.length; j < jLen; j++ ) {
34 if ( ve.getProp( toolGroups[ j ], 'include', 0, 'group' ) === 'cite' ) {
35 // Skip if the cite group exists already
40 // Looking through the object to find what we actually need
41 // to replace. This way, if toolbarGroups are changed in VE code
42 // we won't have to manually change the index here.
43 for ( j = 0, jLen = toolGroups.length; j < jLen; j++ ) {
44 if ( ve.getProp( toolGroups[ j ], 'include', 0 ) === 'link' ) {
50 label = OO.ui.deferMsg( 'cite-ve-toolbar-group-label' );
52 classes: [ 've-test-toolbar-cite' ],
55 include: [ { group: 'cite' }, 'reference', 'reference/existing' ],
56 demote: [ 'reference', 'reference/existing' ]
59 // Treat mobile targets differently
60 if ( ve.init.mw.MobileArticleTarget && target.prototype instanceof ve.init.mw.MobileArticleTarget ) {
63 group.icon = 'reference';
68 // Insert a new group for references after the link group (or at the end).
69 toolGroups.splice( linkIndex + 1, 0, group );
73 * Add reference insertion tools from on-wiki data.
75 * By adding a definition in JSON to MediaWiki:Visualeditor-cite-tool-definition, the cite menu can
76 * be populated with tools that create refrences containing a specific templates. The content of the
77 * definition should be an array containing a series of objects, one for each tool. Each object must
78 * contain a `name`, `icon` and `template` property. An optional `title` property can also be used
79 * to define the tool title in plain text. The `name` property is a unique identifier for the tool,
80 * and also provides a fallback title for the tool by being transformed into a message key. The name
81 * is prefixed with `visualeditor-cite-tool-name-`, and messages can be defined on Wiki. Some common
82 * messages are pre-defined for tool names such as `web`, `book`, `news` and `journal`.
85 * [ { "name": "web", "icon": "cite-web", "template": "Cite web" }, ... ]
89 var i, len, item, name, data, tool, tools, dialog, contextItem,
93 // Must use mw.message to avoid JSON being parsed as Wikitext
94 tools = JSON.parse( mw.message( 'cite-tool-definition.json' ).plain() );
98 // Must use mw.message to avoid JSON being parsed as Wikitext
99 tools = JSON.parse( mw.message( 'visualeditor-cite-tool-definition.json' ).plain() );
103 if ( Array.isArray( tools ) ) {
104 for ( i = 0, len = Math.min( limit, tools.length ); i < len; i++ ) {
106 data = { template: item.template };
108 // Generate citation tool
109 name = 'cite-' + item.name;
110 if ( !ve.ui.toolFactory.lookup( name ) ) {
111 tool = function GeneratedMWCitationDialogTool() {
112 ve.ui.MWCitationDialogTool.apply( this, arguments );
114 OO.inheritClass( tool, ve.ui.MWCitationDialogTool );
115 tool.static.group = 'cite';
116 tool.static.name = name;
117 tool.static.icon = item.icon;
118 if ( mw.config.get( 'wgCiteVisualEditorOtherGroup' ) ) {
119 tool.static.title = mw.msg( 'cite-ve-othergroup-item', item.title );
121 tool.static.title = item.title;
123 tool.static.commandName = name;
124 tool.static.template = item.template;
125 tool.static.autoAddToCatchall = false;
126 tool.static.autoAddToGroup = true;
127 tool.static.associatedWindows = [ name ];
128 ve.ui.toolFactory.register( tool );
129 ve.ui.commandRegistry.register(
131 name, 'mwcite', 'open', { args: [ name, data ], supportedSelections: [ 'linear' ] }
136 // Generate citation context item
137 if ( !ve.ui.contextItemFactory.lookup( name ) ) {
138 contextItem = function GeneratedMWCitationContextItem() {
139 // Parent constructor
140 ve.ui.MWCitationContextItem.apply( this, arguments );
142 OO.inheritClass( contextItem, ve.ui.MWCitationContextItem );
143 contextItem.static.name = name;
144 contextItem.static.icon = item.icon;
145 contextItem.static.label = item.title;
146 contextItem.static.commandName = name;
147 contextItem.static.template = item.template;
148 ve.ui.contextItemFactory.register( contextItem );
152 if ( !ve.ui.windowFactory.lookup( name ) ) {
153 dialog = function GeneratedMWCitationDialog() {
154 ve.ui.MWCitationDialog.apply( this, arguments );
156 OO.inheritClass( dialog, ve.ui.MWCitationDialog );
157 dialog.static.name = name;
158 dialog.static.title = item.title;
159 ve.ui.windowFactory.register( dialog );