Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ce/nodes/ve.ce.MWLanguageVariantNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable MWLanguageVariantNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * ContentEditable MediaWiki language variant node, used for
10 * LanguageConverter markup.
11 *
12 * @class
13 * @abstract
14 * @extends ve.ce.LeafNode
15 * @mixes ve.ce.FocusableNode
16 * @constructor
17 * @param {ve.dm.MWLanguageVariantNode} model Model to observe
18 * @param {Object} [config] Configuration options
19 */
20 ve.ce.MWLanguageVariantNode = function VeCeMWLanguageVariantNode( model, config ) {
21 // Parent constructor
22 ve.ce.MWLanguageVariantNode.super.call( this, model, config );
23
24 // Mixin constructors
25 ve.ce.FocusableNode.call( this, this.$element, config );
26
27 // DOM changes
28 this.$element.addClass( 've-ce-mwLanguageVariantNode' );
29 this.$holder = this.appendHolder(); // null for a hidden node
30
31 // Events
32 this.model.connect( this, { update: 'onUpdate' } );
33
34 // Initialization
35 this.onUpdate();
36 };
37
38 /* Inheritance */
39
40 OO.inheritClass( ve.ce.MWLanguageVariantNode, ve.ce.LeafNode );
41
42 OO.mixinClass( ve.ce.MWLanguageVariantNode, ve.ce.FocusableNode );
43
44 /* Static Properties */
45
46 ve.ce.MWLanguageVariantNode.static.iconWhenInvisible = 'language';
47
48 /* Static Methods */
49
50 /**
51 * @inheritdoc
52 */
53 ve.ce.MWLanguageVariantNode.static.getDescription = function ( model ) {
54 // This is shown when you hover over the node.
55 const variantInfo = model.getVariantInfo(),
56 messageKey = 'visualeditor-mwlanguagevariant-' + model.getRuleType();
57 let languageCodes = [];
58 if ( variantInfo.name ) {
59 languageCodes = [ variantInfo.name.t ];
60 } else if ( variantInfo.filter ) {
61 languageCodes = variantInfo.filter.l;
62 } else if ( variantInfo.twoway ) {
63 languageCodes = variantInfo.twoway.map( ( item ) => item.l );
64 } else if ( variantInfo.oneway ) {
65 languageCodes = variantInfo.oneway.map( ( item ) => item.l );
66 }
67 const languageString = languageCodes.map( ( code ) => ve.init.platform.getLanguageName( code.toLowerCase() ) ).join( ve.msg( 'comma-separator' ) );
68 // The following messages can be used here:
69 // * visualeditor-mwlanguagevariant-disabled
70 // * visualeditor-mwlanguagevariant-filter
71 // * visualeditor-mwlanguagevariant-name
72 // * visualeditor-mwlanguagevariant-oneway
73 // * visualeditor-mwlanguagevariant-twoway
74 // * visualeditor-mwlanguagevariant-unknown
75 return ve.msg( messageKey, languageString );
76 };
77
78 /* Methods */
79
80 /**
81 * Handle model update events.
82 */
83 ve.ce.MWLanguageVariantNode.prototype.onUpdate = function () {
84 if ( !this.model.isHidden() ) {
85 this.model.constructor.static.insertPreviewElements(
86 this.$holder[ 0 ], this.model.getVariantInfo()
87 );
88 }
89 this.updateInvisibleIconLabel();
90 };
91
92 /**
93 * @inheritdoc
94 *
95 * The text preview is a trimmed down version of the actual rule. This
96 * means that we strip whitespace and newlines, and truncate to a
97 * fairly short length. The goal is to provide a fair representation of
98 * typical short rules, and enough context for long rules that the
99 * user can tell whether they want to see the full view by focusing the
100 * node / hovering.
101 */
102 ve.ce.MWLanguageVariantNode.prototype.getInvisibleIconLabel = function () {
103 const variantInfo = this.model.getVariantInfo();
104
105 if ( this.model.isHidden() ) {
106 const $element = $( '<div>' );
107 this.model.constructor.static.insertPreviewElements(
108 // For compactness, just annotate hidden rule w/ its
109 // current variant output.
110 $element[ 0 ], variantInfo
111 );
112 return $element.text().trim().replace( /\s+/, ' ' );
113 }
114 return null;
115 };
116
117 /**
118 * Create a {jQuery} appropriate for holding the output of this
119 * conversion rule.
120 *
121 * @return {jQuery}
122 */
123 ve.ce.MWLanguageVariantNode.prototype.appendHolder = function () {
124 const tagName = this.constructor.static.tagName,
125 document = this.$element[ 0 ].ownerDocument,
126 $holder = $( document.createElement( tagName ) );
127 $holder.addClass( 've-ce-mwLanguageVariantNode-holder' );
128 this.$element.append( $holder );
129 return $holder;
130 };
131
132 /**
133 * @inheritdoc
134 */
135 ve.ce.MWLanguageVariantNode.prototype.hasRendering = function () {
136 // Efficiency improvement: the superclass implementation does a bunch
137 // of DOM measurement to determine if the node is empty.
138 // Instead consult the model for a definitive answer.
139 return !this.model.isHidden();
140 };