2 * @license AngularJS v1.5.0
3 * (c) 2010-2016 Google, Inc. http://angularjs.org
6 (function(window, angular, undefined) {'use strict';
15 * The `ngCookies` module provides a convenient wrapper for reading and writing browser cookies.
18 * <div doc-module-components="ngCookies"></div>
20 * See {@link ngCookies.$cookies `$cookies`} for usage.
24 angular.module('ngCookies', ['ng']).
27 * @name $cookiesProvider
29 * Use `$cookiesProvider` to change the default behavior of the {@link ngCookies.$cookies $cookies} service.
31 provider('$cookies', [function $CookiesProvider() {
34 * @name $cookiesProvider#defaults
37 * Object containing default options to pass when setting cookies.
39 * The object may have following properties:
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
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.
55 var defaults = this.defaults = {};
57 function calcOptions(options) {
58 return options ? angular.extend({}, defaults, options) : defaults;
66 * Provides read/write access to browser's cookies.
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.
74 * Requires the {@link ngCookies `ngCookies`} module to be installed.
79 * angular.module('cookiesExample', ['ngCookies'])
80 * .controller('ExampleController', ['$cookies', function($cookies) {
81 * // Retrieving a cookie
82 * var favoriteCookie = $cookies.get('myFavorite');
84 * $cookies.put('myFavorite', 'oatmeal');
88 this.$get = ['$$cookieReader', '$$cookieWriter', function($$cookieReader, $$cookieWriter) {
95 * Returns the value of given cookie key
97 * @param {string} key Id to use for lookup.
98 * @returns {string} Raw cookie value.
101 return $$cookieReader()[key];
106 * @name $cookies#getObject
109 * Returns the deserialized value of given cookie key
111 * @param {string} key Id to use for lookup.
112 * @returns {Object} Deserialized cookie value.
114 getObject: function(key) {
115 var value = this.get(key);
116 return value ? angular.fromJson(value) : value;
121 * @name $cookies#getAll
124 * Returns a key value object with all the cookies
126 * @returns {Object} All cookies
129 return $$cookieReader();
137 * Sets a value for given cookie key
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}
144 put: function(key, value, options) {
145 $$cookieWriter(key, value, calcOptions(options));
150 * @name $cookies#putObject
153 * Serializes and sets a value for given cookie key
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}
160 putObject: function(key, value, options) {
161 this.put(key, angular.toJson(value), options);
166 * @name $cookies#remove
169 * Remove given cookie
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}
175 remove: function(key, options) {
176 $$cookieWriter(key, undefined, calcOptions(options));
182 angular.module('ngCookies').
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.
194 * Requires the {@link ngCookies `ngCookies`} module to be installed.
196 * <div class="alert alert-danger">
197 * **Note:** The $cookieStore service is **deprecated**.
198 * Please use the {@link ngCookies.$cookies `$cookies`} service instead.
204 * angular.module('cookieStoreExample', ['ngCookies'])
205 * .controller('ExampleController', ['$cookieStore', function($cookieStore) {
207 * $cookieStore.put('myFavorite','oatmeal');
209 * var favoriteCookie = $cookieStore.get('myFavorite');
210 * // Removing a cookie
211 * $cookieStore.remove('myFavorite');
215 factory('$cookieStore', ['$cookies', function($cookies) {
220 * @name $cookieStore#get
223 * Returns the value of given cookie key
225 * @param {string} key Id to use for lookup.
226 * @returns {Object} Deserialized cookie value, undefined if the cookie does not exist.
229 return $cookies.getObject(key);
234 * @name $cookieStore#put
237 * Sets a value for given cookie key
239 * @param {string} key Id for the `value`.
240 * @param {Object} value Value to be stored.
242 put: function(key, value) {
243 $cookies.putObject(key, value);
248 * @name $cookieStore#remove
251 * Remove given cookie
253 * @param {string} key Id of the key-value pair to delete.
255 remove: function(key) {
256 $cookies.remove(key);
263 * @name $$cookieWriter
264 * @requires $document
267 * This is a private service for writing cookies
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.
273 function $$CookieWriter($document, $log, $browser) {
274 var cookiePath = $browser.baseHref();
275 var rawDocument = $document[0];
277 function buildCookieString(name, value, options) {
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';
286 if (angular.isString(expires)) {
287 expires = new Date(expires);
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' : '';
296 // per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum:
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)!");
310 return function(name, value, options) {
311 rawDocument.cookie = buildCookieString(name, value, options);
315 $$CookieWriter.$inject = ['$document', '$log', '$browser'];
317 angular.module('ngCookies').provider('$$cookieWriter', function $$CookieWriterProvider() {
318 this.$get = $$CookieWriter;
322 })(window, window.angular);