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