aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/ui/text-slider.reel/text-slider.js
blob: e5d31d00db91bda28b0049b2690d053998401fa6 (plain)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* <copyright>
Copyright (c) 2012, Motorola Mobility LLC.
All Rights Reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of Motorola Mobility LLC nor the names of its
  contributors may be used to endorse or promote products derived from this
  software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
</copyright> */
/*global require,exports */

/**
    @module "montage/ui/text-slider.reel"
    @requires montage/core/core
    @requires montage/ui/component
    @requires montage/ui/composer/press-composer
 */
var Montage = require("montage").Montage,
    Component = require("ui/component").Component,
    PressComposer = require("ui/composer/press-composer").PressComposer;

/**
    <p>Provides a way for users to quickly and easily manipulate numeric values.
    It takes the form of a numeric value with a dotted underline, optionally
    followed by a unit.

    <p>When the user clicks and drags on the numeric value
    it increases when dragged up or right, and decreases when dragged down or
    left. If the user holds Control or Shift while dragging the value will
    change by a smaller or larger amount respectively.</p>

    <p>If the user clicks without dragging then the component enters "edit mode"
    and turns into a textfield where the user can directly edit the value. If
    user presses the Up or Down arrows in edit mode the value will increase or
    decrease respectively. If the user holds Control or Shift while pressing an
    arrow the value will change by a smaller or larger amount respectively.</p>

    @class module:"montage/ui/text-slider.reel".TextSlider
    @extends module:montage/ui/component.Component
 */
var TextSlider = exports.TextSlider = Montage.create(Component, /** @lends module:"montage/ui/text-slider.reel".TextSlider# */ {

    // Properties

    _converter: {
        enumerable: false,
        value: null
    },
    /**
    A converter that converts from a numeric value to the display value, for
    example to convert to hexadecimal. You may also want to use a converter
    that returns <code>value.toFixed(<i>n</i>)</code> to prevent precision errors from
    being displayed to the user.
    @type {object}
    @default null
    */
    converter: {
        serializable: true,
        get: function() {
            return this._converter;
        },
        set: function(value) {
            if (this._converter !== value) {
                this._converter = value;
                this.needsDraw = true;
            }
        }
    },

    _value: {
        enumerable: false,
        value: 0
    },
    /**
    The value of the TextSlider.
    @type {Number}
    @default 0
    */
    value: {
        serializable: true,
        get: function() {
            return this._value;
        },
        set: function(value) {
            if (isNaN(value = parseFloat(value))) {
                return false;
            }

            // != null also checking for undefined
            if (this._minValue != null && value < this._minValue) {
                value = this._minValue;
            } else if (this._maxValue != null && value > this._maxValue) {
                value = this._maxValue;
            }

            if (this._value !== value) {
                this._value = value;
                this.needsDraw = true;
            }
        }
    },

    /**
    The value of the TextSlider converted using {@link converter} for display.
    Setting this will call <code>revert</code> on the converter and set
    {@link value}.
    @type {String}
    @default "0"
    */
    convertedValue: {
        dependencies: ["value", "converter"],
        get: function() {
            // TODO catch errors from conversion?
            return (this._converter) ? this._converter.convert(this._value) : this._value;
        },
        set: function(value) {
            if (this._converter) {
                this.value = this._converter.revert(value);
            } else {
                this.value = value;
            }
        }
    },

    _minValue: {
        enumerable: false,
        value: null
    },
    /**
    The minimum value the TextSlider can take. If set to <code>null</code> there
    is no minimum.
    @type {number|null}
    @default null
    */
    minValue: {
        serializable: true,
        get: function() {
            return this._minValue;
        },
        set: function(value) {
            if (this._minValue !== value) {
                this._minValue = value;

                this.value = this._value;

                this.needsDraw = true;
            }
        }
    },
    _maxValue: {
        enumerable: false,
        value: null
    },
    /**
    The maximum value the TextSlider can take. If set to <code>null</code> there
    is no maximum.
    @type {number|null}
    @default null
    */
    maxValue: {
        serializable: true,
        get: function() {
            return this._maxValue;
        },
        set: function(value) {
            if (this._maxValue !== value) {
                this._maxValue = value;

                this.value = this._value;

                this.needsDraw = true;
            }
        }
    },

    /**
    The small amount to increase/decrease the value by. Used when the user
    holds the Control key and drags or presses the Up arrow in input mode.
    @type {Number}
    @default 0.1
    */
    smallStepSize: {
        serializable: true,
        enumerable: false,
        value: 0.1
    },
    /**
    The amount to increase/decrease the value by. Used per pixel when the user
    drags the TextSlider or presses the Up arrow in input mode.
    @type {Number}
    @default 1
    */
    stepSize: {
        serializable: true,
        enumerable: false,
        value: 1
    },
    /**
    The large amount to increase/decrease the value by. Used when the user
    holds the Shift key and drags or presses the Up arrow in input mode.
    @type {Number}
    @default 10
    */
    largeStepSize: {
        serializable: true,
        enumerable: false,
        value: 10
    },

    _unit: {
        enumerable: false,
        value: null
    },
    /**
    The unit the value is in. This will be appended to the {@link convertedValue}
    for display.
    @type {String|null}
    @default null
    */
    unit: {
        serializable: true,
        get: function() {
            return this._unit;
        },
        set: function(value) {
            if (this._unit !== value) {
                this._unit = value;
                this.needsDraw = true;
            }
        }
    },

    _units: {
        enumerable: false,
        value: []
    },
    units: {
        serializable: true,
        get: function() {
            return this._units;
        },
        set: function(value) {
            if (this._units !== value) {
                this._units = value;
                this.needsDraw = true;
            }
        }
    },

    _isEditing: {
        enumerable: false,
        value: null
    },
    /**
        Whether the TextSlider is currently being edited as a textbox
        @type {Boolean}
        @default false
    */
    isEditing: {
        get: function() {
            return this._isEditing;
        },
        set: function(value) {
            if (this._isEditing !== value) {
                this._isEditing = value;
                this.needsDraw = true;
            }
        }
    },

    // private

    _inputElement: {
        serializable: true,
        enumerable: false,
        value: null
    },

    _pressComposer: {
        serializable: true,
        enumerable: false,
        value: null
    },
    _translateComposer: {
        serializable: true,
        enumerable: false,
        value: null
    },

    _startX: {
        enumerable: false,
        value: null
    },
    _startY: {
        enumerable: false,
        value: null
    },
    _direction: {
        enumerable: false,
        value: null
    },

    didCreate: {
        value: function() {
            this.handlePress = this.handleFocus;
            this.handleClick = this.handleFocus;
        }
    },

    // draw

    prepareForActivationEvents: {
        value: function() {
            this._element.addEventListener("click", this, false);
        }
    },

    prepareForDraw: {
        value: function() {
            this._element.addEventListener("focus", this, false);
            this._inputElement.addEventListener("blur", this, false);
            this._inputElement.addEventListener("keydown", this, false);
        }
    },

    draw: {
        value: function() {
            if (this._isEditing) {
                this._element.classList.add("montage-text-slider-editing");
                this._inputElement.value = this.convertedValue + ((this._unit) ? " " + this._unit : "");
                // Replace this with just focus when merged
                this._inputElement.focus();
                // When _element gets focus we focus the input. Because of this
                // shift+tab stops working, so prevent _element getting
                // focus while editing
                this._element.tabIndex = -1;
            } else {
                this._element.classList.remove("montage-text-slider-editing");
                this._inputElement.blur();
                this._element.tabIndex = 0;
            }

            if (this._direction === "horizontal") {
                document.body.style.cursor = "ew-resize";
            } else if (this._direction === "vertical") {
                document.body.style.cursor = "ns-resize";
            } else {
                document.body.style.cursor = "auto";
            }

        }
    },

    // handlers

    surrenderPointer: {
        value: function(pointer, component) {
            // Don't allow the value to be dragged when we're being edited.
            return !this._isEditing;
        }
    },

    // handlePress and handleClick are set to equal handleFocus in didCreate
    // handlePress: edit on touch
    // handleClick: edit when parent <label> element is clicked/touched
    // handleFocus: edit when tabbed to
    handleFocus: {
        value: function(event) {
            if (!this._isEditing) {
                this.isEditing = true;
            }
        }
    },
    // stop editing when blurred
    handleBlur: {
        value: function(event) {
            if (this._isEditing) {
                this.convertedValue = this._inputElement.value;
                this.isEditing = false;
            }
        }
    },

    handleKeydown: {
        value: function(event) {
            if (event.target === this._inputElement) {
                var value;
                if (event.keyCode === 38) {
                    // up
                    this.convertedValue = this._inputElement.value;
                    value = Math.round(((event.shiftKey) ? this.largeStepSize : (event.ctrlKey) ? this.smallStepSize : this.stepSize) / this.smallStepSize) * this.smallStepSize;
                    this.value += value;
                    this.needsDraw = true;
                } else if (event.keyCode === 40) {
                    // down
                    this.convertedValue = this._inputElement.value;
                    value = Math.round(((event.shiftKey) ? this.largeStepSize : (event.ctrlKey) ? this.smallStepSize : this.stepSize) / this.smallStepSize) * this.smallStepSize;
                    this.value -= value;
                    this.needsDraw = true;
                } else if (event.keyCode === 13) {
                    // enter
                    this.convertedValue = this._inputElement.value;
                    this.isEditing = false;
                } else if (event.keyCode === 27) {
                    // esc
                    this.isEditing = false;
                }
            } else {
                if (event.shiftKey || event.keyCode === 16) {
                    this._translateComposer.pointerSpeedMultiplier = this.largeStepSize / this.stepSize;
                } else if (event.ctrlKey || event.keyCode === 17) {
                    this._translateComposer.pointerSpeedMultiplier = this.smallStepSize / this.stepSize;
                }
            }
        }
    },
    handleKeyup: {
        value: function(event) {
            if (event.shiftKey || event.keyCode === 16) {
                this._translateComposer.pointerSpeedMultiplier = this.stepSize;
            } else if (event.ctrlKey || event.keyCode === 17) {
                this._translateComposer.pointerSpeedMultiplier = this.stepSize;
            }
        }
    },

    handleTranslateStart: {
        value: function(event) {
            this._direction = null;

            this._startX = this._value;
            this._startY = this._value;

            // reset translate composer ready for more translation
            this._translateComposer.translateX = this._value;
            this._translateComposer.translateY = this._value;

            document.addEventListener("keydown", this, false);
            document.addEventListener("keyup", this, false);
        }
    },
    handleTranslate: {
        value: function(event) {
            if (this._direction === "vertical") {
                this.value = event.translateY;
            } else if (this._direction === "horizontal") {
                this.value = event.translateX;
            } else {
                var value,
                    deltaX = Math.abs(event.translateX - this._startX),
                    deltaY = Math.abs(event.translateY - this._startY);

                if (deltaY > deltaX) {
                    value = event.translateY;
                    if (deltaY > 20) {
                        this._direction = "vertical";
                    }
                } else {
                    value = event.translateX;
                    if (deltaX > 20) {
                        this._direction = "horizontal";
                    }
                }
                this.value = value;
            }
        }
    },
    handleTranslateEnd: {
        value: function(event) {
            this._direction = null;
            this.needsDraw = true;
            document.removeEventListener("keydown", this, false);
            document.removeEventListener("keyup", this, false);
        }
    }

});