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