diff options
| author | “Ryder <“r@okfoc.us”> | 2016-03-29 15:37:38 -0400 |
|---|---|---|
| committer | “Ryder <“r@okfoc.us”> | 2016-03-29 15:37:38 -0400 |
| commit | 6b84198c7c3eeadec90c8a856218c96acf942e45 (patch) | |
| tree | 53bc6a98227290ea3bf956484b7b0fa6dcdcaa4c /public/assets/js/vendor/Router.js | |
| parent | 4fc13cd098f25904acba98d25b03cc777a5d85e8 (diff) | |
| parent | e19b1cd730e943ef5b2072f55026ae260c312ee8 (diff) | |
Merge branch 'master' of https://github.com/okfocus/portfolio
Diffstat (limited to 'public/assets/js/vendor/Router.js')
| -rw-r--r-- | public/assets/js/vendor/Router.js | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/public/assets/js/vendor/Router.js b/public/assets/js/vendor/Router.js new file mode 100644 index 0000000..3b0d939 --- /dev/null +++ b/public/assets/js/vendor/Router.js @@ -0,0 +1,82 @@ +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 = '/' + } + +}) |
