Add uuid to group and refer group in gouplist with their uuid instead of name

This commit is contained in:
Benjamin Renard 2014-01-16 20:16:29 +01:00
parent 7e5fb88a87
commit ad5ed6fd1e
3 changed files with 51 additions and 31 deletions

View file

@ -9,7 +9,7 @@ refresh_group_list=function() {
} }
else { else {
groups.each(function(idx,group) { groups.each(function(idx,group) {
$(grouplist).prepend('<li><a class="group-choice">'+group.name+'</a></li>'); $(grouplist).prepend('<li><a class="group-choice" data-uuid="'+group.uuid+'">'+group.name+'</a></li>');
}); });
$('a.group-choice').each(function(idx,a) { $('a.group-choice').each(function(idx,a) {
@ -43,11 +43,11 @@ on_valid_add_group_modal=function (e) {
alert('Ce groupe exite déjà !'); alert('Ce groupe exite déjà !');
return; return;
} }
groups[name]=new Group(name); var uuid=groups.newGroup(name);
refresh_group_list(); refresh_group_list();
$('#add_group_modal').modal('hide'); $('#add_group_modal').modal('hide');
groups.save(); groups.save();
view_group(groups[name]); view_group(groups[uuid]);
} }
on_close_add_group_modal=function () { on_close_add_group_modal=function () {
@ -58,7 +58,7 @@ on_close_add_group_modal=function () {
* Nav bars * Nav bars
****************/ ****************/
on_group_choice_click=function(e) { on_group_choice_click=function(e) {
view_group(groups[$(e.target).html()]); view_group(groups[$(e.target).data('uuid')]);
navbar_collapse_hide(); navbar_collapse_hide();
} }
@ -102,7 +102,7 @@ view_home=function() {
sum+=value; sum+=value;
diff='<td class="positive">+'+value.toFixed(2)+' €</td>'; diff='<td class="positive">+'+value.toFixed(2)+' €</td>';
} }
tbody.html(tbody.html()+'<tr><td><a class="group-link">'+g+'</a></td>'+diff+'</tr>'); tbody.html(tbody.html()+'<tr><td><a class="group-link">'+balances[g].name+'</a></td>'+diff+'</tr>');
} }
} }
$('#view-home #mybalances a.group-link').bind('click',function(e) { $('#view-home #mybalances a.group-link').bind('click',function(e) {
@ -129,8 +129,8 @@ view_home=function() {
* View group * View group
****************/ ****************/
view_group=function(group) { view_group=function(group) {
$('#view-group').data('uuid',group.uuid);
$('#view-group h1 span:first').html(group.name); $('#view-group h1 span:first').html(group.name);
$('#view-group #group_name')[0].value=group.name;
set_contributors(group); set_contributors(group);
on_contributor_change(); on_contributor_change();
view_part('#view-group'); view_part('#view-group');
@ -149,7 +149,7 @@ set_contributors=function(group) {
} }
on_contributor_change=function(e) { on_contributor_change=function(e) {
show_contributions(groups[$('#view-group #group_name')[0].value],$('#view-group #contributor')[0].value); show_contributions(groups[$('#view-group').data('uuid')],$('#view-group #contributor')[0].value);
} }
show_contributions=function(group,contributor_name) { show_contributions=function(group,contributor_name) {
@ -179,7 +179,7 @@ show_contributions=function(group,contributor_name) {
* Add/Edit/remove contributor * Add/Edit/remove contributor
******************************/ ******************************/
on_click_add_contributor_btn=function() { on_click_add_contributor_btn=function() {
$('#add_contributor_modal #group_name')[0].value=$('#view-group #group_name')[0].value; $('#add_contributor_modal').data('group-uuid',('#view-group').data('uuid'))
$($('#add_contributor_modal h4')[0]).html('Ajouter un participant'); $($('#add_contributor_modal h4')[0]).html('Ajouter un participant');
$($('#add_contributor_modal #add_contributor_submit')[0]).html('Ajouter'); $($('#add_contributor_modal #add_contributor_submit')[0]).html('Ajouter');
$('#view-group #edit_id')[0].value='-1'; $('#view-group #edit_id')[0].value='-1';
@ -192,7 +192,7 @@ on_show_add_contributor_modal=function(e) {
on_valid_add_contributor_modal=function(e) { on_valid_add_contributor_modal=function(e) {
e.preventDefault(); e.preventDefault();
var group=groups[$('#add_contributor_modal #group_name')[0].value]; var group=groups[$('#add_contributor_modal').data('group-uuid')];
var name=$('#add_contributor_name')[0].value; var name=$('#add_contributor_name')[0].value;
edit_id=$('#view-group #edit_id')[0].value; edit_id=$('#view-group #edit_id')[0].value;
if (name=='') { if (name=='') {
@ -233,8 +233,8 @@ on_close_add_contributor_modal=function () {
} }
on_edit_contributor_btn_click=function(e) { on_edit_contributor_btn_click=function(e) {
var group=groups[$('#view-group #group_name')[0].value]; var group=groups[$('#view-group').data('uuid')];
$('#add_contributor_modal #group_name')[0].value=group.name; $('#add_contributor_modal').data('group-uuid',group.uuid);
contributor_name=$('#view-group #contributor')[0].value; contributor_name=$('#view-group #contributor')[0].value;
contributor=group.contributorByName(contributor_name); contributor=group.contributorByName(contributor_name);
$('#view-group #edit_id')[0].value=contributor.id; $('#view-group #edit_id')[0].value=contributor.id;
@ -252,7 +252,7 @@ on_click_del_contributor_btn=function() {
} }
on_confirm_del_contributor=function(contributor) { on_confirm_del_contributor=function(contributor) {
var group=groups[$('#view-group #group_name')[0].value]; var group=groups[$('#view-group').data('uuid')];
group.removeContributor(contributor); group.removeContributor(contributor);
set_contributors(group); set_contributors(group);
groups.save(); groups.save();
@ -272,7 +272,7 @@ on_show_add_contribution_modal=function(e) {
} }
on_click_add_contribution_btn=function() { on_click_add_contribution_btn=function() {
$('#add_contribution_modal #group_name')[0].value=$('#view-group #group_name')[0].value; $('#add_contribution_modal').data('group-uuid',$('#view-group').data('uuid'));
$('#add_contribution_modal #edit_uuid')[0].value='-1'; $('#add_contribution_modal #edit_uuid')[0].value='-1';
$($('#add_contribution_modal h4')[0]).html('Ajouter une contribution'); $($('#add_contribution_modal h4')[0]).html('Ajouter une contribution');
$($('#add_contribution_modal #add_contribution_submit')[0]).html('Ajouter'); $($('#add_contribution_modal #add_contribution_submit')[0]).html('Ajouter');
@ -281,7 +281,7 @@ on_click_add_contribution_btn=function() {
on_valid_add_contribution_modal=function(e) { on_valid_add_contribution_modal=function(e) {
e.preventDefault(); e.preventDefault();
var group=groups[$('#add_contribution_modal #group_name')[0].value]; var group=groups[$('#add_contribution_modal').data('group-uuid')];
var contributor_name=$('#add_contribution_contributor_name')[0].value; var contributor_name=$('#add_contribution_contributor_name')[0].value;
var contributor=group.contributorByName(contributor_name); var contributor=group.contributorByName(contributor_name);
if (contributor==undefined) { if (contributor==undefined) {
@ -342,7 +342,7 @@ on_close_add_contribution_modal=function () {
} }
on_contribution_delete_btn_click=function(e) { on_contribution_delete_btn_click=function(e) {
var group=groups[$('#view-group #group_name')[0].value]; var group=groups[$('#view-group').data('uuid')];
contribution_uuid=$($(e.target).parents('div')[0]).data('uuid'); contribution_uuid=$($(e.target).parents('div')[0]).data('uuid');
contribution=group.contributions[contribution_uuid]; contribution=group.contributions[contribution_uuid];
myconfirm('Etes-vous sûre de vouloir supprimer la participation '+contribution.getTitle()+' de '+contribution.contributor.name+' ?',on_confirm_contribution_delete,null,{'group':group,'contribution_uuid':contribution_uuid, 'contribution': contribution} ); myconfirm('Etes-vous sûre de vouloir supprimer la participation '+contribution.getTitle()+' de '+contribution.contributor.name+' ?',on_confirm_contribution_delete,null,{'group':group,'contribution_uuid':contribution_uuid, 'contribution': contribution} );
@ -355,8 +355,8 @@ on_confirm_contribution_delete=function(data) {
} }
on_contribution_edit_btn_click=function(e) { on_contribution_edit_btn_click=function(e) {
var group=groups[$('#view-group #group_name')[0].value]; var group=groups[$('#view-group').data('uuid')];
$('#add_contribution_modal #group_name')[0].value=$('#view-group #group_name')[0].value; $('#add_contribution_modal').data('group-uuid',$('#view-group').data('uuid'));
$($('#add_contribution_modal h4')[0]).html('Editer une contribution'); $($('#add_contribution_modal h4')[0]).html('Editer une contribution');
$($('#add_contribution_modal #add_contribution_submit')[0]).html('Valider'); $($('#add_contribution_modal #add_contribution_submit')[0]).html('Valider');
contribution_uuid=$($(e.target).parents('div')[0]).data('uuid'); contribution_uuid=$($(e.target).parents('div')[0]).data('uuid');
@ -389,7 +389,7 @@ on_confirm_clear_local_data=function(data) {
*********************/ *********************/
on_display_balance_btn_click=function(e) { on_display_balance_btn_click=function(e) {
group=groups[$('#view-group #group_name')[0].value]; group=groups[$('#view-group').data('uuid')];
display_balance(group); display_balance(group);
} }
@ -415,12 +415,12 @@ display_balance=function(group) {
*********************/ *********************/
on_remove_group_btn_click=function(e) { on_remove_group_btn_click=function(e) {
group=groups[$('#view-group #group_name')[0].value]; group=groups[$('#view-group').data('uuid')];
myconfirm('Etes-vous sûre de vouloir supprimer le group '+group.name+' ?',on_confirm_remove_group,null,group); myconfirm('Etes-vous sûre de vouloir supprimer le group '+group.name+' ?',on_confirm_remove_group,null,group);
} }
on_confirm_remove_group=function(group) { on_confirm_remove_group=function(group) {
if(groups.removeGroup(group.name)) { if(groups.removeGroup(group.uuid)) {
groups.save(); groups.save();
location.reload(); location.reload();
} }

View file

@ -8,7 +8,7 @@ function GroupList() {
var data=JSON.parse(localStorage.groups); var data=JSON.parse(localStorage.groups);
this.lastChange=data.lastChange; this.lastChange=data.lastChange;
for (el in data.groups) { for (el in data.groups) {
this[el]=new Group(el,data.groups[el]); this[el]=new Group(el,false,data.groups[el]);
} }
} }
catch(e) { catch(e) {
@ -41,7 +41,7 @@ function GroupList() {
} }
} }
for (el in groups) { for (el in groups) {
this[el]=new Group(el,groups[el]); this[el]=new Group(el,false,groups[el]);
} }
return true; return true;
} }
@ -77,9 +77,21 @@ function GroupList() {
return (jQuery.type(el)=='object' && jQuery.type(el.isGroup)=='function' && el.isGroup()); return (jQuery.type(el)=='object' && jQuery.type(el.isGroup)=='function' && el.isGroup());
} }
this.removeGroup=function(name) { this.byName=function(name) {
if (this.isGroup(this[name])) { for (el in this) {
delete this[name]; if(this.isGroup(this[el])) {
if (this[el].name==name) {
return this[el];
}
}
}
return false;
}
this.removeGroup=function(uuid) {
if (this.isGroup(this[uuid])) {
delete this[uuid];
return true; return true;
} }
return false; return false;
@ -87,14 +99,23 @@ function GroupList() {
this.balances=function(fct) { this.balances=function(fct) {
return this.each(function(idx,group) { return this.each(function(idx,group) {
return group.balance(); bal=group.balance();
bal.name=group.name;
return bal;
}); });
} }
this.newGroup=function(name,uuid) {
var uuid=uuid||generate_uuid();
this[uuid]=new Group(uuid,name);
return uuid;
}
} }
function Group(name,data) { function Group(uuid,name,data) {
this.name=name; this.uuid=uuid || generate_uuid();
this.name=name || false;
this.contributors=[]; this.contributors=[];
this.contributions={}; this.contributions={};
this.deletedContributions={}; this.deletedContributions={};
@ -114,6 +135,7 @@ function Group(name,data) {
contributions[uuid]=this.contributions[uuid].export(); contributions[uuid]=this.contributions[uuid].export();
} }
return { return {
'uuid': this.uuid,
'name': encodeURIComponent(this.name), 'name': encodeURIComponent(this.name),
'contributors': contributors, 'contributors': contributors,
'contributions': contributions, 'contributions': contributions,
@ -227,6 +249,7 @@ function Group(name,data) {
*/ */
if (jQuery.type(data)=='object') { if (jQuery.type(data)=='object') {
try { try {
this.uuid=data.uuid;
this.name=data.name; this.name=data.name;
if (jQuery.type(data.contributors) == 'array') { if (jQuery.type(data.contributors) == 'array') {
for (idx in data.contributors) { for (idx in data.contributors) {

View file

@ -110,7 +110,6 @@ body{
<div id='view-group' class='part'> <div id='view-group' class='part'>
<h1><span>Noël</span><small><span id='edit_group_btn' class='glyphicon glyphicon-edit btn btn-sm'></span></small></h1> <h1><span>Noël</span><small><span id='edit_group_btn' class='glyphicon glyphicon-edit btn btn-sm'></span></small></h1>
<form class="form-horizontal" role="form"> <form class="form-horizontal" role="form">
<input type='hidden' id='group_name' value='Noël'/>
<input type='hidden' id='edit_id' value='-1' /> <input type='hidden' id='edit_id' value='-1' />
<div class="form-group"> <div class="form-group">
<div class="input-group"> <div class="input-group">
@ -200,7 +199,6 @@ body{
</div> </div>
<div class="modal-body"> <div class="modal-body">
<form class="form-horizontal" role="form"> <form class="form-horizontal" role="form">
<input type='hidden' id='group_name' />
<div class="form-group"> <div class="form-group">
<div class="input-group"> <div class="input-group">
<span class="input-group-addon">Nom *</span> <span class="input-group-addon">Nom *</span>
@ -269,7 +267,6 @@ body{
</div> </div>
<div class="modal-body"> <div class="modal-body">
<form class="form-horizontal" role="form"> <form class="form-horizontal" role="form">
<input type='hidden' id='group_name' />
<input type='hidden' id='edit_uuid' value='-1' /> <input type='hidden' id='edit_uuid' value='-1' />
<div class="form-group"> <div class="form-group">
<div class="input-group"> <div class="input-group">