From 1814b0e781a2de6fba174105f8effff7846bcdae Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Thu, 19 Sep 2013 23:12:57 -0500 Subject: [PATCH] Start of basic route controller --- hacking/RouteController.js | 78 ++++++++++++++++++++++++++++++++++++++ hacking/route.html | 29 ++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 hacking/RouteController.js create mode 100644 hacking/route.html diff --git a/hacking/RouteController.js b/hacking/RouteController.js new file mode 100644 index 0000000000..3437f6714b --- /dev/null +++ b/hacking/RouteController.js @@ -0,0 +1,78 @@ +/** + * Adapted from Backbone.js + */ +(function(window, document, ionic) { + // Cached regex for stripping a leading hash/slash and trailing space. + var routeStripper = /^[#\/]|\s+$/g; + + // Cached regex for stripping leading and trailing slashes. + var rootStripper = /^\/+|\/+$/g; + + // Cached regex for removing a trailing slash. + var trailingSlash = /\/$/; + + RouteViewController = function(options) { + this.options = options; + + this.root = this.options.root || '/'; + + this.handlers = []; + + this._bindEvents(); + + this.location = window.location; + this.history = window.history; + }; + + RouteViewController.prototype = { + + _bindEvents: function() { + var _this = this; + + window.addEventListener('popstate', function(event) { + console.log("POP STATE", event, window.location, window.location.hash); + _this.checkUrl(event); + }); + window.addEventListener('pushstate', function(event) { + console.log("PUSH STATE", event, window.location); + }); + }, + checkUrl: function(e) { + var current = this.getFragment(); + if (current === this.fragment) return false; + this.loadUrl() || this.loadUrl(this.getHash()); + }, + getFragment: function(fragment, forcePushState) { + if (fragment == null) { + if (this._hasPushState || !this._wantsHashChange || forcePushState) { + fragment = this.location.pathname; + var root = this.root.replace(this.trailingSlash, ''); + if (!fragment.indexOf(root)) fragment = fragment.substr(root.length); + } else { + fragment = this.getHash(); + } + } + return fragment.replace(this.routeStripper, ''); + }, + getHash: function(window) { + var match = (window || this).location.href.match(/#(.*)$/); + return match ? match[1] : ''; + }, + + // Attempt to load the current URL fragment. If a route succeeds with a + // match, returns `true`. If no defined routes matches the fragment, + // returns `false`. + loadUrl: function(fragmentOverride) { + var fragment = this.fragment = this.getFragment(fragmentOverride); + var matched = false; + for(var i = 0; i < this.handlers.length; i++) { + var h = this.handlers[i]; + if (h.route.test(fragment)) { + h.callback(fragment); + matched = true; + } + } + return matched; + }, + }; +})(this, document, ion = this.ionic || {}); diff --git a/hacking/route.html b/hacking/route.html new file mode 100644 index 0000000000..965cbab6f9 --- /dev/null +++ b/hacking/route.html @@ -0,0 +1,29 @@ + + + + Route Controller + + + + + + + +
+
+

Routes

+
+ +
+ Cats +
+
+ + + + + +