Sin Descripción

angular-cookies.js 9.5KB

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