-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
459 lines (372 loc) · 17.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Company Data Browser</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/app.css">
<!-- Vendors -->
<script src="js/vendor/underscore-min.js" type="text/javascript"></script>
<script src="js/vendor/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="js/vendor/backbone-min.js" type="text/javascript"></script>
<!-- Used to include the sample data provided as JSON object -->
<script src="data/testdata.js" type="text/javascript"></script>
<!-- Document Template -->
<script type="text/template" id="documentTemplate">
<table>
<tr class="doc-name doc-data">
<td>Name</td><td><%= name %></td>
</tr>
<tr class="doc-data">
<td>Created</td><td><%= TestNs.utils.dateFormatFromISO(created) %></td>
</tr>
<tr class="doc-data">
<td>Updated</td><td><%= TestNs.utils.dateFormatFromISO(updated) %></td>
</tr>
<% if(_.size(children) > 0) { %>
<tr>
<td class="doc-children">Children</td>
<td class="table-children">
<table class="doc-table">
<thead>
<tr>
<th>Name</th>
<th>Created</th>
<th>Updated</th>
</tr>
</thead>
<tbody>
<% _.each(children, function(child) { %>
<tr class="<%= (child.children && child.children.length > 0) ? 'doc-row' : 'doc-data' %>" id="<%= child.id %>">
<td><%= child.name %></td>
<td><%= TestNs.utils.dateFormatFromISO(child.created) %></td>
<td><%= TestNs.utils.dateFormatFromISO(child.updated) %></td>
</tr>
<% }); %>
</tbody>
</table>
</td>
</tr>
<% } %>
</table>
</script>
<script type="text/javascript">
/* Global namespace */
var TestNs = {};
/* ======== Utility Functions ======== */
TestNs.Utils = function() {
this.dateFormatFromISO = function(dateIso) {
var d = new Date(dateIso);
var day = ("0" + d.getDate()).slice(-2);
var month = ("0" + (d.getMonth() + 1)).slice(-2);
var year = d.getFullYear();
var hh = ("0" + d.getHours()).slice(-2);
var mm = ("0" + d.getMinutes()).slice(-2);
return day + '/' + month + '/' + year + ' ' + hh + ':' + mm;
}
};
/* ==================================== */
/* ======== History Controller ======== */
TestNs.HistoryCtrl = function() {
this.state = new Object();
/**
* Sets the active company on the state object. The Company
* selection is what mainly controls the page view
*/
this.setActiveCompany = function(c) {
this.state = {};
this.state = new Object();
this.state = {
company: c,
docs: new Array()
};
console.log(this.state);
this.update();
}
/**
* Push a new item in the document array when a Document view
* gets exploded
*/
this.pushDoc = function(d) {
this.state.docs.push(d);
console.log(this.state);
this.update();
}
/**
* Update the opened documents array removing not required ones
*/
this.popDocUntil = function(d) {
var count = 0;
while(_.last(this.state.docs) != d && this.state.docs.length > 0) {
this.state.docs.pop();
count++;
}
console.log(this.state);
if(count > 0)
this.update();
}
/**
* Formats a custom page title based on the current state
*/
this.setTitle = function() {
var t = 'CDB - ' + this.state.company;
if(this.state.docs.length > 0)
t += ' / ' + _.last(this.state.docs);
document.title = t;
}
/**
* Used by the HistoryCtrl methods to update page title and to
* push in the browser's history
*/
this.update = function() {
this.url = this.state.company + '/' + this.state.docs.join('/');
if(this.state.docs.length > 0) this.url += '/';
console.log('Saving last state and pushing it in history...');
console.log(this.state);
history.pushState(this.state, null, window.location.pathname + '#/' + this.url);
this.setTitle(); // New page title
console.log('Current Path: ' + this.url);
}
/**
* Listen for "back" button events
*/
this.init = function() {
window.addEventListener('popstate', function(e) {
if(e.state) {
TestNs.hC.applyState(e.state);
}
});
}
/**
* Uses the state object to set the correct view on the page,
* exploding iteratively the required documents
*/
this.applyState = function(s) {
this.state = s;
// TODO: check if the state is correct: models exist
if(s.company) {
// Set the root document view for the company
TestNs.rootDoc.changeModelById(s.company);
// Unselect the previous model
TestNs.cCompanies.unselect();
// Select the new one
var m = TestNs.cCompanies.findWhere({name: s.company});
if(m != undefined)
m.set({selected : true});
}
if(s.docs && s.docs.length > 0) {
// Recursively explode the sub document views
var tempView = TestNs.rootDoc;
for(var i = 0; i < s.docs.length; i++) {
tempView = tempView.explodeById(s.docs[i]);
}
}
}
/**
* Used to parse the current URL's hash and create a state object,
* that will be used to generate the correct view and update
* the page title
*/
this.urlToState = function() {
var state = null;
var h = _.compact(window.location.hash.substr(1).split('/'));
if(h != undefined && h != null) {
var state = {
company: _.first(h),
docs: _.rest(h)
};
console.log('State from URI');
console.log(state);
}
return state;
}
this.init();
}
/* ==================================== */
/* =============== Models =============== */
TestNs.Company = Backbone.Model.extend({ });
TestNs.CompanyList = Backbone.Collection.extend({
model: TestNs.Company,
// Unselect the previously selected model in the collection
unselect: function() {
var prevSel = this.findWhere({selected: true});
if(prevSel != undefined)
prevSel.set({selected : false});
}
});
TestNs.Document = Backbone.Model.extend({
initialize: function(docdata) {
this.children = new TestNs.DocumentList(docdata.children);
},
parse: function(res) {
// If the children attribute is present, use it to populate the inner documentlist
// SUPERFLUOUS
//res.children && this.children.reset(res.children);
}
});
TestNs.DocumentList = Backbone.Collection.extend({
model: TestNs.Document
});
/* ====================================== */
/* ================= Views ============== */
TestNs.CompanyView = Backbone.View.extend({
tagName: 'li',
className: 'company-name',
events: {
'click': 'setDocumentView'
},
initialize: function() {
// Auto refresh the DOM on model change
this.model.on('change', this.render, this);
// 'selected' field used to distiguish the currently views company documents hierarchy
this.model.set({selected : false});
this.render();
},
render: function() {
this.$el.html(this.model.attributes.name);
this.$el.addClass(this.model.attributes.name);
if(this.model.get('selected') == true)
this.$el.addClass('company-selected');
else
this.$el.removeClass('company-selected');
},
setDocumentView: function() {
TestNs.EventBus.trigger('company:switched', this.model.get('name'));
// Unselect the previous model
this.model.collection.unselect();
// Select this one
this.model.set({selected : true});
}
});
TestNs.CompanyListView = Backbone.View.extend({
tagName: 'ul',
className: 'company-list',
initialize: function() {
this.render();
},
render: function() {
this.collection.each(function(company) {
var cv = new TestNs.CompanyView({ model: company });
this.$el.append(cv.el);
}, this);
}
});
TestNs.DocumentView = Backbone.View.extend({
template: _.template($('#documentTemplate').html()),
events: {
'click .doc-row': 'explode',
'click .doc-name': 'collapse'
},
initialize: function() {
this.render();
this.innerView = null;
},
render: function() {
this.$el.html(this.template(this.model.attributes));
},
// Used to switch the content of the main view
changeModelById: function(id) {
this.model = this.model.collection.get(id);
this.render();
},
// Triggers the creation of a sub-view using the document id
explode: function(e) {
TestNs.EventBus.trigger('doc:exploded', e.currentTarget.id);
// Replace the docs table with the single doc view
this.explodeById(e.currentTarget.id);
},
// Actually instantiates the view and updates the DOM
explodeById: function(id) {
this.innerView = new TestNs.DocumentView({
model: this.model.children.get(id)
});
this.$('.doc-table').html(this.innerView.el);
return this.innerView;
},
// Destroy all of the non required views
collapse: function() {
TestNs.EventBus.trigger('doc:collapsed', this.model.get('id'));
this.innerView = null;
this.render();
}
});
/* ====================================== */
/* ======== Event Bus ======== */
TestNs.EventBus = _.extend({}, Backbone.Events);
TestNs.EventBus.on('company:switched', function(data) {
// Update the history
TestNs.hC.setActiveCompany(data);
// Update the root document view
TestNs.rootDoc.changeModelById(data);
});
// Add the exploded document to the history
TestNs.EventBus.on('doc:exploded', function(id) {
TestNs.hC.pushDoc(id);
});
// Go back in history till the required doc
TestNs.EventBus.on('doc:collapsed', function(id) {
TestNs.hC.popDocUntil(id);
});
/* =========================== */
/* ======== Global Variables ======== */
TestNs.cCompanies = null;
TestNs.cDocuments = null;
TestNs.compListView = null;
TestNs.rootDoc = null;
TestNs.utils = new TestNs.Utils();
TestNs.hC = new TestNs.HistoryCtrl();
/* ================================== */
/**
* Detect the required view from the URL or use the default one
*/
function applyUrlToView() {
var state = TestNs.hC.urlToState();
if(state == undefined || state.company == undefined)
{
state.company = TestNs.cCompanies.at(0).get('name');
}
TestNs.hC.applyState(state);
TestNs.hC.setTitle();
}
$(window).on('hashchange', applyUrlToView);
/* Application entry point */
$(document).ready(function($) {
// Using a web server data can be easily retrieved via XHR
//$.get('data/testdata.json', function(data) {
var documents = _.values(data);
var companies = _.keys(data);
// Get company names and create a collection
companies = _.map(companies, function(val) { return {'name' : val} });
TestNs.cCompanies = new TestNs.CompanyList(companies);
// Generate the collection of documents
TestNs.cDocuments = new TestNs.DocumentList(documents);
// Renders the company list view
TestNs.compListView = new TestNs.CompanyListView({ collection: TestNs.cCompanies });
$('aside').append(TestNs.compListView.el);
// Default view: first company
TestNs.rootDoc = new TestNs.DocumentView({ model: TestNs.cDocuments.at(0) });
$('main').append(TestNs.rootDoc.el);
// Detect the required view from the URL or use a default one
applyUrlToView();
//});
});
</script>
</head>
<body>
<header id="header">
Company Data Browser
</header>
<aside>
<!-- To be filled with companyes -->
</aside>
<main>
<!-- To be filled with documents -->
</main>
<footer>
Test development made by <a href="http://it.linkedin.com/in/martelliandrea/">Andrea Martelli</a>
</footer>
</body>
</html>