diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/AngularPublic.js | 3 | ||||
| -rw-r--r-- | src/Browser.js | 130 | ||||
| -rw-r--r-- | src/angular-mocks.js | 23 | ||||
| -rw-r--r-- | src/service/location.js | 6 |
4 files changed, 95 insertions, 67 deletions
diff --git a/src/AngularPublic.js b/src/AngularPublic.js index 476de3e3..f63948d8 100644 --- a/src/AngularPublic.js +++ b/src/AngularPublic.js @@ -4,8 +4,9 @@ var browserSingleton; angularService('$browser', function($log){ if (!browserSingleton) { + // TODO(vojta): inject $sniffer service when implemented browserSingleton = new Browser(window, jqLite(window.document), jqLite(window.document.body), - XHR, $log); + XHR, $log, {}); browserSingleton.bind(); } return browserSingleton; diff --git a/src/Browser.js b/src/Browser.js index f25e8a20..8e8f511a 100644 --- a/src/Browser.js +++ b/src/Browser.js @@ -34,15 +34,16 @@ var XHR = window.XMLHttpRequest || function () { * @param {object} body jQuery wrapped document.body. * @param {function()} XHR XMLHttpRequest constructor. * @param {object} $log console.log or an object with the same interface. + * @param {object} $sniffer $sniffer service */ -function Browser(window, document, body, XHR, $log) { +function Browser(window, document, body, XHR, $log, $sniffer) { var self = this, rawDocument = document[0], location = window.location, + history = window.history, setTimeout = window.setTimeout, clearTimeout = window.clearTimeout, - pendingDeferIds = {}, - lastLocationUrl; + pendingDeferIds = {}; self.isMock = false; @@ -194,78 +195,103 @@ function Browser(window, document, body, XHR, $log) { // URL API ////////////////////////////////////////////////////////////// - /** - * @workInProgress - * @ngdoc method - * @name angular.service.$browser#setUrl - * @methodOf angular.service.$browser - * - * @param {string} url New url - * - * @description - * Sets browser's url - */ - self.setUrl = function(url) { - - var existingURL = lastLocationUrl; - if (!existingURL.match(/#/)) existingURL += '#'; - if (!url.match(/#/)) url += '#'; - if (existingURL != url) { - location.href = url; - } - }; + var lastBrowserUrl = location.href; /** * @workInProgress * @ngdoc method - * @name angular.service.$browser#getUrl + * @name angular.service.$browser#url * @methodOf angular.service.$browser * * @description - * Get current browser's url + * GETTER: + * Without any argument, this method just returns current value of location.href. + * + * SETTER: + * With at least one argument, this method sets url to new value. + * If html5 history api supported, pushState/replaceState is used, otherwise + * location.href/location.replace is used. + * Returns its own instance to allow chaining * - * @returns {string} Browser's url + * NOTE: this api is intended for use only by the $location service. Please use the + * {@link angular.service.$location $location service} to change url. + * + * @param {string} url New url (when used as setter) + * @param {boolean=} replace Should new url replace current history record ? */ - self.getUrl = function() { - return lastLocationUrl = location.href; + self.url = function(url, replace) { + // setter + if (url) { + lastBrowserUrl = url; + if ($sniffer.history) { + if (replace) history.replaceState(null, '', url); + else history.pushState(null, '', url); + } else { + if (replace) location.replace(url); + else location.href = url; + } + return self; + // getter + } else { + return location.href; + } }; + var urlChangeListeners = [], + urlChangeInit = false; + + function fireUrlChange() { + if (lastBrowserUrl == self.url()) return; + + lastBrowserUrl = self.url(); + forEach(urlChangeListeners, function(listener) { + listener(self.url()); + }); + } /** * @workInProgress * @ngdoc method - * @name angular.service.$browser#onHashChange + * @name angular.service.$browser#onUrlChange * @methodOf angular.service.$browser + * @TODO(vojta): refactor to use node's syntax for events * * @description - * Detects if browser support onhashchange events and register a listener otherwise registers - * $browser poller. The `listener` will then get called when the hash changes. + * Register callback function that will be called, when url changes. + * + * It's only called when the url is changed by outside of angular: + * - user types different url into address bar + * - user clicks on history (forward/back) button + * - user clicks on a link * - * The listener gets called with either HashChangeEvent object or simple object that also contains - * `oldURL` and `newURL` properties. + * It's not called when url is changed by $browser.url() method * - * Note: this api is intended for use only by the $location service. Please use the - * {@link angular.service.$location $location service} to monitor hash changes in angular apps. + * The listener gets called with new url as parameter. * - * @param {function(event)} listener Listener function to be called when url hash changes. - * @return {function()} Returns the registered listener fn - handy if the fn is anonymous. + * NOTE: this api is intended for use only by the $location service. Please use the + * {@link angular.service.$location $location service} to monitor url changes in angular apps. + * + * @param {function(string)} listener Listener function to be called when url changes. + * @return {function(string)} Returns the registered listener fn - handy if the fn is anonymous. */ - self.onHashChange = function(listener) { - // IE8 comp mode returns true, but doesn't support hashchange event - var dm = window.document.documentMode; - if ('onhashchange' in window && (isUndefined(dm) || dm >= 8)) { - jqLite(window).bind('hashchange', listener); - } else { - var lastBrowserUrl = self.getUrl(); - - self.addPollFn(function() { - if (lastBrowserUrl != self.getUrl()) { - listener(); - lastBrowserUrl = self.getUrl(); - } - }); + self.onUrlChange = function(callback) { + if (!urlChangeInit) { + // We listen on both (hashchange/popstate) when available, as some browsers (e.g. Opera) + // don't fire popstate when user change the address bar and don't fire hashchange when url + // changed by push/replaceState + + // html5 history api - popstate event + if ($sniffer.history) jqLite(window).bind('popstate', fireUrlChange); + // hashchange event + if ($sniffer.hashchange) jqLite(window).bind('hashchange', fireUrlChange); + // polling + else self.addPollFn(fireUrlChange); + + urlChangeInit = true; } - return listener; + + urlChangeListeners.push(callback); + return callback; }; ////////////////////////////////////////////////////////////// diff --git a/src/angular-mocks.js b/src/angular-mocks.js index 066ccea5..8410dc8c 100644 --- a/src/angular-mocks.js +++ b/src/angular-mocks.js @@ -89,19 +89,19 @@ function MockBrowser() { requests = []; this.isMock = true; - self.url = "http://server"; - self.lastUrl = self.url; // used by url polling fn + self.$$url = "http://server"; + self.$$lastUrl = self.$$url; // used by url polling fn self.pollFns = []; // register url polling fn - self.onHashChange = function(listener) { + self.onUrlChange = function(listener) { self.pollFns.push( function() { - if (self.lastUrl != self.url) { - self.lastUrl = self.url; - listener(); + if (self.$$lastUrl != self.$$url) { + self.$$lastUrl = self.$$url; + listener(self.$$url); } } ); @@ -326,12 +326,13 @@ MockBrowser.prototype = { hover: function(onHover) { }, - getUrl: function(){ - return this.url; - }, + url: function(url, replace) { + if (url) { + this.$$url = url; + return this; + } - setUrl: function(url){ - this.url = url; + return this.$$url; }, cookies: function(name, value) { diff --git a/src/service/location.js b/src/service/location.js index 6e646b8b..2f53f520 100644 --- a/src/service/location.js +++ b/src/service/location.js @@ -72,8 +72,8 @@ angularServiceInject("$location", function($browser) { var location = {update: update, updateHash: updateHash}; var lastLocation = {}; // last state since last update(). - $browser.onHashChange(bind(this, this.$apply, function() { //register - update($browser.getUrl()); + $browser.onUrlChange(bind(this, this.$apply, function() { //register + update($browser.url()); }))(); //initialize this.$watch(sync); @@ -120,7 +120,7 @@ angularServiceInject("$location", function($browser) { location.href = composeHref(location); } - $browser.setUrl(location.href); + $browser.url(location.href); copy(location, lastLocation); } |
