diff options
Diffstat (limited to 'src/services.js')
| -rw-r--r-- | src/services.js | 147 |
1 files changed, 90 insertions, 57 deletions
diff --git a/src/services.js b/src/services.js index ed6f73ad..a5158149 100644 --- a/src/services.js +++ b/src/services.js @@ -1,82 +1,115 @@ +var URL_MATCH = /^(file|ftp|http|https):\/\/(\w+:{0,1}\w*@)?([\w\.-]*)(:([0-9]+))?([^\?#]+)(\?([^#]*))?(#(.*))?$/, + HASH_MATCH = /^([^\?]*)?(\?([^\?]*))?$/, + DEFAULT_PORTS = {'http': 80, 'https': 443, 'ftp':21}; + angularService("$window", bind(window, identity, window)); angularService("$document", function(window){ return jqLite(window.document); }, {inject:['$window']}); -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){ - var console = $window.console, - log = console && console.log || noop; + var console = $window.console || {log: noop, warn: noop, info: noop, error: noop}, + log = console.log || noop; return { - log: log, - warn: console && console.warn || log, - info: console && console.info || log, - error: console && console.error || log + log: bind(console, log), + warn: bind(console, console.warn || log), + info: bind(console, console.info || log), + error: bind(console, console.error || log) }; }, {inject:['$window']}); -angularService("$hover", function(browser) { - var tooltip, self = this, error, width = 300, arrowWidth = 10; +angularService('$exceptionHandler', function($log){ + return function(e) { + $log.error(e); + }; +}, {inject:['$log']}); + +angularService("$hover", function(browser, document) { + var tooltip, self = this, error, width = 300, arrowWidth = 10, body = jqLite(document[0].body);; browser.hover(function(element, show){ if (show && (error = element.attr(NG_EXCEPTION) || element.attr(NG_VALIDATION_ERROR))) { if (!tooltip) { @@ -89,9 +122,9 @@ angularService("$hover", function(browser) { tooltip.callout.append(tooltip.arrow); tooltip.callout.append(tooltip.title); tooltip.callout.append(tooltip.content); - self.$browser.body.append(tooltip.callout); + body.append(tooltip.callout); } - var docRect = self.$browser.body[0].getBoundingClientRect(), + var docRect = body[0].getBoundingClientRect(), elementRect = element[0].getBoundingClientRect(), leftSpace = docRect.right - elementRect.right - arrowWidth; tooltip.title.text(element.hasClass("ng-exception") ? "EXCEPTION:" : "Validation error..."); @@ -119,7 +152,7 @@ angularService("$hover", function(browser) { tooltip = null; } }); -}, {inject:['$browser']}); +}, {inject:['$browser', '$document']}); angularService("$invalidWidgets", function(){ var invalidWidgets = []; @@ -181,7 +214,7 @@ function switchRouteMatcher(on, when, dstName) { return match ? dst : null; } -angularService('$route', function(location, params){ +angularService('$route', function(location){ var routes = {}, onChange = [], matcher = switchRouteMatcher, |
