angular-cookies.js 9.8 KB

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