aboutsummaryrefslogtreecommitdiff
path: root/js/components/button.reel/button.js
blob: 2d26c8b4483197c1f965f6c24b20bbf715373062 (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
/* <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,
Component = require("montage/ui/component").Component;

var Button = exports.Button = Montage.create(Component, {
    // Button state
    _focused: {
        value: false
    },

    _pressed: {
        value: false
    },

    _isToggleButton: {
        value: false
    },

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

    _value: {
        value: false
    },

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

    _label: {
        value: ""
    },

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

    // TODO - Allow user to specify up, over and down states
    _onState: {
        value: "on"
    },

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

    _offState: {
        value: "off"
    },

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

    // Low-level event listeners
    handleTouchstart: {
        value: function(event) {
            // TODO preventingDefault disables the magnifying class
            // sadly it also disables double tapping on the button to zoom...
            event.preventDefault();
            this._acknowledgeIntent();
        }
    },

    handleMousedown: {
        value: function(event) {
            this._acknowledgeIntent();
        }
    },

    handleTouchend: {
        value: function(event) {
            this._interpretInteraction(event);
        }
    },

    handleTouchcancel: {
        value: function(event) {
            console.log("cancel!")
            // this._interpretInteraction(event);
        }
    },

    handleMouseup: {
        value: function(event) {
            this._interpretInteraction(event);
        }
    },

    // Internal state management
    _acknowledgeIntent: {
        value: function() {
            this._pressed = true;
            this.element.classList.add("pressed");
        }
    },

    _interpretInteraction: {
        value: function(event) {

            if (!this._pressed) {
                return;
            }

            this.value = !this.value;

            this._pressed = false;
            this._dispatchActionEvent();
        }
    },

    _dispatchActionEvent: {
        value: function() {
            var actionEvent = document.createEvent("CustomEvent");
            actionEvent.initCustomEvent("action", true, true);
            actionEvent.type = "action";
            this.dispatchEvent(actionEvent);
        }
    },

    draw: {
        enumerable: false,
        value: function() {
            if(this.isToggleButton)
            {
                if(this._value === true)
                {
                    this.element.classList.remove(this.offState);
                    this.element.classList.add(this.onState);
                }
                else
                {
                    this.element.classList.remove(this.onState);
                    this.element.classList.add(this.offState);
                }
            }
            
            if(this.label && this.label !== "")
            {
                this.element.textContent = this.label;
            }
        }
    },

    prepareForDraw: {
        value: function() {

            // TODO only install low level event listeners for high level
            // events others listen to us for

            this.element.addEventListener("touchstart", this);
            // TODO listen to mouseup anywhere within the app
            document.addEventListener("touchend", this);
            document.addEventListener("touchcancel", this);


            this.element.addEventListener("mousedown", this);

            // TODO listen to mouseup anywhere within the app
            document.addEventListener("mouseup", this);

            // TODO accept space or enter as a way to trigger action
            // if element targeted; balancing demans of multitouch
            // with traditional single focus model
            document.addEventListener("keydown", this, true);
        }
    }

});