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

26.58% Statements 21/79
0% Branches 0/22
0% Functions 0/20
26.58% Lines 21/79

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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313                                      1x                                                                                           1x                                           1x                   1x                     1x                     1x                   1x                   1x                     1x                           1x                       1x                           1x                                 1x                           1x                                       1x                     1x                       1x                 1x                     1x                       1x                     1x          
/*!
 * VisualEditor UserInterface DimensionsWidget class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Widget that visually displays width and height inputs.
 * This widget is for presentation-only, no calculation is done.
 *
 * @class
 * @extends OO.ui.Widget
 *
 * @constructor
 * @param {Object} [config] Configuration options
 * @cfg {Object} [defaults] Default dimensions
 * @cfg {Object} [validate] Validation pattern passed to TextInputWidgets
 * @cfg {boolean} [readOnly=false] Prevent changes to the value of the widget.
 */
ve.ui.DimensionsWidget = function VeUiDimensionsWidget( config ) {
	// Configuration
	config = config || {};
 
	// Parent constructor
	ve.ui.DimensionsWidget.super.call( this, config );
 
	this.widthInput = new OO.ui.TextInputWidget( {
		validate: config.validate || $.isNumeric
	} );
	this.widthInput.$input.attr( 'aria-label', ve.msg( 'visualeditor-dimensionswidget-width' ) );
	this.heightInput = new OO.ui.TextInputWidget( {
		validate: config.validate || $.isNumeric
	} );
	this.heightInput.$input.attr( 'aria-label', ve.msg( 'visualeditor-dimensionswidget-height' ) );
 
	this.defaults = config.defaults || { width: '', height: '' };
	this.setReadOnly( !!config.readOnly );
	this.renderDefaults();
 
	var labelTimes = new OO.ui.LabelWidget( {
		label: ve.msg( 'visualeditor-dimensionswidget-times' )
	} );
	var labelPx = new OO.ui.LabelWidget( {
		label: ve.msg( 'visualeditor-dimensionswidget-px' )
	} );
 
	// Events
	this.widthInput.connect( this, { change: 'onWidthChange' } );
	this.heightInput.connect( this, { change: 'onHeightChange' } );
 
	// Setup
	this.$element
		.addClass( 've-ui-dimensionsWidget' )
		.append(
			this.widthInput.$element,
			labelTimes.$element
				.addClass( 've-ui-dimensionsWidget-label-times' ),
			this.heightInput.$element,
			labelPx.$element
				.addClass( 've-ui-dimensionsWidget-label-px' )
		);
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.DimensionsWidget, OO.ui.Widget );
 
/* Events */
 
/**
 * @event widthChange
 * @param {string} value The new width
 */
 
/**
 * @event heightChange
 * @param {string} value The new width
 */
 
/* Methods */
 
/**
 * Respond to width change, propagate the input change event
 *
 * @param {string} value The new changed value
 * @fires widthChange
 */
ve.ui.DimensionsWidget.prototype.onWidthChange = function ( value ) {
	this.emit( 'widthChange', value );
};
 
/**
 * Respond to height change, propagate the input change event
 *
 * @param {string} value The new changed value
 * @fires heightChange
 */
ve.ui.DimensionsWidget.prototype.onHeightChange = function ( value ) {
	this.emit( 'heightChange', value );
};
 
/**
 * Set default dimensions
 *
 * @param {Object} dimensions Default dimensions, width and height
 * @return {ve.ui.DimensionsWidget}
 * @chainable
 */
ve.ui.DimensionsWidget.prototype.setDefaults = function ( dimensions ) {
	if ( dimensions.width && dimensions.height ) {
		this.defaults = ve.copy( dimensions );
		this.renderDefaults();
	}
	return this;
};
 
/**
 * Render the default dimensions as input placeholders
 */
ve.ui.DimensionsWidget.prototype.renderDefaults = function () {
	this.widthInput.$input.prop( 'placeholder', this.getDefaults().width );
	this.heightInput.$input.prop( 'placeholder', this.getDefaults().height );
};
 
/**
 * Get the default dimensions
 *
 * @return {Object} Default dimensions
 */
ve.ui.DimensionsWidget.prototype.getDefaults = function () {
	return this.defaults;
};
 
/**
 * Remove the default dimensions
 *
 * @return {ve.ui.DimensionsWidget}
 * @chainable
 */
ve.ui.DimensionsWidget.prototype.removeDefaults = function () {
	this.defaults = { width: '', height: '' };
	this.renderDefaults();
	return this;
};
 
/**
 * Check whether the widget is empty.
 *
 * @return {boolean} Both values are empty
 */
ve.ui.DimensionsWidget.prototype.isEmpty = function () {
	return (
		this.widthInput.getValue() === '' &&
		this.heightInput.getValue() === ''
	);
};
 
/**
 * Set an empty value for the dimensions inputs so they show
 * the placeholders if those exist.
 *
 * @return {ve.ui.DimensionsWidget}
 * @chainable
 */
ve.ui.DimensionsWidget.prototype.clear = function () {
	this.widthInput.setValue( '' );
	this.heightInput.setValue( '' );
	return this;
};
 
/**
 * Reset the dimensions to the default dimensions.
 *
 * @return {ve.ui.DimensionsWidget}
 * @chainable
 */
ve.ui.DimensionsWidget.prototype.reset = function () {
	this.setDimensions( this.getDefaults() );
	return this;
};
 
/**
 * Set the dimensions value of the inputs
 *
 * @param {Object} dimensions The width and height values of the inputs
 * @param {number} dimensions.width The value of the width input
 * @param {number} dimensions.height The value of the height input
 * @return {ve.ui.DimensionsWidget}
 * @chainable
 */
ve.ui.DimensionsWidget.prototype.setDimensions = function ( dimensions ) {
	if ( dimensions.width ) {
		this.setWidth( dimensions.width );
	}
	if ( dimensions.height ) {
		this.setHeight( dimensions.height );
	}
	return this;
};
 
/**
 * Return the current dimension values in the widget
 *
 * @return {Object} dimensions The width and height values of the inputs
 * @return {number} dimensions.width The value of the width input
 * @return {number} dimensions.height The value of the height input
 */
ve.ui.DimensionsWidget.prototype.getDimensions = function () {
	return {
		width: +this.widthInput.getValue(),
		height: +this.heightInput.getValue()
	};
};
 
/**
 * Disable or enable the inputs
 *
 * @param {boolean} disabled Set disabled or enabled
 * @return {ve.ui.DimensionsWidget}
 * @chainable
 */
ve.ui.DimensionsWidget.prototype.setDisabled = function ( disabled ) {
	// Parent method
	ve.ui.DimensionsWidget.super.prototype.setDisabled.call( this, disabled );
 
	// The 'setDisabled' method runs in the constructor before the
	// inputs are initialized
	if ( this.widthInput ) {
		this.widthInput.setDisabled( disabled );
	}
	if ( this.heightInput ) {
		this.heightInput.setDisabled( disabled );
	}
	return this;
};
 
/**
 * Check if the widget is read-only
 *
 * @return {boolean}
 */
ve.ui.DimensionsWidget.prototype.isReadOnly = function () {
	return this.readOnly;
};
 
/**
 * Set the read-only state of the widget
 *
 * @param {boolean} readOnly Make widget read-only
 * @return {ve.ui.DimensionsWidget}
 * @chainable
 */
ve.ui.DimensionsWidget.prototype.setReadOnly = function ( readOnly ) {
	this.readOnly = readOnly;
	this.widthInput.setReadOnly( readOnly );
	this.heightInput.setReadOnly( readOnly );
	return this;
};
 
/**
 * Get the current value in the width input
 *
 * @return {string} Input value
 */
ve.ui.DimensionsWidget.prototype.getWidth = function () {
	return this.widthInput.getValue();
};
 
/**
 * Get the current value in the height input
 *
 * @return {string} Input value
 */
ve.ui.DimensionsWidget.prototype.getHeight = function () {
	return this.heightInput.getValue();
};
 
/**
 * Set a value for the width input
 *
 * @param {string} value
 * @return {ve.ui.DimensionsWidget}
 * @chainable
 */
ve.ui.DimensionsWidget.prototype.setWidth = function ( value ) {
	this.widthInput.setValue( value );
	return this;
};
 
/**
 * Set a value for the height input
 *
 * @param {string} value
 * @return {ve.ui.DimensionsWidget}
 * @chainable
 */
ve.ui.DimensionsWidget.prototype.setHeight = function ( value ) {
	this.heightInput.setValue( value );
	return this;
};
 
/**
 * Sets the 'invalid' flag appropriately on both text inputs.
 *
 * @return {ve.ui.DimensionsWidget}
 * @chainable
 */
ve.ui.DimensionsWidget.prototype.setValidityFlag = function () {
	this.widthInput.setValidityFlag();
	this.heightInput.setValidityFlag();
	return this;
};