All files / ext.wikilambda.edit/store/classes LabelData.js

100% Statements 12/12
90.9% Branches 10/11
100% Functions 5/5
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82                63x                                     11651x 11651x 11651x     11651x 10493x                   11388x 11388x                 1688x                   1028x                     434x           63x  
/*!
 * WikiLambda LabelData class
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
const typeUtils = require( '../../mixins/typeUtils.js' ).methods;
 
/**
 * LabelData class contains the information of a localized label.
 * This is the type of object that's saved in the global store and
 * is returned by the getter getLabelData to those components that
 * need not only the string label of a key, but also the language
 * of the available label.
 *
 * @class
 * @property {string} zid ID of a ZPersistentObject, ZKey or ZArgumentDeclaration
 * @property {string} label
 * @property {string} lang ZID of the ZNaturalLanguage object that identifies the
 *     language the label is in
 * @property {string} langCode language code corresponding to the lang ZID
 * @property {string} langDir language directionality corresponding to the language
 */
class LabelData {
	constructor( zid, label, lang, langCode = null ) {
		this.zid = zid;
		this.label = label;
		this.lang = lang;
 
		// Set language code and directionality
		if ( langCode ) {
			this.setLangCode( langCode );
		}
	}
 
	/**
	 * Sets the language code and finds its directionality using ULS jquery library
	 *
	 * @param {string} langCode
	 */
	setLangCode( langCode ) {
		this.langCode = langCode;
		this.langDir = ( langCode && $.uls ) ? $.uls.data.getDir( langCode ) : '';
	}
 
	/**
	 * Returns whether the zid has no label (zid and label are equal)
	 *
	 * @return {boolean}
	 */
	get isUntitled() {
		return this.zid === this.label;
	}
 
	/**
	 * Returns the label only if it's set. Else, returns the default string
	 * for untitled objects
	 *
	 * @return {string}
	 */
	get labelOrUntitled() {
		return this.isUntitled ?
			this.untitledString :
			this.label;
	}
 
	/**
	 * Returns 'untitled' for Zids, or 'unlabelled' for others
	 *
	 * @return {string}
	 */
	get untitledString() {
		return typeUtils.isValidZidFormat( this.zid ) ?
			mw.message( 'wikilambda-editor-default-name' ).text() :
			mw.message( 'wikilambda-about-widget-unlabelled-input' ).text();
	}
}
 
module.exports = exports = LabelData;