Skip to content

Commit

Permalink
Merge pull request #285 from joeyjojo/cookie-storage-location
Browse files Browse the repository at this point in the history
Enabled options object to set domain and pathless-ness of a cookie
  • Loading branch information
sdclibbery committed Sep 1, 2015
2 parents eabbbb9 + e36511a commit b14beac
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 95 deletions.
4 changes: 2 additions & 2 deletions static/script/devices/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,11 +700,11 @@ require.def('antie/devices/device',
* @param {String} namespace The storage namespace.
* @returns StorageProvider object.
*/
getStorage: function(storageType, namespace) {
getStorage: function(storageType, namespace, opts) {
if (storageType == StorageProvider.STORAGE_TYPE_SESSION) {
return SessionStorage.getNamespace(namespace);
} else if (storageType == StorageProvider.STORAGE_TYPE_PERSISTENT) {
return this.getPersistentStorage(namespace);
return this.getPersistentStorage(namespace, opts);
}
},
/**
Expand Down
207 changes: 114 additions & 93 deletions static/script/devices/storage/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,107 +25,128 @@
*/

require.def(
'antie/devices/storage/cookie',
[
'antie/devices/browserdevice',
'antie/storageprovider'
],
function(Device, StorageProvider) {
'use strict';
'antie/devices/storage/cookie',
[
'antie/devices/browserdevice',
'antie/storageprovider'
],
function(Device, StorageProvider) {
'use strict';

// http://www.quirksmode.org/js/cookies.html
var namespaces = {};
// http://www.quirksmode.org/js/cookies.html
var namespaces = {};

var default_days = 366;
var pathParts = document.location.pathname.split("/");
pathParts.pop();
var path = pathParts.join("/") + "/";
var default_days = 366;
var pathParts = document.location.pathname.split("/");
pathParts.pop();
var path = pathParts.join("/") + "/";

function createCookie(namespace, value, days) {
value = encodeURIComponent(value);
days = days || default_days;
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
document.cookie = namespace + "=" + value + expires + "; path=" + path;
}
function createCookie(namespace, value, days, opts) {
value = encodeURIComponent(value);
days = days || default_days;
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var cookieDataArray = [
namespace + "=" + value,
"expires=" + date.toGMTString(),
"path=" + (opts.isPathless ? '/' : path)
];
if (opts.domain){
cookieDataArray.push("domain="+opts.domain);
}
document.cookie = cookieDataArray.join('; ');
}

function readCookie(namespace) {
var nameEQ = namespace + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return decodeURIComponent(c.substring(nameEQ.length, c.length));
}
}
return null;
}
function readCookie(namespace) {
var nameEQ = namespace + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return decodeURIComponent(c.substring(nameEQ.length, c.length));
}
}
return null;
}

function eraseCookie(namespace) {
createCookie(namespace, "", -1);
}
function eraseCookie(namespace, opts) {
createCookie(namespace, "", -1, opts);
}

var CookieStorage = StorageProvider.extend({
init: function(namespace) {
this._super();
/**
* Class for persistently storing date in cookies
* @name antie.devices.storage.CookieStorage
* @class
* @extends antie.StorageProvider
* @param {String} namespace The cookie name to be used for this cookie storage object
* @param {Object} [opts]
* @param {String} [opts.domain] The domain value of the cookie, if not provided this is not set on the cookie
* @param {Boolean} [opts.isPathless] If <code>true</code> sets the path to '/' else retrieves the path from the location
*/
var CookieStorage = StorageProvider.extend(/** @lends antie.devices.storage.CookieStorage.prototype */{
/**
* @constructor
* @ignore
*/
init: function(namespace, opts) {
this._super();

this._namespace = namespace;
this._namespace = namespace;
this._opts = opts || {};

var cookie = readCookie(namespace);
var cookie = readCookie(namespace);

if(cookie) {
this._valueCache = Device.prototype.decodeJson(cookie);
if(this._valueCache) {
this._save();
} else {
this._valueCache = {};
}
}
},
setItem: function(key, value) {
this._super(key, value);
this._save();
},
removeItem: function(key) {
this._super(key);
this._save();
},
clear: function() {
this._super();
this._save();
if(cookie) {
this._valueCache = Device.prototype.decodeJson(cookie);
if(this._valueCache) {
this._save();
} else {
this._valueCache = {};
}
}
},
setItem: function(key, value) {
this._super(key, value);
this._save();
},
removeItem: function(key) {
this._super(key);
this._save();
},
clear: function() {
this._super();
this._save();

// delete it from the stored namespaces
// so it will be reloaded the next time
// we get it
delete namespaces[this._namespace];
},
_isEmpty: function() {
var prop;
for(prop in this._valueCache) {
return false;
}
return true;
},
_save: function() {
if(this._isEmpty()) {
eraseCookie(this._namespace);
} else {
var json = Device.prototype.encodeJson(this._valueCache);
createCookie(this._namespace, json);
}
}
});

Device.prototype.getPersistentStorage = function(namespace) {
if(!namespaces[namespace]) {
namespaces[namespace] = new CookieStorage(namespace);
}
return namespaces[namespace];
};
}
);
// delete it from the stored namespaces
// so it will be reloaded the next time
// we get it
delete namespaces[this._namespace];
},
_isEmpty: function() {
var prop;
for(prop in this._valueCache) {
return false;
}
return true;
},
_save: function() {
if(this._isEmpty()) {
eraseCookie(this._namespace, this._opts);
} else {
var json = Device.prototype.encodeJson(this._valueCache);
createCookie(this._namespace, json, undefined, this._opts);
}
}
});

Device.prototype.getPersistentStorage = function(namespace, opts) {
if(!namespaces[namespace]) {
namespaces[namespace] = new CookieStorage(namespace, opts);
}
return namespaces[namespace];
};
}
);

0 comments on commit b14beac

Please sign in to comment.