exampleData={ 'lastChange': 0, 'scases': { 0: { 'name': 'Vacances', 'cats': { 'lastChange': 0, 'cats': { 0: { 'name': 'Papier', 'color': '#f00', 'things': [ { 'label': 'Papier blanc', 'checked': false }, { 'label': 'Stylo', 'checked': true }, { 'label': "Carte d'identité", 'checked': false } ] }, 1: { 'name': 'Multimédia', 'color': '#0f0', 'things': [ { 'label': 'Montre', 'checked': false }, { 'label': 'Chargeur montre', 'checked': true }, { 'label': "PC", 'checked': false } ] } } } } } }; function SCaseList() { lastChange=0; this.loadFromLocalStorage=function() { if (jQuery.type(localStorage.scases)!='undefined') { try { var data=JSON.parse(localStorage.scases); this.lastChange=data.lastChange; for (el in data.scases) { this[el]=new SCase(false,data.scases[el]); } } catch(e) { for (el in this) { if (this.isSCase(this[el])) { delete this[el]; } } myconfirm('Erreur en chargeant les données locales. On les purges ?', function(data) { delete localStorage.scases; location.reload(); } ); } } else { myconfirm("

Bienvenu !

Souhaitez-vous charger les données d'exemple ?

", function(data) { localStorage.scases=JSON.stringify(exampleData); location.reload(); } ); } } this.export=function() { return { 'lastChange': this.lastChange, 'scases': this.each(function(idx,scase) { return scase.export(); }) }; } this.import=function(data) { ret={}; for (el in this) { if (this.isSCase(this[el])) { delete ret[el]; } } this.lastChange=data.lastChange; for (el in data.scases) { this[el]=new SCase(false,data.scases[el]); } return true; } this.save=function() { localStorage.scases=JSON.stringify(this.export()); } this.each=function(fct) { var idx=0; var ret={}; for (el in this) { if(this.isSCase(this[el])) { ret[el]=fct(idx++,this[el]); } } return ret; } this.count=function() { len=0; this.each(function(idx,scase) { len=len+1; }); return len; } this.isSCase=function(el) { return (jQuery.type(el)=='object' && jQuery.type(el.isSCase)=='function' && el.isSCase()); } this.byName=function(name) { for (el in this) { if(this.isSCase(this[el])) { if (this[el].name==name) { return this[el]; } } } return false; } this.removeSCase=function(name) { for (el in this) { if (this.isSCase(this[el]) && this[el].name==name) { delete this[el]; return true; } } return false; } this.newSCase=function(name) { if (!this.isSCase(this[name])) { this[name]=new SCase(name); return this[name]; } return false; } this.renameSCase=function(name,newname) { var scase=this.byName(name); if (scase && !this.byName(newname)) { scase.name=newname; return scase; } return false; } this.copySCase=function(name,newname) { var orig_scase=this.byName(name); if (this.isSCase(orig_scase) && !this.byName(newname)) { this[newname]=new SCase(false,orig_scase.export()); this[newname].name=newname; return this[newname]; } return false; } } function SCase(name,data) { this.name=name; this.cats=new CatList(); this.isSCase=function() { return true; } this.export=function() { return { 'name': encodeURIComponent(this.name), 'cats': this.cats.export() }; } this.byName=function(name) { for (idx in this.cats) { if (name==this.cats[idx].name) { return this.cats[idx]; } } return false; } this.count=function() { return this.cats.length; } /* * Contructor */ if (jQuery.type(data)=='object') { try { this.name=decodeURIComponent(data.name); if (jQuery.type(data.cats) == 'object') { this.cats=new CatList(data.cats); } } catch (e) { console.log(e); alert('Une erreur est survenue en chargeant la valise '+this.name+' depuis le cache'); } } } function CatList(data) { lastChange=0; this.export=function() { return { 'lastChange': this.lastChange, 'cats': this.each(function(idx,cat) { return cat.export(); }) }; } this.import=function(data) { for (el in this) { if (this.isCat(this[el])) { delete this[el]; } } this.lastChange=data.lastChange; for (el in data.cats) { this[el]=new Cat(el,false,data.cats[el]); } return true; } this.each=function(fct) { var idx=0; var ret={}; for (el in this) { if(this.isCat(this[el])) { ret[el]=fct(idx++,this[el]); } } return ret; } this.count=function() { len=0; this.each(function(idx,cat) { len=len+1; }); return len; } this.isCat=function(el) { return (jQuery.type(el)=='object' && jQuery.type(el.isCat)=='function' && el.isCat()); } this.byName=function(name) { for (el in this) { if(this.isCat(this[el])) { if (this[el].name==name) { return this[el]; } } } return false; } this.newCat=function(name) { if (!this.isCat(this[name])) { this[name]=new Cat(name); return this[name]; } return false; } this.renameCat=function(name,newname) { var cat=this.byName(name); if (cat && !this.byName(newname)) { cat.name=newname; return cat; } return false; } this.removeCat=function(name) { for (el in this) { if (this.isCat(this[el]) && this[el].name==name) { delete this[el]; return true; } } return false; } /* * Contructor */ if (jQuery.type(data)=='object') { try { this.import(data); } catch (e) { console.log(e); alert('Une erreur est survenue en chargeant la liste de catégorie depuis le cache'); } } } function Cat(name,color,data) { this.name=name; this.color=color || '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6); this.things=[]; this.isCat=function() { return true; } this.export=function() { var things=[]; for (idx in this.things) { things.push(this.things[idx].export()); } return { 'name': encodeURIComponent(this.name), 'color': this.color, 'things': things }; } this.importThing=function(data) { return new Thing( decodeURIComponent(data.label), data.checked ); } this.byLabel=function(label) { for (idx in this.things) { if (label==this.things[idx].label) { return this.things[idx]; } } return false; } this.count=function() { return this.things.length; } this.countDone=function() { var count=0; for (idx in this.things) { if (this.things[idx].checked) { count+=1; } } return count; } this.renameThing=function(label,newlabel) { var thing=this.byLabel(label); if (thing && !this.byLabel(newlabel)) { thing.label=newlabel; return thing; } return false; } this.removeThing=function(label) { for (idx in this.things) { if (this.things[idx].label==label) { delete this.things[idx]; return true; } } return false; } /* * Contructor */ if (jQuery.type(data)=='object') { try { this.name=decodeURIComponent(data.name); this.color=data.color; if (jQuery.type(data.things) == 'array') { for (idx in data.things) { this.things.push(this.importThing(data.things[idx])); } } } catch (e) { console.log(e); alert('Une erreur est survenue en chargeant la catégorie catégorie '+this.name+' depuis le cache'); } } } function Thing(label,checked) { this.label=label; this.checked=checked; this.export=function() { return { 'label': encodeURIComponent(this.label), 'checked': this.checked }; } }