]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - extensions/Cite/modules/ve-cite/ve.ui.MWReference.init.js
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / extensions / Cite / modules / ve-cite / ve.ui.MWReference.init.js
1 /*!
2  * VisualEditor MediaWiki Cite initialisation code.
3  *
4  * @copyright 2011-2017 Cite VisualEditor Team and others; see AUTHORS.txt
5  * @license The MIT License (MIT); see LICENSE.txt
6  */
7 ( function () {
8         var i, j, jLen, toolGroup, toolGroups, linkIndex, target, label, group;
9
10         // HACK: Find the position of the current citation toolbar definition
11         // and manipulate it.
12
13         targetLoop:
14         for ( i in ve.init.mw ) {
15                 target = ve.init.mw[ i ];
16                 if ( !target || !( target.prototype instanceof ve.init.Target ) ) {
17                         continue;
18                 }
19                 toolGroups = target.static.toolbarGroups;
20                 linkIndex = toolGroups.length;
21
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' );
28                                 }
29                         }
30                         continue;
31                 }
32
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
36                                 linkIndex = -1;
37                                 continue targetLoop;
38                         }
39                 }
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' ) {
45                                 linkIndex = j;
46                                 break;
47                         }
48                 }
49
50                 label = OO.ui.deferMsg( 'cite-ve-toolbar-group-label' );
51                 group = {
52                         classes: [ 've-test-toolbar-cite' ],
53                         type: 'list',
54                         indicator: 'down',
55                         include: [ { group: 'cite' }, 'reference', 'reference/existing' ],
56                         demote: [ 'reference', 'reference/existing' ]
57                 };
58
59                 // Treat mobile targets differently
60                 if ( ve.init.mw.MobileArticleTarget && target.prototype instanceof ve.init.mw.MobileArticleTarget ) {
61                         group.header = label;
62                         group.title = label;
63                         group.icon = 'reference';
64                 } else {
65                         group.label = label;
66                 }
67
68                 // Insert a new group for references after the link group (or at the end).
69                 toolGroups.splice( linkIndex + 1, 0, group );
70         }
71
72         /**
73          * Add reference insertion tools from on-wiki data.
74          *
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`.
83          *
84          * Example:
85          * [ { "name": "web", "icon": "cite-web", "template": "Cite web" }, ... ]
86          *
87          */
88         ( function () {
89                 var i, len, item, name, data, tool, tools, dialog, contextItem,
90                         limit = 5;
91
92                 try {
93                         // Must use mw.message to avoid JSON being parsed as Wikitext
94                         tools = JSON.parse( mw.message( 'cite-tool-definition.json' ).plain() );
95                 } catch ( e ) {}
96                 if ( !tools ) {
97                         try {
98                                 // Must use mw.message to avoid JSON being parsed as Wikitext
99                                 tools = JSON.parse( mw.message( 'visualeditor-cite-tool-definition.json' ).plain() );
100                         } catch ( e ) {}
101                 }
102
103                 if ( Array.isArray( tools ) ) {
104                         for ( i = 0, len = Math.min( limit, tools.length ); i < len; i++ ) {
105                                 item = tools[ i ];
106                                 data = { template: item.template };
107
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 );
113                                         };
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 );
120                                         } else {
121                                                 tool.static.title = item.title;
122                                         }
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(
130                                                 new ve.ui.Command(
131                                                         name, 'mwcite', 'open', { args: [ name, data ], supportedSelections: [ 'linear' ] }
132                                                 )
133                                         );
134                                 }
135
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 );
141                                         };
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 );
149                                 }
150
151                                 // Generate dialog
152                                 if ( !ve.ui.windowFactory.lookup( name ) ) {
153                                         dialog = function GeneratedMWCitationDialog() {
154                                                 ve.ui.MWCitationDialog.apply( this, arguments );
155                                         };
156                                         OO.inheritClass( dialog, ve.ui.MWCitationDialog );
157                                         dialog.static.name = name;
158                                         dialog.static.title = item.title;
159                                         ve.ui.windowFactory.register( dialog );
160                                 }
161                         }
162                 }
163         }() );
164
165 }() );