angular.js/src/services.js

56 lines
1.8 KiB
JavaScript
Raw Normal View History

2010-04-01 00:56:16 +00:00
angularService("$window", bind(window, identity, window));
2010-04-04 00:04:36 +00:00
angularService("$document", function(window){
return jqLite(window.document);
}, {inject:['$window']});
2010-04-01 00:56:16 +00:00
2010-04-02 18:10:36 +00:00
var URL_MATCH = /^(file|ftp|http|https):\/\/(\w+:{0,1}\w*@)?([\w\.]*)(:([0-9]+))?([^\?#]+)(\?([^#]*))?((#([^\?]*))?(\?([^\?]*))?)$/;
var DEFAULT_PORTS = {'http': 80, 'https': 443, 'ftp':21};
2010-04-04 00:04:36 +00:00
angularService("$location", function(browser){
2010-04-01 00:56:16 +00:00
var scope = this;
2010-04-01 21:10:28 +00:00
function location(url){
2010-04-01 00:56:16 +00:00
if (isDefined(url)) {
2010-04-01 21:10:28 +00:00
var match = URL_MATCH.exec(url);
2010-04-02 18:10:36 +00:00
if (match) {
location.href = url;
location.protocol = match[1];
location.host = match[3] || '';
location.port = match[5] || DEFAULT_PORTS[location.href] || null;
location.path = match[6];
location.search = parseKeyValue(match[8]);
location.hash = match[9];
if (location.hash) location.hash = location.hash.substr(1);
location.hashPath = match[11] || '';
location.hashSearch = parseKeyValue(match[13]);
}
2010-04-01 00:56:16 +00:00
}
2010-04-02 18:10:36 +00:00
var hashKeyValue = toKeyValue(location.hashSearch);
var hash = (location.hashPath ? location.hashPath : '') +
2010-04-02 18:10:36 +00:00
(hashKeyValue ? '?' + hashKeyValue : '');
return location.href.split('#')[0] + '#' + (hash ? hash : '');
2010-04-04 00:04:36 +00:00
}
browser.watchUrl(function(url){
2010-04-01 21:10:28 +00:00
location(url);
2010-04-01 00:56:16 +00:00
});
2010-04-04 00:04:36 +00:00
location(browser.getUrl());
2010-04-01 00:56:16 +00:00
this.$onEval(PRIORITY_LAST, function(){
2010-04-02 18:10:36 +00:00
var href = location();
if (href != location.href) {
2010-04-04 00:04:36 +00:00
browser.setUrl(href);
2010-04-02 18:10:36 +00:00
location.href = href;
}
2010-04-01 00:56:16 +00:00
});
2010-04-01 21:10:28 +00:00
return location;
2010-04-04 00:04:36 +00:00
}, {inject: ['$browser']});
if (!angularService['$browser']) {
var browserSingleton;
angularService('$browser', function browserFactory(){
if (!browserSingleton) {
browserSingleton = new Browser(window.location);
browserSingleton.startUrlWatcher();
}
return browserSingleton;
});
}