angular-cookies.js 9.5 KB

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