2 * @license AngularJS v1.4.0
\r
3 * (c) 2010-2015 Google, Inc. http://angularjs.org
\r
6 (function(window, angular, undefined) {'use strict';
\r
15 * The `ngCookies` module provides a convenient wrapper for reading and writing browser cookies.
\r
18 * <div doc-module-components="ngCookies"></div>
\r
20 * See {@link ngCookies.$cookies `$cookies`} and
\r
21 * {@link ngCookies.$cookieStore `$cookieStore`} for usage.
\r
25 angular.module('ngCookies', ['ng']).
\r
28 * @name $cookiesProvider
\r
30 * Use `$cookiesProvider` to change the default behavior of the {@link ngCookies.$cookies $cookies} service.
\r
32 provider('$cookies', [function $CookiesProvider() {
\r
35 * @name $cookiesProvider#defaults
\r
38 * Object containing default options to pass when setting cookies.
\r
40 * The object may have following properties:
\r
42 * - **path** - `{string}` - The cookie will be available only for this path and its
\r
43 * sub-paths. By default, this would be the URL that appears in your base tag.
\r
44 * - **domain** - `{string}` - The cookie will be available only for this domain and
\r
45 * its sub-domains. For obvious security reasons the user agent will not accept the
\r
46 * cookie if the current domain is not a sub domain or equals to the requested domain.
\r
47 * - **expires** - `{string|Date}` - String of the form "Wdy, DD Mon YYYY HH:MM:SS GMT"
\r
48 * or a Date object indicating the exact date/time this cookie will expire.
\r
49 * - **secure** - `{boolean}` - The cookie will be available only in secured connection.
\r
51 * Note: by default the address that appears in your <base> tag will be used as path.
\r
52 * This is import so that cookies will be visible for all routes in case html5mode is enabled
\r
55 var defaults = this.defaults = {};
\r
57 function calcOptions(options) {
\r
58 return options ? angular.extend({}, defaults, options) : defaults;
\r
66 * Provides read/write access to browser's cookies.
\r
68 * BREAKING CHANGE: `$cookies` no longer exposes properties that represent the
\r
69 * current browser cookie values. Now you must use the get/put/remove/etc. methods
\r
70 * as described below.
\r
72 * Requires the {@link ngCookies `ngCookies`} module to be installed.
\r
77 * angular.module('cookiesExample', ['ngCookies'])
\r
78 * .controller('ExampleController', ['$cookies', function($cookies) {
\r
79 * // Retrieving a cookie
\r
80 * var favoriteCookie = $cookies.get('myFavorite');
\r
81 * // Setting a cookie
\r
82 * $cookies.put('myFavorite', 'oatmeal');
\r
86 this.$get = ['$$cookieReader', '$$cookieWriter', function($$cookieReader, $$cookieWriter) {
\r
90 * @name $cookies#get
\r
93 * Returns the value of given cookie key
\r
95 * @param {string} key Id to use for lookup.
\r
96 * @returns {string} Raw cookie value.
\r
98 get: function(key) {
\r
99 return $$cookieReader()[key];
\r
104 * @name $cookies#getObject
\r
107 * Returns the deserialized value of given cookie key
\r
109 * @param {string} key Id to use for lookup.
\r
110 * @returns {Object} Deserialized cookie value.
\r
112 getObject: function(key) {
\r
113 var value = this.get(key);
\r
114 return value ? angular.fromJson(value) : value;
\r
119 * @name $cookies#getAll
\r
122 * Returns a key value object with all the cookies
\r
124 * @returns {Object} All cookies
\r
126 getAll: function() {
\r
127 return $$cookieReader();
\r
132 * @name $cookies#put
\r
135 * Sets a value for given cookie key
\r
137 * @param {string} key Id for the `value`.
\r
138 * @param {string} value Raw value to be stored.
\r
139 * @param {Object=} options Options object.
\r
140 * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
\r
142 put: function(key, value, options) {
\r
143 $$cookieWriter(key, value, calcOptions(options));
\r
148 * @name $cookies#putObject
\r
151 * Serializes and sets a value for given cookie key
\r
153 * @param {string} key Id for the `value`.
\r
154 * @param {Object} value Value to be stored.
\r
155 * @param {Object=} options Options object.
\r
156 * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
\r
158 putObject: function(key, value, options) {
\r
159 this.put(key, angular.toJson(value), options);
\r
164 * @name $cookies#remove
\r
167 * Remove given cookie
\r
169 * @param {string} key Id of the key-value pair to delete.
\r
170 * @param {Object=} options Options object.
\r
171 * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
\r
173 remove: function(key, options) {
\r
174 $$cookieWriter(key, undefined, calcOptions(options));
\r
180 angular.module('ngCookies').
\r
183 * @name $cookieStore
\r
185 * @requires $cookies
\r
188 * Provides a key-value (string-object) storage, that is backed by session cookies.
\r
189 * Objects put or retrieved from this storage are automatically serialized or
\r
190 * deserialized by angular's toJson/fromJson.
\r
192 * Requires the {@link ngCookies `ngCookies`} module to be installed.
\r
194 * <div class="alert alert-danger">
\r
195 * **Note:** The $cookieStore service is deprecated.
\r
196 * Please use the {@link ngCookies.$cookies `$cookies`} service instead.
\r
202 * angular.module('cookieStoreExample', ['ngCookies'])
\r
203 * .controller('ExampleController', ['$cookieStore', function($cookieStore) {
\r
205 * $cookieStore.put('myFavorite','oatmeal');
\r
207 * var favoriteCookie = $cookieStore.get('myFavorite');
\r
208 * // Removing a cookie
\r
209 * $cookieStore.remove('myFavorite');
\r
213 factory('$cookieStore', ['$cookies', function($cookies) {
\r
218 * @name $cookieStore#get
\r
221 * Returns the value of given cookie key
\r
223 * @param {string} key Id to use for lookup.
\r
224 * @returns {Object} Deserialized cookie value, undefined if the cookie does not exist.
\r
226 get: function(key) {
\r
227 return $cookies.getObject(key);
\r
232 * @name $cookieStore#put
\r
235 * Sets a value for given cookie key
\r
237 * @param {string} key Id for the `value`.
\r
238 * @param {Object} value Value to be stored.
\r
240 put: function(key, value) {
\r
241 $cookies.putObject(key, value);
\r
246 * @name $cookieStore#remove
\r
249 * Remove given cookie
\r
251 * @param {string} key Id of the key-value pair to delete.
\r
253 remove: function(key) {
\r
254 $cookies.remove(key);
\r
261 * @name $$cookieWriter
\r
262 * @requires $document
\r
265 * This is a private service for writing cookies
\r
267 * @param {string} name Cookie name
\r
268 * @param {string=} value Cookie value (if undefined, cookie will be deleted)
\r
269 * @param {Object=} options Object with options that need to be stored for the cookie.
\r
271 function $$CookieWriter($document, $log, $browser) {
\r
272 var cookiePath = $browser.baseHref();
\r
273 var rawDocument = $document[0];
\r
275 function buildCookieString(name, value, options) {
\r
277 options = options || {};
\r
278 expires = options.expires;
\r
279 path = angular.isDefined(options.path) ? options.path : cookiePath;
\r
280 if (value === undefined) {
\r
281 expires = 'Thu, 01 Jan 1970 00:00:00 GMT';
\r
284 if (angular.isString(expires)) {
\r
285 expires = new Date(expires);
\r
288 var str = encodeURIComponent(name) + '=' + encodeURIComponent(value);
\r
289 str += path ? ';path=' + path : '';
\r
290 str += options.domain ? ';domain=' + options.domain : '';
\r
291 str += expires ? ';expires=' + expires.toUTCString() : '';
\r
292 str += options.secure ? ';secure' : '';
\r
294 // per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum:
\r
296 // - 20 cookies per unique domain
\r
297 // - 4096 bytes per cookie
\r
298 var cookieLength = str.length + 1;
\r
299 if (cookieLength > 4096) {
\r
300 $log.warn("Cookie '" + name +
\r
301 "' possibly not set or overflowed because it was too large (" +
\r
302 cookieLength + " > 4096 bytes)!");
\r
308 return function(name, value, options) {
\r
309 rawDocument.cookie = buildCookieString(name, value, options);
\r
313 $$CookieWriter.$inject = ['$document', '$log', '$browser'];
\r
315 angular.module('ngCookies').provider('$$cookieWriter', function $$CookieWriterProvider() {
\r
316 this.$get = $$CookieWriter;
\r
320 })(window, window.angular);
\r