Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/inspectors/ve.ui.MWLanguageVariantInspector.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface LanguageVariantInspector class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Inspector for a ve.dm.MWLanguageVariantNode.
9 *
10 * @class
11 * @extends ve.ui.NodeInspector
12 *
13 * @constructor
14 * @param {Object} [config] Configuration options
15 */
16 ve.ui.MWLanguageVariantInspector = function VeUiMWLanguageVariantInspector() {
17 // Parent constructor
18 ve.ui.MWLanguageVariantInspector.super.apply( this, arguments );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.ui.MWLanguageVariantInspector, ve.ui.NodeInspector );
24
25 /* Static properties */
26
27 ve.ui.MWLanguageVariantInspector.static.name = 'mwLanguageVariant-disabled';
28
29 ve.ui.MWLanguageVariantInspector.static.title = OO.ui.deferMsg(
30 'visualeditor-mwlanguagevariantinspector-title-disabled'
31 );
32
33 ve.ui.MWLanguageVariantInspector.static.modelClasses = [
34 ve.dm.MWLanguageVariantBlockNode,
35 ve.dm.MWLanguageVariantInlineNode,
36 ve.dm.MWLanguageVariantHiddenNode
37 ];
38
39 ve.ui.MWLanguageVariantInspector.static.size = 'large';
40
41 ve.ui.MWLanguageVariantInspector.static.actions = [
42 {
43 action: 'remove',
44 label: OO.ui.deferMsg( 'visualeditor-inspector-remove-tooltip' ),
45 flags: 'destructive',
46 modes: 'edit'
47 },
48 ...ve.ui.MWLanguageVariantInspector.super.static.actions
49 ];
50
51 ve.ui.MWLanguageVariantInspector.static.defaultVariantType =
52 'mwLanguageVariantInline';
53
54 ve.ui.MWLanguageVariantInspector.static.includeCommands = null;
55
56 // This is very similar to the exclude list in ve.ui.MWMediaDialog
57 ve.ui.MWLanguageVariantInspector.static.excludeCommands = [
58 // No formatting
59 'paragraph',
60 'heading1',
61 'heading2',
62 'heading3',
63 'heading4',
64 'heading5',
65 'heading6',
66 'preformatted',
67 'blockquote',
68 // TODO: Decide if tables tools should be allowed
69 'tableCellHeader',
70 'tableCellData',
71 // No structure
72 'bullet',
73 'bulletWrapOnce',
74 'number',
75 'numberWrapOnce',
76 'indent',
77 'outdent'
78 ];
79
80 /**
81 * Get the import rules for embedded target widgets in this inspector.
82 *
83 * @see ve.ui.MWMediaDialog#getImportRules
84 * @return {Object} Import rules
85 */
86 ve.ui.MWLanguageVariantInspector.static.getImportRules = function () {
87 return ve.extendObject(
88 ve.copy( ve.init.target.constructor.static.importRules ),
89 {
90 // TODO: We might want to include some of the
91 // conversion/sanitization done by ve.ui.MWMediaDialog
92 }
93 );
94 };
95
96 /* Methods */
97
98 /**
99 * Return a valid `variantInfo` object which will be used when a new
100 * node of this subclass is inserted. For instance,
101 * ve.ui.MWLanguageVariantDisabledInspector will return the appropriate
102 * object to use when the equivalent of wikitext `-{}-` is inserted
103 * in the document.
104 *
105 * @return {Object}
106 */
107 ve.ui.MWLanguageVariantInspector.prototype.getDefaultVariantInfo = null;
108
109 /**
110 * Convert the current inspector state to new content which can be used
111 * to update the ve.dm.SurfaceFragment backing this inspector.
112 *
113 * @param {Object} An existing variantInfo object for this node, which will be
114 * mutated to update it with the latest content from this inspector.
115 * @return {string|Array} New content for the ve.dm.SurfaceFragment
116 * being inspected/updated.
117 */
118 ve.ui.MWLanguageVariantInspector.prototype.getContentFromInspector = null;
119
120 // Helper functions for creating sub-document editors for embedded HTML
121
122 /**
123 * Helper function to create a subdocument editor for HTML embedded in the
124 * language variant node.
125 *
126 * @param {string} [placeholder] Placeholder text for this editor.
127 * @return {ve.ui.TargetWidget}
128 */
129 ve.ui.MWLanguageVariantInspector.prototype.createTextTarget = function ( placeholder ) {
130 return ve.init.target.createTargetWidget( {
131 toolbarGroups: [],
132 includeCommands: this.constructor.static.includeCommands,
133 excludeCommands: this.constructor.static.excludeCommands,
134 importRules: this.constructor.static.getImportRules(),
135 inDialog: this.constructor.static.name,
136 placeholder: placeholder || null
137 } );
138 };
139
140 /**
141 * Helper function to initialize a ve.ui.TargetWidget with a given HTML
142 * string extracted from the language variant node.
143 *
144 * @param {ve.ui.TargetWidget} [textTarget] A subdocument editor widget
145 * created by ve.ui.MWLanguageVariantInspector#createTextTarget.
146 * @param {string} [htmlString] The HTML string extracted from this node.
147 * @return {ve.dm.Document} The document model now backing the widget.
148 */
149 ve.ui.MWLanguageVariantInspector.prototype.setupTextTargetDoc = function ( textTarget, htmlString ) {
150 const doc = this.variantNode.getDocument().newFromHtml( htmlString );
151 textTarget.setDocument( doc );
152 return doc;
153 };
154
155 /**
156 * Helper function to serialize the document backing a `ve.ui.TargetWidget`
157 * back into HTML which can be embedded into the language variant node.
158 * This method needs to do a bit of hackery to remove unnecessary p-wrapping
159 * and (TODO) determine if an inline node needs to be converted to a
160 * block node or vice-versa.
161 *
162 * @param {ve.dm.Document} doc The document backing an editor widget, as returned
163 * by ve.ui.MWLanguageVariantInspector#setupTextTargetDoc.
164 * @return {string} An HTML string appropriate for embedding into a
165 * language variant node.
166 */
167 ve.ui.MWLanguageVariantInspector.prototype.getHtmlForDoc = function ( doc ) {
168 const surface = new ve.dm.Surface( doc );
169
170 // Remove outermost p-wrapping, if present
171 try {
172 surface.change(
173 ve.dm.TransactionBuilder.static.newFromWrap( doc, new ve.Range( 0, doc.data.countNonInternalElements() ), [], [], [ { type: 'paragraph' } ], [] )
174 );
175 } catch ( e ) {
176 // Sometimes there is no p-wrapping, for example: "* foo"
177 // Sometimes there are multiple <p> tags in the output.
178 // That's okay: ignore the error and use what we've got.
179 }
180 // XXX return a flag to indicate whether contents are now inline or block?
181 const targetHtmlDoc = ve.dm.converter.getDomFromModel( doc );
182 return ve.properInnerHtml( targetHtmlDoc.body );
183 };
184
185 // Inspector implementation
186
187 /**
188 * Handle frame ready events.
189 */
190 ve.ui.MWLanguageVariantInspector.prototype.initialize = function () {
191 // Parent method
192 ve.ui.MWLanguageVariantInspector.super.prototype.initialize.call( this );
193 this.$content.addClass( 've-ui-mwLanguageVariantInspector-content' );
194 };
195
196 /**
197 * @inheritdoc
198 */
199 ve.ui.MWLanguageVariantInspector.prototype.getActionProcess = function ( action ) {
200 if ( action === 'remove' || action === 'insert' ) {
201 return new OO.ui.Process( () => {
202 this.close( { action: action } );
203 } );
204 }
205 return ve.ui.MWLanguageVariantInspector.super.prototype.getActionProcess.call( this, action );
206 };
207
208 /**
209 * Handle the inspector being setup.
210 *
211 * @param {Object} [data] Inspector opening data
212 * @return {OO.ui.Process}
213 */
214 ve.ui.MWLanguageVariantInspector.prototype.getSetupProcess = function ( data ) {
215 return ve.ui.MWLanguageVariantInspector.super.prototype.getSetupProcess.call( this, data )
216 .next( () => {
217 this.getFragment().getSurface().pushStaging();
218
219 this.variantNode = this.getSelectedNode();
220 if ( !this.variantNode ) {
221 this.getFragment().insertContent( [
222 {
223 type: this.constructor.static.defaultVariantType,
224 attributes: {
225 variantInfo: this.getDefaultVariantInfo()
226 }
227 },
228 { type: '/' + this.constructor.static.defaultVariantType }
229 ] ).select();
230 this.variantNode = this.getSelectedNode();
231 }
232 } );
233 };
234
235 /**
236 * @inheritdoc
237 */
238 ve.ui.MWLanguageVariantInspector.prototype.getTeardownProcess = function ( data ) {
239 data = data || {};
240 return ve.ui.MWLanguageVariantInspector.super.prototype.getTeardownProcess.call( this, data )
241 .first( () => {
242 const surfaceModel = this.getFragment().getSurface();
243
244 if ( data.action === 'remove' ) {
245 surfaceModel.popStaging();
246 // If popStaging removed the node then this will be a no-op
247 this.getFragment().removeContent();
248 } else if ( data.action === 'done' ) {
249 // Edit language variant node
250 const newContent = this.getContentFromInspector(
251 ve.copy( this.variantNode.getVariantInfo() )
252 );
253 if ( newContent[ 0 ].type === this.variantNode.getType() ) {
254 this.getFragment().changeAttributes( {
255 variantInfo: newContent[ 0 ].attributes.variantInfo
256 } );
257 } else {
258 this.getFragment().removeContent();
259 this.getFragment().insertContent( newContent ).select();
260 this.variantNode = this.getSelectedNode();
261 }
262 surfaceModel.applyStaging();
263 } else {
264 surfaceModel.popStaging();
265 }
266
267 } );
268 };
269
270 /* Subclasses */
271
272 /**
273 * Editor for "disabled" rules.
274 *
275 * @class
276 * @extends ve.ui.MWLanguageVariantInspector
277 *
278 * @constructor
279 * @param {Object} [config] Configuration options
280 */
281 ve.ui.MWLanguageVariantDisabledInspector = function VeUiMWLanguageVariantDisabledInspector() {
282 ve.ui.MWLanguageVariantDisabledInspector.super.apply( this, arguments );
283 };
284
285 /* Inheritance */
286
287 OO.inheritClass( ve.ui.MWLanguageVariantDisabledInspector, ve.ui.MWLanguageVariantInspector );
288
289 /* Static properties */
290
291 ve.ui.MWLanguageVariantDisabledInspector.static.name = 'mwLanguageVariant-disabled';
292
293 ve.ui.MWLanguageVariantDisabledInspector.static.title = OO.ui.deferMsg(
294 'visualeditor-mwlanguagevariantinspector-title-disabled'
295 );
296
297 /* Methods */
298
299 ve.ui.MWLanguageVariantDisabledInspector.prototype.initialize = function () {
300 ve.ui.MWLanguageVariantDisabledInspector.super.prototype.initialize.call( this );
301 this.textTarget = this.createTextTarget( OO.ui.msg(
302 'visualeditor-mwlanguagevariantinspector-disabled-placeholder'
303 ) );
304 this.form.$element.append( this.textTarget.$element );
305 };
306
307 ve.ui.MWLanguageVariantDisabledInspector.prototype.getDefaultVariantInfo = function () {
308 return { disabled: { t: '' } };
309 };
310
311 ve.ui.MWLanguageVariantDisabledInspector.prototype.getSetupProcess = function ( data ) {
312 return ve.ui.MWLanguageVariantDisabledInspector.super.prototype.getSetupProcess.call( this, data ).next( () => {
313 const variantInfo = this.variantNode.getVariantInfo();
314 this.textTargetDoc = this.setupTextTargetDoc(
315 this.textTarget,
316 variantInfo.disabled.t
317 );
318 } );
319 };
320
321 ve.ui.MWLanguageVariantDisabledInspector.prototype.getContentFromInspector = function ( variantInfo ) {
322 // TODO should allow type to depend on targetHtmlDoc, maybe switch
323 // from inline to block.
324 const type = this.variantNode.getType();
325 variantInfo.disabled.t = this.getHtmlForDoc( this.textTargetDoc );
326 return [
327 {
328 type: type,
329 attributes: { variantInfo: variantInfo }
330 },
331 {
332 type: '/' + type
333 }
334 ];
335 };
336
337 ve.ui.MWLanguageVariantDisabledInspector.prototype.getReadyProcess = function ( data ) {
338 return ve.ui.MWLanguageVariantDisabledInspector.super.prototype.getReadyProcess.call( this, data )
339 .next( () => {
340 this.textTarget.focus();
341 } );
342 };
343
344 ve.ui.MWLanguageVariantDisabledInspector.prototype.getTeardownProcess = function ( data ) {
345 return ve.ui.MWLanguageVariantDisabledInspector.super.prototype.getTeardownProcess.call( this, data )
346 .next( () => {
347 // Reset inspector
348 this.textTarget.clear();
349 this.textTargetDoc = null;
350 } );
351 };
352
353 /**
354 * Editor for "name" rules.
355 *
356 * @class
357 * @extends ve.ui.MWLanguageVariantInspector
358 *
359 * @constructor
360 * @param {Object} [config] Configuration options
361 */
362 ve.ui.MWLanguageVariantNameInspector = function VeUiMWLanguageVariantNameInspector() {
363 ve.ui.MWLanguageVariantNameInspector.super.apply( this, arguments );
364 };
365
366 /* Inheritance */
367
368 OO.inheritClass( ve.ui.MWLanguageVariantNameInspector, ve.ui.MWLanguageVariantInspector );
369
370 /* Static properties */
371
372 ve.ui.MWLanguageVariantNameInspector.static.name = 'mwLanguageVariant-name';
373
374 ve.ui.MWLanguageVariantNameInspector.static.title = OO.ui.deferMsg(
375 'visualeditor-mwlanguagevariantinspector-title-name'
376 );
377
378 /* Methods */
379
380 ve.ui.MWLanguageVariantNameInspector.prototype.initialize = function () {
381 ve.ui.MWLanguageVariantNameInspector.super.prototype.initialize.call( this );
382 this.languageInput = new ve.ui.LanguageInputWidget( {
383 dialogManager: this.manager.getSurface().getDialogs(),
384 dirInput: 'none'
385 } );
386 this.form.$element.append( this.languageInput.$element );
387 };
388
389 ve.ui.MWLanguageVariantNameInspector.prototype.getDefaultVariantInfo = function () {
390 return { name: { t: mw.config.get( 'wgUserVariant' ) || 'en' } };
391 };
392
393 ve.ui.MWLanguageVariantNameInspector.prototype.getSetupProcess = function ( data ) {
394 return ve.ui.MWLanguageVariantNameInspector.super.prototype.getSetupProcess.call( this, data )
395 .next( () => {
396 const variantInfo = this.variantNode.getVariantInfo();
397 this.languageInput.setLangAndDir(
398 variantInfo.name.t,
399 'auto'
400 );
401 } );
402 };
403
404 ve.ui.MWLanguageVariantNameInspector.prototype.getContentFromInspector = function ( variantInfo ) {
405 const type = this.variantNode.getType();
406 variantInfo.name.t = this.languageInput.getLang();
407 return [
408 {
409 type: type,
410 attributes: { variantInfo: variantInfo }
411 },
412 {
413 type: '/' + type
414 }
415 ];
416 };
417
418 /**
419 * Editor for "filter" rules.
420 *
421 * @class
422 * @extends ve.ui.MWLanguageVariantInspector
423 *
424 * @constructor
425 * @param {Object} [config] Configuration options
426 */
427 ve.ui.MWLanguageVariantFilterInspector = function VeUiMWLanguageVariantFilterInspector() {
428 ve.ui.MWLanguageVariantFilterInspector.super.apply( this, arguments );
429 };
430
431 /* Inheritance */
432
433 OO.inheritClass( ve.ui.MWLanguageVariantFilterInspector, ve.ui.MWLanguageVariantInspector );
434
435 /* Static properties */
436
437 ve.ui.MWLanguageVariantFilterInspector.static.name = 'mwLanguageVariant-filter';
438
439 ve.ui.MWLanguageVariantFilterInspector.static.title = OO.ui.deferMsg(
440 'visualeditor-mwlanguagevariantinspector-title-filter'
441 );
442
443 /* Methods */
444
445 ve.ui.MWLanguageVariantFilterInspector.prototype.initialize = function () {
446 ve.ui.MWLanguageVariantFilterInspector.super.prototype.initialize.call( this );
447 this.textTarget = this.createTextTarget( OO.ui.msg(
448 'visualeditor-mwlanguagevariantinspector-filter-text-placeholder'
449 ) );
450
451 this.langWidget = new OO.ui.TagMultiselectWidget( {
452 allowArbitary: false,
453 allowDisplayInvalidTags: true,
454 allowedValues: ve.init.platform.getLanguageCodes().sort(),
455 placeholder: OO.ui.msg(
456 'visualeditor-mwlanguagevariantinspector-filter-langs-placeholder'
457 ),
458 icon: 'language'
459 } );
460 this.langWidget.createTagItemWidget = function ( data, label ) {
461 const name = ve.init.platform.getLanguageName( data.toLowerCase() );
462 label = label || ( name ? ( name + ' (' + data + ')' ) : data );
463 return OO.ui.TagMultiselectWidget.prototype.createTagItemWidget.call(
464 this, data, label
465 );
466 };
467
468 this.form.$element.append(
469 new OO.ui.FieldLayout( this.langWidget, {
470 align: 'top',
471 label: OO.ui.msg( 'visualeditor-mwlanguagevariantinspector-filter-langs-label' )
472 } ).$element
473 );
474 this.form.$element.append(
475 new OO.ui.FieldLayout( this.textTarget, {
476 align: 'top',
477 label: OO.ui.msg( 'visualeditor-mwlanguagevariantinspector-filter-text-label' )
478 } ).$element
479 );
480 };
481
482 ve.ui.MWLanguageVariantFilterInspector.prototype.getDefaultVariantInfo = function () {
483 return { filter: { l: [ mw.config.get( 'wgUserVariant' ) ], t: '' } };
484 };
485
486 ve.ui.MWLanguageVariantFilterInspector.prototype.getSetupProcess = function ( data ) {
487 return ve.ui.MWLanguageVariantFilterInspector.super.prototype.getSetupProcess.call( this, data ).next( () => {
488 const variantInfo = this.variantNode.getVariantInfo();
489 this.textTargetDoc = this.setupTextTargetDoc(
490 this.textTarget,
491 variantInfo.filter.t
492 );
493 this.langWidget.setValue( variantInfo.filter.l );
494 } );
495 };
496
497 ve.ui.MWLanguageVariantFilterInspector.prototype.getContentFromInspector = function ( variantInfo ) {
498 // TODO should allow type to depend on targetHtmlDoc, maybe switch
499 // from inline to block.
500 const type = this.variantNode.getType();
501 variantInfo.filter.t = this.getHtmlForDoc( this.textTargetDoc );
502 variantInfo.filter.l = this.langWidget.getValue();
503 return [
504 {
505 type: type,
506 attributes: { variantInfo: variantInfo }
507 },
508 {
509 type: '/' + type
510 }
511 ];
512 };
513
514 ve.ui.MWLanguageVariantFilterInspector.prototype.getReadyProcess = function ( data ) {
515 return ve.ui.MWLanguageVariantFilterInspector.super.prototype.getReadyProcess.call( this, data )
516 .next( () => {
517 this.textTarget.focus();
518 } );
519 };
520
521 ve.ui.MWLanguageVariantFilterInspector.prototype.getTeardownProcess = function ( data ) {
522 return ve.ui.MWLanguageVariantFilterInspector.super.prototype.getTeardownProcess.call( this, data )
523 .next( () => {
524 // Reset inspector
525 this.langWidget.clearInput();
526 this.langWidget.clearItems();
527 this.textTarget.clear();
528 this.textTargetDoc = null;
529 } );
530 };
531
532 /**
533 * Editor for "two-way" rules.
534 *
535 * @class
536 * @extends ve.ui.MWLanguageVariantInspector
537 *
538 * @constructor
539 * @param {Object} [config] Configuration options
540 */
541 ve.ui.MWLanguageVariantTwoWayInspector = function VeUiMWLanguageVariantTwoWayInspector() {
542 ve.ui.MWLanguageVariantTwoWayInspector.super.apply( this, arguments );
543 };
544
545 /* Inheritance */
546
547 OO.inheritClass( ve.ui.MWLanguageVariantTwoWayInspector, ve.ui.MWLanguageVariantInspector );
548
549 /* Static properties */
550
551 ve.ui.MWLanguageVariantTwoWayInspector.static.name = 'mwLanguageVariant-twoway';
552
553 ve.ui.MWLanguageVariantTwoWayInspector.static.title = OO.ui.deferMsg(
554 'visualeditor-mwlanguagevariantinspector-title-twoway'
555 );
556
557 /* Methods */
558
559 ve.ui.MWLanguageVariantTwoWayInspector.prototype.initialize = function () {
560 ve.ui.MWLanguageVariantTwoWayInspector.super.prototype.initialize.call( this );
561 this.items = [];
562 this.layout = new OO.ui.FieldsetLayout( { } );
563 this.form.$element.append( this.layout.$element );
564
565 this.addButton = new OO.ui.ButtonInputWidget( {
566 label: OO.ui.msg( 'visualeditor-mwlanguagevariantinspector-twoway-add-button' ),
567 icon: 'add'
568 } );
569 this.form.$element.append( this.addButton.$element );
570
571 // Events
572 this.addButton.connect( this, { click: 'onAddButtonClick' } );
573 };
574
575 ve.ui.MWLanguageVariantTwoWayInspector.prototype.getDefaultVariantInfo = function () {
576 return { twoway: [ { l: mw.config.get( 'wgUserVariant' ), t: '' } ] };
577 };
578
579 ve.ui.MWLanguageVariantTwoWayInspector.prototype.getSetupProcess = function ( data ) {
580 return ve.ui.MWLanguageVariantTwoWayInspector.super.prototype.getSetupProcess.call( this, data ).next( () => {
581 const variantInfo = this.variantNode.getVariantInfo();
582 this.layout.clearItems();
583 this.items = [];
584 variantInfo.twoway.forEach( ( tw, idx ) => {
585 this.items[ idx ] = this.createItem( tw.l, tw.t );
586 this.layout.addItems( [ this.items[ idx ].layout ] );
587 } );
588 } );
589 };
590
591 /**
592 * Create widgets corresponding to a given mapping given by this rule.
593 *
594 * @param {string} [lang] The language code for the content text.
595 * @param {string} [content] The HTML content text.
596 * @return {Object} An object containing the required widgets and backing
597 * documents for this mapping item.
598 */
599 ve.ui.MWLanguageVariantTwoWayInspector.prototype.createItem = function ( lang, content ) {
600 const languageInput = new ve.ui.LanguageInputWidget( {
601 dialogManager: this.manager.getSurface().getDialogs(),
602 dirInput: 'none'
603 } );
604 const textTarget = this.createTextTarget( OO.ui.msg(
605 'visualeditor-mwlanguagevariantinspector-twoway-text-placeholder'
606 ) );
607 const clearButton = new OO.ui.ButtonInputWidget( {
608 icon: 'clear',
609 title: OO.ui.deferMsg(
610 'visualeditor-mwlanguagevariantinspector-twoway-clear-button'
611 ),
612 framed: false
613 } );
614 const layout = new OO.ui.FieldLayout(
615 new OO.ui.Widget( {
616 content: [
617 new OO.ui.ActionFieldLayout(
618 languageInput,
619 clearButton
620 ),
621 textTarget
622 ]
623 } ), {}
624 );
625 const item = {
626 languageInput: languageInput,
627 textTarget: textTarget,
628 clearButton: clearButton,
629 layout: layout
630 };
631
632 // Initialize
633 item.textTargetDoc = this.setupTextTargetDoc( textTarget, content );
634 languageInput.setLangAndDir( lang, 'auto' );
635 clearButton.connect( this, { click: [ 'onClearButtonClick', item ] } );
636 return item;
637 };
638
639 ve.ui.MWLanguageVariantTwoWayInspector.prototype.getContentFromInspector = function ( variantInfo ) {
640 // TODO should allow type to depend on targetHtmlDoc, maybe switch
641 // from inline to block.
642 const type = this.variantNode.getType();
643 variantInfo.twoway = this.items.map( ( item ) => ( {
644 l: item.languageInput.getLang(),
645 t: this.getHtmlForDoc( item.textTargetDoc )
646 } ) );
647 return [
648 {
649 type: type,
650 attributes: { variantInfo: variantInfo }
651 },
652 {
653 type: '/' + type
654 }
655 ];
656 };
657
658 /**
659 * Create a new mapping item in the inspector.
660 */
661 ve.ui.MWLanguageVariantTwoWayInspector.prototype.onAddButtonClick = function () {
662 const idx = this.items.length;
663 this.items[ idx ] = this.createItem( mw.config.get( 'wgUserVariant' ), '' );
664 this.layout.addItems( [ this.items[ idx ].layout ] );
665 };
666
667 /**
668 * Remove a mapping item from the inspector.
669 *
670 * @param {Object} item
671 */
672 ve.ui.MWLanguageVariantTwoWayInspector.prototype.onClearButtonClick = function ( item ) {
673 const idx = this.items.indexOf( item );
674 this.items.splice( idx, 1 );
675 this.layout.removeItems( [ item.layout ] );
676 item.clearButton.disconnect( this );
677 };
678
679 /**
680 * Editor for "one-way" rules.
681 *
682 * @class
683 * @extends ve.ui.MWLanguageVariantInspector
684 *
685 * @constructor
686 * @param {Object} [config] Configuration options
687 */
688 ve.ui.MWLanguageVariantOneWayInspector = function VeUiMWLanguageVariantOneWayInspector() {
689 ve.ui.MWLanguageVariantOneWayInspector.super.apply( this, arguments );
690 };
691
692 /* Inheritance */
693
694 OO.inheritClass( ve.ui.MWLanguageVariantOneWayInspector, ve.ui.MWLanguageVariantInspector );
695
696 /* Static properties */
697
698 ve.ui.MWLanguageVariantOneWayInspector.static.name = 'mwLanguageVariant-oneway';
699
700 ve.ui.MWLanguageVariantOneWayInspector.static.title = OO.ui.deferMsg(
701 'visualeditor-mwlanguagevariantinspector-title-oneway'
702 );
703
704 /* Methods */
705
706 ve.ui.MWLanguageVariantOneWayInspector.prototype.initialize = function () {
707 ve.ui.MWLanguageVariantOneWayInspector.super.prototype.initialize.call( this );
708 this.items = [];
709 this.layout = new OO.ui.FieldsetLayout( { } );
710 this.form.$element.append( this.layout.$element );
711
712 this.addButton = new OO.ui.ButtonInputWidget( {
713 label: OO.ui.msg( 'visualeditor-mwlanguagevariantinspector-oneway-add-button' ),
714 icon: 'add'
715 } );
716 this.form.$element.append( this.addButton.$element );
717
718 // Events
719 this.addButton.connect( this, { click: 'onAddButtonClick' } );
720 };
721
722 ve.ui.MWLanguageVariantOneWayInspector.prototype.getDefaultVariantInfo = function () {
723 return { oneway: [ { f: '', l: mw.config.get( 'wgUserVariant' ), t: '' } ] };
724 };
725
726 ve.ui.MWLanguageVariantOneWayInspector.prototype.getSetupProcess = function ( data ) {
727 return ve.ui.MWLanguageVariantOneWayInspector.super.prototype.getSetupProcess.call( this, data ).next( () => {
728 const variantInfo = this.variantNode.getVariantInfo();
729 this.layout.clearItems();
730 this.items = [];
731 variantInfo.oneway.forEach( ( ow, idx ) => {
732 this.items[ idx ] = this.createItem( ow.f, ow.l, ow.t );
733 this.layout.addItems( [ this.items[ idx ].layout ] );
734 } );
735 } );
736 };
737
738 /**
739 * Create widgets corresponding to a given mapping given by this rule.
740 *
741 * @param {string} [from] The HTML source text.
742 * @param {string} [lang] The language code for the destination text.
743 * @param {string} [to] The HTML destination text.
744 * @return {Object} An object containing the required widgets and backing
745 * documents for this mapping item.
746 */
747 ve.ui.MWLanguageVariantOneWayInspector.prototype.createItem = function ( from, lang, to ) {
748 const fromTextTarget = this.createTextTarget( OO.ui.msg(
749 'visualeditor-mwlanguagevariantinspector-oneway-from-text-placeholder'
750 ) );
751 const languageInput = new ve.ui.LanguageInputWidget( {
752 dialogManager: this.manager.getSurface().getDialogs(),
753 dirInput: 'none'
754 } );
755 const toTextTarget = this.createTextTarget( OO.ui.msg(
756 'visualeditor-mwlanguagevariantinspector-oneway-to-text-placeholder'
757 ) );
758 const clearButton = new OO.ui.ButtonInputWidget( {
759 icon: 'clear',
760 title: OO.ui.deferMsg(
761 'visualeditor-mwlanguagevariantinspector-oneway-clear-button'
762 ),
763 framed: false
764 } );
765 const layout = new OO.ui.FieldLayout(
766 new OO.ui.Widget( {
767 content: [
768 new OO.ui.ActionFieldLayout(
769 fromTextTarget,
770 clearButton
771 ),
772 languageInput,
773 toTextTarget
774 ]
775 } ), {}
776 );
777 const item = {
778 fromTextTarget: fromTextTarget,
779 languageInput: languageInput,
780 toTextTarget: toTextTarget,
781 clearButton: clearButton,
782 layout: layout
783 };
784
785 // Initialize
786 item.fromTextTargetDoc = this.setupTextTargetDoc( fromTextTarget, from );
787 item.toTextTargetDoc = this.setupTextTargetDoc( toTextTarget, to );
788 languageInput.setLangAndDir( lang, 'auto' );
789 clearButton.connect( this, { click: [ 'onClearButtonClick', item ] } );
790 return item;
791 };
792
793 ve.ui.MWLanguageVariantOneWayInspector.prototype.getContentFromInspector = function ( variantInfo ) {
794 // TODO should allow type to depend on targetHtmlDoc, maybe switch
795 // from inline to block.
796 const type = this.variantNode.getType();
797 variantInfo.oneway = this.items.map( ( item ) => ( {
798 f: this.getHtmlForDoc( item.fromTextTargetDoc ),
799 l: item.languageInput.getLang(),
800 t: this.getHtmlForDoc( item.toTextTargetDoc )
801 } ) );
802 return [
803 {
804 type: type,
805 attributes: { variantInfo: variantInfo }
806 },
807 {
808 type: '/' + type
809 }
810 ];
811 };
812
813 /**
814 * Create a new mapping item in the inspector.
815 */
816 ve.ui.MWLanguageVariantOneWayInspector.prototype.onAddButtonClick = function () {
817 const idx = this.items.length;
818 this.items[ idx ] = this.createItem( '', mw.config.get( 'wgUserVariant' ), '' );
819 this.layout.addItems( [ this.items[ idx ].layout ] );
820 };
821
822 /**
823 * Remove a mapping item from the inspector.
824 *
825 * @param {Object} item
826 */
827 ve.ui.MWLanguageVariantOneWayInspector.prototype.onClearButtonClick = function ( item ) {
828 const idx = this.items.indexOf( item );
829 this.items.splice( idx, 1 );
830 this.layout.removeItems( [ item.layout ] );
831 item.clearButton.disconnect( this );
832 };
833
834 /* Registration */
835
836 ve.ui.windowFactory.register( ve.ui.MWLanguageVariantDisabledInspector );
837 ve.ui.windowFactory.register( ve.ui.MWLanguageVariantNameInspector );
838 ve.ui.windowFactory.register( ve.ui.MWLanguageVariantFilterInspector );
839 ve.ui.windowFactory.register( ve.ui.MWLanguageVariantTwoWayInspector );
840 ve.ui.windowFactory.register( ve.ui.MWLanguageVariantOneWayInspector );