Clean code

This commit is contained in:
Benjamin Renard 2014-01-12 01:11:24 +01:00
parent 0c60c95b9f
commit efe943361b
3 changed files with 44 additions and 38 deletions

View file

@ -6,7 +6,6 @@ refresh_group_list=function() {
if (groups.count()==0) {
$(grouplist).prepend('<li><a class="group-choice">Aucune groupe</a></li>');
}
else {
groups.each(function(idx,group) {
@ -113,7 +112,7 @@ show_contributions=function(group,contributor_name) {
}
total.html(sum+' €');
}
$('.contribution_delete_btn').bind('click',on_contribution_delete_btn_click);
$('.contribution_edit_btn').bind('click',on_contribution_edit_btn_click);
}
@ -167,7 +166,6 @@ on_valid_add_contributor_modal=function() {
$('#add_contributor_name')[0].value='';
$('#add_contributor_email')[0].value='';
groups.save();
}
on_close_add_contributor_modal=function () {
@ -249,7 +247,7 @@ on_valid_add_contribution_modal=function(e) {
return;
}
cost=parseFloat(cost.replace(',','.'));
var date=$('#add_contribution_date')[0].value;
if (date!='') {
if(!RegExp('^[0-3][0-9]\/[0-1][0-9]\/[0-2][0-9][0-9][0-9]$').test(date)) {
@ -266,7 +264,7 @@ on_valid_add_contribution_modal=function(e) {
else {
date=new Date();
}
if($('#add_contribution_modal #edit_id')[0].value=='-1') {
group.addContribution(new Contribution(contributor,cost,title,date));
}
@ -305,8 +303,6 @@ on_contribution_edit_btn_click=function(e) {
contribution_id=$($(e.target).parents('div')[0]).data('id');
contribution=group.contributions[contribution_id];
$('#add_contribution_modal #add_contribution_contributor_name')[0].value=contribution.contributor.name;
console.log($('#add_contribution_modal #add_contribution_contributor_name')[0]);
console.log(contribution.contributor.name);
$('#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');
@ -358,7 +354,6 @@ display_balance=function(group) {
on_remove_group_btn_click=function(e) {
group=groups[$('#view-group #group_name')[0].value];
myconfirm('Etes-vous sûre de vouloir supprimer le group '+group.name+' ?',on_confirm_remove_group,null,group);
}
on_confirm_remove_group=function(group) {

View file

@ -79,6 +79,9 @@ function Group(name,data) {
};
}
/*
* Contributors
*/
this.removeContributor=function(c) {
this.contributors=this.contributors.filter(function(v){
return (v.name!=c);
@ -99,6 +102,19 @@ function Group(name,data) {
return undefined;
}
this.addContributor=function(c) {
c.id=this.contributors.length;
this.contributors.push(c);
}
this.replaceContributor=function(idx,c) {
c.id=idx;
this.contributors[idx]=c;
}
/*
* Contributions
*/
this.contributionsByContributorName=function(name) {
var ret=[];
for (idx in this.contributions) {
@ -119,16 +135,9 @@ function Group(name,data) {
this.contributions[idx]=c;
}
this.addContributor=function(c) {
c.id=this.contributors.length;
this.contributors.push(c);
}
this.replaceContributor=function(idx,c) {
c.id=idx;
this.contributors[idx]=c;
}
/*
* Balance
*/
this.balance=function() {
ret={}
for (idx in this.contributors) {
@ -143,27 +152,31 @@ function Group(name,data) {
return ret;
}
/*
* Contructor
*/
if (jQuery.type(data)=='object') {
try {
this.name=data.name;
if (jQuery.type(data.contributors) == 'array') {
for (idx in data.contributors) {
this.contributors.push(new Contributor(
data.contributors[idx].name,
data.contributors[idx].email,
idx
));
this.name=data.name;
if (jQuery.type(data.contributors) == 'array') {
for (idx in data.contributors) {
this.contributors.push(new Contributor(
data.contributors[idx].name,
data.contributors[idx].email,
idx
));
}
}
}
if (jQuery.type(data.contributions) == 'array') {
for (idx in data.contributions) {
this.contributions.push(new Contribution(
this.contributorByName(data.contributions[idx].contributor),
data.contributions[idx].cost,
data.contributions[idx].title,
data.contributions[idx].date,
idx
));
if (jQuery.type(data.contributions) == 'array') {
for (idx in data.contributions) {
this.contributions.push(new Contribution(
this.contributorByName(data.contributions[idx].contributor),
data.contributions[idx].cost,
data.contributions[idx].title,
data.contributions[idx].date,
idx
));
}
}
}
}

View file

@ -60,8 +60,6 @@ body{
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Groupe <b class="caret"></b></a>
<ul id="group-choice" class="dropdown-menu">
<li><a class='group-choice'>Quotidien</a></li>
<li><a class='group-choice'>Noël</a></li>
<li class="divider"></li>
<li><a href="#" id='add_group_btn' data-toggle="modal" data-target="#add_group_modal">Nouveau</a></li>
</ul>