kis-js/kis.js

228 lines
3.9 KiB
JavaScript
Raw Normal View History

2011-06-11 17:08:00 -04:00
/**
Kis JS Keep It Simple JS Library
Copyright Timothy J. Warren
License Public Domain
Version 0.0.1
*/
(function(){
"use strict";
var $_, $, kis;
2011-06-13 11:34:15 -04:00
$_ = {};
2011-06-11 17:08:00 -04:00
window.$_ = window.$_ || $_;
window.$_.hb = (history.pushState) ? false : true;
2011-06-11 17:08:00 -04:00
$ = function(a)
{
var x = document.querySelectorAll(a);
return (x.length === 1) ? x[0] : x;
};
window.$ = window.$ || $;
2011-06-13 11:34:15 -04:00
/**
* Ajax
*
* Object for making ajax requests
*/
2011-06-11 17:08:00 -04:00
(function() {
var $_ = $_ || {};
var ajax = {
_req: function()
{
return (window.XMLHttpRequest)
? new XMLHttpRequest()
: new ActiveXObject("Microsoft.XMLHTTP");
},
_do: function(url, data, callback, isPost)
{
var request = this._req();
var type = (isPost) ? "POST" : "GET";
url += (type === "GET")
? "?" + this._serialize(data)
: '';
request.open(type, url);
request.onreadystatechange = function(){
if(request.readyState === 4)
{
callback(request.responseText);
}
2011-06-13 11:34:15 -04:00
};
2011-06-11 17:08:00 -04:00
if(type === "POST")
{
2011-06-13 11:46:26 -04:00
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
2011-06-11 17:08:00 -04:00
request.send(this._serialize(data));
}
else
{
request.send(null);
}
},
_serialize: function(data)
{
var pairs = [];
for (var name in data)
{
if(!data.hasOwnProperty(name)) continue;
if(typeof data[name] === "function") continue;
var value = data[name].toString();
name = encodeURIComponent(name.replace(" ", "+"));
value = encodeURIComponent(value.replace(" ","+"));
pairs.push(name + "=" + value);
}
return pairs.join("&");
}
};
window.$_.get = function(url, data, callback)
{
ajax._do(url, data, callback, false);
};
window.$_.post = function(url, data, callback)
{
ajax._do(url, data, callback, true);
};
}());
/**
* Qs
*
* Object for encoding and decoding querystrings and hashbang strings
*/
(function(){
var qs = {
parse: function(hb)
2011-06-11 17:08:00 -04:00
{
hb = hb || $_.hb;
var h, i, hString, pairs, pLen, data, y;
data = {};
if(hb === true)
{
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;
}
pairs = hString.split('&');
pLen = pairs.length;
for(i=0;i<pLen;i++)
{
y = pairs[i].split('=');
if(y.length < 2)
{
return data;
}
data[y[0]] = y[1];
}
return data;
},
set: function(key, value, hb)
2011-06-11 17:08:00 -04:00
{
hb = hb || $_.hb;
var pairs = this.parse(hb);
if(key !== undefined && value !== undefined)
{
pairs[key] = value;
}
2011-06-11 17:08:00 -04:00
var vars = [];
for (var x in pairs)
{
if(pairs.hasOwnProperty(x))
{
vars.push(x+'='+pairs[x]);
}
}
var qs = vars.join('&');
2011-06-11 17:08:00 -04:00
if(hb === true)
2011-06-11 17:08:00 -04:00
{
qs = '!/'+ qs;
location.hash = qs;
2011-06-11 17:08:00 -04:00
}
return qs;
},
get: function(key, hb)
2011-06-11 17:08:00 -04:00
{
hb = hb || $_.hb;
var pairs = this.parse(hb);
return (pairs[key]) ? pairs[key] : '';
2011-06-11 17:08:00 -04:00
}
};
2011-06-11 17:08:00 -04:00
window.$_.qs = qs;
}());
/**
* Store object
*
* Wrapper for localstorage data serialization
*/
(function(){
var store = {
get: function(key)
{
return JSON.parse(localStorage.getItem(key));
},
set: function(key, value)
2011-06-11 17:08:00 -04:00
{
if(typeof value === "object")
2011-06-11 17:08:00 -04:00
{
value = JSON.stringify(value);
2011-06-11 17:08:00 -04:00
}
ls.setItem(key, value);
},
getAll: function()
2011-06-11 17:08:00 -04:00
{
var i, len, data;
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;
2011-06-11 17:08:00 -04:00
}
};
window.$_.store = store;
}());
2011-06-11 17:08:00 -04:00
2011-06-13 11:34:15 -04:00
})();