diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/AngularPublic.js | 7 | ||||
| -rw-r--r-- | src/Browser.js | 42 | ||||
| -rw-r--r-- | src/services.js | 51 |
3 files changed, 78 insertions, 22 deletions
diff --git a/src/AngularPublic.js b/src/AngularPublic.js index 40425b8d..617a7e2e 100644 --- a/src/AngularPublic.js +++ b/src/AngularPublic.js @@ -1,16 +1,17 @@ var browserSingleton; -angularService('$browser', function browserFactory(){ +angularService('$browser', function($log){ if (!browserSingleton) { browserSingleton = new Browser( window.location, jqLite(window.document), jqLite(window.document.getElementsByTagName('head')[0]), - XHR); + XHR, + $log); browserSingleton.startPoller(50, function(delay, fn){setTimeout(delay,fn);}); browserSingleton.bind(); } return browserSingleton; -}); +}, {inject:['$log']}); extend(angular, { 'element': jqLite, diff --git a/src/Browser.js b/src/Browser.js index e21e419b..c7e61e97 100644 --- a/src/Browser.js +++ b/src/Browser.js @@ -8,7 +8,7 @@ var XHR = window.XMLHttpRequest || function () { throw new Error("This browser does not support XMLHttpRequest."); }; -function Browser(location, document, head, XHR) { +function Browser(location, document, head, XHR, $log) { var self = this; self.isMock = false; @@ -106,28 +106,50 @@ function Browser(location, document, head, XHR) { var rawDocument = document[0]; var lastCookies = {}; var lastCookieString = ''; + /** - * cookies() -> hash of all cookies - * cookies(name, value) -> set name to value - * if value is undefined delete it - * cookies(name) -> should get value, but deletes (no one calls it right now that way) + * The cookies method provides a 'private' low level access to browser cookies. It is not meant to + * be used directly, use the $cookie service instead. + * + * The return values vary depending on the arguments that the method was called with as follows: + * <ul><li> + * cookies() -> hash of all cookies, this is NOT a copy of the internal state, so do not modify it + * </li><li> + * cookies(name, value) -> set name to value, if value is undefined delete the cookie + * </li><li> + * cookies(name) -> the same as (name, undefined) == DELETES (no one calls it right now that way) + * </li></ul> */ - self.cookies = function (name, value){ + self.cookies = function (/**string*/name, /**string*/value){ + var cookieLength, cookieArray, i, keyValue; + if (name) { if (value === _undefined) { delete lastCookies[name]; rawDocument.cookie = escape(name) + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT"; } else { - rawDocument.cookie = escape(name) + '=' + escape(lastCookies[name] = ''+value); + if (isString(value)) { + rawDocument.cookie = escape(name) + '=' + escape(lastCookies[name] = value); + + cookieLength = name.length + value.length + 1; + if (cookieLength > 4096) { + $log.warn("Cookie '"+ name +"' possibly not set or overflowed because it was too large ("+ + cookieLength + " > 4096 bytes)!"); + } + if (lastCookies.length > 20) { + $log.warn("Cookie '"+ name +"' possibly not set or overflowed because too many cookies " + + "were already set (" + lastCookies.length + " > 20 )"); + } + } } } else { if (rawDocument.cookie !== lastCookieString) { lastCookieString = rawDocument.cookie; - var cookieArray = lastCookieString.split("; "); + cookieArray = lastCookieString.split("; "); lastCookies = {}; - for (var i = 0; i < cookieArray.length; i++) { - var keyValue = cookieArray[i].split("="); + for (i = 0; i < cookieArray.length; i++) { + keyValue = cookieArray[i].split("="); if (keyValue.length === 2) { //ignore nameless cookies lastCookies[unescape(keyValue[0])] = unescape(keyValue[1]); } diff --git a/src/services.js b/src/services.js index f6dc931d..ddeebe1f 100644 --- a/src/services.js +++ b/src/services.js @@ -397,8 +397,18 @@ angularService('$resource', function($xhr){ }, {inject: ['$xhr.cache']}); +/** + * $cookies service provides read/write access to the browser cookies. Currently only session + * cookies are supported. + * + * Only a simple Object is exposed and by adding or removing properties to/from this object, new + * cookies are created or deleted from the browser at the end of the current eval. + */ angularService('$cookies', function($browser) { - var cookies = {}, rootScope = this, lastCookies; + var cookies = {}, + rootScope = this, + lastCookies; + $browser.addPollFn(function(){ var currentCookies = $browser.cookies(); if (lastCookies != currentCookies) { @@ -407,38 +417,61 @@ angularService('$cookies', function($browser) { rootScope.$eval(); } }); + this.$onEval(PRIORITY_FIRST, update); this.$onEval(PRIORITY_LAST, update); + return cookies; function update(){ - var name, browserCookies = $browser.cookies(); + var name, + browserCookies = $browser.cookies(); + + //$cookies -> $browser for(name in cookies) { - if (browserCookies[name] !== cookies[name]) { - $browser.cookies(name, browserCookies[name] = cookies[name]); + if (cookies[name] !== browserCookies[name]) { + $browser.cookies(name, cookies[name]); } } + + //get what was actually stored in the browser + browserCookies = $browser.cookies(); + + //$browser -> $cookies for(name in browserCookies) { - if (browserCookies[name] !== cookies[name]) { + if (isUndefined(cookies[name])) { $browser.cookies(name, _undefined); + } else { + cookies[name] = browserCookies[name]; + } + } + + //drop cookies in $cookies for cookies that $browser or real browser dropped + for (name in cookies) { + if (isUndefined(browserCookies[name])) { + delete cookies[name]; } } } }, {inject: ['$browser']}); -angularService('$sessionStore', function($store) { +/** + * $cookieStore provides a key-value (string-object) storage that is backed by session cookies. + * Objects put or retrieved from this storage are automatically serialized or deserialized. + */ +angularService('$cookieStore', function($store) { return { - get: function(key) { + get: function(/**string*/key) { return fromJson($store[key]); }, - put: function(key, value) { + put: function(/**string*/key, /**Object*/value) { $store[key] = toJson(value); }, - remove: function(key) { + remove: function(/**string*/key) { delete $store[key]; } }; |
