From 824225b5d63cebfdb81c781d45ea97a083246a5f Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Sat, 19 Jul 2014 23:58:59 +0200 Subject: [PATCH] Fix category management --- inc/myco.js | 21 ++++----- inc/myco_objects.js | 102 ++++++++++++++++++++++++++++++++------------ index.html | 4 +- 3 files changed, 85 insertions(+), 42 deletions(-) diff --git a/inc/myco.js b/inc/myco.js index 4682315..8cd1f7c 100644 --- a/inc/myco.js +++ b/inc/myco.js @@ -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+="
  • "+cats[cid]['name']+"
  • "; + for(cid in grp.categories) { + html+="
  • "+grp.categories[cid]['name']+"
  • "; } 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='
    '+group.categories[contributions[idx].categorie]['name']+""; + if (contributions[idx].category && jQuery.type(group.categories[contributions[idx].category])) { + cat='
    '+group.categories[contributions[idx].category]['name']+""; } col_actions='
    '; tbody.append(''+contributions[idx].getTitle()+cat+''+contributions[idx].cost.toFixed(2)+' €
    '+moment(contributions[idx].date).format('DD/MM/YYYY')+''+col_actions+''); @@ -399,7 +396,7 @@ on_show_add_contribution_modal=function(e) { for (u in group.categories) { cats+=""; } - $('#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'); diff --git a/inc/myco_objects.js b/inc/myco_objects.js index 159e6cb..598d38d 100644 --- a/inc/myco_objects.js +++ b/inc/myco_objects.js @@ -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, }; } diff --git a/index.html b/index.html index 3defa93..75b4a0b 100644 --- a/index.html +++ b/index.html @@ -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 {
    Catégorie - +