angular-cookies.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * @license AngularJS v1.3.15
  3. * (c) 2010-2014 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`} and
  20. * {@link ngCookies.$cookieStore `$cookieStore`} for usage.
  21. */
  22. angular.module('ngCookies', ['ng']).
  23. /**
  24. * @ngdoc service
  25. * @name $cookies
  26. *
  27. * @description
  28. * Provides read/write access to browser's cookies.
  29. *
  30. * Only a simple Object is exposed and by adding or removing properties to/from this object, new
  31. * cookies are created/deleted at the end of current $eval.
  32. * The object's properties can only be strings.
  33. *
  34. * Requires the {@link ngCookies `ngCookies`} module to be installed.
  35. *
  36. * @example
  37. *
  38. * ```js
  39. * angular.module('cookiesExample', ['ngCookies'])
  40. * .controller('ExampleController', ['$cookies', function($cookies) {
  41. * // Retrieving a cookie
  42. * var favoriteCookie = $cookies.myFavorite;
  43. * // Setting a cookie
  44. * $cookies.myFavorite = 'oatmeal';
  45. * }]);
  46. * ```
  47. */
  48. factory('$cookies', ['$rootScope', '$browser', function($rootScope, $browser) {
  49. var cookies = {},
  50. lastCookies = {},
  51. lastBrowserCookies,
  52. runEval = false,
  53. copy = angular.copy,
  54. isUndefined = angular.isUndefined;
  55. //creates a poller fn that copies all cookies from the $browser to service & inits the service
  56. $browser.addPollFn(function() {
  57. var currentCookies = $browser.cookies();
  58. if (lastBrowserCookies != currentCookies) { //relies on browser.cookies() impl
  59. lastBrowserCookies = currentCookies;
  60. copy(currentCookies, lastCookies);
  61. copy(currentCookies, cookies);
  62. if (runEval) $rootScope.$apply();
  63. }
  64. })();
  65. runEval = true;
  66. //at the end of each eval, push cookies
  67. //TODO: this should happen before the "delayed" watches fire, because if some cookies are not
  68. // strings or browser refuses to store some cookies, we update the model in the push fn.
  69. $rootScope.$watch(push);
  70. return cookies;
  71. /**
  72. * Pushes all the cookies from the service to the browser and verifies if all cookies were
  73. * stored.
  74. */
  75. function push() {
  76. var name,
  77. value,
  78. browserCookies,
  79. updated;
  80. //delete any cookies deleted in $cookies
  81. for (name in lastCookies) {
  82. if (isUndefined(cookies[name])) {
  83. $browser.cookies(name, undefined);
  84. }
  85. }
  86. //update all cookies updated in $cookies
  87. for (name in cookies) {
  88. value = cookies[name];
  89. if (!angular.isString(value)) {
  90. value = '' + value;
  91. cookies[name] = value;
  92. }
  93. if (value !== lastCookies[name]) {
  94. $browser.cookies(name, value);
  95. updated = true;
  96. }
  97. }
  98. //verify what was actually stored
  99. if (updated) {
  100. updated = false;
  101. browserCookies = $browser.cookies();
  102. for (name in cookies) {
  103. if (cookies[name] !== browserCookies[name]) {
  104. //delete or reset all cookies that the browser dropped from $cookies
  105. if (isUndefined(browserCookies[name])) {
  106. delete cookies[name];
  107. } else {
  108. cookies[name] = browserCookies[name];
  109. }
  110. updated = true;
  111. }
  112. }
  113. }
  114. }
  115. }]).
  116. /**
  117. * @ngdoc service
  118. * @name $cookieStore
  119. * @requires $cookies
  120. *
  121. * @description
  122. * Provides a key-value (string-object) storage, that is backed by session cookies.
  123. * Objects put or retrieved from this storage are automatically serialized or
  124. * deserialized by angular's toJson/fromJson.
  125. *
  126. * Requires the {@link ngCookies `ngCookies`} module to be installed.
  127. *
  128. * @example
  129. *
  130. * ```js
  131. * angular.module('cookieStoreExample', ['ngCookies'])
  132. * .controller('ExampleController', ['$cookieStore', function($cookieStore) {
  133. * // Put cookie
  134. * $cookieStore.put('myFavorite','oatmeal');
  135. * // Get cookie
  136. * var favoriteCookie = $cookieStore.get('myFavorite');
  137. * // Removing a cookie
  138. * $cookieStore.remove('myFavorite');
  139. * }]);
  140. * ```
  141. */
  142. factory('$cookieStore', ['$cookies', function($cookies) {
  143. return {
  144. /**
  145. * @ngdoc method
  146. * @name $cookieStore#get
  147. *
  148. * @description
  149. * Returns the value of given cookie key
  150. *
  151. * @param {string} key Id to use for lookup.
  152. * @returns {Object} Deserialized cookie value.
  153. */
  154. get: function(key) {
  155. var value = $cookies[key];
  156. return value ? angular.fromJson(value) : value;
  157. },
  158. /**
  159. * @ngdoc method
  160. * @name $cookieStore#put
  161. *
  162. * @description
  163. * Sets a value for given cookie key
  164. *
  165. * @param {string} key Id for the `value`.
  166. * @param {Object} value Value to be stored.
  167. */
  168. put: function(key, value) {
  169. $cookies[key] = angular.toJson(value);
  170. },
  171. /**
  172. * @ngdoc method
  173. * @name $cookieStore#remove
  174. *
  175. * @description
  176. * Remove given cookie
  177. *
  178. * @param {string} key Id of the key-value pair to delete.
  179. */
  180. remove: function(key) {
  181. delete $cookies[key];
  182. }
  183. };
  184. }]);
  185. })(window, window.angular);