Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / fusion / external / ebz / angular_js / angular-cookies.js
1 /**
2  * @license AngularJS v1.2.25
3  * (c) 2010-2014 Google, Inc. http://angularjs.org
4  * License: MIT
5  */
6 (function(window, angular, undefined) {'use strict';
7
8 /**
9  * @ngdoc module
10  * @name ngCookies
11  * @description
12  *
13  * # ngCookies
14  *
15  * The `ngCookies` module provides a convenient wrapper for reading and writing browser cookies.
16  *
17  *
18  * <div doc-module-components="ngCookies"></div>
19  *
20  * See {@link ngCookies.$cookies `$cookies`} and
21  * {@link ngCookies.$cookieStore `$cookieStore`} for usage.
22  */
23
24
25 angular.module('ngCookies', ['ng']).
26   /**
27    * @ngdoc service
28    * @name $cookies
29    *
30    * @description
31    * Provides read/write access to browser's cookies.
32    *
33    * Only a simple Object is exposed and by adding or removing properties to/from this object, new
34    * cookies are created/deleted at the end of current $eval.
35    * The object's properties can only be strings.
36    *
37    * Requires the {@link ngCookies `ngCookies`} module to be installed.
38    *
39    * @example
40    *
41    * ```js
42    * angular.module('cookiesExample', ['ngCookies'])
43    *   .controller('ExampleController', ['$cookies', function($cookies) {
44    *     // Retrieving a cookie
45    *     var favoriteCookie = $cookies.myFavorite;
46    *     // Setting a cookie
47    *     $cookies.myFavorite = 'oatmeal';
48    *   }]);
49    * ```
50    */
51    factory('$cookies', ['$rootScope', '$browser', function ($rootScope, $browser) {
52       var cookies = {},
53           lastCookies = {},
54           lastBrowserCookies,
55           runEval = false,
56           copy = angular.copy,
57           isUndefined = angular.isUndefined;
58
59       //creates a poller fn that copies all cookies from the $browser to service & inits the service
60       $browser.addPollFn(function() {
61         var currentCookies = $browser.cookies();
62         if (lastBrowserCookies != currentCookies) { //relies on browser.cookies() impl
63           lastBrowserCookies = currentCookies;
64           copy(currentCookies, lastCookies);
65           copy(currentCookies, cookies);
66           if (runEval) $rootScope.$apply();
67         }
68       })();
69
70       runEval = true;
71
72       //at the end of each eval, push cookies
73       //TODO: this should happen before the "delayed" watches fire, because if some cookies are not
74       //      strings or browser refuses to store some cookies, we update the model in the push fn.
75       $rootScope.$watch(push);
76
77       return cookies;
78
79
80       /**
81        * Pushes all the cookies from the service to the browser and verifies if all cookies were
82        * stored.
83        */
84       function push() {
85         var name,
86             value,
87             browserCookies,
88             updated;
89
90         //delete any cookies deleted in $cookies
91         for (name in lastCookies) {
92           if (isUndefined(cookies[name])) {
93             $browser.cookies(name, undefined);
94           }
95         }
96
97         //update all cookies updated in $cookies
98         for(name in cookies) {
99           value = cookies[name];
100           if (!angular.isString(value)) {
101             value = '' + value;
102             cookies[name] = value;
103           }
104           if (value !== lastCookies[name]) {
105             $browser.cookies(name, value);
106             updated = true;
107           }
108         }
109
110         //verify what was actually stored
111         if (updated){
112           updated = false;
113           browserCookies = $browser.cookies();
114
115           for (name in cookies) {
116             if (cookies[name] !== browserCookies[name]) {
117               //delete or reset all cookies that the browser dropped from $cookies
118               if (isUndefined(browserCookies[name])) {
119                 delete cookies[name];
120               } else {
121                 cookies[name] = browserCookies[name];
122               }
123               updated = true;
124             }
125           }
126         }
127       }
128     }]).
129
130
131   /**
132    * @ngdoc service
133    * @name $cookieStore
134    * @requires $cookies
135    *
136    * @description
137    * Provides a key-value (string-object) storage, that is backed by session cookies.
138    * Objects put or retrieved from this storage are automatically serialized or
139    * deserialized by angular's toJson/fromJson.
140    *
141    * Requires the {@link ngCookies `ngCookies`} module to be installed.
142    *
143    * @example
144    *
145    * ```js
146    * angular.module('cookieStoreExample', ['ngCookies'])
147    *   .controller('ExampleController', ['$cookieStore', function($cookieStore) {
148    *     // Put cookie
149    *     $cookieStore.put('myFavorite','oatmeal');
150    *     // Get cookie
151    *     var favoriteCookie = $cookieStore.get('myFavorite');
152    *     // Removing a cookie
153    *     $cookieStore.remove('myFavorite');
154    *   }]);
155    * ```
156    */
157    factory('$cookieStore', ['$cookies', function($cookies) {
158
159       return {
160         /**
161          * @ngdoc method
162          * @name $cookieStore#get
163          *
164          * @description
165          * Returns the value of given cookie key
166          *
167          * @param {string} key Id to use for lookup.
168          * @returns {Object} Deserialized cookie value.
169          */
170         get: function(key) {
171           var value = $cookies[key];
172           return value ? angular.fromJson(value) : value;
173         },
174
175         /**
176          * @ngdoc method
177          * @name $cookieStore#put
178          *
179          * @description
180          * Sets a value for given cookie key
181          *
182          * @param {string} key Id for the `value`.
183          * @param {Object} value Value to be stored.
184          */
185         put: function(key, value) {
186           $cookies[key] = angular.toJson(value);
187         },
188
189         /**
190          * @ngdoc method
191          * @name $cookieStore#remove
192          *
193          * @description
194          * Remove given cookie
195          *
196          * @param {string} key Id of the key-value pair to delete.
197          */
198         remove: function(key) {
199           delete $cookies[key];
200         }
201       };
202
203     }]);
204
205
206 })(window, window.angular);