aboutsummaryrefslogtreecommitdiffstats
path: root/src/services.js
diff options
context:
space:
mode:
authorAndres Ornelas2010-08-04 11:45:42 -0700
committerAndres Ornelas2010-08-04 11:45:42 -0700
commitec12285c9d213a50b86b2ff8d968686acd6d1693 (patch)
tree94c0792946bfa461c4fab525b3758b06c6da9f9f /src/services.js
parentef88eb9a71ee7666029c4fb5eb731ce2e986cecc (diff)
parent89245f3a527415a80d46b37054b558454c314532 (diff)
downloadangular.js-ec12285c9d213a50b86b2ff8d968686acd6d1693.tar.bz2
Merge branch 'master' of github.com:angular/angular.js into future
Diffstat (limited to 'src/services.js')
-rw-r--r--src/services.js110
1 files changed, 68 insertions, 42 deletions
diff --git a/src/services.js b/src/services.js
index 106f8954..8df23564 100644
--- a/src/services.js
+++ b/src/services.js
@@ -7,61 +7,87 @@ var URL_MATCH = /^(file|ftp|http|https):\/\/(\w+:{0,1}\w*@)?([\w\.-]*)(:([0-9]+)
var HASH_MATCH = /^([^\?]*)?(\?([^\?]*))?$/;
var DEFAULT_PORTS = {'http': 80, 'https': 443, 'ftp':21};
angularService("$location", function(browser){
- var scope = this, location = {parse:parseUrl, toString:toString};
- var lastHash, lastUrl;
+ var scope = this,
+ location = {parse:parseUrl, toString:toString, update:update},
+ lastLocation = {};
+
+ browser.watchUrl(function(url){
+ update(url);
+ scope.$root.$eval();
+ });
+ this.$onEval(PRIORITY_FIRST, update);
+ this.$onEval(PRIORITY_LAST, update);
+ update(browser.getUrl());
+ return location;
+
+ function update(href){
+ if (href) {
+ parseUrl(href);
+ } else {
+ href = check('href') || checkProtocol();
+ var hash = check('hash');
+ if (isUndefined(hash)) hash = checkHashPathSearch();
+ if (isDefined(hash)) {
+ href = (href || location.href).split('#')[0];
+ href+= '#' + hash;
+ }
+ if (isDefined(href)) {
+ parseUrl(href);
+ browser.setUrl(href);
+ }
+ }
+ }
+
+ function check(param) {
+ return lastLocation[param] == location[param] ? undefined : location[param];
+ }
+
+ function checkProtocol(){
+ if (lastLocation.protocol === location.protocol &&
+ lastLocation.host === location.host &&
+ lastLocation.port === location.port &&
+ lastLocation.path === location.path &&
+ equals(lastLocation.search, location.search))
+ return undefined;
+ var url = toKeyValue(location.search);
+ var port = (location.port == DEFAULT_PORTS[location.protocol] ? null : location.port);
+ return location.protocol + '://' + location.host +
+ (port ? ':' + port : '') + location.path +
+ (url ? '?' + url : '');
+ }
+
+ function checkHashPathSearch(){
+ if (lastLocation.hashPath === location.hashPath &&
+ equals(lastLocation.hashSearch, location.hashSearch) )
+ return undefined;
+ var url = toKeyValue(location.hashSearch);
+ return escape(location.hashPath) + (url ? '?' + url : '');
+ }
+
function parseUrl(url){
if (isDefined(url)) {
var match = URL_MATCH.exec(url);
if (match) {
- location.href = url;
+ location.href = url.replace('#$', '');
location.protocol = match[1];
location.host = match[3] || '';
- location.port = match[5] || DEFAULT_PORTS[location.href] || null;
+ location.port = match[5] || DEFAULT_PORTS[location.protocol] || null;
location.path = match[6];
location.search = parseKeyValue(match[8]);
- location.hash = match[9] || '';
- if (location.hash)
- location.hash = location.hash.substr(1);
- parseHash(location.hash);
+ location.hash = match[10] || '';
+ match = HASH_MATCH.exec(location.hash);
+ location.hashPath = unescape(match[1] || '');
+ location.hashSearch = parseKeyValue(match[3]);
+
+ copy(location, lastLocation);
}
}
}
- function parseHash(hash) {
- var match = HASH_MATCH.exec(hash);
- location.hashPath = match[1] || '';
- location.hashSearch = parseKeyValue(match[3]);
- lastHash = hash;
- }
+
function toString() {
- if (lastHash === location.hash) {
- var hashKeyValue = toKeyValue(location.hashSearch),
- hash = (location.hashPath ? location.hashPath : '') + (hashKeyValue ? '?' + hashKeyValue : ''),
- url = location.href.split('#')[0] + '#' + (hash ? hash : '');
- if (url !== location.href) parseUrl(url);
- return url;
- } else {
- parseUrl(location.href.split('#')[0] + '#' + location.hash);
- return toString();
- }
+ update();
+ return location.href;
}
- browser.watchUrl(function(url){
- parseUrl(url);
- scope.$root.$eval();
- });
- parseUrl(browser.getUrl());
- this.$onEval(PRIORITY_FIRST, function(){
- if (location.hash != lastHash) {
- parseHash(location.hash);
- }
- });
- this.$onEval(PRIORITY_LAST, function(){
- var url = toString();
- if (lastUrl != url) {
- browser.setUrl(url);
- lastUrl = url;
- }
- });
- return location;
}, {inject: ['$browser']});
angularService("$log", function($window){