All files / src/ui/widgets ve.ui.LanguageInputWidget.js

87.35% Statements 76/87
57.14% Branches 24/42
81.81% Functions 9/11
87.35% Lines 76/87

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279                                                1x   1x     1x     1x 1x 1x   1x 1x   1x       1x 1x       1x     1x   1x     1x         1x 1x     1x 1x   1x     1x 1x 1x     1x                   1x   1x 1x             1x 1x   1x       1x 1x           1x                             1x                                 1x 2x 2x                                         1x 2x   1x       1x 1x 1x 1x 1x       1x                 1x 1x   1x 1x 1x 1x               1x 2x               1x 2x                   1x 4x 4x 2x   4x 2x   4x                   1x   1x       1x     1x 1x               1x 4x                   1x 3x 3x 2x   3x 3x    
/*!
 * VisualEditor UserInterface LanguageInputWidget class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Creates an ve.ui.LanguageInputWidget object.
 *
 * @class
 * @extends OO.ui.Widget
 *
 * @constructor
 * @param {Object} [config] Configuration options
 * @cfg {string} [dirInput='auto'] How to display the directionality input. Options are:
 *      - none: Directionality input is hidden.
 *      - no-auto: Directionality input is visible and options are LTR or RTL.
 *      - auto: Directionality input is visible and options include "auto" in
 *            addition to LTR and RTL.
 * @cfg {boolean} [readOnly=false] Prevent changes to the value of the input.
 * @cfg {boolean} [hideCodeInput] Prevent user from entering a language code as free text
 * @cfg {ve.ui.WindowManager} [dialogManager] Window manager to launch the language search dialog in
 * @cfg {string[]} [availableLanguages] Available language codes to show in search dialog
 */
ve.ui.LanguageInputWidget = function VeUiLanguageInputWidget( config ) {
	// Configuration initialization
	config = config || {};
 
	// Parent constructor
	ve.ui.LanguageInputWidget.super.call( this, config );
 
	// Properties
	this.lang = null;
	this.dir = null;
	this.setReadOnly( !!config.readOnly );
 
	this.dialogs = config.dialogManager || new ve.ui.WindowManager( { factory: ve.ui.windowFactory } );
	this.availableLanguages = config.availableLanguages;
 
	this.findLanguageButton = new OO.ui.ButtonWidget( {
		classes: [ 've-ui-languageInputWidget-findLanguageButton' ],
		icon: 'ellipsis'
	} );
	this.findLanguageButton.$button.attr( 'aria-label', ve.msg( 'visualeditor-dialog-language-search-title' ) );
	this.selectedLanguageLabel = new OO.ui.LabelWidget( {
		classes: [ 've-ui-languageInputWidget-selectedLanguageLabel' ],
		label: ve.msg( 'visualeditor-languageinspector-widget-changelang' )
	} );
	this.languageCodeTextInput = new OO.ui.TextInputWidget( {
		classes: [ 've-ui-languageInputWidget-languageCodeTextInput' ]
	} );
	this.languageCodeTextInput.$input.attr( 'aria-label', ve.msg( 'visualeditor-languageinspector-widget-label-langcode' ) );
 
	this.directionSelect = new OO.ui.ButtonSelectWidget( {
		classes: [ 've-ui-languageInputWidget-directionSelect' ]
	} );
	this.directionLabel = new OO.ui.LabelWidget( {
		classes: [ 've-ui-languageInputWidget-directionLabel' ],
		label: ve.msg( 'visualeditor-languageinspector-widget-label-direction' )
	} );
 
	var $language = $( '<div>' ).addClass( 've-ui-languageInputWidget-languageInput' );
	$language.append(
		this.findLanguageButton.$element
	);
	Eif ( !config.hideCodeInput ) {
		$language.prepend( this.languageCodeTextInput.$element );
	}
	this.findLanguageButton.$element.before( this.selectedLanguageLabel.$element );
 
	// Events
	this.findLanguageButton.connect( this, { click: 'onFindLanguageButtonClick' } );
	this.languageCodeTextInput.connect( this, { change: 'onChange' } );
	this.directionSelect.connect( this, { select: 'onChange' } );
 
	// Initialization
	var dirItems = [
		new OO.ui.ButtonOptionWidget( {
			data: 'rtl',
			icon: 'textDirRTL'
		} ),
		new OO.ui.ButtonOptionWidget( {
			data: 'ltr',
			icon: 'textDirLTR'
		} )
	];
	var dirInput = ( config.dirInput === undefined ) ? 'auto' : config.dirInput;
 
	Eif ( dirInput === 'auto' ) {
		dirItems.splice(
			1, 0, new OO.ui.ButtonOptionWidget( {
				data: null,
				label: ve.msg( 'visualeditor-dialog-language-auto-direction' )
			} )
		);
	}
	this.directionSelect.addItems( dirItems );
	$( OO.ui.getTeleportTarget() ).append( this.dialogs.$element );
 
	this.$element
		.addClass( 've-ui-languageInputWidget' )
		.append( $language );
 
	Eif ( dirInput !== 'none' ) {
		this.$element.append( this.directionLabel.$element, this.directionSelect.$element );
	}
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.LanguageInputWidget, OO.ui.Widget );
 
/* Events */
 
/**
 * @event change
 * @param {string} lang Language code
 * @param {string} dir Directionality
 */
 
/* Methods */
 
/**
 * Handle find language button click events.
 */
ve.ui.LanguageInputWidget.prototype.onFindLanguageButtonClick = function () {
	var widget = this;
 
	this.dialogs.openWindow( 'languageSearch', {
		availableLanguages: this.availableLanguages,
		$returnFocusTo: null
	} ).closing.then( function ( data ) {
		data = data || {};
		if ( data.action === 'done' ) {
			widget.setLangAndDir( data.lang, data.dir );
		}
	} );
};
 
/**
 * Handle input widget change events.
 */
ve.ui.LanguageInputWidget.prototype.onChange = function () {
	Eif ( this.updating ) {
		return;
	}
 
	var selectedItem = this.directionSelect.findSelectedItem();
	this.setLangAndDir(
		this.languageCodeTextInput.getValue(),
		selectedItem ? selectedItem.getData() : null
	);
};
 
/**
 * Set language and directionality
 *
 * The inputs value will automatically be updated.
 *
 * @param {string|null} lang Language code
 * @param {string|null} dir Directionality
 * @fires change
 * @return {ve.ui.LanguageInputWidget}
 * @chainable
 */
ve.ui.LanguageInputWidget.prototype.setLangAndDir = function ( lang, dir ) {
	if ( lang === this.lang && dir === this.dir ) {
		// No change
		return this;
	}
 
	// Set state flag while programmatically changing input widget values
	this.updating = true;
	if ( lang || dir ) {
		lang = lang || '';
		this.languageCodeTextInput.setValue( lang );
		this.selectedLanguageLabel.setLabel(
			ve.init.platform.getLanguageName( lang.toLowerCase() ) ||
			ve.msg( 'visualeditor-languageinspector-widget-changelang' )
		);
		this.directionSelect.selectItemByData( dir );
	} else E{
		this.languageCodeTextInput.setValue( '' );
		this.selectedLanguageLabel.setLabel(
			ve.msg( 'visualeditor-languageinspector-widget-changelang' )
		);
		this.directionSelect.selectItem( null );
	}
	// Set title as long language may be truncated
	this.selectedLanguageLabel.setTitle( this.selectedLanguageLabel.$label.text() );
	this.updating = false;
 
	this.lang = lang;
	this.dir = dir;
	this.emit( 'change', lang, dir );
	return this;
};
 
/**
 * Get the language
 *
 * @return {string|null} Language code
 */
ve.ui.LanguageInputWidget.prototype.getLang = function () {
	return this.lang;
};
 
/**
 * Get the directionality
 *
 * @return {string|null} Directionality (ltr/rtl)
 */
ve.ui.LanguageInputWidget.prototype.getDir = function () {
	return this.dir;
};
 
/**
 * Update the disabled state of the controls
 *
 * @chainable
 * @protected
 * @return {OO.ui.NumberInputWidget} The widget, for chaining
 */
ve.ui.LanguageInputWidget.prototype.updateControlsDisabled = function () {
	var disabled = this.isDisabled() || this.isReadOnly();
	if ( this.findLanguageButton ) {
		this.findLanguageButton.setDisabled( disabled );
	}
	if ( this.directionSelect ) {
		this.directionSelect.setDisabled( disabled );
	}
	return this;
};
 
/**
 * Disable or enable the inputs
 *
 * @param {boolean} disabled Set disabled or enabled
 * @return {ve.ui.LanguageInputWidget}
 * @chainable
 */
ve.ui.LanguageInputWidget.prototype.setDisabled = function ( disabled ) {
	// Parent method
	ve.ui.LanguageInputWidget.super.prototype.setDisabled.call( this, disabled );
 
	// The 'setDisabled' method runs in the constructor before the
	// inputs are initialized
	Iif ( this.languageCodeTextInput ) {
		this.languageCodeTextInput.setDisabled( disabled );
	}
	this.updateControlsDisabled();
	return this;
};
 
/**
 * Check if the widget is read-only
 *
 * @return {boolean}
 */
ve.ui.LanguageInputWidget.prototype.isReadOnly = function () {
	return this.readOnly;
};
 
/**
 * Set the read-only state of the widget
 *
 * @param {boolean} readOnly Make widget read-only
 * @return {ve.ui.LanguageInputWidget}
 * @chainable
 */
ve.ui.LanguageInputWidget.prototype.setReadOnly = function ( readOnly ) {
	this.readOnly = readOnly;
	if ( this.languageCodeTextInput ) {
		this.languageCodeTextInput.setReadOnly( readOnly );
	}
	this.updateControlsDisabled();
	return this;
};