Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/contextitems/ve.ui.MWDefinedTransclusionContextItem.js

raw symbols SCIP
1 /*!
2 * VisualEditor MWDefinedTransclusionContextItem class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Context item for a defined MWTransclusion.
9 *
10 * Templates are defined on-wiki using the message page
11 * [[MediaWiki:Visualeditor-template-tools-definition.json]]
12 *
13 * Example:
14 * {
15 * // This key is the static name of the ve.ui.ContextItem
16 * "citationNeeded": [
17 * {
18 * // Normalized title string, or list of strings (for redirects)
19 * "title": [ "Citation needed", "cn" ],
20 * // Extra params. This whole object can be accessed
21 * // via #getMatchedTool
22 * "params": {
23 * "reason": "reason",
24 * "date": "date"
25 * }
26 * },
27 * {
28 * "title": "Cite quote",
29 * "params": {
30 * "date": "date"
31 * }
32 * }
33 * ]
34 * }
35 *
36 * @class
37 * @abstract
38 * @extends ve.ui.MWTransclusionContextItem
39 *
40 * @constructor
41 * @param {ve.ui.LinearContext} context Context the item is in
42 * @param {ve.dm.Model} model Model the item is related to
43 * @param {Object} [config]
44 */
45 ve.ui.MWDefinedTransclusionContextItem = function VeUiMWDefinedTransclusionContextItem() {
46 // Parent constructor
47 ve.ui.MWDefinedTransclusionContextItem.super.apply( this, arguments );
48
49 this.tool = this.constructor.static.getMatchedTool( this.model );
50 };
51
52 /* Inheritance */
53
54 OO.inheritClass( ve.ui.MWDefinedTransclusionContextItem, ve.ui.MWTransclusionContextItem );
55
56 /* Static Properties */
57
58 ve.ui.MWDefinedTransclusionContextItem.static.name = null;
59
60 ve.ui.MWDefinedTransclusionContextItem.static.toolDefinitions = ( function () {
61 let tools;
62
63 try {
64 // Must use mw.message to avoid JSON being parsed as Wikitext
65 tools = JSON.parse( mw.message( 'visualeditor-template-tools-definition.json' ).plain() );
66 } catch ( e ) {}
67
68 return tools || {};
69 }() );
70
71 /**
72 * Only display item for single-template transclusions of these templates.
73 *
74 * @property {string|string[]|null}
75 * @static
76 * @inheritable
77 */
78 ve.ui.MWDefinedTransclusionContextItem.static.template = null;
79
80 /* Static Methods */
81
82 /**
83 * @inheritdoc
84 */
85 ve.ui.MWDefinedTransclusionContextItem.static.isCompatibleWith = function ( model ) {
86 // Parent method
87 return ve.ui.MWDefinedTransclusionContextItem.super.static.isCompatibleWith.apply( this, arguments ) &&
88 !!this.getMatchedTool( model );
89 };
90
91 /**
92 * Get tool definitions, indexed by normalized title
93 *
94 * @return {Object} Collection of tool definitions
95 */
96 ve.ui.MWDefinedTransclusionContextItem.static.getToolsByTitle = function () {
97 if ( !this.toolsByTitle ) {
98 let toolsByTitle;
99 this.toolsByTitle = toolsByTitle = {};
100 ( this.toolDefinitions[ this.name ] || [] ).forEach( ( template ) => {
101 const titles = Array.isArray( template.title ) ? template.title : [ template.title ];
102 // 'title' can be a single title, or list of titles (including redirects)
103 titles.forEach( ( title ) => {
104 toolsByTitle[ mw.Title.newFromText( title, mw.config.get( 'wgNamespaceIds' ).template ).getPrefixedText() ] = template;
105 } );
106 } );
107 }
108 return this.toolsByTitle;
109 };
110
111 /**
112 * Get the tool definition that matches a specific model, if any
113 *
114 * @param {ve.dm.Model} model
115 * @return {Object|null} Tool definition, or null if no match
116 */
117 ve.ui.MWDefinedTransclusionContextItem.static.getMatchedTool = function ( model ) {
118 const resource = ve.getProp( model.getAttribute( 'mw' ), 'parts', 0, 'template', 'target', 'href' );
119 if ( resource ) {
120 const title = mw.Title.newFromText( mw.libs.ve.normalizeParsoidResourceName( resource ) ).getPrefixedText();
121 return this.getToolsByTitle()[ title ] || null;
122 }
123 return null;
124 };
125
126 /**
127 * Get a template param using its canonical name
128 *
129 * @param {string} name Canonical parameter name
130 * @return {string|null} Param wikitext, null if not found
131 */
132 ve.ui.MWDefinedTransclusionContextItem.prototype.getCanonicalParam = function ( name ) {
133 const params = this.tool.params || {};
134
135 if ( Object.prototype.hasOwnProperty.call( params, name ) ) {
136 const aliases = Array.isArray( params[ name ] ) ? params[ name ] : [ params[ name ] ];
137 // Find the first non-empty value from the alias list
138 for ( let i = 0; i < aliases.length; i++ ) {
139 const value = ve.getProp( this.model.getAttribute( 'mw' ), 'parts', 0, 'template', 'params', aliases[ i ], 'wt' );
140 if ( value ) {
141 return value;
142 }
143 }
144 }
145 return null;
146 };