-
Notifications
You must be signed in to change notification settings - Fork 15
/
TabBar.js
151 lines (134 loc) · 4.51 KB
/
TabBar.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
/* 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 Utility = require('famous/utilities/Utility');
var View = require('famous/core/View');
var GridLayout = require('famous/views/GridLayout');
var ToggleButton = require('./ToggleButton');
/**
* A view for displaying various tabs that dispatch events
* based on the id of the button that was clicked
*
* @class TabBar
* @extends View
* @constructor
*
* @param {object} options overrides of default options
*/
function TabBar(options) {
View.apply(this, arguments);
this.layout = new GridLayout();
this.buttons = [];
this._buttonIds = {};
this._buttonCallbacks = {};
this.layout.sequenceFrom(this.buttons);
this._add(this.layout);
this._optionsManager.on('change', _updateOptions.bind(this));
}
TabBar.prototype = Object.create(View.prototype);
TabBar.prototype.constructor = TabBar;
TabBar.DEFAULT_OPTIONS = {
sections: [],
widget: ToggleButton,
size: [undefined, 50],
direction: Utility.Direction.X,
buttons: {
toggleMode: ToggleButton.ON
}
};
/**
* Update the options for all components of the view
*
* @method _updateOptions
*
* @param {object} data component options
*/
function _updateOptions(data) {
var id = data.id;
var value = data.value;
if (id === 'direction') {
this.layout.setOptions({dimensions: _resolveGridDimensions.call(this.buttons.length, this.options.direction)});
}
else if (id === 'buttons') {
for (var i in this.buttons) {
this.buttons[i].setOptions(value);
}
}
else if (id === 'sections') {
for (var sectionId in this.options.sections) {
this.defineSection(sectionId, this.options.sections[sectionId]);
}
}
}
/**
* Return an array of the proper dimensions for the tabs
*
* @method _resolveGridDimensions
*
* @param {number} count number of buttons
* @param {number} direction direction of the layout
*
* @return {array} the dimensions of the tab section
*/
function _resolveGridDimensions(count, direction) {
if (direction === Utility.Direction.X) return [count, 1];
else return [1, count];
}
/**
* Create a new button with the specified id. If one already exists with
* that id, unbind all listeners.
*
* @method defineSection
*
* @param {string} id name of the button
* @param {object} content data for the creation of a new ToggleButton
*/
TabBar.prototype.defineSection = function defineSection(id, content) {
var button;
var i = this._buttonIds[id];
if (i === undefined) {
i = this.buttons.length;
this._buttonIds[id] = i;
var widget = this.options.widget;
button = new widget();
this.buttons[i] = button;
this.layout.setOptions({dimensions: _resolveGridDimensions(this.buttons.length, this.options.direction)});
}
else {
button = this.buttons[i];
button.unbind('select', this._buttonCallbacks[id]);
}
if (this.options.buttons) button.setOptions(this.options.buttons);
button.setOptions(content);
this._buttonCallbacks[id] = this.select.bind(this, id);
button.on('select', this._buttonCallbacks[id]);
};
/**
* Select a particular button and dispatch the id of the selection
* to any listeners. Deselect all others
*
* @method select
*
* @param {string} id button id
*/
TabBar.prototype.select = function select(id) {
var btn = this._buttonIds[id];
// this prevents event loop
if (this.buttons[btn] && this.buttons[btn].isSelected()) {
this._eventOutput.emit('select', {id: id});
}
else if (this.buttons[btn]) {
this.buttons[btn].select();
}
for (var i = 0; i < this.buttons.length; i++) {
if (i !== btn) this.buttons[i].deselect();
}
};
module.exports = TabBar;
});