Skip to content

Commit 1777110

Browse files
committed
split up services into individual files
- split up services into files under src/service - split up specs into files under test/service - rewrite all specs so that they don't depend on one global forEach - get rid of obsolete code and tests in ng:switch - rename mock $log spec from "$log" to "$log mock"
1 parent d2089a1 commit 1777110

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2842
-2642
lines changed

Rakefile

+17-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,23 @@ ANGULAR = [
1515
'src/filters.js',
1616
'src/formatters.js',
1717
'src/validators.js',
18-
'src/services.js',
18+
'src/service/cookieStore.js',
19+
'src/service/cookies.js',
20+
'src/service/defer.js',
21+
'src/service/document.js',
22+
'src/service/exceptionHandler.js',
23+
'src/service/hover.js',
24+
'src/service/invalidWidgets.js',
25+
'src/service/location.js',
26+
'src/service/log.js',
27+
'src/service/resource.js',
28+
'src/service/route.js',
29+
'src/service/updateView.js',
30+
'src/service/window.js',
31+
'src/service/xhr.bulk.js',
32+
'src/service/xhr.cache.js',
33+
'src/service/xhr.error.js',
34+
'src/service/xhr.js',
1935
'src/directives.js',
2036
'src/markups.js',
2137
'src/widgets.js',

jsTestDriver-coverage.conf

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ load:
88
- src/Angular.js
99
- src/JSON.js
1010
- src/*.js
11+
- src/service/*.js
1112
- example/personalLog/*.js
1213
- test/testabilityPatch.js
1314
- src/scenario/Scenario.js
@@ -18,6 +19,7 @@ load:
1819
- test/scenario/*.js
1920
- test/scenario/output/*.js
2021
- test/*.js
22+
- test/service/*.js
2123
- example/personalLog/test/*.js
2224

2325
exclude:

jsTestDriver-jquery.conf

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ load:
88
- src/Angular.js
99
- src/JSON.js
1010
- src/*.js
11+
- src/service/*.js
1112
- example/personalLog/*.js
1213
- test/testabilityPatch.js
1314
- src/scenario/Scenario.js
@@ -18,6 +19,7 @@ load:
1819
- test/scenario/*.js
1920
- test/scenario/output/*.js
2021
- test/*.js
22+
- test/service/*.js
2123
- example/personalLog/test/*.js
2224

2325
exclude:

jsTestDriver.conf

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ load:
88
- src/Angular.js
99
- src/JSON.js
1010
- src/*.js
11+
- src/service/*.js
1112
- example/personalLog/*.js
1213
- test/testabilityPatch.js
1314
- src/scenario/Scenario.js
@@ -18,6 +19,7 @@ load:
1819
- test/scenario/*.js
1920
- test/scenario/output/*.js
2021
- test/*.js
22+
- test/service/*.js
2123
- example/personalLog/test/*.js
2224

2325
exclude:

lib/jsl/jsl.default.conf

+2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@
122122
# or "+process Folder\Path\*.htm".
123123
#
124124
+process src/*.js
125+
+process src/service/*.js
125126
+process src/scenario/*.js
126127
+process test/*.js
128+
+process test/service/*.js
127129
+process test/scenario/*.js
128130

src/Injector.js

+4
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ function injectService(services, fn) {
7676
function injectUpdateView(fn) {
7777
return injectService(['$updateView'], fn);
7878
}
79+
80+
function angularServiceInject(name, fn, inject, eager) {
81+
angularService(name, fn, {$inject:inject, $eager:eager});
82+
}

src/angular-bootstrap.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,25 @@
118118
'AngularPublic.js',
119119

120120
// Extension points
121-
'services.js',
121+
122+
'service/cookieStore.js',
123+
'service/cookies.js',
124+
'service/defer.js',
125+
'service/document.js',
126+
'service/exceptionHandler.js',
127+
'service/hover.js',
128+
'service/invalidWidgets.js',
129+
'service/location.js',
130+
'service/log.js',
131+
'service/resource.js',
132+
'service/route.js',
133+
'service/updateView.js',
134+
'service/window.js',
135+
'service/xhr.bulk.js',
136+
'service/xhr.cache.js',
137+
'service/xhr.error.js',
138+
'service/xhr.js',
139+
122140
'apis.js',
123141
'filters.js',
124142
'formatters.js',

src/service/cookieStore.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @workInProgress
3+
* @ngdoc service
4+
* @name angular.service.$cookieStore
5+
* @requires $cookies
6+
*
7+
* @description
8+
* Provides a key-value (string-object) storage, that is backed by session cookies.
9+
* Objects put or retrieved from this storage are automatically serialized or
10+
* deserialized by angular's toJson/fromJson.
11+
* @example
12+
*/
13+
angularServiceInject('$cookieStore', function($store) {
14+
15+
return {
16+
/**
17+
* @workInProgress
18+
* @ngdoc method
19+
* @name angular.service.$cookieStore#get
20+
* @methodOf angular.service.$cookieStore
21+
*
22+
* @description
23+
* Returns the value of given cookie key
24+
*
25+
* @param {string} key Id to use for lookup.
26+
* @returns {Object} Deserialized cookie value.
27+
*/
28+
get: function(key) {
29+
return fromJson($store[key]);
30+
},
31+
32+
/**
33+
* @workInProgress
34+
* @ngdoc method
35+
* @name angular.service.$cookieStore#put
36+
* @methodOf angular.service.$cookieStore
37+
*
38+
* @description
39+
* Sets a value for given cookie key
40+
*
41+
* @param {string} key Id for the `value`.
42+
* @param {Object} value Value to be stored.
43+
*/
44+
put: function(key, value) {
45+
$store[key] = toJson(value);
46+
},
47+
48+
/**
49+
* @workInProgress
50+
* @ngdoc method
51+
* @name angular.service.$cookieStore#remove
52+
* @methodOf angular.service.$cookieStore
53+
*
54+
* @description
55+
* Remove given cookie
56+
*
57+
* @param {string} key Id of the key-value pair to delete.
58+
*/
59+
remove: function(key) {
60+
delete $store[key];
61+
}
62+
};
63+
64+
}, ['$cookies']);

src/service/cookies.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @workInProgress
3+
* @ngdoc service
4+
* @name angular.service.$cookies
5+
* @requires $browser
6+
*
7+
* @description
8+
* Provides read/write access to browser's cookies.
9+
*
10+
* Only a simple Object is exposed and by adding or removing properties to/from
11+
* this object, new cookies are created/deleted at the end of current $eval.
12+
*
13+
* @example
14+
*/
15+
angularServiceInject('$cookies', function($browser) {
16+
var rootScope = this,
17+
cookies = {},
18+
lastCookies = {},
19+
lastBrowserCookies;
20+
21+
//creates a poller fn that copies all cookies from the $browser to service & inits the service
22+
$browser.addPollFn(function() {
23+
var currentCookies = $browser.cookies();
24+
if (lastBrowserCookies != currentCookies) { //relies on browser.cookies() impl
25+
lastBrowserCookies = currentCookies;
26+
copy(currentCookies, lastCookies);
27+
copy(currentCookies, cookies);
28+
rootScope.$eval();
29+
}
30+
})();
31+
32+
//at the end of each eval, push cookies
33+
//TODO: this should happen before the "delayed" watches fire, because if some cookies are not
34+
// strings or browser refuses to store some cookies, we update the model in the push fn.
35+
this.$onEval(PRIORITY_LAST, push);
36+
37+
return cookies;
38+
39+
40+
/**
41+
* Pushes all the cookies from the service to the browser and verifies if all cookies were stored.
42+
*/
43+
function push(){
44+
var name,
45+
value,
46+
browserCookies,
47+
updated;
48+
49+
//delete any cookies deleted in $cookies
50+
for (name in lastCookies) {
51+
if (isUndefined(cookies[name])) {
52+
$browser.cookies(name, _undefined);
53+
}
54+
}
55+
56+
//update all cookies updated in $cookies
57+
for(name in cookies) {
58+
value = cookies[name];
59+
if (!isString(value)) {
60+
if (isDefined(lastCookies[name])) {
61+
cookies[name] = lastCookies[name];
62+
} else {
63+
delete cookies[name];
64+
}
65+
} else if (value !== lastCookies[name]) {
66+
$browser.cookies(name, value);
67+
updated = true;
68+
}
69+
}
70+
71+
//verify what was actually stored
72+
if (updated){
73+
updated = false;
74+
browserCookies = $browser.cookies();
75+
76+
for (name in cookies) {
77+
if (cookies[name] !== browserCookies[name]) {
78+
//delete or reset all cookies that the browser dropped from $cookies
79+
if (isUndefined(browserCookies[name])) {
80+
delete cookies[name];
81+
} else {
82+
cookies[name] = browserCookies[name];
83+
}
84+
updated = true;
85+
}
86+
}
87+
}
88+
}
89+
}, ['$browser']);

src/service/defer.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @workInProgress
3+
* @ngdoc service
4+
* @name angular.service.$defer
5+
* @requires $browser
6+
* @requires $exceptionHandler
7+
* @requires $updateView
8+
*
9+
* @description
10+
* Delegates to {@link angular.service.$browser.defer $browser.defer}, but wraps the `fn` function
11+
* into a try/catch block and delegates any exceptions to
12+
* {@link angular.services.$exceptionHandler $exceptionHandler} service.
13+
*
14+
* In tests you can use `$browser.defer.flush()` to flush the queue of deferred functions.
15+
*
16+
* @param {function()} fn A function, who's execution should be deferred.
17+
*/
18+
angularServiceInject('$defer', function($browser, $exceptionHandler, $updateView) {
19+
var scope = this;
20+
21+
return function(fn) {
22+
$browser.defer(function() {
23+
try {
24+
fn();
25+
} catch(e) {
26+
$exceptionHandler(e);
27+
} finally {
28+
$updateView();
29+
}
30+
});
31+
};
32+
}, ['$browser', '$exceptionHandler', '$updateView']);

src/service/document.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @workInProgress
3+
* @ngdoc service
4+
* @name angular.service.$document
5+
* @requires $window
6+
*
7+
* @description
8+
* Reference to the browser window.document, but wrapped into angular.element().
9+
*/
10+
angularServiceInject("$document", function(window){
11+
return jqLite(window.document);
12+
}, ['$window'], true);

src/service/exceptionHandler.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @workInProgress
3+
* @ngdoc service
4+
* @name angular.service.$exceptionHandler
5+
* @requires $log
6+
*
7+
* @description
8+
* Any uncaught exception in angular expressions is delegated to this service.
9+
* The default implementation simply delegates to `$log.error` which logs it into
10+
* the browser console.
11+
*
12+
* In unit tests, if `angular-mocks.js` is loaded, this service is overriden by
13+
* {@link angular.mock.service.$exceptionHandler mock $exceptionHandler}
14+
*
15+
* @example
16+
*/
17+
var $exceptionHandlerFactory; //reference to be used only in tests
18+
angularServiceInject('$exceptionHandler', $exceptionHandlerFactory = function($log){
19+
return function(e) {
20+
$log.error(e);
21+
};
22+
}, ['$log'], true);

src/service/hover.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @workInProgress
3+
* @ngdoc service
4+
* @name angular.service.$hover
5+
* @requires $browser
6+
* @requires $document
7+
*
8+
* @description
9+
*
10+
* @example
11+
*/
12+
angularServiceInject("$hover", function(browser, document) {
13+
var tooltip, self = this, error, width = 300, arrowWidth = 10, body = jqLite(document[0].body);
14+
browser.hover(function(element, show){
15+
if (show && (error = element.attr(NG_EXCEPTION) || element.attr(NG_VALIDATION_ERROR))) {
16+
if (!tooltip) {
17+
tooltip = {
18+
callout: jqLite('<div id="ng-callout"></div>'),
19+
arrow: jqLite('<div></div>'),
20+
title: jqLite('<div class="ng-title"></div>'),
21+
content: jqLite('<div class="ng-content"></div>')
22+
};
23+
tooltip.callout.append(tooltip.arrow);
24+
tooltip.callout.append(tooltip.title);
25+
tooltip.callout.append(tooltip.content);
26+
body.append(tooltip.callout);
27+
}
28+
var docRect = body[0].getBoundingClientRect(),
29+
elementRect = element[0].getBoundingClientRect(),
30+
leftSpace = docRect.right - elementRect.right - arrowWidth;
31+
tooltip.title.text(element.hasClass("ng-exception") ? "EXCEPTION:" : "Validation error...");
32+
tooltip.content.text(error);
33+
if (leftSpace < width) {
34+
tooltip.arrow.addClass('ng-arrow-right');
35+
tooltip.arrow.css({left: (width + 1)+'px'});
36+
tooltip.callout.css({
37+
position: 'fixed',
38+
left: (elementRect.left - arrowWidth - width - 4) + "px",
39+
top: (elementRect.top - 3) + "px",
40+
width: width + "px"
41+
});
42+
} else {
43+
tooltip.arrow.addClass('ng-arrow-left');
44+
tooltip.callout.css({
45+
position: 'fixed',
46+
left: (elementRect.right + arrowWidth) + "px",
47+
top: (elementRect.top - 3) + "px",
48+
width: width + "px"
49+
});
50+
}
51+
} else if (tooltip) {
52+
tooltip.callout.remove();
53+
tooltip = _null;
54+
}
55+
});
56+
}, ['$browser', '$document'], true);

0 commit comments

Comments
 (0)