aboutsummaryrefslogtreecommitdiff
path: root/js/components/hottext.reel/hottext.js
blob: 93e6e7459e0f6a83315c118a3211a608bd7c3d0a (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
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
</copyright> */

var Montage = require("montage/core/core").Montage;
var SliderBase = require("js/components/sliderbase").SliderBase;

var HotText = exports.HotText = Montage.create(SliderBase, {
    /* Allow users to specify a function to format the display.
    * For example, the Color Picker can specify a function to map
    * the numeric hot text value to hex color values.
     */
    labelFunction: {
        serializable: true,
        enumerable: true,
        value: null
    },

    inputFunction: {
        serializable: true,
        enumerable: true,
        value: parseFloat
    },

    _numValue: {
        enumerable: false,
        value: 0
    },

    numValue: {
        serializable: false,
        enumerable: true,
        get: function() {
            return this._numValue;
        },
        set: function(value) {
            if (value < this._minValue) {
                value = this._minValue;
            }
            if (value > this._maxValue) {
                value = this._maxValue;
            }
            if (value !== this._numValue) {
                this._numValue = Math.round(value * this._decimalPlace)/this._decimalPlace;
            }
        }
    },

    _previousValue: {
        enumerable: false,
        value: null
    },

    _stepSize: {
        enumerable: false,
        value: 1
    },

    stepSize: {
        serializable: true,
        enumerable: true,
        get: function() {
            return this._stepSize;
        },
        set: function(value) {
            if (value !== this._stepSize) {
                this._stepSize = value;
                this.needsDraw = true;
            }
        }
    },

    _stepSizeShift: {
        enumerable: false,
        value: 10
    },

    _xStart: {
        enumerable: false,
        value: 0
    },

    _yStart: {
        enumerable: false,
        value: 0
    },

    // Needed to determine when to commit a value change
    _wasShiftKeyPressed: {
        enumerable: false,
        value: false
    },

    // for ones, use 1
    // for tenths, use 10
    // for hundredths, use 100, etc.
    _decimalPlace: {
        enumerable: false,
        value: 1
    },

    decimalPlace: {
        serializable: true,
        enumerable: true,
        get: function() {
            return this._decimalPlace;
        },
        set: function(value) {
            if (value !== this._decimalPlace) {
                this._decimalPlace = value;
                this.needsDraw = true;
            }
        }
    },

    // TODO - Need to set max value to 2000 for demo.
    _maxValue: {
        enumerable: false,
        value: 2000
    },

    // Flag used to dispatch a single change event if either or both of value and units are changed
    _unitsModified: {
        enumerable: false,
        value: false
    },

    value: {
        serializable: true,
        enumerable: true,
        get: function() {
            return this._value;
        },
        set: function(value) {
            if (isNaN(value)) {
                this._valueSyncedWithInputField = false;
                this.needsDraw = true;
                return;
            }
            if (value < this._minValue) {
                value = this._minValue;
                this._valueSyncedWithInputField = false;
                this.needsDraw = true;
            }
            else if (value > this._maxValue) {
                value = this._maxValue;
                this._valueSyncedWithInputField = false;
                this.needsDraw = true;
            }

            if (value !== this._value) {
                this._value = this._numValue = Math.round(value * this._decimalPlace)/this._decimalPlace;
                this._valueSyncedWithInputField = false;
                this.needsDraw = true;
                this._dispatchActionEvent();
            } else if(this._unitsModified) {
                // Need to dispatch change event if units changed
                this._dispatchActionEvent();
            }
        }
    },

    _valueSyncedWithInputField: {
        enumerable: false,
        value: false
    },

    // We don't want to handle every input; we only want to handle input from tab or enter
    // Thus, we don't listen for an input event; we call this from handleKeydown
    handleInput: {
        enumerable: false,
        value: function() {
            this._setEventFlags("change", false);
            this.value = this.inputFunction(this.element.value);
        }
    },


    _valueFromPageOffset: {
        value: function(offset, pageY, isShiftKeyPressed, wasSetByCode) {
            if(!this._isMouseDown)
            {
                this._handleMoveEnd();  // If the user has moused up, check if we should go into input mode
                return;
            }

            var clickPoint = webkitConvertPointFromPageToNode(this.element, new WebKitPoint(offset,pageY));

            var dX = clickPoint.x - this._xStart;
            var dY = clickPoint.y - this._yStart;
            
            var dXAbs = Math.abs(dX);
            var dYAbs = Math.abs(dY);

            if( (dXAbs < 5) && (dYAbs < 5) )
            {
                return; // Don't process unless the user moves at least 5 pixels
            }

            var incrementVal = dXAbs-4;     // otherwise, the first value change will be 5 pixels
            var multFactor = 1;            

            if(dXAbs > dYAbs)
            {
                if(dX < 0)
                {
                    multFactor = -1;
                }
            }
            else
            {
                if(dY > 0)
                {
                    multFactor = -1;
                }
                incrementVal = dYAbs-4;
            }

            if(isShiftKeyPressed)
            {
                if(!this._wasShiftKeyPressed)
                {
                    this._xStart = clickPoint.x;
                    this._yStart = clickPoint.y;
                    this._previousValue = this._numValue;
                    incrementVal = 1;
                }
                this.numValue = this._previousValue + multFactor * incrementVal * this._stepSizeShift;
                this._wasShiftKeyPressed = true;
            }
            else
            {
                if(this._wasShiftKeyPressed)
                {
                    this._xStart = clickPoint.x;
                    this._yStart = clickPoint.y;
                    this._previousValue = this._numValue;
                    incrementVal = 1;
                    this._wasShiftKeyPressed = false;
                }
                this.numValue = this._previousValue + multFactor * incrementVal * this._stepSize;
            }

            this.value = this._numValue;
        }
    },

    handleKeydown: {
        enumerable: false,
        value: function(event) {
            switch(event.keyCode)
            {
                case 9: //tab
                case 13: // enter
                    this.handleInput();
                    break;
                case 27: // esc
                    this._valueSyncedWithInputField = false;
                    this.needsDraw = true;
                    break;
                case 38: // up
                    this._setEventFlags("change", false);
                    this.value += this._stepSize;
                    break;
                case 40: // down
                    this._setEventFlags("change", false);
                    this.value -= this._stepSize;
                    break;
                default:
//                    return;
            }
        }
    },

    handleBlur: {
        enumerable: false,
        value: function(event) {
            event.target = this;
            this._hasFocus = false;

            this.handleInput(); // Check if value has changed when focusing out
            this.needsDraw = true;

            this.dispatchEvent(event);
        }
    },

    handleFocus: {
        enumerable: false,
        value: function(event) {
            event.target = this;
            this._hasFocus = true;
            this.dispatchEvent(event);
        }
    },

    _handleMoveEnd: {
        value: function() {
            // If we don't change value (mouse up on ourself), we should go into text edit mode
            if(this._numValue === this._previousValue)
            {
                this._hasFocus = true;
            }
            else
            {
                this._hasFocus = false;
                this._dispatchActionEvent();
            }
            this.needsDraw = true;
        }
    },

    draw: {
        enumerable: false,
        value: function() {
            if(this._hasFocus)
            {
                if(!this._isMouseDown)
                {
                    this.element.classList.remove("hottext");
                    this.element.classList.add("hottextInput");

                    // if element targeted; balancing demands of multitouch
                    // with traditional single focus model
                    this.element.addEventListener("keydown", this, false);
                }
            }
            else
            {
                this.element.classList.remove("hottextInput");
                this.element.classList.add("hottext");
            }

            if (!this._valueSyncedWithInputField)
            {
                if(this.labelFunction)
                {
                    this.element.value = this.labelFunction(this._value);
                }
                else
                {
                    this.element.value = this._value;
                }
            }
        }
    },

    didDraw: {
        enumerable: false,
        value: function() {
            if(!this._isMouseDown && this._hasFocus)
            {
                var length = 0;
                if(this.labelFunction)
                {
                    length = this.labelFunction(this._value).length;
                }
                else
                {
                    length = this._value.toString().length;
                }
                this.element.setSelectionRange(0, length);
            }
            this._valueSyncedWithInputField = true;
        }
    },

    prepareForDraw: {
        value: function() {
            if(this._value)
            {
                this._numValue = this._value;
            }

            if(this._enabled)
            {
                this.element.addEventListener("blur", this);
                this.element.addEventListener("focus", this);

                // TODO only install low level event listeners for high level
                // events others listen to us for
                this.element.addEventListener("touchstart", this, false);
                this.element.addEventListener("mousedown", this, false);
            }
        }
    }

});