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