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) {
grp_id=$(e.target).parents('div.btn-group').data('grp');
grp=groups[grp_id];
console.log(grp.getCategories());
ul=$('#grp_cat_modal ul');
html="";
cats=grp.getCategories();
for(cid in cats) {
html+="<li><span class='cat-color' style='background-color: "+cats[cid]['color']+"'></span> "+cats[cid]['name']+"</li>";
for(cid in grp.categories) {
html+="<li><span class='cat-color' style='background-color: "+grp.categories[cid]['color']+"'></span> "+grp.categories[cid]['name']+"</li>";
}
ul.html(html);
$('#grp_cat_modal').modal('show');
@ -191,9 +189,8 @@ show_contributions=function(group,contributor_email) {
sum=0;
for (idx in contributions) {
var cat='';
console.log(contributions[idx].categorie);
if (contributions[idx].categorie && jQuery.type(group.categories[contributions[idx].categorie])) {
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>";
if (contributions[idx].category && jQuery.type(group.categories[contributions[idx].category])) {
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>";
}
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>');
@ -399,7 +396,7 @@ on_show_add_contribution_modal=function(e) {
for (u in group.categories) {
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();
}
@ -458,9 +455,9 @@ on_valid_add_contribution_modal=function(e) {
date=new Date();
}
var cat=$('#add_contribution_categorie')[0].value;
var cat=$('#add_contribution_category')[0].value;
if (cat != '' && !jQuery.type(group.categories[cat])) {
alert('Categorie incorrect');
alert('Category incorrect');
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_cost')[0].value=contribution.cost;
$('#add_contribution_modal #add_contribution_date')[0].value=moment(contribution.date).format('DD/MM/YYYY');
if (contribution.categorie) {
$('#add_contribution_modal #add_contribution_categorie')[0].value=contribution.categorie;
if (contribution.category) {
$('#add_contribution_modal #add_contribution_category')[0].value=contribution.category;
}
$('#add_contribution_modal #edit_uuid')[0].value=contribution_uuid;
$('#add_contribution_modal').modal('show');

View file

@ -122,6 +122,7 @@ function Group(uuid,name,data) {
this.contributions={};
this.deletedContributions={};
this.categories={};
this.deletedCategories={};
this.isGroup=function() {
return true;
@ -136,6 +137,10 @@ function Group(uuid,name,data) {
for (uuid in this.contributions) {
contributions[uuid]=this.contributions[uuid].export();
}
var categories={}
for (uuid in this.categories) {
categories[uuid]=this.categories[uuid].export();
}
return {
'uuid': this.uuid,
'name': encodeURIComponent(this.name),
@ -143,7 +148,8 @@ function Group(uuid,name,data) {
'deletedContributors': this.deletedContributors,
'contributions': contributions,
'deletedContributions': this.deletedContributions,
'categories': this.categories
'categories': categories,
'deletedCategories': this.deletedCategories
};
}
@ -206,6 +212,13 @@ function Group(uuid,name,data) {
delete this.contributors[email];
this.contributors[c.email]=c;
}
this.importContributor=function(data) {
return new Contributor(
decodeURIComponent(data.name),
data.email
);
}
/*
* Contributions
@ -240,7 +253,7 @@ function Group(uuid,name,data) {
this.deletedContributions[uuid].cost,
decodeURIComponent(this.deletedContributions[uuid].title),
this.deletedContributions[uuid].date,
this.deletedContributions[uuid].categorie,
this.deletedContributions[uuid].category,
uuid,
this.deletedContributions[uuid].lastChange
));
@ -281,24 +294,50 @@ function Group(uuid,name,data) {
delete this.deletedContributions[uuid];
}
this.importContributor=function(data) {
return new Contributor(
decodeURIComponent(data.name),
data.email
);
}
this.importContribution=function(data) {
return new Contribution(
this.contributorByEmail(data.contributor),
data.cost,
decodeURIComponent(data.title),
data.date,
data.categorie,
data.category,
data.uuid,
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
@ -339,13 +378,6 @@ function Group(uuid,name,data) {
};
}
/*
* Categories
*/
this.getCategories=function() {
return this.categories;
}
/*
* Contructor
*/
@ -373,12 +405,10 @@ function Group(uuid,name,data) {
this.deletedContributions[uuid]=data.deletedContributions[uuid];
}
}
console.log(data.categories);
if (jQuery.type(data.categories) == 'object') {
for (cid in data.categories) {
if (jQuery.type(data.categories[cid]['color'])!='string') {
data.categories[cid]['color']='#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
}
this.categories[cid]=data.categories[cid];
for (uuid in data.categories) {
this.categories[uuid]=this.importCategory(data.categories[uuid]);
}
}
else {
@ -393,14 +423,17 @@ function Group(uuid,name,data) {
'Cadeau': '#a700fa'
};
for (c in categories) {
this.categories[generate_uuid()]={
'name': c,
'color': categories[c]
};
this.categories[generate_uuid()]=new Category(c,categories[c]);
}
}
if (jQuery.type(data.deletedCategories) == 'object') {
for (uuid in data.deletedCategories) {
this.deletedCategories[uuid]=data.deletedCategories[uuid];
}
}
}
catch (e) {
console.log(e);
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) {
this.contributor=contributor;
this.cost=cost;
@ -426,7 +472,7 @@ function Contribution(contributor,cost,title,date,cat,uuid,lastChange) {
}
this.date=date;
this.uuid=uuid || generate_uuid();
this.categorie=cat;
this.category=cat;
this.lastChange=lastChange || new Date().getTime();
this.export=function() {
return {
@ -435,7 +481,7 @@ function Contribution(contributor,cost,title,date,cat,uuid,lastChange) {
'cost': this.cost,
'title': encodeURIComponent(this.title),
'date': this.date.getTime(),
'categorie': this.categorie,
'category': this.category,
'lastChange': this.lastChange,
};
}

View file

@ -55,7 +55,7 @@ body{
text-transform: capitalize;
}
#view-group span.date, #view-group span.categorie {
#view-group span.date, #view-group span.category {
color: #999;
font-size: 0.8em;
font-style: italic;
@ -408,7 +408,7 @@ span.cat-color {
<div class="form-group">
<div class="input-group">
<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 class="form-group">