work on $location and autobind

This commit is contained in:
Misko Hevery 2010-04-01 14:10:28 -07:00
parent 11a6431f89
commit 85f13d602e
8 changed files with 68 additions and 54 deletions

View file

@ -2,9 +2,9 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <head>
<link rel="stylesheet" type="text/css" href="style.css"></link> <link rel="stylesheet" type="text/css" href="style.css"></link>
<script type="text/javascript" src="../src/angular-bootstrap.js#autobind&rootScope=$view"></script> <script type="text/javascript" src="../src/angular-bootstrap.js#autobind"></script>
</head> </head>
<body> <body ng-init="$window.$scope = $root">
<table> <table>
<tr> <tr>
<th>Description</th> <th>Description</th>

View file

@ -45,7 +45,7 @@ UrlWatcher.prototype = {
} }
}; };
////////////////////////////////////
if (typeof document.getAttribute == 'undefined') if (typeof document.getAttribute == 'undefined')
document.getAttribute = function() {}; document.getAttribute = function() {};
@ -386,3 +386,20 @@ function compile(element, config) {
} }
///////////////////////////////////////////////// /////////////////////////////////////////////////
function parseKeyValue(keyValue) {
var obj = {}, key_value, key;
foreach((keyValue || "").split('&'), function(keyValue){
if (keyValue) {
key_value = keyValue.split('=');
key = decodeURIComponent(key_value[0]);
obj[key] = key_value[1] ? decodeURIComponent(key_value[1]) : true;
}
});
return obj;
}
function angularInit(config){
if (config.autobind) {
compile(window.document, config).$init();
}
}

View file

@ -22,23 +22,16 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
(function(previousOnLoad){ (function(previousOnLoad){
var filename = /(.*)\/angular-(.*).js(#(.*))?/; var filename = /(.*)\/angular-(.*).js(#(.*))?/,
var scripts = document.getElementsByTagName("SCRIPT"); scripts = document.getElementsByTagName("SCRIPT"),
var serverPath; serverPath,
var config = {}; config,
match;
for(var j = 0; j < scripts.length; j++) { for(var j = 0; j < scripts.length; j++) {
var match = (scripts[j].src || "").match(filename); match = (scripts[j].src || "").match(filename);
if (match) { if (match) {
serverPath = match[1]; serverPath = match[1];
parseConfig(match[4]); config = match[4];
}
}
function parseConfig(args) {
var keyValues = args.split('&'), keyValue, i = 0;
for (; i < keyValues.length; i++) {
keyValue = keyValues[i].split('=');
config[keyValue[0]] = keyValue[1] || true;
} }
} }
@ -53,7 +46,6 @@
addScript("/jqlite.js"); addScript("/jqlite.js");
addScript("/Parser.js"); addScript("/Parser.js");
addScript("/Resource.js"); addScript("/Resource.js");
addScript("/URLWatcher.js");
// Extension points // Extension points
addScript("/apis.js"); addScript("/apis.js");
@ -63,17 +55,14 @@
addScript("/directives.js"); addScript("/directives.js");
addScript("/markups.js"); addScript("/markups.js");
addScript("/widgets.js"); addScript("/widgets.js");
addScript("/services.js");
if (config.autobind) { window.onload = function(){
window.onload = function(){ try {
try { if (previousOnLoad) previousOnLoad();
if (previousOnLoad) previousOnLoad(); } catch(e) {}
} catch(e) {} angularInit(parseKeyValue(config));
var scope = angular.compile(window.document, config); };
if (config.rootScope) window[config.rootScope] = scope;
scope.$init();
};
}
})(window.onload); })(window.onload);

View file

@ -51,6 +51,7 @@ angularTextMarkup('{{}}', function(text, textNode, parentElement) {
} }
}); });
// TODO: this should be widget not a markup
angularTextMarkup('OPTION', function(text, textNode, parentElement){ angularTextMarkup('OPTION', function(text, textNode, parentElement){
if (parentElement[0].nodeName == "OPTION") { if (parentElement[0].nodeName == "OPTION") {
var select = document.createElement('select'); var select = document.createElement('select');

View file

@ -1,34 +1,34 @@
angularService("$window", bind(window, identity, window)); angularService("$window", bind(window, identity, window));
angularService("$anchor", function(){ var URL_MATCH = /^(file|ftp|http|https):\/\/(\w+:{0,1}\w*@)?([\w\.]+)(:([0-9]+))?([^\?#]+)?(\?([^#]*))((#([^\?]*))(\?([^\?]*))?)$/;
angularService("$location", function(){
var scope = this; var scope = this;
function anchor(url){ function location(url){
if (isDefined(url)) { if (isDefined(url)) {
if (url.charAt(0) == '#') url = url.substr(1); var match = URL_MATCH.exec(url);
var pathQuery = url.split('?'); dump(match);
anchor.path = decodeURIComponent(pathQuery[0]); location.href = url;
anchor.param = {}; location.protocol = match[1];
foreach((pathQuery[1] || "").split('&'), function(keyValue){ location.host = match[3];
if (keyValue) { location.port = match[5];
var parts = keyValue.split('='); location.path = match[6];
var key = decodeURIComponent(parts[0]); location.search = parseKeyValue(match[8]);
var value = parts[1]; location.hash = match[9];
if (!value) value = true; location.hashPath = match[11];
anchor.param[key] = decodeURIComponent(value); location.hashSearch = parseKeyValue(match[13]);
} foreach(location, dump);
});
} }
var params = []; var params = [];
foreach(anchor.param, function(value, key){ foreach(location.param, function(value, key){
params.push(encodeURIComponent(key) + '=' + encodeURIComponent(value)); params.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
}); });
return (anchor.path ? anchor.path : '') + (params.length ? '?' + params.join('&') : ''); return (location.path ? location.path : '') + (params.length ? '?' + params.join('&') : '');
}; };
this.$config.location.watch(function(url){ this.$config.location.watch(function(url){
anchor(url); location(url);
}); });
this.$onEval(PRIORITY_LAST, function(){ this.$onEval(PRIORITY_LAST, function(){
scope.$config.location.set(anchor()); scope.$config.location.set(location());
}); });
return anchor; return location;
}); });

View file

@ -721,13 +721,13 @@ BinderTest.prototype.testItShouldSelectTheCorrectRadioBox = function() {
var male = jqLite(c.node[0].childNodes[1]); var male = jqLite(c.node[0].childNodes[1]);
female.click(); female.click();
assertEquals("female", c.scope.$get("sex")); assertEquals("female", c.scope.sex);
assertEquals(true, female[0].checked); assertEquals(true, female[0].checked);
assertEquals(false, male[0].checked); assertEquals(false, male[0].checked);
assertEquals("female", female.val()); assertEquals("female", female.val());
male.click(); male.click();
assertEquals("male", c.scope.$get("sex")); assertEquals("male", c.scope.sex);
assertEquals(false, female[0].checked); assertEquals(false, female[0].checked);
assertEquals(true, male[0].checked); assertEquals(true, male[0].checked);
assertEquals("male", male.val()); assertEquals("male", male.val());

View file

@ -13,10 +13,17 @@ describe("services", function(){
expect(scope.$window).toEqual(window); expect(scope.$window).toEqual(window);
}); });
it("should inject $anchor", function(){ it("should inject $location", function(){
scope.$anchor('#path?key=value'); scope.$location('http://host:1234/p/a/t/h?query=value#path?key=value');
expect(scope.$anchor.path).toEqual("path"); expect(scope.$location.href).toEqual("http://host:123/p/a/t/h?query=value#path?key=value");
expect(scope.$anchor.param).toEqual({key:'value'}); expect(scope.$location.protocol).toEqual("http");
expect(scope.$location.host).toEqual("host");
expect(scope.$location.port).toEqual("1234");
expect(scope.$location.path).toEqual("/p/a/t/h");
expect(scope.$location.search).toEqual({query:'value'});
expect(scope.$location.hash).toEqual('path?key=value');
expect(scope.$location.hashPath).toEqual('path');
expect(scope.$location.hashSearch).toEqual({key:'value'});
scope.$anchor.path = 'page=http://path'; scope.$anchor.path = 'page=http://path';
scope.$anchor.param = {k:'a=b'}; scope.$anchor.param = {k:'a=b'};