angular.js/src/services.js

192 lines
6.2 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-16 21:01:29 +00:00
var URL_MATCH = /^(file|ftp|http|https):\/\/(\w+:{0,1}\w*@)?([\w\.]*)(:([0-9]+))?([^\?#]+)(\?([^#]*))?(#(.*))?$/;
var HASH_MATCH = /^([^\?]*)?(\?([^\?]*))?$/;
2010-04-02 18:10:36 +00:00
var DEFAULT_PORTS = {'http': 80, 'https': 443, 'ftp':21};
2010-04-04 00:04:36 +00:00
angularService("$location", function(browser){
2010-04-16 21:01:29 +00:00
var scope = this, location = {parse:parseUrl, toString:toString};
var lastHash, lastUrl;
function parseUrl(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]);
2010-04-16 21:01:29 +00:00
location.hash = match[9] || '';
2010-04-09 23:20:15 +00:00
if (location.hash)
location.hash = location.hash.substr(1);
2010-04-16 21:01:29 +00:00
parseHash(location.hash);
2010-04-02 18:10:36 +00:00
}
2010-04-01 00:56:16 +00:00
}
2010-04-05 18:46:53 +00:00
}
2010-04-16 21:01:29 +00:00
function parseHash(hash) {
var match = HASH_MATCH.exec(hash);
location.hashPath = match[1] || '';
location.hashSearch = parseKeyValue(match[3]);
lastHash = hash;
}
2010-04-05 18:46:53 +00:00
function toString() {
2010-04-09 23:20:15 +00:00
if (lastHash === location.hash) {
var hashKeyValue = toKeyValue(location.hashSearch),
hash = (location.hashPath ? location.hashPath : '') + (hashKeyValue ? '?' + hashKeyValue : ''),
url = location.href.split('#')[0] + '#' + (hash ? hash : '');
2010-04-16 21:01:29 +00:00
if (url !== location.href) parseUrl(url);
2010-04-09 23:20:15 +00:00
return url;
} else {
2010-04-16 21:01:29 +00:00
parseUrl(location.href.split('#')[0] + '#' + location.hash);
2010-04-09 23:20:15 +00:00
return toString();
}
2010-04-04 00:04:36 +00:00
}
browser.watchUrl(function(url){
2010-04-16 21:01:29 +00:00
parseUrl(url);
2010-04-05 18:46:53 +00:00
scope.$root.$eval();
2010-04-01 00:56:16 +00:00
});
2010-04-16 21:01:29 +00:00
parseUrl(browser.getUrl());
this.$onEval(PRIORITY_FIRST, function(){
if (location.hash != lastHash) {
parseHash(location.hash);
}
});
2010-04-01 00:56:16 +00:00
this.$onEval(PRIORITY_LAST, function(){
2010-04-13 21:25:12 +00:00
var url = toString();
2010-04-16 21:01:29 +00:00
if (lastUrl != url) {
2010-04-13 21:25:12 +00:00
browser.setUrl(url);
2010-04-16 21:01:29 +00:00
lastUrl = url;
2010-04-13 21:25:12 +00:00
}
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']});
2010-04-06 03:53:33 +00:00
angularService("$hover", function(browser) {
var tooltip, self = this, error, width = 300, arrowWidth = 10;
browser.hover(function(element, show){
if (show && (error = element.attr(NG_EXCEPTION) || element.attr(NG_VALIDATION_ERROR))) {
2010-04-06 03:53:33 +00:00
if (!tooltip) {
tooltip = {
callout: jqLite('<div id="ng-callout"></div>'),
arrow: jqLite('<div></div>'),
title: jqLite('<div class="ng-title"></div>'),
content: jqLite('<div class="ng-content"></div>')
};
tooltip.callout.append(tooltip.arrow);
tooltip.callout.append(tooltip.title);
tooltip.callout.append(tooltip.content);
self.$browser.body.append(tooltip.callout);
}
var docRect = self.$browser.body[0].getBoundingClientRect(),
elementRect = element[0].getBoundingClientRect(),
leftSpace = docRect.right - elementRect.right - arrowWidth;
tooltip.title.text(element.hasClass("ng-exception") ? "EXCEPTION:" : "Validation error...");
tooltip.content.text(error);
if (leftSpace < width) {
tooltip.arrow.addClass('ng-arrow-right');
tooltip.arrow.css({left: (width + 1)+'px'});
tooltip.callout.css({
2010-04-09 23:20:15 +00:00
position: 'fixed',
2010-04-06 03:53:33 +00:00
left: (elementRect.left - arrowWidth - width - 4) + "px",
top: (elementRect.top - 3) + "px",
width: width + "px"
});
} else {
tooltip.arrow.addClass('ng-arrow-left');
tooltip.callout.css({
2010-04-09 23:20:15 +00:00
position: 'fixed',
2010-04-06 03:53:33 +00:00
left: (elementRect.right + arrowWidth) + "px",
top: (elementRect.top - 3) + "px",
width: width + "px"
});
}
2010-04-22 22:50:20 +00:00
} else if (tooltip && false) {
2010-04-06 03:53:33 +00:00
tooltip.callout.remove();
tooltip = null;
}
});
}, {inject:['$browser']});
2010-04-07 21:13:10 +00:00
angularService("$invalidWidgets", function(){
var invalidWidgets = [];
invalidWidgets.markValid = function(element){
var index = indexOf(invalidWidgets, element);
if (index != -1)
invalidWidgets.splice(index, 1);
};
invalidWidgets.markInvalid = function(element){
var index = indexOf(invalidWidgets, element);
if (index === -1)
invalidWidgets.push(element);
};
2010-04-08 22:05:05 +00:00
invalidWidgets.visible = function() {
var count = 0;
foreach(invalidWidgets, function(widget){
count = count + (isVisible(widget) ? 1 : 0);
});
return count;
};
2010-04-13 02:05:39 +00:00
invalidWidgets.clearOrphans = function() {
for(var i = 0; i < invalidWidgets.length;) {
var widget = invalidWidgets[i];
if (isOrphan(widget[0])) {
invalidWidgets.splice(i, 1);
} else {
i++;
}
}
};
function isOrphan(widget) {
if (widget == window.document) return false;
var parent = widget.parentNode;
return !parent || isOrphan(parent);
}
2010-04-07 21:13:10 +00:00
return invalidWidgets;
});
2010-04-15 21:17:33 +00:00
angularService('$route', function(location, params){
var routes = {},
onChange = [],
matcher = angularWidget('NG:SWITCH').route,
2010-04-16 21:01:29 +00:00
parentScope = this,
2010-04-15 21:17:33 +00:00
$route = {
routes: routes,
onChange: bind(onChange, onChange.push),
when:function (path, params){
if (angular.isUndefined(path)) return routes;
var route = routes[path];
if (!route) route = routes[path] = {};
if (params) angular.extend(route, params);
2010-04-16 21:01:29 +00:00
if (matcher(location.hashPath, path)) updateRoute();
2010-04-15 21:17:33 +00:00
return route;
}
};
2010-04-16 21:01:29 +00:00
function updateRoute(){
var childScope;
2010-04-15 21:17:33 +00:00
$route.current = null;
angular.foreach(routes, function(routeParams, route) {
if (!childScope) {
var pathParams = matcher(location.hashPath, route);
if (pathParams) {
childScope = angular.scope(parentScope);
$route.current = angular.extend({}, routeParams, {
scope: childScope,
params: angular.extend({}, location.hashSearch, pathParams)
});
}
}
});
angular.foreach(onChange, parentScope.$tryEval);
if (childScope) {
childScope.$become($route.current.controller);
parentScope.$tryEval(childScope.init);
}
2010-04-16 21:01:29 +00:00
}
this.$watch(function(){return location.hash;}, updateRoute);
2010-04-15 21:17:33 +00:00
return $route;
}, {inject: ['$location']});