forked from two-dudes/backbone-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
navigation.js
executable file
·126 lines (104 loc) · 4.07 KB
/
navigation.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
$(function() {
var Navigation = function() {
this.d = $.Deferred();
this.breadcrumbs = new Backbone.Collection();
this.routers = {};
this.tree = {};
var that = this;
this.appendRouter = function(router) {
if (typeof(router.navigation) != "undefined") {
_.extend(that.tree, router.navigation.pages);
that.routers[router.navigation.prefix] = router;
}
};
this.mapRouters = function() {
for (var i in that.routers) {
that.routers[i].bind('route', function (route, args) {
var router = this;
that.breadcrumbs.reset();
var url = that._getRouteUrl(router, route);
var mappedArgs = that._getMappedArgs(args, url);
that._mapNavigation(router.navigation.prefix + '.' + route, mappedArgs);
that.getBreadcrumbs();
});
}
};
this._getMappedArgs = function(args, link) {
var route = Backbone.Router.prototype._routeToRegExp(link);
var argNames = Backbone.Router.prototype._extractParameters(route, link);
var namedArgs = {};
for (var i in argNames) {
namedArgs[argNames[i]] = args[i];
}
return namedArgs;
};
this._getMappedUrl = function(url, mappedArgs) {
for (var argName in mappedArgs) {
var arg = mappedArgs[argName];
url = url.replace(argName, arg);
}
return url;
};
this._getProcessedText = function(template, mappedArgs) {
if (typeof(template) == "function") {
// return template({ args: mappedArgs });
return template(mappedArgs).done(function(newText) {
});
}
return template;
};
this._mapNavigation = function(route, mappedArgs) {
if (!that.tree[route]) {
return;
}
var isFunction = typeof(that.tree[route].template) == "function";
var routeParts = route.split('.');
var router = that.routers[routeParts[0]];
var shortRoute = routeParts[1];
var page = new Backbone.Model({
url: this._getMappedUrl(this._getRouteUrl(router, shortRoute), mappedArgs),
text: isFunction ? that.tree[route].template(mappedArgs): that.tree[route].template,
mappedArgs: mappedArgs,
isResolved: false
});
var isDeferred = (typeof(page.get('text') == 'Object') && page.get('text').resolve);
if (isDeferred) {
page.get('text').done(function(template){
page.set({'text': template, 'isResolved': true});
that.checkBreadcrumbs();
});
} else {
page.set('isResolved', true);
}
this.breadcrumbs.unshift(page);
if (typeof(that.tree[route].parent) != "undefined") {
this._mapNavigation(that.tree[route].parent, mappedArgs);
}
};
this._getRouteUrl = function(router, route) {
for (var url in router.routes) {
if (route == router.routes[url]) {
return url;
}
}
};
this.getBreadcrumbs = function() {
this.checkBreadcrumbs();
return this.d;
};
this.isAllPagesResolved = function () {
for (var i = 0; i < this.breadcrumbs.size(); i++) {
if (!this.breadcrumbs.at(i).get('isResolved')) {
return false;
}
}
return true;
};
this.checkBreadcrumbs = function() {
if (this.isAllPagesResolved() && this.breadcrumbs.size() > 0) {
this.d.resolve(this.breadcrumbs);
}
};
};
Backbone.Navigation = Navigation;
});