|
| 1 | +/** |
| 2 | + * @workInProgress |
| 3 | + * @ngdoc service |
| 4 | + * @name angular.service.$cookies |
| 5 | + * @requires $browser |
| 6 | + * |
| 7 | + * @description |
| 8 | + * Provides read/write access to browser's cookies. |
| 9 | + * |
| 10 | + * Only a simple Object is exposed and by adding or removing properties to/from |
| 11 | + * this object, new cookies are created/deleted at the end of current $eval. |
| 12 | + * |
| 13 | + * @example |
| 14 | + */ |
| 15 | +angularServiceInject('$cookies', function($browser) { |
| 16 | + var rootScope = this, |
| 17 | + cookies = {}, |
| 18 | + lastCookies = {}, |
| 19 | + lastBrowserCookies; |
| 20 | + |
| 21 | + //creates a poller fn that copies all cookies from the $browser to service & inits the service |
| 22 | + $browser.addPollFn(function() { |
| 23 | + var currentCookies = $browser.cookies(); |
| 24 | + if (lastBrowserCookies != currentCookies) { //relies on browser.cookies() impl |
| 25 | + lastBrowserCookies = currentCookies; |
| 26 | + copy(currentCookies, lastCookies); |
| 27 | + copy(currentCookies, cookies); |
| 28 | + rootScope.$eval(); |
| 29 | + } |
| 30 | + })(); |
| 31 | + |
| 32 | + //at the end of each eval, push cookies |
| 33 | + //TODO: this should happen before the "delayed" watches fire, because if some cookies are not |
| 34 | + // strings or browser refuses to store some cookies, we update the model in the push fn. |
| 35 | + this.$onEval(PRIORITY_LAST, push); |
| 36 | + |
| 37 | + return cookies; |
| 38 | + |
| 39 | + |
| 40 | + /** |
| 41 | + * Pushes all the cookies from the service to the browser and verifies if all cookies were stored. |
| 42 | + */ |
| 43 | + function push(){ |
| 44 | + var name, |
| 45 | + value, |
| 46 | + browserCookies, |
| 47 | + updated; |
| 48 | + |
| 49 | + //delete any cookies deleted in $cookies |
| 50 | + for (name in lastCookies) { |
| 51 | + if (isUndefined(cookies[name])) { |
| 52 | + $browser.cookies(name, _undefined); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + //update all cookies updated in $cookies |
| 57 | + for(name in cookies) { |
| 58 | + value = cookies[name]; |
| 59 | + if (!isString(value)) { |
| 60 | + if (isDefined(lastCookies[name])) { |
| 61 | + cookies[name] = lastCookies[name]; |
| 62 | + } else { |
| 63 | + delete cookies[name]; |
| 64 | + } |
| 65 | + } else if (value !== lastCookies[name]) { |
| 66 | + $browser.cookies(name, value); |
| 67 | + updated = true; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + //verify what was actually stored |
| 72 | + if (updated){ |
| 73 | + updated = false; |
| 74 | + browserCookies = $browser.cookies(); |
| 75 | + |
| 76 | + for (name in cookies) { |
| 77 | + if (cookies[name] !== browserCookies[name]) { |
| 78 | + //delete or reset all cookies that the browser dropped from $cookies |
| 79 | + if (isUndefined(browserCookies[name])) { |
| 80 | + delete cookies[name]; |
| 81 | + } else { |
| 82 | + cookies[name] = browserCookies[name]; |
| 83 | + } |
| 84 | + updated = true; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +}, ['$browser']); |
0 commit comments