Fix category management

This commit is contained in:
Benjamin Renard 2014-07-19 23:58:59 +02:00
parent e358209d67
commit 824225b5d6
3 changed files with 85 additions and 42 deletions

View file

@ -133,12 +133,10 @@ view_home=function() {
on_home_grp_cat_btn_click=function(e) { on_home_grp_cat_btn_click=function(e) {
grp_id=$(e.target).parents('div.btn-group').data('grp'); grp_id=$(e.target).parents('div.btn-group').data('grp');
grp=groups[grp_id]; grp=groups[grp_id];
console.log(grp.getCategories());
ul=$('#grp_cat_modal ul'); ul=$('#grp_cat_modal ul');
html=""; html="";
cats=grp.getCategories(); for(cid in grp.categories) {
for(cid in cats) { html+="<li><span class='cat-color' style='background-color: "+grp.categories[cid]['color']+"'></span> "+grp.categories[cid]['name']+"</li>";
html+="<li><span class='cat-color' style='background-color: "+cats[cid]['color']+"'></span> "+cats[cid]['name']+"</li>";
} }
ul.html(html); ul.html(html);
$('#grp_cat_modal').modal('show'); $('#grp_cat_modal').modal('show');
@ -191,9 +189,8 @@ show_contributions=function(group,contributor_email) {
sum=0; sum=0;
for (idx in contributions) { for (idx in contributions) {
var cat=''; var cat='';
console.log(contributions[idx].categorie); if (contributions[idx].category && jQuery.type(group.categories[contributions[idx].category])) {
if (contributions[idx].categorie && jQuery.type(group.categories[contributions[idx].categorie])) { cat='<br/><span class="category"><span class="cat-color" style="background-color: '+group.categories[contributions[idx].category]['color']+'"></span> '+group.categories[contributions[idx].category]['name']+"</span>";
cat='<br/><span class="categorie"><span class="cat-color" style="background-color: '+group.categories[contributions[idx].categorie]['color']+'"></span> '+group.categories[contributions[idx].categorie]['name']+"</span>";
} }
col_actions='<td><div class="btn-group" data-uuid="'+contributions[idx].uuid+'"><button type="button" class="btn btn-default contribution_edit_btn"><span class="glyphicon glyphicon-edit"></span></button><button type="button" class="btn btn-default contribution_delete_btn"><span class="glyphicon glyphicon-trash"></span></button></div></td>'; col_actions='<td><div class="btn-group" data-uuid="'+contributions[idx].uuid+'"><button type="button" class="btn btn-default contribution_edit_btn"><span class="glyphicon glyphicon-edit"></span></button><button type="button" class="btn btn-default contribution_delete_btn"><span class="glyphicon glyphicon-trash"></span></button></div></td>';
tbody.append('<tr><td>'+contributions[idx].getTitle()+cat+'</td><td>'+contributions[idx].cost.toFixed(2)+' €<br/><span class="date">'+moment(contributions[idx].date).format('DD/MM/YYYY')+'</span></td>'+col_actions+'</tr>'); tbody.append('<tr><td>'+contributions[idx].getTitle()+cat+'</td><td>'+contributions[idx].cost.toFixed(2)+' €<br/><span class="date">'+moment(contributions[idx].date).format('DD/MM/YYYY')+'</span></td>'+col_actions+'</tr>');
@ -399,7 +396,7 @@ on_show_add_contribution_modal=function(e) {
for (u in group.categories) { for (u in group.categories) {
cats+="<option value='"+u+"'>"+group.categories[u]['name']+"</option>"; cats+="<option value='"+u+"'>"+group.categories[u]['name']+"</option>";
} }
$('#add_contribution_modal #add_contribution_categorie').html(cats); $('#add_contribution_modal #add_contribution_category').html(cats);
$('#add_contribution_modal #add_contribution_title').focus(); $('#add_contribution_modal #add_contribution_title').focus();
} }
@ -458,9 +455,9 @@ on_valid_add_contribution_modal=function(e) {
date=new Date(); date=new Date();
} }
var cat=$('#add_contribution_categorie')[0].value; var cat=$('#add_contribution_category')[0].value;
if (cat != '' && !jQuery.type(group.categories[cat])) { if (cat != '' && !jQuery.type(group.categories[cat])) {
alert('Categorie incorrect'); alert('Category incorrect');
return; return;
} }
@ -505,8 +502,8 @@ on_contribution_edit_btn_click=function(e) {
$('#add_contribution_modal #add_contribution_title')[0].value=contribution.getTitle(); $('#add_contribution_modal #add_contribution_title')[0].value=contribution.getTitle();
$('#add_contribution_modal #add_contribution_cost')[0].value=contribution.cost; $('#add_contribution_modal #add_contribution_cost')[0].value=contribution.cost;
$('#add_contribution_modal #add_contribution_date')[0].value=moment(contribution.date).format('DD/MM/YYYY'); $('#add_contribution_modal #add_contribution_date')[0].value=moment(contribution.date).format('DD/MM/YYYY');
if (contribution.categorie) { if (contribution.category) {
$('#add_contribution_modal #add_contribution_categorie')[0].value=contribution.categorie; $('#add_contribution_modal #add_contribution_category')[0].value=contribution.category;
} }
$('#add_contribution_modal #edit_uuid')[0].value=contribution_uuid; $('#add_contribution_modal #edit_uuid')[0].value=contribution_uuid;
$('#add_contribution_modal').modal('show'); $('#add_contribution_modal').modal('show');

View file

@ -122,6 +122,7 @@ function Group(uuid,name,data) {
this.contributions={}; this.contributions={};
this.deletedContributions={}; this.deletedContributions={};
this.categories={}; this.categories={};
this.deletedCategories={};
this.isGroup=function() { this.isGroup=function() {
return true; return true;
@ -136,6 +137,10 @@ function Group(uuid,name,data) {
for (uuid in this.contributions) { for (uuid in this.contributions) {
contributions[uuid]=this.contributions[uuid].export(); contributions[uuid]=this.contributions[uuid].export();
} }
var categories={}
for (uuid in this.categories) {
categories[uuid]=this.categories[uuid].export();
}
return { return {
'uuid': this.uuid, 'uuid': this.uuid,
'name': encodeURIComponent(this.name), 'name': encodeURIComponent(this.name),
@ -143,7 +148,8 @@ function Group(uuid,name,data) {
'deletedContributors': this.deletedContributors, 'deletedContributors': this.deletedContributors,
'contributions': contributions, 'contributions': contributions,
'deletedContributions': this.deletedContributions, 'deletedContributions': this.deletedContributions,
'categories': this.categories 'categories': categories,
'deletedCategories': this.deletedCategories
}; };
} }
@ -207,6 +213,13 @@ function Group(uuid,name,data) {
this.contributors[c.email]=c; this.contributors[c.email]=c;
} }
this.importContributor=function(data) {
return new Contributor(
decodeURIComponent(data.name),
data.email
);
}
/* /*
* Contributions * Contributions
*/ */
@ -240,7 +253,7 @@ function Group(uuid,name,data) {
this.deletedContributions[uuid].cost, this.deletedContributions[uuid].cost,
decodeURIComponent(this.deletedContributions[uuid].title), decodeURIComponent(this.deletedContributions[uuid].title),
this.deletedContributions[uuid].date, this.deletedContributions[uuid].date,
this.deletedContributions[uuid].categorie, this.deletedContributions[uuid].category,
uuid, uuid,
this.deletedContributions[uuid].lastChange this.deletedContributions[uuid].lastChange
)); ));
@ -281,25 +294,51 @@ function Group(uuid,name,data) {
delete this.deletedContributions[uuid]; delete this.deletedContributions[uuid];
} }
this.importContributor=function(data) {
return new Contributor(
decodeURIComponent(data.name),
data.email
);
}
this.importContribution=function(data) { this.importContribution=function(data) {
return new Contribution( return new Contribution(
this.contributorByEmail(data.contributor), this.contributorByEmail(data.contributor),
data.cost, data.cost,
decodeURIComponent(data.title), decodeURIComponent(data.title),
data.date, data.date,
data.categorie, data.category,
data.uuid, data.uuid,
data.lastChange data.lastChange
); );
} }
/*
* Categories
*/
this.addCategory=function(c) {
this.categories[c.uuid]=c;
}
this.updateCategory=function(uuid,c) {
c.uuid=uuid;
this.categories[uuid]=c;
}
this.deleteCategory=function(uuid,time) {
this.categories[uuid].lastChange=time || new Date().getTime();
this.deletedCategory[uuid]=this.categories[uuid].export();
delete this.categories[uuid];
}
this.restoreCategory=function(uuid) {
this.deletedCategory[uuid].lastChange=new Date().getTime();
this.categories[uuid]=this.importCategory(this.deletedCategory[uuid]);
delete this.deletedCategory[uuid];
}
this.importCategory=function(data) {
return new Category(
decodeURIComponent(data.name),
data.color,
data.lastChange
);
}
/* /*
* Balance * Balance
*/ */
@ -339,13 +378,6 @@ function Group(uuid,name,data) {
}; };
} }
/*
* Categories
*/
this.getCategories=function() {
return this.categories;
}
/* /*
* Contructor * Contructor
*/ */
@ -373,12 +405,10 @@ function Group(uuid,name,data) {
this.deletedContributions[uuid]=data.deletedContributions[uuid]; this.deletedContributions[uuid]=data.deletedContributions[uuid];
} }
} }
console.log(data.categories);
if (jQuery.type(data.categories) == 'object') { if (jQuery.type(data.categories) == 'object') {
for (cid in data.categories) { for (uuid in data.categories) {
if (jQuery.type(data.categories[cid]['color'])!='string') { this.categories[uuid]=this.importCategory(data.categories[uuid]);
data.categories[cid]['color']='#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
}
this.categories[cid]=data.categories[cid];
} }
} }
else { else {
@ -393,14 +423,17 @@ function Group(uuid,name,data) {
'Cadeau': '#a700fa' 'Cadeau': '#a700fa'
}; };
for (c in categories) { for (c in categories) {
this.categories[generate_uuid()]={ this.categories[generate_uuid()]=new Category(c,categories[c]);
'name': c, }
'color': categories[c] }
}; if (jQuery.type(data.deletedCategories) == 'object') {
for (uuid in data.deletedCategories) {
this.deletedCategories[uuid]=data.deletedCategories[uuid];
} }
} }
} }
catch (e) { catch (e) {
console.log(e);
alert('Une erreur est survenue en chargeant le groupe '+this.name+' depuis le cache'); alert('Une erreur est survenue en chargeant le groupe '+this.name+' depuis le cache');
} }
} }
@ -417,6 +450,19 @@ function Contributor(name,email) {
} }
} }
function Category(name,color,lastChange) {
this.name=name;
this.color=color || '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
this.lastChange=lastChange || new Date().getTime();
this.export=function() {
return {
'name': encodeURIComponent(this.name),
'color': this.color,
'lastChange': this.lastChange
};
}
}
function Contribution(contributor,cost,title,date,cat,uuid,lastChange) { function Contribution(contributor,cost,title,date,cat,uuid,lastChange) {
this.contributor=contributor; this.contributor=contributor;
this.cost=cost; this.cost=cost;
@ -426,7 +472,7 @@ function Contribution(contributor,cost,title,date,cat,uuid,lastChange) {
} }
this.date=date; this.date=date;
this.uuid=uuid || generate_uuid(); this.uuid=uuid || generate_uuid();
this.categorie=cat; this.category=cat;
this.lastChange=lastChange || new Date().getTime(); this.lastChange=lastChange || new Date().getTime();
this.export=function() { this.export=function() {
return { return {
@ -435,7 +481,7 @@ function Contribution(contributor,cost,title,date,cat,uuid,lastChange) {
'cost': this.cost, 'cost': this.cost,
'title': encodeURIComponent(this.title), 'title': encodeURIComponent(this.title),
'date': this.date.getTime(), 'date': this.date.getTime(),
'categorie': this.categorie, 'category': this.category,
'lastChange': this.lastChange, 'lastChange': this.lastChange,
}; };
} }

View file

@ -55,7 +55,7 @@ body{
text-transform: capitalize; text-transform: capitalize;
} }
#view-group span.date, #view-group span.categorie { #view-group span.date, #view-group span.category {
color: #999; color: #999;
font-size: 0.8em; font-size: 0.8em;
font-style: italic; font-style: italic;
@ -408,7 +408,7 @@ span.cat-color {
<div class="form-group"> <div class="form-group">
<div class="input-group"> <div class="input-group">
<span class="input-group-addon">Catégorie </span> <span class="input-group-addon">Catégorie </span>
<select id='add_contribution_categorie' class="form-control"></select> <select id='add_contribution_category' class="form-control"></select>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">