summaryrefslogtreecommitdiff
path: root/public/assets/js/vendor/view/router.js
blob: 0f977e0c05cfd25a181225c5e1b3ac763c8758ee (plain)
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
var Router = View.extend({
	routeByHash: false,

	go: function (url) {
		this.parseRoute(url);
	},

	pushState: function (url) {
		if (this.routeByHash) {
			window.location.hash = url;
		} else if (window.history) {
			window.history.pushState(null, null, url);
		}
	},

	route: function () {
		var path = this.routeByHash
			? window.location.hash.substr(0)
			: window.location.pathname;
		path = path || "/";
		this.originalPath = path;
		this.parseRoute(path);
	},

	parseRoute: function (pathname) {
		pathname = pathname.replace(/^#/, "");

		if (pathname[0] !== "/") {
			pathname = "/" + pathname;
		}

		var routes = this.routes,
			path = pathname.split("/");

		for (var i = 0; i < path.length; i++) {
			if (!path[i].length) {
				path[i] = null;
			}
		}

		if (pathname in routes) {
			this[this.routes[pathname]]();
			return;
		}

		if (path[path.length - 1] == null) {
			path.pop();
		}

		for (var route in routes) {
			var routePath = route.split("/");
			if (routePath[1] == path[1]) {
				if (
					routePath[2] &&
					routePath[2].indexOf(":") !== -1 &&
					path[2] &&
					path[3] === routePath[3]
				) {
					this[this.routes[route]](path[2]);
					return;
				} else if (routePath[2] == path[2]) {
					if (routePath[3] && path[3]) {
						if (routePath[3].indexOf(":") !== -1) {
							this[this.routes[route]](path[3]);
							return;
						} else if (routePath[3] == path[3]) {
							this[this.routes[route]]();
							return;
						}
					} else if (!routePath[3] && !path[3]) {
						this[this.routes[route]]();
						return;
					}
				} else if (!routePath[2] && (!path[2].length || !path[2])) {
					this[this.routes[route]]();
					return;
				}
			}
		}
		// Redirect to root on 404
		window.location = "/";
	},
});