2017-05-31 16:46:37 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor ContentEditable MWLanguageVariantNode class.
|
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
2017-05-31 16:46:37 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ContentEditable MediaWiki language variant node, used for
|
|
|
|
* LanguageConverter markup.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
* @extends ve.ce.LeafNode
|
|
|
|
* @mixins ve.ce.FocusableNode
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.dm.MWLanguageVariantNode} model Model to observe
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
|
|
|
ve.ce.MWLanguageVariantNode = function VeCeMWLanguageVariantNode( model, config ) {
|
|
|
|
// Parent constructor
|
|
|
|
ve.ce.MWLanguageVariantNode.super.call( this, model, config );
|
|
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
ve.ce.FocusableNode.call( this, this.$element, config );
|
|
|
|
|
|
|
|
// DOM changes
|
|
|
|
this.$element.addClass( 've-ce-mwLanguageVariantNode' );
|
|
|
|
this.$holder = this.appendHolder(); // null for a hidden node
|
|
|
|
|
|
|
|
// Events
|
|
|
|
this.model.connect( this, { update: 'onUpdate' } );
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.onUpdate();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ce.MWLanguageVariantNode, ve.ce.LeafNode );
|
|
|
|
|
|
|
|
OO.mixinClass( ve.ce.MWLanguageVariantNode, ve.ce.FocusableNode );
|
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
ve.ce.MWLanguageVariantNode.static.iconWhenInvisible = 'language';
|
|
|
|
|
|
|
|
/* Static Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ce.MWLanguageVariantNode.static.getDescription = function ( model ) {
|
|
|
|
// This is shown when you hover over the node.
|
|
|
|
var variantInfo = model.getVariantInfo(),
|
|
|
|
messageKey = 'visualeditor-mwlanguagevariant-' + model.getRuleType(),
|
|
|
|
languageCodes = [],
|
|
|
|
languageString;
|
|
|
|
if ( variantInfo.name ) {
|
|
|
|
languageCodes = [ variantInfo.name.t ];
|
|
|
|
} else if ( variantInfo.filter ) {
|
|
|
|
languageCodes = variantInfo.filter.l;
|
2017-08-09 14:54:35 +00:00
|
|
|
} else if ( variantInfo.twoway ) {
|
|
|
|
languageCodes = variantInfo.twoway.map( function ( item ) {
|
2017-05-31 16:46:37 +00:00
|
|
|
return item.l;
|
|
|
|
} );
|
2017-08-09 14:54:35 +00:00
|
|
|
} else if ( variantInfo.oneway ) {
|
|
|
|
languageCodes = variantInfo.oneway.map( function ( item ) {
|
2017-05-31 16:46:37 +00:00
|
|
|
return item.l;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
languageString = languageCodes.map( function ( code ) {
|
|
|
|
return ve.init.platform.getLanguageName( code.toLowerCase() );
|
|
|
|
} ).join( ve.msg( 'comma-separator' ) );
|
2019-11-01 16:20:22 +00:00
|
|
|
// The following messages can be used here:
|
|
|
|
// * visualeditor-mwlanguagevariant-disabled
|
|
|
|
// * visualeditor-mwlanguagevariant-filter
|
|
|
|
// * visualeditor-mwlanguagevariant-name
|
|
|
|
// * visualeditor-mwlanguagevariant-oneway
|
|
|
|
// * visualeditor-mwlanguagevariant-twoway
|
|
|
|
// * visualeditor-mwlanguagevariant-unknown
|
2017-05-31 16:46:37 +00:00
|
|
|
return ve.msg( messageKey, languageString );
|
|
|
|
};
|
|
|
|
|
2018-05-29 16:27:08 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2017-05-31 16:46:37 +00:00
|
|
|
/**
|
2018-05-29 16:27:08 +00:00
|
|
|
* Handle model update events.
|
|
|
|
*/
|
|
|
|
ve.ce.MWLanguageVariantNode.prototype.onUpdate = function () {
|
|
|
|
if ( !this.model.isHidden() ) {
|
|
|
|
this.model.constructor.static.insertPreviewElements(
|
|
|
|
this.$holder[ 0 ], this.model.getVariantInfo()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.updateInvisibleIconLabel();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
2017-05-31 16:46:37 +00:00
|
|
|
*
|
|
|
|
* The text preview is a trimmed down version of the actual rule. This
|
|
|
|
* means that we strip whitespace and newlines, and truncate to a
|
|
|
|
* fairly short length. The goal is to provide a fair representation of
|
|
|
|
* typical short rules, and enough context for long rules that the
|
|
|
|
* user can tell whether they want to see the full view by focusing the
|
|
|
|
* node / hovering.
|
|
|
|
*/
|
2018-05-29 16:27:08 +00:00
|
|
|
ve.ce.MWLanguageVariantNode.prototype.getInvisibleIconLabel = function () {
|
|
|
|
var $element,
|
|
|
|
variantInfo = this.model.getVariantInfo();
|
2017-05-31 16:46:37 +00:00
|
|
|
|
|
|
|
if ( this.model.isHidden() ) {
|
|
|
|
$element = $( '<div>' );
|
|
|
|
this.model.constructor.static.insertPreviewElements(
|
|
|
|
// For compactness, just annotate hidden rule w/ its
|
|
|
|
// current variant output.
|
|
|
|
$element[ 0 ], variantInfo
|
|
|
|
);
|
2018-05-29 16:27:08 +00:00
|
|
|
return $element.text().trim().replace( /\s+/, ' ' );
|
2017-05-31 16:46:37 +00:00
|
|
|
}
|
2018-05-29 16:27:08 +00:00
|
|
|
return null;
|
2017-05-31 16:46:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a {jQuery} appropriate for holding the output of this
|
|
|
|
* conversion rule.
|
2020-06-02 20:30:00 +00:00
|
|
|
*
|
2017-05-31 16:46:37 +00:00
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
|
|
|
ve.ce.MWLanguageVariantNode.prototype.appendHolder = function () {
|
|
|
|
var tagName = this.constructor.static.tagName,
|
|
|
|
document = this.$element[ 0 ].ownerDocument,
|
|
|
|
$holder = $( document.createElement( tagName ) );
|
|
|
|
$holder.addClass( 've-ce-mwLanguageVariantNode-holder' );
|
|
|
|
this.$element.append( $holder );
|
|
|
|
return $holder;
|
|
|
|
};
|
|
|
|
|
2017-09-14 15:03:09 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ce.MWLanguageVariantNode.prototype.hasRendering = function () {
|
|
|
|
// Efficiency improvement: the superclass implementation does a bunch
|
|
|
|
// of DOM measurement to determine if the node is empty.
|
|
|
|
// Instead consult the model for a definitive answer.
|
|
|
|
return !this.model.isHidden();
|
|
|
|
};
|