angular-cookies.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. The MIT License
  3. Copyright (c) 2010-2015 Google, Inc. http://angularjs.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. /**
  21. * @license AngularJS v1.3.8
  22. * (c) 2010-2014 Google, Inc. http://angularjs.org
  23. * License: MIT
  24. */
  25. (function(window, angular, undefined) {'use strict';
  26. /**
  27. * @ngdoc module
  28. * @name ngCookies
  29. * @description
  30. *
  31. * # ngCookies
  32. *
  33. * The `ngCookies` module provides a convenient wrapper for reading and writing browser cookies.
  34. *
  35. *
  36. * <div doc-module-components="ngCookies"></div>
  37. *
  38. * See {@link ngCookies.$cookies `$cookies`} and
  39. * {@link ngCookies.$cookieStore `$cookieStore`} for usage.
  40. */
  41. angular.module('ngCookies', ['ng']).
  42. /**
  43. * @ngdoc service
  44. * @name $cookies
  45. *
  46. * @description
  47. * Provides read/write access to browser's cookies.
  48. *
  49. * Only a simple Object is exposed and by adding or removing properties to/from this object, new
  50. * cookies are created/deleted at the end of current $eval.
  51. * The object's properties can only be strings.
  52. *
  53. * Requires the {@link ngCookies `ngCookies`} module to be installed.
  54. *
  55. * @example
  56. *
  57. * ```js
  58. * angular.module('cookiesExample', ['ngCookies'])
  59. * .controller('ExampleController', ['$cookies', function($cookies) {
  60. * // Retrieving a cookie
  61. * var favoriteCookie = $cookies.myFavorite;
  62. * // Setting a cookie
  63. * $cookies.myFavorite = 'oatmeal';
  64. * }]);
  65. * ```
  66. */
  67. factory('$cookies', ['$rootScope', '$browser', function($rootScope, $browser) {
  68. var cookies = {},
  69. lastCookies = {},
  70. lastBrowserCookies,
  71. runEval = false,
  72. copy = angular.copy,
  73. isUndefined = angular.isUndefined;
  74. //creates a poller fn that copies all cookies from the $browser to service & inits the service
  75. $browser.addPollFn(function() {
  76. var currentCookies = $browser.cookies();
  77. if (lastBrowserCookies != currentCookies) { //relies on browser.cookies() impl
  78. lastBrowserCookies = currentCookies;
  79. copy(currentCookies, lastCookies);
  80. copy(currentCookies, cookies);
  81. if (runEval) $rootScope.$apply();
  82. }
  83. })();
  84. runEval = true;
  85. //at the end of each eval, push cookies
  86. //TODO: this should happen before the "delayed" watches fire, because if some cookies are not
  87. // strings or browser refuses to store some cookies, we update the model in the push fn.
  88. $rootScope.$watch(push);
  89. return cookies;
  90. /**
  91. * Pushes all the cookies from the service to the browser and verifies if all cookies were
  92. * stored.
  93. */
  94. function push() {
  95. var name,
  96. value,
  97. browserCookies,
  98. updated;
  99. //delete any cookies deleted in $cookies
  100. for (name in lastCookies) {
  101. if (isUndefined(cookies[name])) {
  102. $browser.cookies(name, undefined);
  103. }
  104. }
  105. //update all cookies updated in $cookies
  106. for (name in cookies) {
  107. value = cookies[name];
  108. if (!angular.isString(value)) {
  109. value = '' + value;
  110. cookies[name] = value;
  111. }
  112. if (value !== lastCookies[name]) {
  113. $browser.cookies(name, value);
  114. updated = true;
  115. }
  116. }
  117. //verify what was actually stored
  118. if (updated) {
  119. updated = false;
  120. browserCookies = $browser.cookies();
  121. for (name in cookies) {
  122. if (cookies[name] !== browserCookies[name]) {
  123. //delete or reset all cookies that the browser dropped from $cookies
  124. if (isUndefined(browserCookies[name])) {
  125. delete cookies[name];
  126. } else {
  127. cookies[name] = browserCookies[name];
  128. }
  129. updated = true;
  130. }
  131. }
  132. }
  133. }
  134. }]).
  135. /**
  136. * @ngdoc service
  137. * @name $cookieStore
  138. * @requires $cookies
  139. *
  140. * @description
  141. * Provides a key-value (string-object) storage, that is backed by session cookies.
  142. * Objects put or retrieved from this storage are automatically serialized or
  143. * deserialized by angular's toJson/fromJson.
  144. *
  145. * Requires the {@link ngCookies `ngCookies`} module to be installed.
  146. *
  147. * @example
  148. *
  149. * ```js
  150. * angular.module('cookieStoreExample', ['ngCookies'])
  151. * .controller('ExampleController', ['$cookieStore', function($cookieStore) {
  152. * // Put cookie
  153. * $cookieStore.put('myFavorite','oatmeal');
  154. * // Get cookie
  155. * var favoriteCookie = $cookieStore.get('myFavorite');
  156. * // Removing a cookie
  157. * $cookieStore.remove('myFavorite');
  158. * }]);
  159. * ```
  160. */
  161. factory('$cookieStore', ['$cookies', function($cookies) {
  162. return {
  163. /**
  164. * @ngdoc method
  165. * @name $cookieStore#get
  166. *
  167. * @description
  168. * Returns the value of given cookie key
  169. *
  170. * @param {string} key Id to use for lookup.
  171. * @returns {Object} Deserialized cookie value.
  172. */
  173. get: function(key) {
  174. var value = $cookies[key];
  175. return value ? angular.fromJson(value) : value;
  176. },
  177. /**
  178. * @ngdoc method
  179. * @name $cookieStore#put
  180. *
  181. * @description
  182. * Sets a value for given cookie key
  183. *
  184. * @param {string} key Id for the `value`.
  185. * @param {Object} value Value to be stored.
  186. */
  187. put: function(key, value) {
  188. $cookies[key] = angular.toJson(value);
  189. },
  190. /**
  191. * @ngdoc method
  192. * @name $cookieStore#remove
  193. *
  194. * @description
  195. * Remove given cookie
  196. *
  197. * @param {string} key Id of the key-value pair to delete.
  198. */
  199. remove: function(key) {
  200. delete $cookies[key];
  201. }
  202. };
  203. }]);
  204. })(window, window.angular);