-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathToggleButton.js
159 lines (143 loc) · 4.98 KB
/
ToggleButton.js
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Owner: [email protected]
* @license MPL 2.0
* @copyright Famous Industries, Inc. 2014
*/
define(function(require, exports, module) {
var Surface = require('famous/core/Surface');
var EventHandler = require('famous/core/EventHandler');
var RenderController = require('famous/views/RenderController');
/**
* A view for transitioning between two surfaces based
* on a 'on' and 'off' state
*
* @class TabBar
* @extends View
* @constructor
*
* @param {object} options overrides of default options
*/
function ToggleButton(options) {
this.options = {
content: '',
offClasses: ['off'],
onClasses: ['on'],
size: undefined,
outTransition: {curve: 'easeInOut', duration: 300},
inTransition: {curve: 'easeInOut', duration: 300},
toggleMode: ToggleButton.TOGGLE,
crossfade: true
};
this._eventOutput = new EventHandler();
EventHandler.setOutputHandler(this, this._eventOutput);
this.offSurface = new Surface();
this.offSurface.on('click', function() {
if (this.options.toggleMode !== ToggleButton.OFF) this.select();
}.bind(this));
this.offSurface.pipe(this._eventOutput);
this.onSurface = new Surface();
this.onSurface.on('click', function() {
if (this.options.toggleMode !== ToggleButton.ON) this.deselect();
}.bind(this));
this.onSurface.pipe(this._eventOutput);
this.arbiter = new RenderController({
overlap : this.options.crossfade
});
this.deselect();
if (options) this.setOptions(options);
}
ToggleButton.OFF = 0;
ToggleButton.ON = 1;
ToggleButton.TOGGLE = 2;
/**
* Transition towards the 'on' state and dispatch an event to
* listeners to announce it was selected
*
* @method select
*/
ToggleButton.prototype.select = function select() {
this.selected = true;
this.arbiter.show(this.onSurface, this.options.inTransition);
// this.arbiter.setMode(ToggleButton.ON, this.options.inTransition);
this._eventOutput.emit('select');
};
/**
* Transition towards the 'off' state and dispatch an event to
* listeners to announce it was deselected
*
* @method deselect
*/
ToggleButton.prototype.deselect = function deselect() {
this.selected = false;
this.arbiter.show(this.offSurface, this.options.outTransition);
this._eventOutput.emit('deselect');
};
/**
* Return the state of the button
*
* @method isSelected
*
* @return {boolean} selected state
*/
ToggleButton.prototype.isSelected = function isSelected() {
return this.selected;
};
/**
* Override the current options
*
* @method setOptions
*
* @param {object} options JSON
*/
ToggleButton.prototype.setOptions = function setOptions(options) {
if (options.content !== undefined) {
this.options.content = options.content;
this.offSurface.setContent(this.options.content);
this.onSurface.setContent(this.options.content);
}
if (options.offClasses) {
this.options.offClasses = options.offClasses;
this.offSurface.setClasses(this.options.offClasses);
}
if (options.onClasses) {
this.options.onClasses = options.onClasses;
this.onSurface.setClasses(this.options.onClasses);
}
if (options.size !== undefined) {
this.options.size = options.size;
this.onSurface.setSize(this.options.size);
this.offSurface.setSize(this.options.size);
}
if (options.toggleMode !== undefined) this.options.toggleMode = options.toggleMode;
if (options.outTransition !== undefined) this.options.outTransition = options.outTransition;
if (options.inTransition !== undefined) this.options.inTransition = options.inTransition;
if (options.crossfade !== undefined) {
this.options.crossfade = options.crossfade;
this.arbiter.setOptions({overlap: this.options.crossfade});
}
};
/**
* Return the size defined in the options object
*
* @method getSize
*
* @return {array} two element array [height, width]
*/
ToggleButton.prototype.getSize = function getSize() {
return this.options.size;
};
/**
* Generate a render spec from the contents of this component.
*
* @private
* @method render
* @return {number} Render spec for this component
*/
ToggleButton.prototype.render = function render() {
return this.arbiter.render();
};
module.exports = ToggleButton;
});