mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
chore(ngCookies): moved to module
This commit is contained in:
parent
798bca62c6
commit
7b22d59b4a
11 changed files with 207 additions and 195 deletions
4
Rakefile
4
Rakefile
|
|
@ -81,9 +81,11 @@ task :compile => [:init, :compile_scenario, :compile_jstd_scenario_adapter] do
|
|||
|
||||
FileUtils.cp 'src/ngMock/angular-mocks.js', path_to('angular-mocks.js')
|
||||
FileUtils.cp 'src/ngResource/resource.js', path_to('angular-resource.js')
|
||||
FileUtils.cp 'src/ngCookies/cookies.js', path_to('angular-cookies.js')
|
||||
|
||||
|
||||
closureCompile('angular.js')
|
||||
closureCompile('angular-cookies.js')
|
||||
closureCompile('angular-loader.js')
|
||||
closureCompile('angular-resource.js')
|
||||
|
||||
|
|
@ -115,6 +117,8 @@ task :package => [:clean, :compile, :docs] do
|
|||
path_to('angular-loader.js'),
|
||||
path_to('angular-loader.min.js'),
|
||||
path_to('angular-mocks.js'),
|
||||
path_to('angular-cookies.js'),
|
||||
path_to('angular-cookies.min.js'),
|
||||
path_to('angular-resource.js'),
|
||||
path_to('angular-resource.min.js'),
|
||||
path_to('angular-scenario.js'),
|
||||
|
|
|
|||
7
angularFiles.js
vendored
7
angularFiles.js
vendored
|
|
@ -14,8 +14,6 @@ angularFiles = {
|
|||
'src/ng/cacheFactory.js',
|
||||
'src/ng/compiler.js',
|
||||
'src/ng/controller.js',
|
||||
'src/ng/cookieStore.js',
|
||||
'src/ng/cookies.js',
|
||||
'src/ng/defer.js',
|
||||
'src/ng/document.js',
|
||||
'src/ng/exceptionHandler.js',
|
||||
|
|
@ -66,6 +64,7 @@ angularFiles = {
|
|||
],
|
||||
|
||||
'angularSrcModules': [
|
||||
'src/ngCookies/cookies.js',
|
||||
'src/ngResource/resource.js',
|
||||
'src/ngMock/angular-mocks.js'
|
||||
],
|
||||
|
|
@ -98,6 +97,7 @@ angularFiles = {
|
|||
'test/ng/*.js',
|
||||
'test/ng/directive/*.js',
|
||||
'test/ng/filter/*.js',
|
||||
'test/ngCookies/*.js',
|
||||
'test/ngResource/*.js',
|
||||
'test/ngMock/*.js'
|
||||
],
|
||||
|
|
@ -135,9 +135,11 @@ angularFiles = {
|
|||
'lib/jasmine-jstd-adapter/JasmineAdapter.js',
|
||||
'build/angular.js',
|
||||
'src/ngMock/angular-mocks.js',
|
||||
'src/ngCookies/cookies.js',
|
||||
'src/ngResource/resource.js',
|
||||
'test/matchers.js',
|
||||
'test/ngMock/*.js',
|
||||
'test/ngCookies/*.js',
|
||||
'test/ngResource/*.js'
|
||||
],
|
||||
|
||||
|
|
@ -169,6 +171,7 @@ angularFiles = {
|
|||
'src/ngScenario/jstd-scenario-adapter/Adapter.js',
|
||||
'@angularTest',
|
||||
'example/personalLog/*.js',
|
||||
|
||||
'example/personalLog/test/*.js'
|
||||
],
|
||||
|
||||
|
|
|
|||
|
|
@ -146,7 +146,7 @@ function TutorialInstructionsCtrl($cookieStore) {
|
|||
};
|
||||
}
|
||||
|
||||
angular.module('ngdocs', ['ngdocs.directives', 'ngResource'], function($locationProvider, $filterProvider, $compileProvider) {
|
||||
angular.module('ngdocs', ['ngdocs.directives', 'ngResource', 'ngCookies'], function($locationProvider, $filterProvider, $compileProvider) {
|
||||
$locationProvider.html5Mode(true).hashPrefix('!');
|
||||
|
||||
$filterProvider.register('title', function(){
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@
|
|||
if (jQuery) addTag('script', {src: 'jquery.min.js'});
|
||||
addTag('script', {src: path('angular.js')}, sync);
|
||||
addTag('script', {src: path('angular-resource.js') }, sync);
|
||||
addTag('script', {src: path('angular-cookies.js') }, sync);
|
||||
addTag('script', {src: 'docs-combined.js'}, sync);
|
||||
addTag('script', {src: 'docs-keywords.js'}, sync);
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ describe('example.personalLog.LogCtrl', function() {
|
|||
var logScope;
|
||||
|
||||
beforeEach(function() {
|
||||
var injector = angular.injector(['ng', 'ngMock']);
|
||||
var injector = angular.injector(['ng', 'ngMock', 'ngCookies']);
|
||||
logScope = injector.get('$rootScope');
|
||||
logScope.$cookies = injector.get('$cookies');
|
||||
injector.instantiate(example.personalLog.LogCtrl, {$scope: logScope});
|
||||
|
|
|
|||
|
|
@ -110,8 +110,6 @@ function publishExternalAPI(angular){
|
|||
$browser: $BrowserProvider,
|
||||
$cacheFactory: $CacheFactoryProvider,
|
||||
$controller: $ControllerProvider,
|
||||
$cookies: $CookiesProvider,
|
||||
$cookieStore: $CookieStoreProvider,
|
||||
$defer: $DeferProvider,
|
||||
$document: $DocumentProvider,
|
||||
$exceptionHandler: $ExceptionHandlerProvider,
|
||||
|
|
|
|||
|
|
@ -1,64 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
/**
|
||||
* @ngdoc object
|
||||
* @name angular.module.ng.$cookieStore
|
||||
* @requires $cookies
|
||||
*
|
||||
* @description
|
||||
* Provides a key-value (string-object) storage, that is backed by session cookies.
|
||||
* Objects put or retrieved from this storage are automatically serialized or
|
||||
* deserialized by angular's toJson/fromJson.
|
||||
* @example
|
||||
*/
|
||||
function $CookieStoreProvider(){
|
||||
this.$get = ['$cookies', function($cookies) {
|
||||
|
||||
return {
|
||||
/**
|
||||
* @ngdoc method
|
||||
* @name angular.module.ng.$cookieStore#get
|
||||
* @methodOf angular.module.ng.$cookieStore
|
||||
*
|
||||
* @description
|
||||
* Returns the value of given cookie key
|
||||
*
|
||||
* @param {string} key Id to use for lookup.
|
||||
* @returns {Object} Deserialized cookie value.
|
||||
*/
|
||||
get: function(key) {
|
||||
return fromJson($cookies[key]);
|
||||
},
|
||||
|
||||
/**
|
||||
* @ngdoc method
|
||||
* @name angular.module.ng.$cookieStore#put
|
||||
* @methodOf angular.module.ng.$cookieStore
|
||||
*
|
||||
* @description
|
||||
* Sets a value for given cookie key
|
||||
*
|
||||
* @param {string} key Id for the `value`.
|
||||
* @param {Object} value Value to be stored.
|
||||
*/
|
||||
put: function(key, value) {
|
||||
$cookies[key] = toJson(value);
|
||||
},
|
||||
|
||||
/**
|
||||
* @ngdoc method
|
||||
* @name angular.module.ng.$cookieStore#remove
|
||||
* @methodOf angular.module.ng.$cookieStore
|
||||
*
|
||||
* @description
|
||||
* Remove given cookie
|
||||
*
|
||||
* @param {string} key Id of the key-value pair to delete.
|
||||
*/
|
||||
remove: function(key) {
|
||||
delete $cookies[key];
|
||||
}
|
||||
};
|
||||
|
||||
}];
|
||||
}
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
/**
|
||||
* @ngdoc object
|
||||
* @name angular.module.ng.$cookies
|
||||
* @requires $browser
|
||||
*
|
||||
* @description
|
||||
* Provides read/write access to browser's cookies.
|
||||
*
|
||||
* Only a simple Object is exposed and by adding or removing properties to/from
|
||||
* this object, new cookies are created/deleted at the end of current $eval.
|
||||
*
|
||||
* @example
|
||||
*/
|
||||
function $CookiesProvider() {
|
||||
this.$get = ['$rootScope', '$browser', function ($rootScope, $browser) {
|
||||
var cookies = {},
|
||||
lastCookies = {},
|
||||
lastBrowserCookies,
|
||||
runEval = false;
|
||||
|
||||
//creates a poller fn that copies all cookies from the $browser to service & inits the service
|
||||
$browser.addPollFn(function() {
|
||||
var currentCookies = $browser.cookies();
|
||||
if (lastBrowserCookies != currentCookies) { //relies on browser.cookies() impl
|
||||
lastBrowserCookies = currentCookies;
|
||||
copy(currentCookies, lastCookies);
|
||||
copy(currentCookies, cookies);
|
||||
if (runEval) $rootScope.$apply();
|
||||
}
|
||||
})();
|
||||
|
||||
runEval = true;
|
||||
|
||||
//at the end of each eval, push cookies
|
||||
//TODO: this should happen before the "delayed" watches fire, because if some cookies are not
|
||||
// strings or browser refuses to store some cookies, we update the model in the push fn.
|
||||
$rootScope.$watch(push);
|
||||
|
||||
return cookies;
|
||||
|
||||
|
||||
/**
|
||||
* Pushes all the cookies from the service to the browser and verifies if all cookies were stored.
|
||||
*/
|
||||
function push() {
|
||||
var name,
|
||||
value,
|
||||
browserCookies,
|
||||
updated;
|
||||
|
||||
//delete any cookies deleted in $cookies
|
||||
for (name in lastCookies) {
|
||||
if (isUndefined(cookies[name])) {
|
||||
$browser.cookies(name, undefined);
|
||||
}
|
||||
}
|
||||
|
||||
//update all cookies updated in $cookies
|
||||
for(name in cookies) {
|
||||
value = cookies[name];
|
||||
if (!isString(value)) {
|
||||
if (isDefined(lastCookies[name])) {
|
||||
cookies[name] = lastCookies[name];
|
||||
} else {
|
||||
delete cookies[name];
|
||||
}
|
||||
} else if (value !== lastCookies[name]) {
|
||||
$browser.cookies(name, value);
|
||||
updated = true;
|
||||
}
|
||||
}
|
||||
|
||||
//verify what was actually stored
|
||||
if (updated){
|
||||
updated = false;
|
||||
browserCookies = $browser.cookies();
|
||||
|
||||
for (name in cookies) {
|
||||
if (cookies[name] !== browserCookies[name]) {
|
||||
//delete or reset all cookies that the browser dropped from $cookies
|
||||
if (isUndefined(browserCookies[name])) {
|
||||
delete cookies[name];
|
||||
} else {
|
||||
cookies[name] = browserCookies[name];
|
||||
}
|
||||
updated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}];
|
||||
}
|
||||
163
src/ngCookies/cookies.js
Normal file
163
src/ngCookies/cookies.js
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
'use strict';
|
||||
|
||||
/**
|
||||
* @ngdoc overview
|
||||
* @name angular.module.ngCookies
|
||||
*/
|
||||
|
||||
|
||||
angular.module('ngCookies', ['ng']).
|
||||
/**
|
||||
* @ngdoc object
|
||||
* @name angular.module.ng.$cookies
|
||||
* @requires $browser
|
||||
*
|
||||
* @description
|
||||
* Provides read/write access to browser's cookies.
|
||||
*
|
||||
* Only a simple Object is exposed and by adding or removing properties to/from
|
||||
* this object, new cookies are created/deleted at the end of current $eval.
|
||||
*
|
||||
* @example
|
||||
*/
|
||||
factory('$cookies', ['$rootScope', '$browser', function ($rootScope, $browser) {
|
||||
var cookies = {},
|
||||
lastCookies = {},
|
||||
lastBrowserCookies,
|
||||
runEval = false,
|
||||
copy = angular.copy,
|
||||
isUndefined = angular.isUndefined;
|
||||
|
||||
//creates a poller fn that copies all cookies from the $browser to service & inits the service
|
||||
$browser.addPollFn(function() {
|
||||
var currentCookies = $browser.cookies();
|
||||
if (lastBrowserCookies != currentCookies) { //relies on browser.cookies() impl
|
||||
lastBrowserCookies = currentCookies;
|
||||
copy(currentCookies, lastCookies);
|
||||
copy(currentCookies, cookies);
|
||||
if (runEval) $rootScope.$apply();
|
||||
}
|
||||
})();
|
||||
|
||||
runEval = true;
|
||||
|
||||
//at the end of each eval, push cookies
|
||||
//TODO: this should happen before the "delayed" watches fire, because if some cookies are not
|
||||
// strings or browser refuses to store some cookies, we update the model in the push fn.
|
||||
$rootScope.$watch(push);
|
||||
|
||||
return cookies;
|
||||
|
||||
|
||||
/**
|
||||
* Pushes all the cookies from the service to the browser and verifies if all cookies were stored.
|
||||
*/
|
||||
function push() {
|
||||
var name,
|
||||
value,
|
||||
browserCookies,
|
||||
updated;
|
||||
|
||||
//delete any cookies deleted in $cookies
|
||||
for (name in lastCookies) {
|
||||
if (isUndefined(cookies[name])) {
|
||||
$browser.cookies(name, undefined);
|
||||
}
|
||||
}
|
||||
|
||||
//update all cookies updated in $cookies
|
||||
for(name in cookies) {
|
||||
value = cookies[name];
|
||||
if (!angular.isString(value)) {
|
||||
if (angular.isDefined(lastCookies[name])) {
|
||||
cookies[name] = lastCookies[name];
|
||||
} else {
|
||||
delete cookies[name];
|
||||
}
|
||||
} else if (value !== lastCookies[name]) {
|
||||
$browser.cookies(name, value);
|
||||
updated = true;
|
||||
}
|
||||
}
|
||||
|
||||
//verify what was actually stored
|
||||
if (updated){
|
||||
updated = false;
|
||||
browserCookies = $browser.cookies();
|
||||
|
||||
for (name in cookies) {
|
||||
if (cookies[name] !== browserCookies[name]) {
|
||||
//delete or reset all cookies that the browser dropped from $cookies
|
||||
if (isUndefined(browserCookies[name])) {
|
||||
delete cookies[name];
|
||||
} else {
|
||||
cookies[name] = browserCookies[name];
|
||||
}
|
||||
updated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}]).
|
||||
|
||||
|
||||
/**
|
||||
* @ngdoc object
|
||||
* @name angular.module.ng.$cookieStore
|
||||
* @requires $cookies
|
||||
*
|
||||
* @description
|
||||
* Provides a key-value (string-object) storage, that is backed by session cookies.
|
||||
* Objects put or retrieved from this storage are automatically serialized or
|
||||
* deserialized by angular's toJson/fromJson.
|
||||
* @example
|
||||
*/
|
||||
factory('$cookieStore', ['$cookies', function($cookies) {
|
||||
|
||||
return {
|
||||
/**
|
||||
* @ngdoc method
|
||||
* @name angular.module.ng.$cookieStore#get
|
||||
* @methodOf angular.module.ng.$cookieStore
|
||||
*
|
||||
* @description
|
||||
* Returns the value of given cookie key
|
||||
*
|
||||
* @param {string} key Id to use for lookup.
|
||||
* @returns {Object} Deserialized cookie value.
|
||||
*/
|
||||
get: function(key) {
|
||||
return angular.fromJson($cookies[key]);
|
||||
},
|
||||
|
||||
/**
|
||||
* @ngdoc method
|
||||
* @name angular.module.ng.$cookieStore#put
|
||||
* @methodOf angular.module.ng.$cookieStore
|
||||
*
|
||||
* @description
|
||||
* Sets a value for given cookie key
|
||||
*
|
||||
* @param {string} key Id for the `value`.
|
||||
* @param {Object} value Value to be stored.
|
||||
*/
|
||||
put: function(key, value) {
|
||||
$cookies[key] = angular.toJson(value);
|
||||
},
|
||||
|
||||
/**
|
||||
* @ngdoc method
|
||||
* @name angular.module.ng.$cookieStore#remove
|
||||
* @methodOf angular.module.ng.$cookieStore
|
||||
*
|
||||
* @description
|
||||
* Remove given cookie
|
||||
*
|
||||
* @param {string} key Id of the key-value pair to delete.
|
||||
*/
|
||||
remove: function(key) {
|
||||
delete $cookies[key];
|
||||
}
|
||||
};
|
||||
|
||||
}]);
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
describe('$cookieStore', function() {
|
||||
|
||||
|
||||
it('should serialize objects to json', inject(function($cookieStore, $browser, $rootScope) {
|
||||
$cookieStore.put('objectCookie', {id: 123, name: 'blah'});
|
||||
$rootScope.$digest();
|
||||
expect($browser.cookies()).toEqual({'objectCookie': '{"id":123,"name":"blah"}'});
|
||||
}));
|
||||
|
||||
|
||||
it('should deserialize json to object', inject(function($cookieStore, $browser) {
|
||||
$browser.cookies('objectCookie', '{"id":123,"name":"blah"}');
|
||||
$browser.poll();
|
||||
expect($cookieStore.get('objectCookie')).toEqual({id: 123, name: 'blah'});
|
||||
}));
|
||||
|
||||
|
||||
it('should delete objects from the store when remove is called', inject(function($cookieStore, $browser, $rootScope) {
|
||||
$cookieStore.put('gonner', { "I'll":"Be Back"});
|
||||
$rootScope.$digest(); //force eval in test
|
||||
$browser.poll();
|
||||
expect($browser.cookies()).toEqual({'gonner': '{"I\'ll":"Be Back"}'});
|
||||
|
||||
$cookieStore.remove('gonner');
|
||||
$rootScope.$digest();
|
||||
expect($browser.cookies()).toEqual({});
|
||||
}));
|
||||
});
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
describe('$cookies', function() {
|
||||
beforeEach(module(function($provide) {
|
||||
beforeEach(module('ngCookies', function($provide) {
|
||||
$provide.factory('$browser', function(){
|
||||
return angular.extend(new angular.mock.$Browser(), {cookieHash: {preexisting:'oldCookie'}});
|
||||
});
|
||||
|
|
@ -95,3 +95,34 @@ describe('$cookies', function() {
|
|||
expect($cookies).toEqual({'preexisting': 'oldCookie', 'longCookie': 'shortVal'});
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
describe('$cookieStore', function() {
|
||||
|
||||
beforeEach(module('ngCookies'));
|
||||
|
||||
it('should serialize objects to json', inject(function($cookieStore, $browser, $rootScope) {
|
||||
$cookieStore.put('objectCookie', {id: 123, name: 'blah'});
|
||||
$rootScope.$digest();
|
||||
expect($browser.cookies()).toEqual({'objectCookie': '{"id":123,"name":"blah"}'});
|
||||
}));
|
||||
|
||||
|
||||
it('should deserialize json to object', inject(function($cookieStore, $browser) {
|
||||
$browser.cookies('objectCookie', '{"id":123,"name":"blah"}');
|
||||
$browser.poll();
|
||||
expect($cookieStore.get('objectCookie')).toEqual({id: 123, name: 'blah'});
|
||||
}));
|
||||
|
||||
|
||||
it('should delete objects from the store when remove is called', inject(function($cookieStore, $browser, $rootScope) {
|
||||
$cookieStore.put('gonner', { "I'll":"Be Back"});
|
||||
$rootScope.$digest(); //force eval in test
|
||||
$browser.poll();
|
||||
expect($browser.cookies()).toEqual({'gonner': '{"I\'ll":"Be Back"}'});
|
||||
|
||||
$cookieStore.remove('gonner');
|
||||
$rootScope.$digest();
|
||||
expect($browser.cookies()).toEqual({});
|
||||
}));
|
||||
});
|
||||
Loading…
Reference in a new issue