Rework on data structure to add uuid as objects identifier and add lastChange metadata to each objects

This commit is contained in:
Benjamin Renard 2016-09-08 01:23:15 +02:00
parent 78322eaac5
commit 412617115c
4 changed files with 143 additions and 108 deletions

View file

@ -0,0 +1,6 @@
generate_uuid=function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}

View file

@ -280,7 +280,7 @@ on_li_click=function(event) {
if (cat) { if (cat) {
var thing=cat.byLabel(li.data('label')); var thing=cat.byLabel(li.data('label'));
if (thing) { if (thing) {
thing.checked=li.hasClass('done'); thing.setChecked(li.hasClass('done'));
scases.save(); scases.save();
} }
show_scase(scase,cat.name); show_scase(scase,cat.name);
@ -315,7 +315,7 @@ on_valid_add_thing_modal=function (e) {
alert("Cet élément existe déjà !"); alert("Cet élément existe déjà !");
return; return;
} }
cat.things.push(new Thing(label,false)); cat.newThing(label);
scases.save(); scases.save();
show_scase(scase,cat.name); show_scase(scase,cat.name);
} }
@ -405,15 +405,13 @@ show_cat=function(cat,displayed) {
var panel_title=$('<h4 class="panel-title">'+cat.name+' </h4>'); var panel_title=$('<h4 class="panel-title">'+cat.name+' </h4>');
panel_title.bind('click',on_title_click); panel_title.bind('click',on_title_click);
var stats=cat.stats();
var count=cat.count();
var countDone=cat.countDone();
var tag=$('<span class="count-tag pull-right"></span>'); var tag=$('<span class="count-tag pull-right"></span>');
if (count==countDone) { if (stats.things==stats.done) {
tag.append($('<span class="label label-success"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></span>')); tag.append($('<span class="label label-success"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></span>'));
} }
else { else {
tag.append($('<span class="badge">'+countDone+' / '+count+'</span>')); tag.append($('<span class="badge">'+stats.done+' / '+stats.things+'</span>'));
} }
var delete_btn=$('<button class="btn btn-default btn-xs pull-right"><span class="glyphicon glyphicon-trash"></button>'); var delete_btn=$('<button class="btn btn-default btn-xs pull-right"><span class="glyphicon glyphicon-trash"></button>');

View file

@ -1,54 +1,31 @@
exampleData={
'lastChange': 0,
'scases': { function SCaseList() {
0: { lastChange=0;
'name': 'Vacances',
'cats': { this.importExampleData=function() {
'lastChange': 0, var exampleData={
'cats': { 'Vacances': {
0: { 'Papier': {
'name': 'Papier', 'color': '#f00',
'color': '#f00', 'things': ['Papier blanc', 'Stylo', "Carte d'identité"]
'things': [ },
{ 'Multimédia' : {
'label': 'Papier blanc', 'color': '#0f0',
'checked': false 'things': ['Montre', 'Chargeur montre', "PC portable"]
}, }
{ }
'label': 'Stylo', };
'checked': true for (scaseName in exampleData) {
}, var scase=this.newSCase(scaseName);
{ for (catName in exampleData[scaseName]) {
'label': "Carte d'identité", var cat=scase.cats.newCat(catName);
'checked': false for (idx in exampleData[scaseName][catName].things) {
} cat.newThing(exampleData[scaseName][catName].things[idx]);
]
},
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() { this.loadFromLocalStorage=function() {
if (jQuery.type(localStorage.scases)!='undefined') { if (jQuery.type(localStorage.scases)!='undefined') {
@ -56,7 +33,7 @@ function SCaseList() {
var data=JSON.parse(localStorage.scases); var data=JSON.parse(localStorage.scases);
this.lastChange=data.lastChange; this.lastChange=data.lastChange;
for (el in data.scases) { for (el in data.scases) {
this[el]=new SCase(false,data.scases[el]); this[el]=new SCase(false,false,data.scases[el]);
} }
} }
catch(e) { catch(e) {
@ -75,10 +52,13 @@ function SCaseList() {
} }
else { else {
myconfirm("<h2>Bienvenu !</h2><p>Souhaitez-vous charger les données d'exemple ?</p>", myconfirm("<h2>Bienvenu !</h2><p>Souhaitez-vous charger les données d'exemple ?</p>",
function(data) { function(scases) {
localStorage.scases=JSON.stringify(exampleData); scases.importExampleData();
location.reload(); scases.save();
} show_scases();
},
false,
this
); );
} }
} }
@ -101,7 +81,7 @@ function SCaseList() {
} }
this.lastChange=data.lastChange; this.lastChange=data.lastChange;
for (el in data.scases) { for (el in data.scases) {
this[el]=new SCase(false,data.scases[el]); this[el]=new SCase(false,false,data.scases[el]);
} }
return true; return true;
} }
@ -155,9 +135,10 @@ function SCaseList() {
} }
this.newSCase=function(name) { this.newSCase=function(name) {
if (!this.isSCase(this[name])) { if (!this.byName(this[name])) {
this[name]=new SCase(name); var uuid=uuid||generate_uuid();
return this[name]; this[uuid]=new SCase(uuid,name);
return this[uuid];
} }
return false; return false;
} }
@ -166,6 +147,7 @@ function SCaseList() {
var scase=this.byName(name); var scase=this.byName(name);
if (scase && !this.byName(newname)) { if (scase && !this.byName(newname)) {
scase.name=newname; scase.name=newname;
scase.lastChange=new Date().getTime();
return scase; return scase;
} }
return false; return false;
@ -174,9 +156,12 @@ function SCaseList() {
this.copySCase=function(name,newname) { this.copySCase=function(name,newname) {
var orig_scase=this.byName(name); var orig_scase=this.byName(name);
if (this.isSCase(orig_scase) && !this.byName(newname)) { if (this.isSCase(orig_scase) && !this.byName(newname)) {
this[newname]=new SCase(false,orig_scase.export()); var uuid=uuid||generate_uuid();
this[newname].name=newname; this[uuid]=new SCase(false,false,orig_scase.export());
return this[newname]; this[uuid].uuid=uuid;
this[uuid].lastChange=new Date().getTime();
this[uuid].name=newname;
return this[uuid];
} }
return false; return false;
} }
@ -191,16 +176,30 @@ function SCaseList() {
} }
} }
function SCase(name,data) { function SCase(uuid,name,data) {
this.uuid=uuid||generate_uuid();
this.name=name; this.name=name;
this.cats=new CatList(); this.cats=new CatList();
this.lastChange=new Date().getTime();
this.isSCase=function() { this.isSCase=function() {
return true; return true;
} }
this.import=function(data) {
this.uuid=data.uuid || generate_uuid();
this.lastChange=data.lastChange || new Date().getTime();
this.name=decodeURIComponent(data.name);
if (jQuery.type(data.cats) == 'object') {
this.cats=new CatList(data.cats);
}
return true;
}
this.export=function() { this.export=function() {
return { return {
'uuid': this.uuid,
'lastChange': this.lastChange,
'name': encodeURIComponent(this.name), 'name': encodeURIComponent(this.name),
'cats': this.cats.export() 'cats': this.cats.export()
}; };
@ -243,6 +242,7 @@ function SCase(name,data) {
} }
} }
}); });
this.lastChange=new Date().getTime();
return true; return true;
} }
@ -251,10 +251,7 @@ function SCase(name,data) {
*/ */
if (jQuery.type(data)=='object') { if (jQuery.type(data)=='object') {
try { try {
this.name=decodeURIComponent(data.name); this.import(data);
if (jQuery.type(data.cats) == 'object') {
this.cats=new CatList(data.cats);
}
} }
catch (e) { catch (e) {
console.log(e); console.log(e);
@ -266,15 +263,10 @@ function SCase(name,data) {
function CatList(data) { function CatList(data) {
lastChange=0;
this.export=function() { this.export=function() {
return { return this.each(function(idx,cat) {
'lastChange': this.lastChange, return cat.export();
'cats': this.each(function(idx,cat) { });
return cat.export();
})
};
} }
this.import=function(data) { this.import=function(data) {
@ -283,9 +275,8 @@ function CatList(data) {
delete this[el]; delete this[el];
} }
} }
this.lastChange=data.lastChange; for (el in data) {
for (el in data.cats) { this[el]=new Cat(el,false,false,data[el]);
this[el]=new Cat(el,false,data.cats[el]);
} }
return true; return true;
} }
@ -326,8 +317,9 @@ function CatList(data) {
this.newCat=function(name) { this.newCat=function(name) {
if (!this.isCat(this[name])) { if (!this.isCat(this[name])) {
this[name]=new Cat(name); var uuid=uuid||generate_uuid();
return this[name]; this[uuid]=new Cat(uuid,name);
return this[uuid];
} }
return false; return false;
} }
@ -336,6 +328,7 @@ function CatList(data) {
var cat=this.byName(name); var cat=this.byName(name);
if (cat && !this.byName(newname)) { if (cat && !this.byName(newname)) {
cat.name=newname; cat.name=newname;
cat.lastChange=new Date().getTime();
return cat; return cat;
} }
return false; return false;
@ -368,34 +361,45 @@ function CatList(data) {
} }
function Cat(name,color,data) { function Cat(uuid,name,color,data) {
this.uuid=generate_uuid();
this.lastChange=new Date().getTime();
this.name=name; this.name=name;
this.color=color || '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6); this.color=color || '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
this.things=[]; this.things={};
this.isCat=function() { this.isCat=function() {
return true; return true;
} }
this.import=function(data) {
this.uuid=data.uuid || generate_uuid();
this.lastChange=data.lastChange||new Date().getTime();
this.name=decodeURIComponent(data.name);
this.color=data.color;
if (jQuery.type(data.things) == 'object') {
for (tuuid in data.things) {
this.things[tuuid]=new Thing(tuuid);
this.things[tuuid].import(data.things[tuuid]);
}
}
return true;
}
this.export=function() { this.export=function() {
var things=[]; var things={};
for (idx in this.things) { for (tuuid in this.things) {
things.push(this.things[idx].export()); things[tuuid]=this.things[tuuid].export();
} }
return { return {
'uuid': this.uuid,
'lastChange': this.lastChange,
'name': encodeURIComponent(this.name), 'name': encodeURIComponent(this.name),
'color': this.color, 'color': this.color,
'things': things 'things': things
}; };
} }
this.importThing=function(data) {
return new Thing(
decodeURIComponent(data.label),
data.checked
);
}
this.byLabel=function(label) { this.byLabel=function(label) {
for (idx in this.things) { for (idx in this.things) {
if (label==this.things[idx].label) { if (label==this.things[idx].label) {
@ -406,23 +410,38 @@ function Cat(name,color,data) {
} }
this.count=function() { this.count=function() {
return this.things.length; return keys(this.things).length;
} }
this.countDone=function() { this.stats=function() {
var count=0; var count=0;
var done=0;
for (idx in this.things) { for (idx in this.things) {
if (this.things[idx].checked) { if (this.things[idx].checked) {
count+=1; done+=1;
} }
count+=1;
} }
return count; return {
'things': count,
'done': done
};
}
this.newThing=function(label) {
if (!this.byLabel(label)) {
var uuid=generate_uuid();
this.things[uuid]=new Thing(uuid,label);
return true;
}
return false;
} }
this.renameThing=function(label,newlabel) { this.renameThing=function(label,newlabel) {
var thing=this.byLabel(label); var thing=this.byLabel(label);
if (thing && !this.byLabel(newlabel)) { if (thing && !this.byLabel(newlabel)) {
thing.label=newlabel; thing.label=newlabel;
thing.lastChange=new Date().getTime();
return thing; return thing;
} }
return false; return false;
@ -443,13 +462,7 @@ function Cat(name,color,data) {
*/ */
if (jQuery.type(data)=='object') { if (jQuery.type(data)=='object') {
try { try {
this.name=decodeURIComponent(data.name); this.import(data);
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) { catch (e) {
console.log(e); console.log(e);
@ -459,13 +472,30 @@ function Cat(name,color,data) {
} }
function Thing(label,checked) { function Thing(uuid,label,checked) {
this.uuid=uuid||generate_uuid();
this.lastChange=new Date().getTime();
this.label=label; this.label=label;
this.checked=checked; this.checked=checked;
this.import=function(data) {
this.uuid=data.uuid||generate_uuid();
this.lastChange=data.lastChange||new Date().getTime();
this.label=decodeURIComponent(data.label),
this.checked=data.checked;
}
this.export=function() { this.export=function() {
return { return {
'uuid': this.uuid,
'lastChange': this.lastChange,
'label': encodeURIComponent(this.label), 'label': encodeURIComponent(this.label),
'checked': this.checked 'checked': this.checked
}; };
} }
this.setChecked=function(value) {
this.checked=value;
this.lastChange=new Date().getTime();
}
} }

View file

@ -284,6 +284,7 @@ div.panel-heading, li.list-group-item, a {
</script> </script>
<script src="inc/lib/uuid.js"></script>
<script src="inc/mydialog.js"></script> <script src="inc/mydialog.js"></script>
<script src="inc/mysc_objects.js"></script> <script src="inc/mysc_objects.js"></script>
<script src="inc/main.js"></script> <script src="inc/main.js"></script>