Added store module, ecapsulated QS module

This commit is contained in:
Timothy Warren 2011-06-14 12:20:53 -04:00
parent 34a660651c
commit 6ecc805fd2
1 changed files with 109 additions and 64 deletions

173
kis.js
View File

@ -14,6 +14,7 @@
$_ = {}; $_ = {};
window.$_ = window.$_ || $_; window.$_ = window.$_ || $_;
window.$_.hb = (history.pushState) ? false : true;
$ = function(a) $ = function(a)
{ {
@ -102,82 +103,126 @@
* *
* Object for encoding and decoding querystrings and hashbang strings * Object for encoding and decoding querystrings and hashbang strings
*/ */
$_.qs = { (function(){
_parse: function(hb) var qs = {
{ parse: function(hb)
hb = hb || $_.hb;
var h, i, hString, pairs, pLen, data, y;
data = {};
if(hb === true)
{ {
h = location.hash.split('#!/'); hb = hb || $_.hb;
hString = (h.length > 1) ? h[1] : ''; var h, i, hString, pairs, pLen, data, y;
}
else if(hb === false || hb === undefined)
{
hString = window.location.search.substring(1);
}
else
{
return false;
}
pairs = hString.split('&');
pLen = pairs.length;
for(i=0;i<pLen;i++)
{
y = pairs[i].split('=');
if(y.length < 2) data = {};
if(hb === true)
{ {
return data; h = location.hash.split('#!/');
hString = (h.length > 1) ? h[1] : '';
}
else if(hb === false || hb === undefined)
{
hString = window.location.search.substring(1);
}
else
{
return false;
} }
data[y[0]] = y[1]; pairs = hString.split('&');
}
pLen = pairs.length;
return data;
}, for(i=0;i<pLen;i++)
set: function(key, value, hb) {
{ y = pairs[i].split('=');
hb = hb || $_.hb;
var pairs = this._parse(hb); if(y.length < 2)
{
if(key !== undefined && value !== undefined) return data;
}
data[y[0]] = y[1];
}
return data;
},
set: function(key, value, hb)
{ {
pairs[key] = value; hb = hb || $_.hb;
var pairs = this.parse(hb);
if(key !== undefined && value !== undefined)
{
pairs[key] = value;
}
var vars = [];
for (var x in pairs)
{
if(pairs.hasOwnProperty(x))
{
vars.push(x+'='+pairs[x]);
}
}
var qs = vars.join('&');
if(hb === true)
{
qs = '!/'+ qs;
location.hash = qs;
}
return qs;
},
get: function(key, hb)
{
hb = hb || $_.hb;
var pairs = this.parse(hb);
return (pairs[key]) ? pairs[key] : '';
} }
};
var vars = []; window.$_.qs = qs;
for (var x in pairs) }());
/**
* Store object
*
* Wrapper for localstorage data serialization
*/
(function(){
var store = {
get: function(key)
{ {
if(pairs.hasOwnProperty(x)) return JSON.parse(localStorage.getItem(key));
},
set: function(key, value)
{
if(typeof value === "object")
{ {
vars.push(x+'='+pairs[x]); value = JSON.stringify(value);
} }
} ls.setItem(key, value);
},
var qs = vars.join('&'); getAll: function()
if(hb === true)
{ {
qs = '!/'+ qs; var i, len, data;
location.hash = qs; len = localStorage.length;
data = {};
for(i=0;i<len;i++)
{
var name = localStorage.key(i);
var value = localStorage.getTime(name);
data[name] = value;
}
return data;
} }
};
return qs;
}, window.$_.store = store;
get: function(key, hb) }());
{
hb = hb || $_.hb;
var pairs = this._parse(hb);
return (pairs[key]) ? pairs[key] : '';
}
};
})(); })();