diff options
Diffstat (limited to 'src/service/location.js')
| -rw-r--r-- | src/service/location.js | 151 |
1 files changed, 78 insertions, 73 deletions
diff --git a/src/service/location.js b/src/service/location.js index c9b76122..7cc5c273 100644 --- a/src/service/location.js +++ b/src/service/location.js @@ -419,94 +419,99 @@ function locationGetterSetter(property, preprocess) { * * For more information see {@link guide/dev_guide.services.$location Developer Guide: Angular Services: Using $location} */ -angularServiceInject('$location', function($rootScope, $browser, $sniffer, $locationConfig, $document) { - var currentUrl, - basePath = $browser.baseHref() || '/', - pathPrefix = pathPrefixFromBase(basePath), - hashPrefix = $locationConfig.hashPrefix || '', - initUrl = $browser.url(); - - if ($locationConfig.html5Mode) { - if ($sniffer.history) { - currentUrl = new LocationUrl(convertToHtml5Url(initUrl, basePath, hashPrefix), pathPrefix); - } else { - currentUrl = new LocationHashbangUrl(convertToHashbangUrl(initUrl, basePath, hashPrefix), - hashPrefix); - } - - // link rewriting - var u = currentUrl, - absUrlPrefix = composeProtocolHostPort(u.protocol(), u.host(), u.port()) + pathPrefix; +function $LocationProvider(){ + this.$get = ['$rootScope', '$browser', '$sniffer', '$locationConfig', '$document', + function( $rootScope, $browser, $sniffer, $locationConfig, $document) { + var currentUrl, + basePath = $browser.baseHref() || '/', + pathPrefix = pathPrefixFromBase(basePath), + hashPrefix = $locationConfig.hashPrefix || '', + initUrl = $browser.url(); - $document.bind('click', function(event) { - // TODO(vojta): rewrite link when opening in new tab/window (in legacy browser) - // currently we open nice url link and redirect then + if ($locationConfig.html5Mode) { + if ($sniffer.history) { + currentUrl = new LocationUrl(convertToHtml5Url(initUrl, basePath, hashPrefix), pathPrefix); + } else { + currentUrl = new LocationHashbangUrl(convertToHashbangUrl(initUrl, basePath, hashPrefix), + hashPrefix); + } - if (event.ctrlKey || event.metaKey || event.which == 2) return; + // link rewriting + var u = currentUrl, + absUrlPrefix = composeProtocolHostPort(u.protocol(), u.host(), u.port()) + pathPrefix; - var elm = jqLite(event.target); + $document.bind('click', function(event) { + // TODO(vojta): rewrite link when opening in new tab/window (in legacy browser) + // currently we open nice url link and redirect then - // traverse the DOM up to find first A tag - while (elm.length && lowercase(elm[0].nodeName) !== 'a') { - elm = elm.parent(); - } + if (event.ctrlKey || event.metaKey || event.which == 2) return; - var href = elm.attr('href'); - if (!href || isDefined(elm.attr('ng:ext-link')) || elm.attr('target')) return; + var elm = jqLite(event.target); - // remove same domain from full url links (IE7 always returns full hrefs) - href = href.replace(absUrlPrefix, ''); + // traverse the DOM up to find first A tag + while (elm.length && lowercase(elm[0].nodeName) !== 'a') { + elm = elm.parent(); + } - // link to different domain (or base path) - if (href.substr(0, 4) == 'http') return; + var href = elm.attr('href'); + if (!href || isDefined(elm.attr('ng:ext-link')) || elm.attr('target')) return; - // remove pathPrefix from absolute links - href = href.indexOf(pathPrefix) === 0 ? href.substr(pathPrefix.length) : href; + // remove same domain from full url links (IE7 always returns full hrefs) + href = href.replace(absUrlPrefix, ''); - currentUrl.url(href); - $rootScope.$apply(); - event.preventDefault(); - // hack to work around FF6 bug 684208 when scenario runner clicks on links - window.angular['ff-684208-preventDefault'] = true; - }); - } else { - currentUrl = new LocationHashbangUrl(initUrl, hashPrefix); - } + // link to different domain (or base path) + if (href.substr(0, 4) == 'http') return; - // rewrite hashbang url <> html5 url - if (currentUrl.absUrl() != initUrl) { - $browser.url(currentUrl.absUrl(), true); - } + // remove pathPrefix from absolute links + href = href.indexOf(pathPrefix) === 0 ? href.substr(pathPrefix.length) : href; - // update $location when $browser url changes - $browser.onUrlChange(function(newUrl) { - if (currentUrl.absUrl() != newUrl) { - currentUrl.$$parse(newUrl); - $rootScope.$apply(); - } - }); - - // update browser - var changeCounter = 0; - $rootScope.$watch(function() { - if ($browser.url() != currentUrl.absUrl()) { - changeCounter++; - $rootScope.$evalAsync(function() { - $browser.url(currentUrl.absUrl(), currentUrl.$$replace); - currentUrl.$$replace = false; + currentUrl.url(href); + $rootScope.$apply(); + event.preventDefault(); + // hack to work around FF6 bug 684208 when scenario runner clicks on links + window.angular['ff-684208-preventDefault'] = true; }); + } else { + currentUrl = new LocationHashbangUrl(initUrl, hashPrefix); } - return changeCounter; - }); + // rewrite hashbang url <> html5 url + if (currentUrl.absUrl() != initUrl) { + $browser.url(currentUrl.absUrl(), true); + } - return currentUrl; -}, ['$rootScope', '$browser', '$sniffer', '$locationConfig', '$document']); + // update $location when $browser url changes + $browser.onUrlChange(function(newUrl) { + if (currentUrl.absUrl() != newUrl) { + currentUrl.$$parse(newUrl); + $rootScope.$apply(); + } + }); + // update browser + var changeCounter = 0; + $rootScope.$watch(function() { + if ($browser.url() != currentUrl.absUrl()) { + changeCounter++; + $rootScope.$evalAsync(function() { + $browser.url(currentUrl.absUrl(), currentUrl.$$replace); + currentUrl.$$replace = false; + }); + } -angular.service('$locationConfig', function() { - return { - html5Mode: false, - hashPrefix: '' + return changeCounter; + }); + + return currentUrl; +}]; +} + +//TODO(misko): refactor to service +function $LocationConfigProvider(){ + this.$get = function() { + return { + html5Mode: false, + hashPrefix: '' + }; }; -}); +} |
