2 * @license AngularJS v1.4.8
3 * (c) 2010-2015 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 would be 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 obvious security reasons the user agent will not accept the
45 * cookie if the current domain is not a sub domain or equals to the requested domain.
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}` - The cookie will be available only in secured connection.
50 * Note: by default the address that appears in your `<base>` tag will be used as path.
51 * This is important so that cookies will be visible for all routes in case html5mode is enabled
54 var defaults = this.defaults = {};
56 function calcOptions(options) {
57 return options ? angular.extend({}, defaults, options) : defaults;
65 * Provides read/write access to browser's cookies.
67 * <div class="alert alert-info">
68 * Up until Angular 1.3, `$cookies` exposed properties that represented the
69 * current browser cookie values. In version 1.4, this behavior has changed, and
70 * `$cookies` now provides a standard api of getters, setters etc.
73 * Requires the {@link ngCookies `ngCookies`} module to be installed.
78 * angular.module('cookiesExample', ['ngCookies'])
79 * .controller('ExampleController', ['$cookies', function($cookies) {
80 * // Retrieving a cookie
81 * var favoriteCookie = $cookies.get('myFavorite');
83 * $cookies.put('myFavorite', 'oatmeal');
87 this.$get = ['$$cookieReader', '$$cookieWriter', function($$cookieReader, $$cookieWriter) {
94 * Returns the value of given cookie key
96 * @param {string} key Id to use for lookup.
97 * @returns {string} Raw cookie value.
100 return $$cookieReader()[key];
105 * @name $cookies#getObject
108 * Returns the deserialized value of given cookie key
110 * @param {string} key Id to use for lookup.
111 * @returns {Object} Deserialized cookie value.
113 getObject: function(key) {
114 var value = this.get(key);
115 return value ? angular.fromJson(value) : value;
120 * @name $cookies#getAll
123 * Returns a key value object with all the cookies
125 * @returns {Object} All cookies
128 return $$cookieReader();
136 * Sets a value for given cookie key
138 * @param {string} key Id for the `value`.
139 * @param {string} value Raw value to be stored.
140 * @param {Object=} options Options object.
141 * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
143 put: function(key, value, options) {
144 $$cookieWriter(key, value, calcOptions(options));
149 * @name $cookies#putObject
152 * Serializes and sets a value for given cookie key
154 * @param {string} key Id for the `value`.
155 * @param {Object} value Value to be stored.
156 * @param {Object=} options Options object.
157 * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
159 putObject: function(key, value, options) {
160 this.put(key, angular.toJson(value), options);
165 * @name $cookies#remove
168 * Remove given cookie
170 * @param {string} key Id of the key-value pair to delete.
171 * @param {Object=} options Options object.
172 * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
174 remove: function(key, options) {
175 $$cookieWriter(key, undefined, calcOptions(options));
181 angular.module('ngCookies').
189 * Provides a key-value (string-object) storage, that is backed by session cookies.
190 * Objects put or retrieved from this storage are automatically serialized or
191 * deserialized by angular's toJson/fromJson.
193 * Requires the {@link ngCookies `ngCookies`} module to be installed.
195 * <div class="alert alert-danger">
196 * **Note:** The $cookieStore service is **deprecated**.
197 * Please use the {@link ngCookies.$cookies `$cookies`} service instead.
203 * angular.module('cookieStoreExample', ['ngCookies'])
204 * .controller('ExampleController', ['$cookieStore', function($cookieStore) {
206 * $cookieStore.put('myFavorite','oatmeal');
208 * var favoriteCookie = $cookieStore.get('myFavorite');
209 * // Removing a cookie
210 * $cookieStore.remove('myFavorite');
214 factory('$cookieStore', ['$cookies', function($cookies) {
219 * @name $cookieStore#get
222 * Returns the value of given cookie key
224 * @param {string} key Id to use for lookup.
225 * @returns {Object} Deserialized cookie value, undefined if the cookie does not exist.
228 return $cookies.getObject(key);
233 * @name $cookieStore#put
236 * Sets a value for given cookie key
238 * @param {string} key Id for the `value`.
239 * @param {Object} value Value to be stored.
241 put: function(key, value) {
242 $cookies.putObject(key, value);
247 * @name $cookieStore#remove
250 * Remove given cookie
252 * @param {string} key Id of the key-value pair to delete.
254 remove: function(key) {
255 $cookies.remove(key);
262 * @name $$cookieWriter
263 * @requires $document
266 * This is a private service for writing cookies
268 * @param {string} name Cookie name
269 * @param {string=} value Cookie value (if undefined, cookie will be deleted)
270 * @param {Object=} options Object with options that need to be stored for the cookie.
272 function $$CookieWriter($document, $log, $browser) {
273 var cookiePath = $browser.baseHref();
274 var rawDocument = $document[0];
276 function buildCookieString(name, value, options) {
278 options = options || {};
279 expires = options.expires;
280 path = angular.isDefined(options.path) ? options.path : cookiePath;
281 if (angular.isUndefined(value)) {
282 expires = 'Thu, 01 Jan 1970 00:00:00 GMT';
285 if (angular.isString(expires)) {
286 expires = new Date(expires);
289 var str = encodeURIComponent(name) + '=' + encodeURIComponent(value);
290 str += path ? ';path=' + path : '';
291 str += options.domain ? ';domain=' + options.domain : '';
292 str += expires ? ';expires=' + expires.toUTCString() : '';
293 str += options.secure ? ';secure' : '';
295 // per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum:
297 // - 20 cookies per unique domain
298 // - 4096 bytes per cookie
299 var cookieLength = str.length + 1;
300 if (cookieLength > 4096) {
301 $log.warn("Cookie '" + name +
302 "' possibly not set or overflowed because it was too large (" +
303 cookieLength + " > 4096 bytes)!");
309 return function(name, value, options) {
310 rawDocument.cookie = buildCookieString(name, value, options);
314 $$CookieWriter.$inject = ['$document', '$log', '$browser'];
316 angular.module('ngCookies').provider('$$cookieWriter', function $$CookieWriterProvider() {
317 this.$get = $$CookieWriter;
321 })(window, window.angular);