Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/widgets/ve.ui.DimensionsWidget.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface DimensionsWidget class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Widget that visually displays width and height inputs.
9 * This widget is for presentation-only, no calculation is done.
10 *
11 * @class
12 * @extends OO.ui.Widget
13 *
14 * @constructor
15 * @param {Object} [config] Configuration options
16 * @param {Object} [config.defaults] Default dimensions
17 * @param {Object} [config.validate] Validation pattern passed to TextInputWidgets
18 * @param {boolean} [config.readOnly=false] Prevent changes to the value of the widget.
19 */
20 ve.ui.DimensionsWidget = function VeUiDimensionsWidget( config ) {
21 // Configuration
22 config = config || {};
23
24 // Parent constructor
25 ve.ui.DimensionsWidget.super.call( this, config );
26
27 this.widthInput = new OO.ui.TextInputWidget( {
28 validate: config.validate || $.isNumeric
29 } );
30 this.widthInput.$input.attr( 'aria-label', ve.msg( 'visualeditor-dimensionswidget-width' ) );
31 this.heightInput = new OO.ui.TextInputWidget( {
32 validate: config.validate || $.isNumeric
33 } );
34 this.heightInput.$input.attr( 'aria-label', ve.msg( 'visualeditor-dimensionswidget-height' ) );
35
36 this.defaults = config.defaults || { width: '', height: '' };
37 this.setReadOnly( !!config.readOnly );
38 this.renderDefaults();
39
40 const labelTimes = new OO.ui.LabelWidget( {
41 label: ve.msg( 'visualeditor-dimensionswidget-times' )
42 } );
43 const labelPx = new OO.ui.LabelWidget( {
44 label: ve.msg( 'visualeditor-dimensionswidget-px' )
45 } );
46
47 // Events
48 this.widthInput.connect( this, { change: 'onWidthChange' } );
49 this.heightInput.connect( this, { change: 'onHeightChange' } );
50
51 // Setup
52 this.$element
53 .addClass( 've-ui-dimensionsWidget' )
54 .append(
55 this.widthInput.$element,
56 labelTimes.$element
57 .addClass( 've-ui-dimensionsWidget-label-times' ),
58 this.heightInput.$element,
59 labelPx.$element
60 .addClass( 've-ui-dimensionsWidget-label-px' )
61 );
62 };
63
64 /* Inheritance */
65
66 OO.inheritClass( ve.ui.DimensionsWidget, OO.ui.Widget );
67
68 /* Events */
69
70 /**
71 * @event ve.ui.DimensionsWidget#widthChange
72 * @param {string} value The new width
73 */
74
75 /**
76 * @event ve.ui.DimensionsWidget#heightChange
77 * @param {string} value The new width
78 */
79
80 /* Methods */
81
82 /**
83 * Respond to width change, propagate the input change event
84 *
85 * @param {string} value The new changed value
86 * @fires ve.ui.DimensionsWidget#widthChange
87 */
88 ve.ui.DimensionsWidget.prototype.onWidthChange = function ( value ) {
89 this.emit( 'widthChange', value );
90 };
91
92 /**
93 * Respond to height change, propagate the input change event
94 *
95 * @param {string} value The new changed value
96 * @fires ve.ui.DimensionsWidget#heightChange
97 */
98 ve.ui.DimensionsWidget.prototype.onHeightChange = function ( value ) {
99 this.emit( 'heightChange', value );
100 };
101
102 /**
103 * Set default dimensions
104 *
105 * @param {Object} dimensions Default dimensions, width and height
106 * @return {ve.ui.DimensionsWidget}
107 * @chainable
108 */
109 ve.ui.DimensionsWidget.prototype.setDefaults = function ( dimensions ) {
110 if ( dimensions.width && dimensions.height ) {
111 this.defaults = ve.copy( dimensions );
112 this.renderDefaults();
113 }
114 return this;
115 };
116
117 /**
118 * Render the default dimensions as input placeholders
119 */
120 ve.ui.DimensionsWidget.prototype.renderDefaults = function () {
121 this.widthInput.$input.prop( 'placeholder', this.getDefaults().width );
122 this.heightInput.$input.prop( 'placeholder', this.getDefaults().height );
123 };
124
125 /**
126 * Get the default dimensions
127 *
128 * @return {Object} Default dimensions
129 */
130 ve.ui.DimensionsWidget.prototype.getDefaults = function () {
131 return this.defaults;
132 };
133
134 /**
135 * Remove the default dimensions
136 *
137 * @return {ve.ui.DimensionsWidget}
138 * @chainable
139 */
140 ve.ui.DimensionsWidget.prototype.removeDefaults = function () {
141 this.defaults = { width: '', height: '' };
142 this.renderDefaults();
143 return this;
144 };
145
146 /**
147 * Check whether the widget is empty.
148 *
149 * @return {boolean} Both values are empty
150 */
151 ve.ui.DimensionsWidget.prototype.isEmpty = function () {
152 return (
153 this.widthInput.getValue() === '' &&
154 this.heightInput.getValue() === ''
155 );
156 };
157
158 /**
159 * Set an empty value for the dimensions inputs so they show
160 * the placeholders if those exist.
161 *
162 * @return {ve.ui.DimensionsWidget}
163 * @chainable
164 */
165 ve.ui.DimensionsWidget.prototype.clear = function () {
166 this.widthInput.setValue( '' );
167 this.heightInput.setValue( '' );
168 return this;
169 };
170
171 /**
172 * Reset the dimensions to the default dimensions.
173 *
174 * @return {ve.ui.DimensionsWidget}
175 * @chainable
176 */
177 ve.ui.DimensionsWidget.prototype.reset = function () {
178 this.setDimensions( this.getDefaults() );
179 return this;
180 };
181
182 /**
183 * Set the dimensions value of the inputs
184 *
185 * @param {Object} dimensions The width and height values of the inputs
186 * @param {number} dimensions.width The value of the width input
187 * @param {number} dimensions.height The value of the height input
188 * @return {ve.ui.DimensionsWidget}
189 * @chainable
190 */
191 ve.ui.DimensionsWidget.prototype.setDimensions = function ( dimensions ) {
192 if ( dimensions.width ) {
193 this.setWidth( dimensions.width );
194 }
195 if ( dimensions.height ) {
196 this.setHeight( dimensions.height );
197 }
198 return this;
199 };
200
201 /**
202 * @typedef {Object} Dimensions
203 * @memberof ve.ui.DimensionsWidget
204 * @property {number} width The value of the width input
205 * @property {number} height The value of the height input
206 */
207
208 /**
209 * Return the current dimension values in the widget
210 *
211 * @return {ve.ui.DimensionsWidget.Dimensions} dimensions The width and height values of the inputs
212 */
213 ve.ui.DimensionsWidget.prototype.getDimensions = function () {
214 return {
215 width: +this.widthInput.getValue(),
216 height: +this.heightInput.getValue()
217 };
218 };
219
220 /**
221 * Disable or enable the inputs
222 *
223 * @param {boolean} disabled Set disabled or enabled
224 * @return {ve.ui.DimensionsWidget}
225 * @chainable
226 */
227 ve.ui.DimensionsWidget.prototype.setDisabled = function ( disabled ) {
228 // Parent method
229 ve.ui.DimensionsWidget.super.prototype.setDisabled.call( this, disabled );
230
231 // The 'setDisabled' method runs in the constructor before the
232 // inputs are initialized
233 if ( this.widthInput ) {
234 this.widthInput.setDisabled( disabled );
235 }
236 if ( this.heightInput ) {
237 this.heightInput.setDisabled( disabled );
238 }
239 return this;
240 };
241
242 /**
243 * Check if the widget is read-only
244 *
245 * @return {boolean}
246 */
247 ve.ui.DimensionsWidget.prototype.isReadOnly = function () {
248 return this.readOnly;
249 };
250
251 /**
252 * Set the read-only state of the widget
253 *
254 * @param {boolean} readOnly Make widget read-only
255 * @return {ve.ui.DimensionsWidget}
256 * @chainable
257 */
258 ve.ui.DimensionsWidget.prototype.setReadOnly = function ( readOnly ) {
259 this.readOnly = readOnly;
260 this.widthInput.setReadOnly( readOnly );
261 this.heightInput.setReadOnly( readOnly );
262 return this;
263 };
264
265 /**
266 * Get the current value in the width input
267 *
268 * @return {string} Input value
269 */
270 ve.ui.DimensionsWidget.prototype.getWidth = function () {
271 return this.widthInput.getValue();
272 };
273
274 /**
275 * Get the current value in the height input
276 *
277 * @return {string} Input value
278 */
279 ve.ui.DimensionsWidget.prototype.getHeight = function () {
280 return this.heightInput.getValue();
281 };
282
283 /**
284 * Set a value for the width input
285 *
286 * @param {string} value
287 * @return {ve.ui.DimensionsWidget}
288 * @chainable
289 */
290 ve.ui.DimensionsWidget.prototype.setWidth = function ( value ) {
291 this.widthInput.setValue( value );
292 return this;
293 };
294
295 /**
296 * Set a value for the height input
297 *
298 * @param {string} value
299 * @return {ve.ui.DimensionsWidget}
300 * @chainable
301 */
302 ve.ui.DimensionsWidget.prototype.setHeight = function ( value ) {
303 this.heightInput.setValue( value );
304 return this;
305 };
306
307 /**
308 * Sets the 'invalid' flag appropriately on both text inputs.
309 *
310 * @return {ve.ui.DimensionsWidget}
311 * @chainable
312 */
313 ve.ui.DimensionsWidget.prototype.setValidityFlag = function () {
314 this.widthInput.setValidityFlag();
315 this.heightInput.setValidityFlag();
316 return this;
317 };