Skip to content

Instantly share code, notes, and snippets.

@pachanka
Last active October 7, 2015 06:53
Show Gist options
  • Save pachanka/3b71c3de8c520950f175 to your computer and use it in GitHub Desktop.
Save pachanka/3b71c3de8c520950f175 to your computer and use it in GitHub Desktop.
A options suite in jQuery that will use localstorage or cookies and set the options in the html page.
/* Cookie or storage functions */
function storage_test(){
var test = 'test';
try {
localStorage.setItem(test, test);
localStorage.removeItem(test);
return true;
} catch(e) {
return false;
}
}
function read_storage(name){
if(storage_test() === true){
var ls = localStorage.getItem(name);
if(ls){
ls = JSON.parse(ls);
if(!ls.hasOwnProperty('expires')
|| ls.timestamp < new Date()){
delete_storage(name);
ls = null;
}
}else{
ls = null;
}
return ls;
}else{
var nameEQ = name + "=";
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 JSON.parse(c.substring(nameEQ.length,c.length));
}
return null;
}
}
function set_storage(name,value,days){
if(storage_test() === true){
value.expires = (days*24*60*60*1000);
return localStorage.setItem(name,JSON.stringify(value));
}else{
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}else{
var expires = "";
}
document.cookie = name+"="+JSON.stringify(value)+expires+"; path=/";
return (read_storage(name) !== null) ? true : false;
}
}
function delete_storage(name){
if(storage_test() === true){
return localStorage.removeItem(name);
}else{
return set_storage(name,"",-1);
}
}
function first_time(){
var options = read_storage(Global.options_name);
if(!options){
var count = 5;
var warning = $('<div style="position:fixed;bottom:0;height:50px;width:100%;background-color:#000;color:#fff;">'+
'This site uses cookies to store data about its users preferences. '+
'If you disagree with this policy you can leave in the next <i>'+count+'</i> seconds.'+
'</div>').appendTo('body');
var intervalID = window.setInterval(
function(){
count = count - 1;
if(count < 0){
update_options({
'#first-time' : {
input : 'checkbox',
value : false
}
});
clearInterval(intervalID);
}else{
warning.find('i').text(count);
}
}
, 1000);
}
}
function delete_options(){
delete_storage(Global.options_name);
Global['options'] = load_options();
return true;
}
// opts can be array or string
function get_options(opts){
var all_opts = read_storage(Global.options_name);
var out = false;
if(all_opts){
if(typeof opts == 'object' && opts.length){
var ret = {};
for (var i = 0; i < opts.length; i++) {
if(all_opts.hasOwnProperty(opts[i])){
ret[opts[i]] = all_opts[opts[i]];
}
};
out = ($.isEmptyObject(ret)) ? false : ret;
}else if(typeof opts == 'string'){
out = (all_opts.hasOwnProperty(opts)) ? all_opts[opts] : false;
}else{
out = all_opts;
}
}
return out;
}
function update_options(new_options){
var old_options = read_storage(Global.options_name);
if(!old_options){
old_options = {};
}
options = $.extend(true, old_options, new_options);
if(set_storage(Global.options_name, options, Global.options_name_days)){
Global['options'] = load_options();
return true;
}
return false;
}
/* Sets any defaults pre-saved by the user */
function load_options(){
var opts = read_storage(Global.options_name);
var out = {};
if(opts){
var optionbox = $('#toolbox, #options');
for (var key in opts) {
if (opts.hasOwnProperty(key)) {
if(opts[key].input == 'checkbox'){
optionbox.find(key).prop('checked', opts[key].value); // true or false
}
if(opts[key].input == 'radio'){
inputs.find(key+"[value=" + opts[key].value + "]").prop('checked', true);
}
if(opts[key].input == 'text'
|| opts[key].input == 'password'
|| opts[key].input == 'hidden'){
optionbox.find(key).val(opts[key].value);
}
if(opts[key].input == 'select'){
optionbox.find(key+' option[value='+opts[key].value+']').attr('selected', true);
}
if(opts[key].input == 'hashtag'){
optionbox.find(key).val(opts[key].value);
window.location.hash = opts[key].value;
}
}
}
out = opts;
}
return out;
}
/* Example Usage */
var Global = { options_name : 'options', options_name_days : 7 }
(function(){
/* Set the values in the html */
load_options();
/* Example usage */
update_options({
'#my-option' : {
input : 'text',
value : 'this is my option.'
}
});
var my_option = get_options('#my-option');
console.log(my_option);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment