Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / policyApp / libs / bower_components / angular-cookies / angular-cookies.js
1 /**
2  * @license AngularJS v1.3.20
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             delete lastCookies[name];
95           }
96         }
97
98         //update all cookies updated in $cookies
99         for (name in cookies) {
100           value = cookies[name];
101           if (!angular.isString(value)) {
102             value = '' + value;
103             cookies[name] = value;
104           }
105           if (value !== lastCookies[name]) {
106             $browser.cookies(name, value);
107             lastCookies[name] = value;
108             updated = true;
109           }
110         }
111
112         //verify what was actually stored
113         if (updated) {
114           browserCookies = $browser.cookies();
115
116           for (name in cookies) {
117             if (cookies[name] !== browserCookies[name]) {
118               //delete or reset all cookies that the browser dropped from $cookies
119               if (isUndefined(browserCookies[name])) {
120                 delete cookies[name];
121                 delete lastCookies[name];
122               } else {
123                 cookies[name] = lastCookies[name] = browserCookies[name];
124               }
125             }
126           }
127         }
128       }
129     }]).
130
131
132   /**
133    * @ngdoc service
134    * @name $cookieStore
135    * @requires $cookies
136    *
137    * @description
138    * Provides a key-value (string-object) storage, that is backed by session cookies.
139    * Objects put or retrieved from this storage are automatically serialized or
140    * deserialized by angular's toJson/fromJson.
141    *
142    * Requires the {@link ngCookies `ngCookies`} module to be installed.
143    *
144    * @example
145    *
146    * ```js
147    * angular.module('cookieStoreExample', ['ngCookies'])
148    *   .controller('ExampleController', ['$cookieStore', function($cookieStore) {
149    *     // Put cookie
150    *     $cookieStore.put('myFavorite','oatmeal');
151    *     // Get cookie
152    *     var favoriteCookie = $cookieStore.get('myFavorite');
153    *     // Removing a cookie
154    *     $cookieStore.remove('myFavorite');
155    *   }]);
156    * ```
157    */
158    factory('$cookieStore', ['$cookies', function($cookies) {
159
160       return {
161         /**
162          * @ngdoc method
163          * @name $cookieStore#get
164          *
165          * @description
166          * Returns the value of given cookie key
167          *
168          * @param {string} key Id to use for lookup.
169          * @returns {Object} Deserialized cookie value.
170          */
171         get: function(key) {
172           var value = $cookies[key];
173           return value ? angular.fromJson(value) : value;
174         },
175
176         /**
177          * @ngdoc method
178          * @name $cookieStore#put
179          *
180          * @description
181          * Sets a value for given cookie key
182          *
183          * @param {string} key Id for the `value`.
184          * @param {Object} value Value to be stored.
185          */
186         put: function(key, value) {
187           $cookies[key] = angular.toJson(value);
188         },
189
190         /**
191          * @ngdoc method
192          * @name $cookieStore#remove
193          *
194          * @description
195          * Remove given cookie
196          *
197          * @param {string} key Id of the key-value pair to delete.
198          */
199         remove: function(key) {
200           delete $cookies[key];
201         }
202       };
203
204     }]);
205
206
207 })(window, window.angular);