-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.js
More file actions
217 lines (206 loc) · 6.27 KB
/
main.js
File metadata and controls
217 lines (206 loc) · 6.27 KB
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
/** @jsx React.DOM */
var LOCATIONS = ['crossroads', 'cafe3', 'foothill', 'clarkkerr'];
var MEALS = ['breakfast', 'lunch', 'dinner'];
var LOCATION_TO_NAME = {
'crossroads': 'Crossroads', 'cafe3': 'Cafe 3',
'foothill': 'Foothill', 'clarkkerr': 'Clark Kerr'
}
var mealFromTime = function() {
var currentTime = new Date();
var breakfastTime = new Date(); breakfastTime.setHours(10, 1);
var lunchTime = new Date(); lunchTime.setHours(14, 1);
var dinnerTime = new Date(); dinnerTime.setHours(21, 1);
if(breakfastTime < currentTime && currentTime < lunchTime)
return 'lunch';
else if(currentTime < dinnerTime)
return 'dinner';
else
return 'breakfast';
}
var App = React.createClass({
getDefaultProps: function() {
return {
api: 'https://www.ocf.berkeley.edu/~eye/cal-dining/menu'
}
},
getInitialState: function() {
return {
active: {
'crossroads': false, 'cafe3': false,
'foothill': false, 'clarkkerr': false
},
open: {
'crossroads': false, 'cafe3': false,
'foothill': false, 'clarkkerr': false
},
meal: mealFromTime(),
menu: {
'crossroads': {}, 'cafe3': {},
'foothill': {}, 'clarkkerr': {}
}
}
},
countActive: function() {
var count = 0;
for(var key in this.state.active){
if(this.state.active[key])
count++;
}
return count;
},
setActiveItem: function(item) {
if ($.inArray(item, MEALS) !== -1) { // setting the meal
this.state.meal = item;
this.componentDidMount();
} else if(item in this.state.active) { // setting the active menu
if(this.state.active[item] === false || this.countActive() > 1) {
var newActive = this.state.active;
newActive[item] = !newActive[item];
this.setState({active: newActive});
}
}
},
componentDidMount: function() {
$.get(this.props.api + "?meal=" + this.state.meal, function(result) {
var startSelectedCounter = 0;
var active = this.state.active;
var open = this.state.open;
LOCATIONS.forEach(function(location) {
if (Object.keys(result[location]).length) {
open[location] = true;
if (startSelectedCounter < 2) {
active[location] = true;
startSelectedCounter++;
}
}
});
this.setState({
active: active,
open: open,
menu: result
});
}.bind(this));
},
render: function() {
var menus = [];
var colSize = 'col-xs-' + (12 / this.countActive());
LOCATIONS.forEach(function(location) {
if(this.state.active[location])
menus.push(
<div key={location} className={colSize}>
<Menu key={location} entries={this.state.menu[location]} />
</div>
);
}.bind(this));
return (
<div>
<Navbar active={this.state.active} onSelect={this.setActiveItem}
open={this.state.open} meal={this.state.meal}/>
<div className="container">
<div className="row">
{menus}
</div>
</div>
</div>
);
}
});
var Navbar = React.createClass({
render: function() {
var createItem = function(location) {
return <NavbarItem key={location}
active={this.props.active[location]}
open={this.props.open[location]}
onSelect={this.props.onSelect} />
}
return (
<nav className="navbar navbar-default" role="navigation">
<div className="container-fluid">
<div className="navbar-header">
<button type="button" className="navbar-toggle" data-toggle="collapse" data-target="#navbar">
<span className="sr-only">Toggle navigation</span>
<span className="icon-bar"></span>
<span className="icon-bar"></span>
<span className="icon-bar"></span>
</button>
<a className="navbar-brand title" href="#">Cal Dining Menu</a>
</div>
<div className="collapse navbar-collapse" id="navbar">
<ul className="nav navbar-nav">
{LOCATIONS.map(createItem.bind(this))}
</ul>
<ul className="nav navbar-nav navbar-right">
<Timepicker meal={this.props.meal} onSelect={this.props.onSelect}/>
</ul>
</div>
</div>
</nav>
);
}
});
var NavbarItem = React.createClass({
handleClick: function(event) {
event.preventDefault();
if (this.props.open)
this.props.onSelect(this.props.key);
},
render: function() {
return <li className={this.props.active ? 'active' : ''}>
<a onClick={this.handleClick}>{this.props.key}</a>
</li>
}
});
var Timepicker = React.createClass({
handleClick: function(event) {
event.preventDefault();
this.props.onSelect($(event.target).text());
},
render: function() {
var meals = [];
MEALS.forEach(function(meal) {
if(this.props.meal !== meal){
meals.push(<li key={meal}><a onClick={this.handleClick}>{meal}</a></li>);
}
}.bind(this));
return <li className="dropdown">
<a href="#" className="dropdown-toggle" data-toggle="dropdown">
{this.props.meal}
<b className="caret"></b>
</a>
<ul className="dropdown-menu">{meals}</ul>
</li>;
}
});
var Menu = React.createClass({
componentDidMount: function() {
$('.entree li').tooltip();
},
render: function() {
var entries = [];
for (var entryName in this.props.entries) {
var entree = this.props.entries[entryName];
var ingredients = entree.ingredients;
var type = 'normal';
if (entree.vegetarian)
type = 'vegetarian';
else if (entree.vegan)
type = 'vegan';
entries.push(
<li key={entryName} className="list-group-item"
data-toggle="tooltip" title={ingredients}>
<p className={type}>{entryName}</p>
</li>
);
}
return <div className="entree">
<h3 className="title">{LOCATION_TO_NAME[this.props.key]}</h3>
<ul className="list-group">
{entries}
</ul>
</div>;
}
});
React.renderComponent(
<App />,
document.getElementById('main')
);