-
Notifications
You must be signed in to change notification settings - Fork 41
/
main.js
89 lines (75 loc) · 3 KB
/
main.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
// This app view is responsible for rendering all content that goes into
// <html>. It's initted right away and renders itself on DOM ready.
var app = require('ampersand-app');
var setFavicon = require('favicon-setter');
var View = require('ampersand-view');
var dom = require('ampersand-dom');
var ViewSwitcher = require('ampersand-view-switcher');
var _ = require('underscore');
var domify = require('domify');
var localLinks = require('local-links');
var templates = require('../templates');
module.exports = View.extend({
template: templates.body,
autoRender: true,
initialize: function () {
// this marks the correct nav item selected
this.listenTo(app, 'page', this.handleNewPage);
},
events: {
'click a[href]': 'handleLinkClick'
},
render: function () {
// some additional stuff we want to add to the document head
document.head.appendChild(domify(templates.head()));
// main renderer
this.renderWithTemplate(this);
// init and configure our page switcher
this.pageSwitcher = new ViewSwitcher(this.queryByHook('page-container'), {
show: function (newView, oldView) {
// it's inserted and rendered for me
document.title = _.result(newView, 'pageTitle') || '{{{title}}}';
document.scrollTop = 0;
// add a class specifying it's active
dom.addClass(newView.el, 'active');
// store an additional reference, just because
app.currentPage = newView;
}
});
// setting a favicon for fun (note, it's dynamic)
setFavicon('/favicon.ico');
return this;
},
handleNewPage: function (view) {
// tell the view switcher to render the new one
this.pageSwitcher.set(view);
// mark the correct nav item selected
this.updateActiveNav();
},
// Handles all `<a>` clicks in the app not handled
// by another view. This lets us determine if this is
// a click that should be handled internally by the app.
handleLinkClick: function (e) {
// This module determines whether a click event is
// a local click (making sure the for modifier keys, etc)
// and dealing with browser quirks to determine if this
// event was from clicking an internal link. That we should
// treat like local navigation.
var localPath = localLinks.pathname(e);
if (localPath) {
e.preventDefault();
app.navigate(localPath);
}
},
updateActiveNav: function () {
var path = window.location.pathname.slice(1);
this.queryAll('.nav a[href]').forEach(function (aTag) {
var aPath = aTag.pathname.slice(1);
if ((!aPath && !path) || (aPath && path.indexOf(aPath) === 0)) {
dom.addClass(aTag.parentNode, 'active');
} else {
dom.removeClass(aTag.parentNode, 'active');
}
});
}
});