Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 85f13d6

Browse files
committed
work on $location and autobind
1 parent 11a6431 commit 85f13d6

File tree

8 files changed

+68
-54
lines changed

8 files changed

+68
-54
lines changed

scenario/widgets.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
33
<head>
44
<link rel="stylesheet" type="text/css" href="style.css"></link>
5-
<script type="text/javascript" src="../src/angular-bootstrap.js#autobind&rootScope=$view"></script>
5+
<script type="text/javascript" src="../src/angular-bootstrap.js#autobind"></script>
66
</head>
7-
<body>
7+
<body ng-init="$window.$scope = $root">
88
<table>
99
<tr>
1010
<th>Description</th>

src/Angular.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ UrlWatcher.prototype = {
4545
}
4646
};
4747

48-
48+
////////////////////////////////////
4949

5050
if (typeof document.getAttribute == 'undefined')
5151
document.getAttribute = function() {};
@@ -386,3 +386,20 @@ function compile(element, config) {
386386
}
387387
/////////////////////////////////////////////////
388388

389+
function parseKeyValue(keyValue) {
390+
var obj = {}, key_value, key;
391+
foreach((keyValue || "").split('&'), function(keyValue){
392+
if (keyValue) {
393+
key_value = keyValue.split('=');
394+
key = decodeURIComponent(key_value[0]);
395+
obj[key] = key_value[1] ? decodeURIComponent(key_value[1]) : true;
396+
}
397+
});
398+
return obj;
399+
}
400+
401+
function angularInit(config){
402+
if (config.autobind) {
403+
compile(window.document, config).$init();
404+
}
405+
}

src/angular-bootstrap.js

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,16 @@
2222
* THE SOFTWARE.
2323
*/
2424
(function(previousOnLoad){
25-
var filename = /(.*)\/angular-(.*).js(#(.*))?/;
26-
var scripts = document.getElementsByTagName("SCRIPT");
27-
var serverPath;
28-
var config = {};
25+
var filename = /(.*)\/angular-(.*).js(#(.*))?/,
26+
scripts = document.getElementsByTagName("SCRIPT"),
27+
serverPath,
28+
config,
29+
match;
2930
for(var j = 0; j < scripts.length; j++) {
30-
var match = (scripts[j].src || "").match(filename);
31+
match = (scripts[j].src || "").match(filename);
3132
if (match) {
3233
serverPath = match[1];
33-
parseConfig(match[4]);
34-
}
35-
}
36-
37-
function parseConfig(args) {
38-
var keyValues = args.split('&'), keyValue, i = 0;
39-
for (; i < keyValues.length; i++) {
40-
keyValue = keyValues[i].split('=');
41-
config[keyValue[0]] = keyValue[1] || true;
34+
config = match[4];
4235
}
4336
}
4437

@@ -53,7 +46,6 @@
5346
addScript("/jqlite.js");
5447
addScript("/Parser.js");
5548
addScript("/Resource.js");
56-
addScript("/URLWatcher.js");
5749

5850
// Extension points
5951
addScript("/apis.js");
@@ -63,17 +55,14 @@
6355
addScript("/directives.js");
6456
addScript("/markups.js");
6557
addScript("/widgets.js");
58+
addScript("/services.js");
6659

67-
if (config.autobind) {
68-
window.onload = function(){
69-
try {
70-
if (previousOnLoad) previousOnLoad();
71-
} catch(e) {}
72-
var scope = angular.compile(window.document, config);
73-
if (config.rootScope) window[config.rootScope] = scope;
74-
scope.$init();
75-
};
76-
}
60+
window.onload = function(){
61+
try {
62+
if (previousOnLoad) previousOnLoad();
63+
} catch(e) {}
64+
angularInit(parseKeyValue(config));
65+
};
7766

7867
})(window.onload);
7968

File renamed without changes.

src/markups.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ angularTextMarkup('{{}}', function(text, textNode, parentElement) {
5151
}
5252
});
5353

54+
// TODO: this should be widget not a markup
5455
angularTextMarkup('OPTION', function(text, textNode, parentElement){
5556
if (parentElement[0].nodeName == "OPTION") {
5657
var select = document.createElement('select');

src/services.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
angularService("$window", bind(window, identity, window));
22

3-
angularService("$anchor", function(){
3+
var URL_MATCH = /^(file|ftp|http|https):\/\/(\w+:{0,1}\w*@)?([\w\.]+)(:([0-9]+))?([^\?#]+)?(\?([^#]*))((#([^\?]*))(\?([^\?]*))?)$/;
4+
angularService("$location", function(){
45
var scope = this;
5-
function anchor(url){
6+
function location(url){
67
if (isDefined(url)) {
7-
if (url.charAt(0) == '#') url = url.substr(1);
8-
var pathQuery = url.split('?');
9-
anchor.path = decodeURIComponent(pathQuery[0]);
10-
anchor.param = {};
11-
foreach((pathQuery[1] || "").split('&'), function(keyValue){
12-
if (keyValue) {
13-
var parts = keyValue.split('=');
14-
var key = decodeURIComponent(parts[0]);
15-
var value = parts[1];
16-
if (!value) value = true;
17-
anchor.param[key] = decodeURIComponent(value);
18-
}
19-
});
8+
var match = URL_MATCH.exec(url);
9+
dump(match);
10+
location.href = url;
11+
location.protocol = match[1];
12+
location.host = match[3];
13+
location.port = match[5];
14+
location.path = match[6];
15+
location.search = parseKeyValue(match[8]);
16+
location.hash = match[9];
17+
location.hashPath = match[11];
18+
location.hashSearch = parseKeyValue(match[13]);
19+
foreach(location, dump);
2020
}
2121
var params = [];
22-
foreach(anchor.param, function(value, key){
22+
foreach(location.param, function(value, key){
2323
params.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
2424
});
25-
return (anchor.path ? anchor.path : '') + (params.length ? '?' + params.join('&') : '');
25+
return (location.path ? location.path : '') + (params.length ? '?' + params.join('&') : '');
2626
};
2727
this.$config.location.watch(function(url){
28-
anchor(url);
28+
location(url);
2929
});
3030
this.$onEval(PRIORITY_LAST, function(){
31-
scope.$config.location.set(anchor());
31+
scope.$config.location.set(location());
3232
});
33-
return anchor;
33+
return location;
3434
});

test/BinderTest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,13 +721,13 @@ BinderTest.prototype.testItShouldSelectTheCorrectRadioBox = function() {
721721
var male = jqLite(c.node[0].childNodes[1]);
722722

723723
female.click();
724-
assertEquals("female", c.scope.$get("sex"));
724+
assertEquals("female", c.scope.sex);
725725
assertEquals(true, female[0].checked);
726726
assertEquals(false, male[0].checked);
727727
assertEquals("female", female.val());
728728

729729
male.click();
730-
assertEquals("male", c.scope.$get("sex"));
730+
assertEquals("male", c.scope.sex);
731731
assertEquals(false, female[0].checked);
732732
assertEquals(true, male[0].checked);
733733
assertEquals("male", male.val());

test/servicesSpec.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,17 @@ describe("services", function(){
1313
expect(scope.$window).toEqual(window);
1414
});
1515

16-
it("should inject $anchor", function(){
17-
scope.$anchor('#path?key=value');
18-
expect(scope.$anchor.path).toEqual("path");
19-
expect(scope.$anchor.param).toEqual({key:'value'});
16+
it("should inject $location", function(){
17+
scope.$location('http://host:1234/p/a/t/h?query=value#path?key=value');
18+
expect(scope.$location.href).toEqual("http://host:123/p/a/t/h?query=value#path?key=value");
19+
expect(scope.$location.protocol).toEqual("http");
20+
expect(scope.$location.host).toEqual("host");
21+
expect(scope.$location.port).toEqual("1234");
22+
expect(scope.$location.path).toEqual("/p/a/t/h");
23+
expect(scope.$location.search).toEqual({query:'value'});
24+
expect(scope.$location.hash).toEqual('path?key=value');
25+
expect(scope.$location.hashPath).toEqual('path');
26+
expect(scope.$location.hashSearch).toEqual({key:'value'});
2027

2128
scope.$anchor.path = 'page=http://path';
2229
scope.$anchor.param = {k:'a=b'};

0 commit comments

Comments
 (0)