refresh_group_list=function() {
grouplist=$('#group-choice')[0];
$('a.group-choice').each(function(idx,a) {
$(a).parent().remove();
});
if (groups.count()==0) {
$(grouplist).prepend('
Aucune groupe');
}
else {
groups.each(function(idx,group) {
$(grouplist).prepend(''+group.name+'');
});
$('a.group-choice').each(function(idx,a) {
$(a).bind('click',on_group_choice_click);
});
}
}
/****************
* Add group
****************/
on_click_add_group_btn=function(e) {
navbar_collapse_hide();
$('#add_group_modal').modal('show');
}
on_show_add_group_modal=function(e) {
$('#add_group_modal #add_group_name').focus();
}
on_valid_add_group_modal=function (e) {
e.preventDefault();
var name=$('#add_group_name')[0].value;
if (name=='') {
alert('Vous devez saisir un nom !');
return;
}
if (groups[name]!==undefined) {
alert('Ce groupe exite déjà !');
return;
}
var uuid=groups.newGroup(name);
refresh_group_list();
$('#add_group_modal').modal('hide');
groups.save();
view_group(groups[uuid]);
}
on_close_add_group_modal=function () {
$('#add_group_modal form')[0].reset();
}
/****************
* Nav bars
****************/
on_group_choice_click=function(e) {
view_group(groups[$(e.target).data('uuid')]);
navbar_collapse_hide();
}
navbar_collapse_hide=function() {
if ($('#navbar-groupe-collapse').hasClass('in')) {
$('#navbar-groupe-collapse').collapse('hide');
}
}
/****************
* Parts
****************/
view_part=function(part) {
$('.part').each(function(idx,div) {
$(div).css('display','none');
});
$(part).css('display','block');
}
view_home=function() {
navbar_collapse_hide();
if (user) {
$('#view-home #title').html('Bonjour '+user.name);
}
else {
$('#view-home #title').html('Bonjour');
}
if (groups.count()>0) {
tbody=$('#view-home #mybalances tbody');
tbody.html('');
balances=groups.balances();
var sum=0;
for (g in balances) {
if (jQuery.type(balances[g]['balance'][user.email])!='undefined') {
if (balances[g]['balance'][user.email]['diff']<0) {
sum+=balances[g]['balance'][user.email]['diff'];
diff=''+balances[g]['balance'][user.email]['diff'].toFixed(2)+' | ';
}
else {
value=balances[g]['balance'][user.email]['total']-balances[g]['min'];
sum+=value;
diff='+'+value.toFixed(2)+' € | ';
}
tbody.html(tbody.html()+''+balances[g].name+' | '+diff+'
');
}
}
$('#view-home #mybalances a.group-link').bind('click',function(e) {
g=$(e.target).data('uuid');
if (jQuery.type(groups[g])!='undefined') {
view_group(groups[g]);
}
});
if (sum<0) {
$('#view-home #mybalances #total-value').html(''+sum.toFixed(2)+' €');
}
else {
$('#view-home #mybalances #total-value').html(''+(sum==0?'':'+')+sum.toFixed(2)+' €');
}
$('#view-home #mybalances').css('display','table');
}
else {
$('#view-home #mybalances').css('display','none');
}
view_part('#view-home');
}
/****************
* View group
****************/
view_group=function(group) {
$('#view-group').data('uuid',group.uuid);
$('#view-group h1 span:first').html(group.name);
set_contributors(group);
on_contributor_change();
view_part('#view-group');
}
set_contributors=function(group) {
$('#view-group #contributor option').remove();
if(group.contributors.length==0) {
$('#view-group #contributor').append('');
}
else {
for (email in group.contributors) {
$('#view-group #contributor').append('');
}
}
}
on_contributor_change=function(e) {
show_contributions(groups[$('#view-group').data('uuid')],$('#view-group #contributor')[0].value);
}
show_contributions=function(group,contributor_email) {
var tbody=$($('#view-group #contributions tbody')[0]);
tbody.html('');
total=$($('#view-group #total-value')[0]);
contributions=group.contributionsByContributorEmail(contributor_email);
contributions.reverse();
if (contributions.length==0) {
tbody.append('Aucune contributions |
');
total.html('0,00 €');
}
else {
sum=0;
for (idx in contributions) {
col_actions=' | ';
tbody.append(''+contributions[idx].getTitle()+' | '+contributions[idx].cost.toFixed(2)+' € '+moment(contributions[idx].date).format('DD/MM/YYYY')+' | '+col_actions+'
');
sum+=contributions[idx].cost;
}
total.html(sum.toFixed(2)+' €');
}
$('.contribution_delete_btn').bind('click',on_contribution_delete_btn_click);
$('.contribution_edit_btn').bind('click',on_contribution_edit_btn_click);
}
/******************************
* Add/Edit/remove contributor
******************************/
on_click_add_contributor_btn=function() {
$('#add_contributor_modal').data('group-uuid',$('#view-group').data('uuid'))
$($('#add_contributor_modal h4')[0]).html('Ajouter un participant');
$($('#add_contributor_modal #add_contributor_submit')[0]).html('Ajouter');
$('#view-group #edit_id')[0].value='-1';
$('#add_contributor_modal #add_contributor_email').attr('disabled',false);
$('#add_contributor_modal').modal('show');
}
on_show_add_contributor_modal=function(e) {
$('#add_contributor_modal #add_contributor_name').focus();
}
on_valid_add_contributor_modal=function(e) {
e.preventDefault();
var group=groups[$('#add_contributor_modal').data('group-uuid')];
var name=$('#add_contributor_name')[0].value;
edit_id=$('#view-group #edit_id')[0].value;
if (name=='') {
alert('Vous devez saisir un nom !');
return;
}
var email=$('#add_contributor_email')[0].value;
if (email=='') {
alert('Vous devez saisir un email !');
return;
}
duplicate=group.contributorByEmail(email)
if (duplicate!=undefined && edit=='-1') {
alert('Un contributor utilise déjà cet email !');
return;
}
if(edit_id=='-1') {
group.addContributor(new Contributor(name,email));
}
else {
group.replaceContributor(email,new Contributor(name,email));
}
set_contributors(group);
$('#add_contributor_modal').modal('hide');
groups.save();
}
on_close_add_contributor_modal=function () {
$('#add_contributor_name')[0].value='';
$('#add_contributor_email')[0].value='';
}
on_edit_contributor_btn_click=function(e) {
var group=groups[$('#view-group').data('uuid')];
$('#add_contributor_modal').data('group-uuid',group.uuid);
contributor_email=$('#view-group #contributor')[0].value;
contributor=group.contributorByEmail(contributor_email);
$('#view-group #edit_id')[0].value=contributor_email;
$('#add_contributor_modal #add_contributor_name')[0].value=contributor.name;
$('#add_contributor_modal #add_contributor_email')[0].value=contributor.email;
$('#add_contributor_modal #add_contributor_email').attr('disabled',true);
$($('#add_contributor_modal h4')[0]).html('Editer un participant');
$($('#add_contributor_modal #add_contributor_submit')[0]).html('Valider');
$('#add_contributor_modal').modal('show');
}
on_click_del_contributor_btn=function() {
contributor=$('#view-group #contributor')[0].value;
question='Voulez-vous vraiment supprimer '+contributor+' ?';
myconfirm(question,on_confirm_del_contributor,null,contributor);
}
on_confirm_del_contributor=function(contributor) {
var group=groups[$('#view-group').data('uuid')];
group.removeContributor(contributor);
set_contributors(group);
groups.save();
}
/********************************
* Add/edit/delete contribution
********************************/
on_show_add_contribution_modal=function(e) {
$('#add_contribution_modal #add_contribution_contributor_email').html($('#view-group #contributor').html());
$('#add_contribution_modal #add_contribution_contributor_email')[0].value=$('#view-group #contributor')[0].value;
$('#add_contribution_modal #add_contribution_title').focus();
}
on_click_add_contribution_btn=function() {
$('#add_contribution_modal').data('group-uuid',$('#view-group').data('uuid'));
$('#add_contribution_modal #edit_uuid')[0].value='-1';
$($('#add_contribution_modal h4')[0]).html('Ajouter une contribution');
$($('#add_contribution_modal #add_contribution_submit')[0]).html('Ajouter');
$('#add_contribution_modal').modal('show');
}
on_valid_add_contribution_modal=function(e) {
e.preventDefault();
var group=groups[$('#add_contribution_modal').data('group-uuid')];
var contributor_email=$('#add_contribution_contributor_email')[0].value;
var contributor=group.contributorByEmail(contributor_email);
if (contributor==undefined) {
alert('Participant inconu !');
return;
}
var title=$('#add_contribution_title')[0].value;
if (title=='') {
$('#add_contribution_title')[0].focus();
alert('Vous devez saisir un titre !');
return;
}
var cost=$('#add_contribution_cost')[0].value;
if (cost=='') {
$('#add_contribution_cost')[0].focus();
alert('Vous devez saisir un coût !');
return;
}
else if(!RegExp('^[0-9]*((,|\.)[0-9]{1,2})*$').test(cost)) {
$('#add_contribution_cost')[0].focus();
alert('Coût incorrect !');
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)) {
$('#add_contribution_date')[0].focus();
alert('Date incorrect !');
return;
}
date=moment(date,'DD-MM-YYYY').toDate();
if (jQuery.type(date)!='date') {
alert('Date non-reconnu !');
return;
}
}
else {
date=new Date();
}
if($('#add_contribution_modal #edit_uuid')[0].value=='-1') {
group.addContribution(new Contribution(contributor,cost,title,date));
}
else {
contribution_uuid=$('#add_contribution_modal #edit_uuid')[0].value;
group.updateContribution(contribution_uuid,new Contribution(contributor,cost,title,date));
}
show_contributions(group,contributor_email);
$('#add_contribution_modal').modal('hide');
$('#add_contribution_modal form')[0].reset();
groups.save();
}
on_close_add_contribution_modal=function () {
$('#add_contribution_modal form')[0].reset();
}
on_contribution_delete_btn_click=function(e) {
var group=groups[$('#view-group').data('uuid')];
contribution_uuid=$($(e.target).parents('div')[0]).data('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} );
}
on_confirm_contribution_delete=function(data) {
data.group.deleteContribution(data.contribution_uuid);
groups.save();
on_contributor_change();
}
on_contribution_edit_btn_click=function(e) {
var group=groups[$('#view-group').data('uuid')];
$('#add_contribution_modal').data('group-uuid',$('#view-group').data('uuid'));
$($('#add_contribution_modal h4')[0]).html('Editer une contribution');
$($('#add_contribution_modal #add_contribution_submit')[0]).html('Valider');
contribution_uuid=$($(e.target).parents('div')[0]).data('uuid');
contribution=group.contributions[contribution_uuid];
$('#add_contribution_modal #add_contribution_contributor_email')[0].value=contribution.contributor.email;
$('#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');
$('#add_contribution_modal #edit_uuid')[0].value=contribution_uuid;
$('#add_contribution_modal').modal('show');
}
/********************
* Clear local data
********************/
clear_local_data=function() {
navbar_collapse_hide();
myconfirm('Etes-vous sûre de vouloir supprimer les données locales ?',on_confirm_clear_local_data);
}
on_confirm_clear_local_data=function(data) {
delete localStorage.groups;
location.reload();
}
/*********************
* View balance
*********************/
on_display_balance_btn_click=function(e) {
group=groups[$('#view-group').data('uuid')];
display_balance(group);
}
display_balance=function(group) {
bal=group.balance();
tbody=$($('#display_balance_modal tbody')[0]);
tbody.html('');
for (c in bal['balance']) {
if(bal['balance'][c]['diff']<0) {
diff=''+bal['balance'][c]['diff'].toFixed(2)+' € | ';
}
else {
diff=' | ';
}
tbody.append(''+bal['balance'][c]['name']+' | '+bal['balance'][c]['total'].toFixed(2)+' € | '+diff+'
');
}
$('#display_balance_modal #total-value').html(bal.sum.toFixed(2)+' €');
$('#display_balance_modal').modal('show');
}
/*********************
* Remove group
*********************/
on_remove_group_btn_click=function(e) {
group=groups[$('#view-group').data('uuid')];
myconfirm('Etes-vous sûre de vouloir supprimer le group '+group.name+' ?',on_confirm_remove_group,null,group);
}
on_confirm_remove_group=function(group) {
if(groups.removeGroup(group.uuid)) {
groups.save();
location.reload();
}
else {
alert('Erreur en supprimant le groupe '+group.name+' :(');
}
}
/********************
* Login
********************/
on_close_login_modal=function(e) {
$('#login_modal form')[0].reset();
}
sync_server=false;
on_valid_login_modal=function(e) {
e.preventDefault();
email=$('#login_modal #login_email')[0].value;
pass=$('#login_modal #login_pass')[0].value;
server=$('#login_modal #login_server')[0].value;
$('#login_modal').modal('hide');
pleaseWaitShow();
sync_server.login(server,email,pass,
function(data) {
localStorage.user=JSON.stringify({
'name': data.name,
'email': email,
'password': pass,
'server': server,
});
pleaseWaitHide();
logged_menu();
},
function(data) {
pleaseWaitHide();
if (jQuery.type(data) != 'object' && data.loginerror) {
alert(data.loginerror);
}
else {
console.log(data);
alert('Erreur durant la connexion au serveur');
}
}
);
}
on_click_go_to_subscribe_btn=function() {
$('#login_modal').modal('hide');
$('#subscribe_modal').modal('show');
}
/********************
* Subscribe
********************/
on_close_subscribe_modal=function(e) {
$('#subscribe_modal form')[0].reset();
}
sync_server=false;
on_valid_subscribe_modal=function(e) {
e.preventDefault();
email=$('#subscribe_modal #subscribe_email')[0].value;
name=$('#subscribe_modal #subscribe_name')[0].value;
pass=$('#subscribe_modal #subscribe_pass')[0].value;
server=$('#subscribe_modal #subscribe_server')[0].value;
$('#subscribe_modal').modal('hide');
pleaseWaitShow();
sync_server.subscribe(server,email,name,pass,
function(data) {
localStorage.user=JSON.stringify({
'name': data.name,
'email': data.email,
'password': data.password,
'server': server,
});
pleaseWaitHide();
logged_menu();
alert('Bienvenu '+data.name+' !');
},
function(data) {
pleaseWaitHide();
if (jQuery.type(data) == 'object' && jQuery.type(data.subscribeerror)!='undefined') {
alert(data.subscribeerror);
}
else {
console.log(data);
alert('Erreur durant la connexion au serveur');
}
}
);
}
/********************
* Sync
********************/
on_click_sync_btn=function(e) {
navbar_collapse_hide();
if (user) {
pleaseWaitShow();
onsuccess=function(data) {
console.log(data);
if (data.groups) {
groups.import(data.groups);
groups.save();
refresh_group_list();
pleaseWaitHide();
view_home();
alert('Groupes synchronisés');
}
else {
onerror(data);
}
}
onerror=function(data) {
pleaseWaitHide();
console.log(data);
if (jQuery.type(data['loginerror'])!='undefined') {
alert(data['loginerror']);
}
else if (jQuery.type(data['syncerror'])!='undefined') {
alert(data['syncerror']);
}
else {
alert('Erreur durant la synchronisation :(');
}
}
sync_server.sync(user.server, user.email, user.password, groups.export(),onsuccess,onerror);
}
else {
alert("Vous devez vous connecter pour commencer");
$('#login_modal').modal('show');
}
}
/*
* User menu
*/
user=false;
logged_menu=function() {
user=JSON.parse(localStorage.user);
$('#user-name').html(user.name);
$('#user-menu li').remove();
menu=$('#user-menu');
menu.html("Mon compte"+
"Synchroniser" +
"" +
"Déconnexion");
$('#myaccount_btn').bind('click',on_click_myaccount_btn);
$('#sync_btn').bind('click',on_click_sync_btn);
$('#logoff_btn').bind('click',on_click_logoff_btn);
}
logged_out_menu=function() {
user=false;
$('#user-name').html('Connexion');
$('#user-menu li').remove();
menu=$('#user-menu');
menu.html("Connexion"+
"Inscription");
$('#login_btn').bind('click',on_click_login_btn);
$('#subscribe_btn').bind('click',on_click_subscribe_btn);
}
on_click_myaccount_btn=function() {
$('#myaccount_modal #myaccount_email').html(user.email);
$('#myaccount_modal #myaccount_name')[0].value=user.name;
$('#myaccount_modal #myaccount_server').html(user.server);
$('#myaccount_modal').modal('show');
}
on_click_login_btn=function() {
$('#login_modal').modal('show');
}
on_click_subscribe_btn=function() {
$('#subscribe_modal').modal('show');
}
on_click_logoff_btn=function() {
delete localStorage.user;
logged_out_menu();
}
/*******************
* pleaseWaitDialog
*******************/
pleaseWaitShow=function() {
$('#please_wait_modal').modal('show');
}
pleaseWaitHide=function() {
$('#please_wait_modal').modal('hide');
}
/*********************
* Activate
*********************/
$( document ).ready( function() {
pleaseWaitShow();
if(typeof(Storage)!=="undefined"){
groups=new GroupList();
groups.loadFromLocalStorage();
refresh_group_list();
if (jQuery.type(localStorage.user)!='undefined') {
logged_menu();
}
else {
logged_out_menu();
}
}
else {
alert('Local storage not supported !');
pleaseWaitHide();
return;
}
sync_server=new SyncServer();
$('#app-name').bind('click',view_home);
$('#add_group_btn').bind('click',on_click_add_group_btn);
$('#add_group_submit').bind('click',on_valid_add_group_modal);
$("#add_group_modal").on('shown.bs.modal',on_show_add_group_modal);
$("#add_group_modal").on('hidden.bs.modal',on_close_add_group_modal);
$("#add_group_modal form").bind('submit',on_valid_add_group_modal);
$('#clear_local_data').bind('click',clear_local_data);
$('#login_modal').on('hidden.bs.modal',on_close_login_modal);
$('#login_modal #login_submit').bind('click',on_valid_login_modal);
$('#login_modal #go_to_subscribe_btn').bind('click',on_click_go_to_subscribe_btn);
$('#login_modal form').bind('submit',on_valid_login_modal);
$('#subscribe_modal').on('hidden.bs.modal',on_close_subscribe_modal);
$('#subscribe_modal #subscribe_submit').bind('click',on_valid_subscribe_modal);
$('#subscribe_modal form').bind('submit',on_valid_subscribe_modal);
$('#view-group #contributor').bind('change',on_contributor_change);
$('#add_contributor_btn').bind('click',on_click_add_contributor_btn);
$('#add_contributor_submit').bind('click',on_valid_add_contributor_modal);
$("#add_contributor_modal").on('shown.bs.modal',on_show_add_contributor_modal);
$("#add_contributor_modal").on('hidden.bs.modal',on_close_add_contributor_modal);
$("#add_contributor_modal form").bind('submit',on_valid_add_contributor_modal);
$('#edit_contributor_btn').bind('click',on_edit_contributor_btn_click);
$('#del_contributor_btn').bind('click',on_click_del_contributor_btn);
$('#add_contribution_btn').bind('click',on_click_add_contribution_btn);
$('#add_contribution_submit').bind('click',on_valid_add_contribution_modal);
$("#add_contribution_modal").on('shown.bs.modal',on_show_add_contribution_modal);
$("#add_contribution_modal").on('hidden.bs.modal',on_close_add_contribution_modal);
$("#add_contribution_modal form").on('submit',on_valid_add_contribution_modal);
$("#display_balance_btn").bind('click',on_display_balance_btn_click);
$('#remove_group_btn').bind('click',on_remove_group_btn_click);
view_home();
pleaseWaitHide();
} );