-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
333 lines (325 loc) · 13.9 KB
/
index.html
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<!DOCTYPE html>
<html>
<head>
<title>Bike Tracker</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
<style>
/* theres probably a way to do both of these in bootstrap; should look for that. */
.stage{padding:40px 15px;}
.bike_list .active, .location_list .active { font-weight:700; }
</style>
</head>
<!--
some high level to dos
add in bike location tracking into the storage locations model/view and BikeView view
replace the App.init event handlers with a NavView view
-->
<body>
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#home">Bike Tracker</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#home">Home</a></li>
<li><a href="#bikes">Bikes</a></li>
<li><a href="#locations">Locations</a></li>
</ul>
</div>
</div>
</div>
<div class="container stage">
<div class="view-group jumbotron" id="home">
<div class="welcome">
<h1>Hello</h1>
<p>So this is just a sandbox backbone project.</p>
<p>In theory, you can track where your bikes are with this. Thats still a bit off into the future.</p>
<p>This project's github is <a target="_blank" href="https://github.com/7e7/bike-tracker">here</a>.</p>
</div>
</div>
<div class="view-group row" id="bikes">
<div class="bikes col-md-3"></div>
<div class="bike col-md-9"></div>
</div>
<div class="view-group" id="locations">
<div class="store bikes col-md-3"></div>
<div class="location bikes col-md-9"></div>
</div>
</div>
<script>
var Bike = Backbone.Model.extend({
initialize: function(attributes, options) {
this.attributes.location = this.attributes.location || "";
this.attributes.serial = this.attributes.serial || "";
}
}),
Bikes = Backbone.Collection.extend({ model:Bike }),
StorageLocation = Backbone.Model.extend({
addBike:function(id){
if (id && _.indexOf(this.attributes.bikes, id) < 0){
this.attributes.bikes.push(id)
return true;
}
else{
return false;
}
},
removeBike:function(){
}
}),
Store = Backbone.Collection.extend({ model: StorageLocation })
BikeListView = Backbone.View.extend({
initialize: function(){
this.render();
},
template: _.template(["<h1>Bikes</h1><ul class='bike_list'>",
"<% items.each(function(item) { %>",
"<%= itemTemplate(item) %>",
"<% }); %>",
"</ul><button class='add'>Add a bike</button>"
].join('')),
itemTemplate:_.template("<li><a href='#<%= cid %>'><%= attributes.name %></a></li>"),
newItemTemplate:_.template(["<li><form>",
"<input type='text' class='name' name='name' placeholder='name'/>",
"<input type='text' class='make' name='make' placeholder='make'/>",
"<input type='text' class='model' name='model' placeholder='model'/>",
"<input type='number' class='year' name='year' placeholder='2014' value='2014' step='1' /><br/>",
"<input type='submit' value='add' />",
"<button class='cancel'>cancel</button>",
"</form></li>"].join('')),
render: function() {
$(this.el).empty().append(this.template({
items: App.bikes,
itemTemplate: this.itemTemplate
}));
},
el: $('.bikes'),
events:{
"click .bike_list a":"showBike",
"click button.add": 'newBikeLine',
"click button.cancel":'removeBikeLine',
"submit .bike_list form": 'addNewBike'
},
showBike:function(event){
event.preventDefault();
$('.bike_list li').removeClass('active').filter($(event.target).parents('li')).addClass('active');
bv.render(event.target.hash.substring(1));
},
newBikeLine: function(){
$(this.el).find('.bike_list').append(this.newItemTemplate())
},
removeBikeLine: function(event){
event.preventDefault();
$(event.target).parents('li').remove()
},
addNewBike:function(event){
event.preventDefault();
$(event.target).parents('li').after(this.itemTemplate(App.bikes.add({
name:event.target.name.value,
make:event.target.make.value,
model:event.target.model.value,
year:event.target.year.value
}))).remove()
App.save()
},
}),
BikeView = Backbone.View.extend({
initialize:function(){},
render:function(id){ $(this.el).empty().append( this.template({ bike: App.bikes.get(id) })) },
template: _.template(['<h1><%= bike.get("name") %></h1>',
'<p><button class="edit" data-id="<%= bike.cid %>">Edit</button> <button class="delete" data-id="<%= bike.cid %>">Delete</button></p>',
'<dl>',
'<dt>Make</dt><dd><%= bike.get("make") %></dd>',
'<dt>Model</dt><dd><%= bike.get("model") %></dd>',
'<dt>Year</dt><dd><%= bike.get("year") %></dd>',
'<dt>Serial No.</dt><dd><%= bike.get("serial") %></dd>',
'<dt>Location</dt><dd><% if(bike.get("location").length){ %><%= bike.get("location") %><% }else{ %>None<% } %></dd>',
'</dl>'].join('')),
editTemplate: _.template(['<form>',
'<h1><input type="text" name="name" value="<%= bike.get("name") %>" required /></h1>',
'<input type="hidden" name="cid" value="<%= bike.cid %>" /><dl>',
'<dt>Make</dt><dd><input type="text" name="make" value="<%= bike.get("make") %>" /></dd>',
'<dt>Model</dt><dd><input type="text" name="model" value="<%= bike.get("model") %>" /></dd>',
'<dt>Year</dt><dd><input type="text" name="year" value="<%= bike.get("year") %>" /></dd>',
'<dt>Serial No.</dt><dd><input type="text" name="serial" value="<%= bike.get("serial") %>" /></dd>',
'<dt>Location</dt><dd><input type="text" name="location" value="<%= bike.get("location") %>" /></dd>',
'</dl><input type="submit" /><button class="cancel" data-id="<%= bike.cid %>">cancel</button></form>'].join('')),
el:$('.bike'),
events:{
"click .edit":"showEditMode",
"click .cancel":"rerender",
"click .delete":"removeBike",
'submit form':'updateBike'
},
showEditMode:function(event){
$(this.el).empty().append( this.editTemplate({ bike: App.bikes.get( $(event.target).attr('data-id') ) }))
},
rerender:function(event){
this.render(event.target.cid.value);
},
removeBike:function(event){
App.bikes.remove($(event.target).attr('data-id'));
App.save();
blv.render();
$(this.el).empty();
},
updateBike:function(event){
event.preventDefault();
App.bikes.get(event.target.cid.value).set({
name:event.target.name.value,
make:event.target.make.value,
model:event.target.model.value,
year:event.target.year.value,
location:event.target.location.value
})
App.save();
this.render(event.target.cid.value);
blv.render();
}
})
StoreView = Backbone.View.extend({
initialize:function(){
this.render();
},
render:function(){
$(this.el).empty().append(this.template({
items: App.store,
itemTemplate: this.itemTemplate
}));
},
el:$('.store'),
template: _.template(["<h1>Storage Locations</h1><ul class='location_list'>",
"<% items.each(function(item) { %>",
"<%= itemTemplate(item) %>",
"<% }); %>",
"</ul><button class='add'>Add a location</button>"
].join('')),
itemTemplate:_.template("<li><a href='#<%= cid %>'><%= attributes.name %></a></li>"),
newItemTemplate:_.template('<li><form><input type="text" name="name" placeholder="name" required/><br/><input type="submit" value="add"><button class="cancel">cancel</button></form></li>'),
events:{
"click a":"showStorageLocation",
"click button.add":"addNewStorageLocationLine",
"submit form":"addNewLocation"
},
showStorageLocation:function(event){
event.preventDefault();
$('.location_list li').removeClass('active').filter($(event.target).parents('li')).addClass('active')
slv.render(event.target.hash.substring(1));
},
addNewStorageLocationLine:function(event){
$(this.el).find('.location_list').append(this.newItemTemplate())
},
addNewLocation:function(event){
event.preventDefault();
$(event.target).parents('li').after(this.itemTemplate(App.store.add({
name:event.target.name.value,
bikes:[]
}))).remove();
App.save();
}
}),
StorageLocationView = Backbone.View.extend({
initialize:function(){},
render:function(id){
$(this.el).empty().append(this.template({location: App.store.get(id)}))
},
el:$('.location'),
template: _.template(['<h1><%= location.get("name") %></h1>',
'<p><button class="edit" data-id="<%= location.cid %>">edit</button><button class="delete" data-id="<%= location.cid %>">delete</button></p>',
"<ul>",
"<% for(var i=0, bikes=location.get('bikes');i<bikes.length;i++){ %> <li><%= bikes[i] %></li> <% } %>",
"</ul>"].join('')),
editTemplate:_.template(['<form>',
'<input type="hidden" name="cid" value="<%= location.cid %>" />',
'<h1><input type="" name="name" value="<%= location.get("name") %>"/></h1>',
'<input type="submit" value="update" /><button class="cancel" data-id="<%= location.cid %>">cancel</button>',
'</form>'].join('')),
events:{
'click .edit': 'showEditMode',
'click .cancel': 'rerender',
'click .delete': 'removeLocation',
'submit form':'updateLocation'
},
showEditMode:function(event){
$(this.el).empty().append( this.editTemplate({ location: App.store.get( $(event.target).attr('data-id') ) }))
},
rerender:function(event){
this.render($(event.target).attr('data-id'));
},
updateLocation:function(event){
event.preventDefault();
App.store.get(event.target.cid.value).set({
name:event.target.name.value
})
App.save();
this.render(event.target.cid.value);
sv.render();
},
removeLocation:function(event){
event.preventDefault();
App.store.remove( $(event.target).attr('data-id') )
App.save();
$(this.el).empty();
sv.render();
}
}),
NavView = Backbone.View.extend({
initialize:function(){ this.render(); },
render:function(){},
template:_.template(''),
el:$()
});
App={
init:function(){
if(!App.load()){
App.scaffold();
}
blv = new BikeListView(),
bv = new BikeView(),
sv = new StoreView(),
slv = new StorageLocationView();
$('.view-group').hide().filter('#home').show();
$('.navbar a').on('click', function(event){
event.preventDefault();
$('.view-group').hide().filter(event.target.hash).show();
$('.navbar li').removeClass('active').filter($(this).parents('li')).addClass('active');
if($(this).hasClass('navbar-brand')){ $('a[href=#home]').parents('li').addClass('active') }
//add in view rerenders, for funsies?
})
},
save:function(){
window.localStorage.setItem('bikes', JSON.stringify(App.bikes.toJSON()));
window.localStorage.setItem('store', JSON.stringify(App.store.toJSON()))
},
load:function(){
var bikes = window.localStorage.bikes,
store = window.localStorage.store;
if( bikes && store && typeof JSON.parse(bikes) == 'object' && typeof JSON.parse(store) == 'object' ){
App.bikes = new Bikes(JSON.parse(bikes));
App.store = new Store(JSON.parse(store));
return true;
}
else{
return false;
}
},
scaffold: function(){
App.bikes=new Bikes([{"name":"my bike", "make":"Trek", "model": "7.5", "year":2014}]);
App.store=new Store([{"name":"test location", "bikes":[]}])
}
};
App.init();
</script>
</body>
</html>