Skip to content

Instantly share code, notes, and snippets.

@peteristhegreat
Created January 24, 2018 17:54
Show Gist options
  • Save peteristhegreat/bbccf7f8be1f722a364d2cccfd58ac01 to your computer and use it in GitHub Desktop.
Save peteristhegreat/bbccf7f8be1f722a364d2cccfd58ac01 to your computer and use it in GitHub Desktop.
CssSelectorGenerator (css-selector-generator.js) with JQuery to save/load all textfields and all checkboxes to Cookies (js.cookie.js)
Views = {};
Views.save = function(){
var CssPath = new CssSelectorGenerator;
var text_fields = {};
var checkboxes = {};
$(":checkbox").each(function(index, element){
var checked = element.checked;
var css_path = CssPath.getSelector(element);
checkboxes[css_path] = checked;
});
$(":text").each(function(index, element){
var text = $(this).val();
var css_path = CssPath.getSelector(element);
text_fields[css_path] = text;
});
Cookies.set("text_fields", text_fields);
Cookies.set("checkboxes", checkboxes);
};
Views.loaded = false;
Views.load = function(){
if(Views.loaded){
return;
}
Views.loaded = true;
//var CssPath = new CssSelectorGenerator;
var checkboxes = Cookies.getJSON("checkboxes");// details buttons
var text_fields = Cookies.getJSON("text_fields");
// Iterate thru checkboxes and click on them
_.each(checkboxes, function(value, key, list){
//console.log(key, value);
var element = $(key);
if(value != element.is(':checked')){
// make the number of columns change
element.trigger('click');
// wait for the column count to change
}
});
var e = jQuery.Event("keyup", { which: $.ui.keyCode.ENTER });
_.each(text_fields, function(value, key, list){
//console.log(key, value);
var element = $(key);
if(value != element.text()){
element.val(value);
element.trigger(e);
}
});
};
$(window).bind('beforeunload', function(){
// Views.save();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment