blob: a03ff49f2fea056c8e39bd967b71b7d26850f877 [file] [log] [blame]
Ed Tanous904063f2017-03-02 16:48:24 -08001/**
2 * @license AngularJS v1.5.8
3 * (c) 2010-2016 Google, Inc. http://angularjs.org
4 * License: MIT
5 */
6(function(window, angular) {'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`} for usage.
21 */
22
23
24angular.module('ngCookies', ['ng']).
25 /**
26 * @ngdoc provider
27 * @name $cookiesProvider
28 * @description
29 * Use `$cookiesProvider` to change the default behavior of the {@link ngCookies.$cookies $cookies} service.
30 * */
31 provider('$cookies', [function $CookiesProvider() {
32 /**
33 * @ngdoc property
34 * @name $cookiesProvider#defaults
35 * @description
36 *
37 * Object containing default options to pass when setting cookies.
38 *
39 * The object may have following properties:
40 *
41 * - **path** - `{string}` - The cookie will be available only for this path and its
42 * sub-paths. By default, this is the URL that appears in your `<base>` tag.
43 * - **domain** - `{string}` - The cookie will be available only for this domain and
44 * its sub-domains. For security reasons the user agent will not accept the cookie
45 * if the current domain is not a sub-domain of this domain or equal to it.
46 * - **expires** - `{string|Date}` - String of the form "Wdy, DD Mon YYYY HH:MM:SS GMT"
47 * or a Date object indicating the exact date/time this cookie will expire.
48 * - **secure** - `{boolean}` - If `true`, then the cookie will only be available through a
49 * secured connection.
50 *
51 * Note: By default, the address that appears in your `<base>` tag will be used as the path.
52 * This is important so that cookies will be visible for all routes when html5mode is enabled.
53 *
54 **/
55 var defaults = this.defaults = {};
56
57 function calcOptions(options) {
58 return options ? angular.extend({}, defaults, options) : defaults;
59 }
60
61 /**
62 * @ngdoc service
63 * @name $cookies
64 *
65 * @description
66 * Provides read/write access to browser's cookies.
67 *
68 * <div class="alert alert-info">
69 * Up until Angular 1.3, `$cookies` exposed properties that represented the
70 * current browser cookie values. In version 1.4, this behavior has changed, and
71 * `$cookies` now provides a standard api of getters, setters etc.
72 * </div>
73 *
74 * Requires the {@link ngCookies `ngCookies`} module to be installed.
75 *
76 * @example
77 *
78 * ```js
79 * angular.module('cookiesExample', ['ngCookies'])
80 * .controller('ExampleController', ['$cookies', function($cookies) {
81 * // Retrieving a cookie
82 * var favoriteCookie = $cookies.get('myFavorite');
83 * // Setting a cookie
84 * $cookies.put('myFavorite', 'oatmeal');
85 * }]);
86 * ```
87 */
88 this.$get = ['$$cookieReader', '$$cookieWriter', function($$cookieReader, $$cookieWriter) {
89 return {
90 /**
91 * @ngdoc method
92 * @name $cookies#get
93 *
94 * @description
95 * Returns the value of given cookie key
96 *
97 * @param {string} key Id to use for lookup.
98 * @returns {string} Raw cookie value.
99 */
100 get: function(key) {
101 return $$cookieReader()[key];
102 },
103
104 /**
105 * @ngdoc method
106 * @name $cookies#getObject
107 *
108 * @description
109 * Returns the deserialized value of given cookie key
110 *
111 * @param {string} key Id to use for lookup.
112 * @returns {Object} Deserialized cookie value.
113 */
114 getObject: function(key) {
115 var value = this.get(key);
116 return value ? angular.fromJson(value) : value;
117 },
118
119 /**
120 * @ngdoc method
121 * @name $cookies#getAll
122 *
123 * @description
124 * Returns a key value object with all the cookies
125 *
126 * @returns {Object} All cookies
127 */
128 getAll: function() {
129 return $$cookieReader();
130 },
131
132 /**
133 * @ngdoc method
134 * @name $cookies#put
135 *
136 * @description
137 * Sets a value for given cookie key
138 *
139 * @param {string} key Id for the `value`.
140 * @param {string} value Raw value to be stored.
141 * @param {Object=} options Options object.
142 * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
143 */
144 put: function(key, value, options) {
145 $$cookieWriter(key, value, calcOptions(options));
146 },
147
148 /**
149 * @ngdoc method
150 * @name $cookies#putObject
151 *
152 * @description
153 * Serializes and sets a value for given cookie key
154 *
155 * @param {string} key Id for the `value`.
156 * @param {Object} value Value to be stored.
157 * @param {Object=} options Options object.
158 * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
159 */
160 putObject: function(key, value, options) {
161 this.put(key, angular.toJson(value), options);
162 },
163
164 /**
165 * @ngdoc method
166 * @name $cookies#remove
167 *
168 * @description
169 * Remove given cookie
170 *
171 * @param {string} key Id of the key-value pair to delete.
172 * @param {Object=} options Options object.
173 * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
174 */
175 remove: function(key, options) {
176 $$cookieWriter(key, undefined, calcOptions(options));
177 }
178 };
179 }];
180 }]);
181
182angular.module('ngCookies').
183/**
184 * @ngdoc service
185 * @name $cookieStore
186 * @deprecated
187 * @requires $cookies
188 *
189 * @description
190 * Provides a key-value (string-object) storage, that is backed by session cookies.
191 * Objects put or retrieved from this storage are automatically serialized or
192 * deserialized by angular's toJson/fromJson.
193 *
194 * Requires the {@link ngCookies `ngCookies`} module to be installed.
195 *
196 * <div class="alert alert-danger">
197 * **Note:** The $cookieStore service is **deprecated**.
198 * Please use the {@link ngCookies.$cookies `$cookies`} service instead.
199 * </div>
200 *
201 * @example
202 *
203 * ```js
204 * angular.module('cookieStoreExample', ['ngCookies'])
205 * .controller('ExampleController', ['$cookieStore', function($cookieStore) {
206 * // Put cookie
207 * $cookieStore.put('myFavorite','oatmeal');
208 * // Get cookie
209 * var favoriteCookie = $cookieStore.get('myFavorite');
210 * // Removing a cookie
211 * $cookieStore.remove('myFavorite');
212 * }]);
213 * ```
214 */
215 factory('$cookieStore', ['$cookies', function($cookies) {
216
217 return {
218 /**
219 * @ngdoc method
220 * @name $cookieStore#get
221 *
222 * @description
223 * Returns the value of given cookie key
224 *
225 * @param {string} key Id to use for lookup.
226 * @returns {Object} Deserialized cookie value, undefined if the cookie does not exist.
227 */
228 get: function(key) {
229 return $cookies.getObject(key);
230 },
231
232 /**
233 * @ngdoc method
234 * @name $cookieStore#put
235 *
236 * @description
237 * Sets a value for given cookie key
238 *
239 * @param {string} key Id for the `value`.
240 * @param {Object} value Value to be stored.
241 */
242 put: function(key, value) {
243 $cookies.putObject(key, value);
244 },
245
246 /**
247 * @ngdoc method
248 * @name $cookieStore#remove
249 *
250 * @description
251 * Remove given cookie
252 *
253 * @param {string} key Id of the key-value pair to delete.
254 */
255 remove: function(key) {
256 $cookies.remove(key);
257 }
258 };
259
260 }]);
261
262/**
263 * @name $$cookieWriter
264 * @requires $document
265 *
266 * @description
267 * This is a private service for writing cookies
268 *
269 * @param {string} name Cookie name
270 * @param {string=} value Cookie value (if undefined, cookie will be deleted)
271 * @param {Object=} options Object with options that need to be stored for the cookie.
272 */
273function $$CookieWriter($document, $log, $browser) {
274 var cookiePath = $browser.baseHref();
275 var rawDocument = $document[0];
276
277 function buildCookieString(name, value, options) {
278 var path, expires;
279 options = options || {};
280 expires = options.expires;
281 path = angular.isDefined(options.path) ? options.path : cookiePath;
282 if (angular.isUndefined(value)) {
283 expires = 'Thu, 01 Jan 1970 00:00:00 GMT';
284 value = '';
285 }
286 if (angular.isString(expires)) {
287 expires = new Date(expires);
288 }
289
290 var str = encodeURIComponent(name) + '=' + encodeURIComponent(value);
291 str += path ? ';path=' + path : '';
292 str += options.domain ? ';domain=' + options.domain : '';
293 str += expires ? ';expires=' + expires.toUTCString() : '';
294 str += options.secure ? ';secure' : '';
295
296 // per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum:
297 // - 300 cookies
298 // - 20 cookies per unique domain
299 // - 4096 bytes per cookie
300 var cookieLength = str.length + 1;
301 if (cookieLength > 4096) {
302 $log.warn("Cookie '" + name +
303 "' possibly not set or overflowed because it was too large (" +
304 cookieLength + " > 4096 bytes)!");
305 }
306
307 return str;
308 }
309
310 return function(name, value, options) {
311 rawDocument.cookie = buildCookieString(name, value, options);
312 };
313}
314
315$$CookieWriter.$inject = ['$document', '$log', '$browser'];
316
317angular.module('ngCookies').provider('$$cookieWriter', function $$CookieWriterProvider() {
318 this.$get = $$CookieWriter;
319});
320
321
322})(window, window.angular);