angular-cookies.js 12 KB

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