aboutsummaryrefslogtreecommitdiff
path: root/js/components/hottextunit.reel/hottextunit.js
blob: 2a89db033e49b1f30eb2414f7934022ff207f03b (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
/* <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 HotText = require("js/components/hottext.reel").HotText;

var HotTextUnit = exports.HotTextUnit = Montage.create(HotText, {

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

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

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

    _units: {
        enumerable: false,
        value: "px"
    },

    units: {
        serializable: true,
        enumerable: true,
        get: function() {
            return this._units;
        },
        set: function(value) {
            if (value !== this._units) {
                if(this._acceptableUnits.indexOf(value) !== -1)
                {
                    this._units = value;
                    this._unitsModified = true;
                    this.needsDraw = true;

                    this._setEventFlags("change", true);
                    this._dispatchActionEvent();
                }
            } else {
                this._unitsModified = false;
            }
        }
    },

    // Some controls will only support certain units
    // For example, Oval would specify an innerRadius with acceptableUnits = ["%"]
    // and Stroke Size with acceptableUnits = ["px", "pt", "%"]
    _acceptableUnits: {
        enumerable: false,
        value: ["px"]
    },


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

    // 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() {
            var inputString = this.inputField.value;

            // Ignore all whitespace, digits, negative sign and "." when looking for units label
            // The units must come after one or more digits
            var objRegExp = /(\-*\d+\.*\d*)(\s*)(\w*\%*)/;
            var unitsString = inputString.replace(objRegExp, "$3");
            if(unitsString)
            {
                var noSpaces = /(\s*)(\S*)(\s*)/;
                // strip out spaces and convert to lower case
                var match = (unitsString.replace(noSpaces, "$2")).toLowerCase();
                if(match)
                {
                    this.units = match;
                }
            }

            this._setEventFlags("change", false);
            // Moving this call to after setting the value since value changes are dispatching events before units are set
            this.value = this.inputFunction(inputString);
        }
    },

    draw: {
        enumerable: false,
        value: function() {
            this.inputField.value = this._value + " " + this._units;
            this.numericField.innerText = this._value;
            this.unitsField.innerText = " " + this._units;


            if(this._hasFocus)
            {
                this.numericField.classList.add("hide");
                this.unitsField.classList.add("hide");

                this.inputField.classList.remove("hide");

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

                this.inputField.classList.add("hide");
            }
        }
    },

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

    prepareForDraw: {
        value: function() {
            this.numericField = document.createElement("span");
            this.numericField.classList.add("underline");
            this.numericField.innerText = this._value;

            this.unitsField = document.createElement("span");
            this.unitsField.innerText = " " + this._units;

            this.inputField = document.createElement("input");
            this.inputField.value = this._value + " " + this._units;
            this.inputField.classList.add("hide");

            this.element.appendChild(this.numericField);
            this.element.appendChild(this.unitsField);
            this.element.appendChild(this.inputField);


            if(this._value)
            {
                this._numValue = this._value;
            }

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

                this.element.addEventListener("touchstart", this, false);
                this.element.addEventListener("mousedown", this, false);
            }
        }
    }

});