upgrade angular to 1.0.1
[angular-drzb] / app / lib / angular / angular-cookies.js
1 /**
2  * @license AngularJS v1.0.1
3  * (c) 2010-2012 Google, Inc. http://angularjs.org
4  * License: MIT
5  */
6 (function(window, angular, undefined) {
7 'use strict';
8
9 /**
10  * @ngdoc overview
11  * @name ngCookies
12  */
13
14
15 angular.module('ngCookies', ['ng']).
16   /**
17    * @ngdoc object
18    * @name ngCookies.$cookies
19    * @requires $browser
20    *
21    * @description
22    * Provides read/write access to browser's cookies.
23    *
24    * Only a simple Object is exposed and by adding or removing properties to/from
25    * this object, new cookies are created/deleted at the end of current $eval.
26    *
27    * @example
28    */
29    factory('$cookies', ['$rootScope', '$browser', function ($rootScope, $browser) {
30       var cookies = {},
31           lastCookies = {},
32           lastBrowserCookies,
33           runEval = false,
34           copy = angular.copy,
35           isUndefined = angular.isUndefined;
36
37       //creates a poller fn that copies all cookies from the $browser to service & inits the service
38       $browser.addPollFn(function() {
39         var currentCookies = $browser.cookies();
40         if (lastBrowserCookies != currentCookies) { //relies on browser.cookies() impl
41           lastBrowserCookies = currentCookies;
42           copy(currentCookies, lastCookies);
43           copy(currentCookies, cookies);
44           if (runEval) $rootScope.$apply();
45         }
46       })();
47
48       runEval = true;
49
50       //at the end of each eval, push cookies
51       //TODO: this should happen before the "delayed" watches fire, because if some cookies are not
52       //      strings or browser refuses to store some cookies, we update the model in the push fn.
53       $rootScope.$watch(push);
54
55       return cookies;
56
57
58       /**
59        * Pushes all the cookies from the service to the browser and verifies if all cookies were stored.
60        */
61       function push() {
62         var name,
63             value,
64             browserCookies,
65             updated;
66
67         //delete any cookies deleted in $cookies
68         for (name in lastCookies) {
69           if (isUndefined(cookies[name])) {
70             $browser.cookies(name, undefined);
71           }
72         }
73
74         //update all cookies updated in $cookies
75         for(name in cookies) {
76           value = cookies[name];
77           if (!angular.isString(value)) {
78             if (angular.isDefined(lastCookies[name])) {
79               cookies[name] = lastCookies[name];
80             } else {
81               delete cookies[name];
82             }
83           } else if (value !== lastCookies[name]) {
84             $browser.cookies(name, value);
85             updated = true;
86           }
87         }
88
89         //verify what was actually stored
90         if (updated){
91           updated = false;
92           browserCookies = $browser.cookies();
93
94           for (name in cookies) {
95             if (cookies[name] !== browserCookies[name]) {
96               //delete or reset all cookies that the browser dropped from $cookies
97               if (isUndefined(browserCookies[name])) {
98                 delete cookies[name];
99               } else {
100                 cookies[name] = browserCookies[name];
101               }
102               updated = true;
103             }
104           }
105         }
106       }
107     }]).
108
109
110   /**
111    * @ngdoc object
112    * @name ngCookies.$cookieStore
113    * @requires $cookies
114    *
115    * @description
116    * Provides a key-value (string-object) storage, that is backed by session cookies.
117    * Objects put or retrieved from this storage are automatically serialized or
118    * deserialized by angular's toJson/fromJson.
119    * @example
120    */
121    factory('$cookieStore', ['$cookies', function($cookies) {
122
123       return {
124         /**
125          * @ngdoc method
126          * @name ngCookies.$cookieStore#get
127          * @methodOf ngCookies.$cookieStore
128          *
129          * @description
130          * Returns the value of given cookie key
131          *
132          * @param {string} key Id to use for lookup.
133          * @returns {Object} Deserialized cookie value.
134          */
135         get: function(key) {
136           return angular.fromJson($cookies[key]);
137         },
138
139         /**
140          * @ngdoc method
141          * @name ngCookies.$cookieStore#put
142          * @methodOf ngCookies.$cookieStore
143          *
144          * @description
145          * Sets a value for given cookie key
146          *
147          * @param {string} key Id for the `value`.
148          * @param {Object} value Value to be stored.
149          */
150         put: function(key, value) {
151           $cookies[key] = angular.toJson(value);
152         },
153
154         /**
155          * @ngdoc method
156          * @name ngCookies.$cookieStore#remove
157          * @methodOf ngCookies.$cookieStore
158          *
159          * @description
160          * Remove given cookie
161          *
162          * @param {string} key Id of the key-value pair to delete.
163          */
164         remove: function(key) {
165           delete $cookies[key];
166         }
167       };
168
169     }]);
170
171 })(window, window.angular);