Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor MWLanuageVariantNodeContextItem class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Context item for a ve.dm.MWLanguageVariantNode.
9 *
10 * @class
11 * @extends ve.ui.LinearContextItem
12 *
13 * @constructor
14 * @param {ve.ui.LinearContext} context Context the item is in
15 * @param {ve.dm.MWLanguageVariantNode} model Model the item is related to
16 * @param {Object} [config]
17 */
18 ve.ui.MWLanguageVariantNodeContextItem = function VeUiMWLanguageVariantNodeContextItem() {
19 // Parent constructor
20 ve.ui.MWLanguageVariantNodeContextItem.super.apply( this, arguments );
21
22 // Initialization
23 this.$element.addClass( 've-ui-mwLanguageVariantNodeContextItem' );
24 };
25
26 /* Inheritance */
27
28 OO.inheritClass( ve.ui.MWLanguageVariantNodeContextItem, ve.ui.LinearContextItem );
29
30 /* Static Properties */
31
32 ve.ui.MWLanguageVariantNodeContextItem.static.name = 'mwLanguageVariant';
33
34 ve.ui.MWLanguageVariantNodeContextItem.static.icon = 'language';
35
36 ve.ui.MWLanguageVariantNodeContextItem.static.label = null; // see #setup()
37
38 ve.ui.MWLanguageVariantNodeContextItem.static.modelClasses = [
39 ve.dm.MWLanguageVariantBlockNode,
40 ve.dm.MWLanguageVariantInlineNode,
41 ve.dm.MWLanguageVariantHiddenNode
42 ];
43
44 ve.ui.MWLanguageVariantNodeContextItem.static.commandName = 'mwLanguageVariant';
45
46 /* Methods */
47
48 /**
49 * @inheritdoc
50 */
51 ve.ui.MWLanguageVariantNodeContextItem.prototype.setup = function () {
52 // Set up label
53 const messageKey = 'visualeditor-mwlanguagevariantcontextitem-title-' +
54 this.model.getRuleType();
55
56 // The following messages are used here:
57 // * visualeditor-mwlanguagevariantcontextitem-title-disabled
58 // * visualeditor-mwlanguagevariantcontextitem-title-filter
59 // * visualeditor-mwlanguagevariantcontextitem-title-name
60 // * visualeditor-mwlanguagevariantcontextitem-title-oneway
61 // * visualeditor-mwlanguagevariantcontextitem-title-twoway
62 // * visualeditor-mwlanguagevariantcontextitem-title-unknown
63 this.setLabel( OO.ui.deferMsg( messageKey ) );
64
65 // Invoke superclass method.
66 return ve.ui.MWLanguageVariantNodeContextItem.super.prototype.setup.call( this );
67 };
68
69 /**
70 * @inheritdoc
71 */
72 ve.ui.MWLanguageVariantNodeContextItem.prototype.renderBody = function () {
73 const $body = this.$body,
74 $table = $( '<table>' ),
75 $header = $( '<tr>' ),
76 variantInfo = this.model.getVariantInfo(),
77 type = this.model.getRuleType(),
78 isHidden = this.model.isHidden();
79
80 $table.addClass(
81 've-ui-mwLanguageVariantNodeContextItem-rule-table'
82 );
83 $table.append( $header );
84
85 function languageNameIfKnown( code ) {
86 return ve.init.platform.hasLanguageCode( code ) ?
87 mw.html.escape( ve.init.platform.getLanguageName( code ) ) :
88 mw.message(
89 'visualeditor-mwlanguagevariantcontextitem-rule-invalid-language-label'
90 ).parse();
91 }
92
93 switch ( type ) {
94 case 'filter':
95 case 'name': {
96 $header
97 .append( $( '<th>' ).text(
98 ve.msg( 'visualeditor-mwlanguagevariantcontextitem-rule-name-label' )
99 ) )
100 .append( $( '<th>' ).text(
101 ve.msg( 'visualeditor-mwlanguagevariantcontextitem-rule-code-label' )
102 ) );
103
104 const languageCodes = ( type === 'filter' ) ?
105 variantInfo.filter.l : [ variantInfo.name.t ];
106 languageCodes.forEach( ( code ) => {
107 const name = languageNameIfKnown( code.toLowerCase() );
108
109 $table
110 .append( $( '<tr>' )
111 // eslint-disable-next-line no-jquery/no-html
112 .append( $( '<td>' ).html( name ).attr( 'lang', code ) )
113 .append( $( '<td>' ).text( code ) )
114 );
115 } );
116 break;
117 }
118
119 case 'oneway':
120 $header
121 .append( $( '<th>' ).text(
122 ve.msg( 'visualeditor-mwlanguagevariantcontextitem-rule-name-label' )
123 ) )
124 .append( $( '<th>' ).text(
125 ve.msg( 'visualeditor-mwlanguagevariantcontextitem-rule-code-label' )
126 ) )
127 .append( $( '<th>' ).text(
128 ve.msg( 'visualeditor-mwlanguagevariantcontextitem-rule-text-from-label' )
129 ) )
130 .append( $( '<th>' ).text(
131 ve.msg( 'visualeditor-mwlanguagevariantcontextitem-rule-text-to-label' )
132 ) );
133
134 variantInfo.oneway.forEach( ( item ) => {
135 // Safe HTML from the parser
136 // eslint-disable-next-line no-jquery/no-html
137 const $fromText = $( '<td>' ).html( item.f ),
138 // eslint-disable-next-line no-jquery/no-html
139 $toText = $( '<td>' ).html( item.t ),
140 code = item.l,
141 name = languageNameIfKnown( code.toLowerCase() );
142 $table
143 .append( $( '<tr>' )
144 // eslint-disable-next-line no-jquery/no-html
145 .append( $( '<td>' ).html( name ).attr( 'lang', code ) )
146 .append( $( '<td>' ).text( code ) )
147 .append( $fromText )
148 .append( $toText )
149 );
150 } );
151 break;
152
153 case 'twoway':
154 $header
155 .append( $( '<th>' ).text(
156 ve.msg( 'visualeditor-mwlanguagevariantcontextitem-rule-name-label' )
157 ) )
158 .append( $( '<th>' ).text(
159 ve.msg( 'visualeditor-mwlanguagevariantcontextitem-rule-code-label' )
160 ) )
161 .append( $( '<th>' ).text(
162 ve.msg( 'visualeditor-mwlanguagevariantcontextitem-rule-text-twoway-label' )
163 ) );
164
165 variantInfo.twoway.forEach( ( item ) => {
166 const code = item.l,
167 name = languageNameIfKnown( code.toLowerCase() ),
168 // eslint-disable-next-line no-jquery/no-html
169 $text = $( '<td>' ).html( item.t );
170 ve.dm.MWLanguageVariantNode.static.processVariants(
171 $text[ 0 ], { showHidden: true }
172 );
173 $table
174 .append( $( '<tr>' )
175 // eslint-disable-next-line no-jquery/no-html
176 .append( $( '<td>' ).html( name ).attr( 'lang', code ) )
177 .append( $( '<td>' ).text( code ) )
178 .append( $text )
179 );
180 } );
181 break;
182
183 default:
184 break;
185 }
186 if ( $table.find( 'tr' ).length > 1 ) {
187 // Don't put $table in $body if the table is empty; this allows
188 // CSS :empty rules to have their proper effect.
189 ve.dm.MWLanguageVariantNode.static.processVariants(
190 $table[ 0 ], { showHidden: true }
191 );
192 $body.append( $table );
193 }
194 // Show "extra" properties, like describe, title, etc.
195 [ 'describe', 'title', 'hidden' ].forEach( ( flag ) => {
196 const f = ( flag === 'hidden' ) ? isHidden : variantInfo[ flag ];
197 if ( f ) {
198 // The following messages can be used here:
199 // * visualeditor-mwlanguagevariantcontextitem-flag-describe
200 // * visualeditor-mwlanguagevariantcontextitem-flag-hidden
201 // * visualeditor-mwlanguagevariantcontextitem-flag-title
202 $body.append( $( '<p>' ).text( OO.ui.msg(
203 'visualeditor-mwlanguagevariantcontextitem-flag-' + flag
204 ) ) );
205 }
206 } );
207 };
208
209 /**
210 * @inheritdoc
211 */
212 ve.ui.MWLanguageVariantNodeContextItem.prototype.getCommand = function () {
213 const type = this.model.getRuleType(),
214 cmdName = this.constructor.static.commandName + '-' + type;
215 return this.context.getSurface().commandRegistry.lookup( cmdName );
216 };
217
218 /* Registration */
219
220 ve.ui.contextItemFactory.register( ve.ui.MWLanguageVariantNodeContextItem );
221
222 [ 'disabled', 'filter', 'name', 'twoway', 'oneway' ].forEach( ( type ) => {
223 ve.ui.commandRegistry.register(
224 new ve.ui.Command(
225 'mwLanguageVariant-' + type, 'window', 'open',
226 {
227 args: [ 'mwLanguageVariant-' + type ],
228 supportedSelections: [ 'linear' ]
229 }
230 )
231 );
232 } );