Debian Codegraph @

extensions/Cite/modules/ve-cite/ve.ui.MWReference.init.js

raw symbols SCIP
1 'use strict';
2
3 /*!
4 * VisualEditor MediaWiki Cite initialisation code.
5 *
6 * @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
7 * @license MIT
8 */
9
10 ( function () {
11 function fixTarget( target ) {
12 const toolGroups = target.static.toolbarGroups;
13
14 if ( mw.config.get( 'wgCiteVisualEditorOtherGroup' ) ) {
15 for ( let i = 0; i < toolGroups.length; i++ ) {
16 const toolGroup = toolGroups[ i ];
17 if ( toolGroup.name === 'insert' && ( !toolGroup.demote || toolGroup.demote.indexOf( 'reference' ) === -1 ) ) {
18 toolGroup.demote = toolGroup.demote || [];
19 toolGroup.demote.push( { group: 'cite' }, 'reference', 'reference/existing' );
20 }
21 }
22 } else {
23 // Find the reference placeholder group and replace it
24 for ( let i = 0; i < toolGroups.length; i++ ) {
25 if ( toolGroups[ i ].name === 'reference' ) {
26 const group = {
27 // Change the name so it isn't replaced twice
28 name: 'cite',
29 type: 'list',
30 indicator: 'down',
31 include: [ { group: 'cite' }, 'reference', 'reference/existing' ],
32 demote: [ 'reference', 'reference/existing' ]
33 };
34 const label = OO.ui.deferMsg( 'cite-ve-toolbar-group-label' );
35 // Treat mobile targets differently
36 if ( target === ve.init.mw.MobileArticleTarget ) {
37 group.header = label;
38 group.title = label;
39 group.icon = 'reference';
40 } else {
41 group.label = label;
42 }
43 toolGroups[ i ] = group;
44 break;
45 }
46 }
47 }
48 }
49
50 for ( const n in ve.init.mw.targetFactory.registry ) {
51 fixTarget( ve.init.mw.targetFactory.lookup( n ) );
52 }
53
54 ve.init.mw.targetFactory.on( 'register', ( name, target ) => {
55 fixTarget( target );
56 } );
57
58 /**
59 * Add reference insertion tools from on-wiki data.
60 *
61 * By adding a definition in JSON to
62 * MediaWiki:Visualeditor-cite-tool-definition, the cite menu can be populated
63 * with tools that create refrences containing a specific templates. The
64 * content of the definition should be an array containing a series of
65 * objects, one for each tool. Each object must contain a `name`, `icon` and
66 * `template` property. An optional `title` property can also be used to
67 * define the tool title in plain text. The `name` property is a unique
68 * identifier for the tool, and also provides a fallback title for the tool by
69 * being transformed into a message key. The name is prefixed with
70 * `visualeditor-cite-tool-name-`, and messages can be defined on Wiki. Some
71 * common messages are pre-defined for tool names such as `web`, `book`,
72 * `news` and `journal`.
73 *
74 * Example:
75 * [ { "name": "web", "icon": "browser", "template": "Cite web" }, ... ]
76 *
77 */
78 ( function () {
79 const deprecatedIcons = {
80 'ref-cite-book': 'book',
81 'ref-cite-journal': 'journal',
82 'ref-cite-news': 'newspaper',
83 'ref-cite-web': 'browser',
84 'reference-existing': 'referenceExisting'
85 },
86 defaultIcons = {
87 book: 'book',
88 journal: 'journal',
89 news: 'newspaper',
90 web: 'browser'
91 };
92
93 // This is assigned server-side by CitationToolDefinition.php, before this file runs.
94 // Ensure it has a fallback, just in case.
95 ve.ui.mwCitationTools = ve.ui.mwCitationTools || [];
96
97 ve.ui.mwCitationTools.forEach( ( item ) => {
98 const hasOwn = Object.prototype.hasOwnProperty;
99 const data = { template: item.template, title: item.title };
100
101 if ( !item.icon && hasOwn.call( defaultIcons, item.name ) ) {
102 item.icon = defaultIcons[ item.name ];
103 }
104
105 if ( hasOwn.call( deprecatedIcons, item.icon ) ) {
106 item.icon = deprecatedIcons[ item.icon ];
107 }
108
109 // Generate citation tool
110 const name = 'cite-' + item.name;
111 if ( !ve.ui.toolFactory.lookup( name ) ) {
112 const tool = function GeneratedMWCitationDialogTool() {
113 ve.ui.MWCitationDialogTool.apply( this, arguments );
114 };
115 OO.inheritClass( tool, ve.ui.MWCitationDialogTool );
116 tool.static.group = 'cite';
117 tool.static.name = name;
118 tool.static.icon = item.icon;
119 if ( mw.config.get( 'wgCiteVisualEditorOtherGroup' ) ) {
120 tool.static.title = mw.msg( 'cite-ve-othergroup-item', item.title );
121 } else {
122 tool.static.title = item.title;
123 }
124 tool.static.commandName = name;
125 tool.static.template = item.template;
126 tool.static.autoAddToCatchall = false;
127 tool.static.autoAddToGroup = true;
128 tool.static.associatedWindows = [ name ];
129 ve.ui.toolFactory.register( tool );
130 ve.ui.commandRegistry.register(
131 new ve.ui.Command(
132 name, 'mwcite', 'open', { args: [ data ], supportedSelections: [ 'linear' ] }
133 )
134 );
135 }
136
137 // Generate citation context item
138 if ( !ve.ui.contextItemFactory.lookup( name ) ) {
139 const contextItem = function GeneratedMWCitationContextItem() {
140 // Parent constructor
141 ve.ui.MWCitationContextItem.apply( this, arguments );
142 };
143 OO.inheritClass( contextItem, ve.ui.MWCitationContextItem );
144 contextItem.static.name = name;
145 contextItem.static.icon = item.icon;
146 contextItem.static.label = item.title;
147 contextItem.static.commandName = name;
148 contextItem.static.template = item.template;
149 // If the grand-parent class (ve.ui.MWReferenceContextItem) is extended
150 // and re-registered (e.g. by Citoid), then the inheritance chain is
151 // broken, and the generic 'reference' context item would show. Instead
152 // manually specify that that context should never show when a more
153 // specific context item is shown.
154 contextItem.static.suppresses = [ 'reference' ];
155 ve.ui.contextItemFactory.register( contextItem );
156 }
157 } );
158 }() );
159
160 }() );