2014-01-06 01:46:22 +01:00
|
|
|
function GroupList() {
|
2014-01-12 01:13:30 +01:00
|
|
|
|
|
|
|
lastChange=0;
|
2014-01-06 01:46:22 +01:00
|
|
|
|
|
|
|
this.loadFromLocalStorage=function() {
|
2014-01-12 03:05:14 +01:00
|
|
|
if (jQuery.type(localStorage.groups)!='undefined') {
|
|
|
|
try {
|
|
|
|
var data=JSON.parse(localStorage.groups);
|
|
|
|
this.lastChange=data.lastChange;
|
|
|
|
for (el in data.groups) {
|
|
|
|
this[el]=new Group(el,data.groups[el]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
for (el in this) {
|
|
|
|
if (this.isGroup(this[el])) {
|
|
|
|
delete this[el];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
myconfirm('Erreur en chargeant les données locales. On les purges ?',
|
|
|
|
function(data) {
|
|
|
|
delete localStorage.groups;
|
|
|
|
location.reload();
|
|
|
|
}
|
|
|
|
);
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.export=function() {
|
2014-01-12 20:08:27 +01:00
|
|
|
return this.each(function(idx,group) {
|
|
|
|
return group.export();
|
|
|
|
});
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
|
|
|
|
2014-01-12 01:24:16 +01:00
|
|
|
this.import=function(groups) {
|
|
|
|
ret={};
|
|
|
|
for (el in this) {
|
|
|
|
if (this.isGroup(this[el])) {
|
|
|
|
delete ret[el];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (el in groups) {
|
|
|
|
this[el]=new Group(el,groups[el]);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-06 01:46:22 +01:00
|
|
|
this.save=function() {
|
2014-01-12 01:13:30 +01:00
|
|
|
localStorage.groups=JSON.stringify({
|
|
|
|
'lastChange': new Date().getTime(),
|
|
|
|
'groups': this.export()
|
|
|
|
});
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.each=function(fct) {
|
|
|
|
var idx=0;
|
2014-01-12 20:08:27 +01:00
|
|
|
var ret={};
|
2014-01-06 01:46:22 +01:00
|
|
|
for (el in this) {
|
|
|
|
if(this.isGroup(this[el])) {
|
2014-01-12 20:08:27 +01:00
|
|
|
ret[el]=fct(idx++,this[el]);
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
|
|
|
}
|
2014-01-12 20:08:27 +01:00
|
|
|
return ret;
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.count=function() {
|
|
|
|
len=0;
|
2014-01-12 20:08:27 +01:00
|
|
|
this.each(function(idx,group) {
|
|
|
|
len=len+1;
|
|
|
|
});
|
2014-01-06 01:46:22 +01:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isGroup=function(el) {
|
2014-01-12 20:09:20 +01:00
|
|
|
return (jQuery.type(el)=='object' && jQuery.type(el.isGroup)=='function' && el.isGroup());
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.removeGroup=function(name) {
|
|
|
|
if (this.isGroup(this[name])) {
|
|
|
|
delete this[name];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-12 20:09:47 +01:00
|
|
|
|
|
|
|
this.balances=function(fct) {
|
|
|
|
return this.each(function(idx,group) {
|
|
|
|
return group.balance();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function Group(name,data) {
|
|
|
|
this.name=name;
|
|
|
|
this.contributors=[];
|
2014-01-14 00:29:49 +01:00
|
|
|
this.contributions={};
|
|
|
|
this.deletedContributions={};
|
2014-01-06 01:46:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
this.isGroup=function() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.export=function() {
|
|
|
|
var contributors=[];
|
|
|
|
for (idx in this.contributors) {
|
|
|
|
contributors.push(this.contributors[idx].export());
|
|
|
|
}
|
2014-01-14 00:29:49 +01:00
|
|
|
var contributions={}
|
|
|
|
for (uuid in this.contributions) {
|
|
|
|
contributions[uuid]=this.contributions[uuid].export();
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
|
|
|
return {
|
2014-01-12 23:04:03 +01:00
|
|
|
'name': encodeURIComponent(this.name),
|
2014-01-06 01:46:22 +01:00
|
|
|
'contributors': contributors,
|
2014-01-14 00:29:49 +01:00
|
|
|
'contributions': contributions,
|
|
|
|
'deletedContributions': this.deletedContributions
|
2014-01-06 01:46:22 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-01-12 01:11:24 +01:00
|
|
|
/*
|
|
|
|
* Contributors
|
|
|
|
*/
|
2014-01-06 01:46:22 +01:00
|
|
|
this.removeContributor=function(c) {
|
|
|
|
this.contributors=this.contributors.filter(function(v){
|
|
|
|
return (v.name!=c);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.contributorByName=function(name) {
|
|
|
|
for (c in this.contributors) {
|
|
|
|
if (this.contributors[c].name == name) return this.contributors[c];
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.contributorByEmail=function(email) {
|
|
|
|
for (c in this.contributors) {
|
|
|
|
if (this.contributors[c].email == email) return this.contributors[c];
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2014-01-12 01:11:24 +01:00
|
|
|
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
|
|
|
|
*/
|
2014-01-06 01:46:22 +01:00
|
|
|
this.contributionsByContributorName=function(name) {
|
2014-01-14 00:29:49 +01:00
|
|
|
var ret={};
|
|
|
|
for (uuid in this.contributions) {
|
|
|
|
if (this.contributions[uuid].contributor.name==name) {
|
|
|
|
ret[uuid]=this.contributions[uuid];
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addContribution=function(c) {
|
2014-01-14 00:29:49 +01:00
|
|
|
this.contributions[c.uuid]=c;
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
|
|
|
|
2014-01-14 00:29:49 +01:00
|
|
|
this.updateContribution=function(uuid,c) {
|
|
|
|
c.uuid=uuid;
|
|
|
|
this.contributions[uuid]=c;
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
|
|
|
|
2014-01-14 00:29:49 +01:00
|
|
|
this.deleteContribution=function(uuid) {
|
|
|
|
this.contributions[uuid].lastChange=new Date().getTime();
|
|
|
|
this.deletedContributions[uuid]=this.contributions[uuid].export();
|
|
|
|
delete this.contributions[uuid];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-12 01:11:24 +01:00
|
|
|
/*
|
|
|
|
* Balance
|
|
|
|
*/
|
2014-01-06 01:46:22 +01:00
|
|
|
this.balance=function() {
|
2014-01-12 20:07:01 +01:00
|
|
|
total={}
|
|
|
|
min=-1;
|
|
|
|
max=0;
|
2014-01-06 01:46:22 +01:00
|
|
|
for (idx in this.contributors) {
|
2014-01-12 20:07:01 +01:00
|
|
|
var sum=0;
|
2014-01-06 01:46:22 +01:00
|
|
|
c=this.contributors[idx].name;
|
|
|
|
cl=this.contributionsByContributorName(c);
|
2014-01-14 00:29:49 +01:00
|
|
|
for (uuid in cl) {
|
|
|
|
sum+=cl[uuid].cost;
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
2014-01-12 20:07:01 +01:00
|
|
|
if (min==-1 || min>sum) {
|
|
|
|
min=sum;
|
|
|
|
}
|
|
|
|
if(max<sum) {
|
|
|
|
max=sum;
|
|
|
|
}
|
|
|
|
total[c]=sum;
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
2014-01-12 20:07:01 +01:00
|
|
|
balance={}
|
|
|
|
var sum=0;
|
|
|
|
for (c in total) {
|
|
|
|
balance[c]={
|
|
|
|
'total': total[c],
|
|
|
|
'diff': total[c]-max,
|
|
|
|
}
|
|
|
|
sum=sum+total[c];
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
'balance': balance,
|
|
|
|
'sum': sum,
|
|
|
|
'min': min,
|
|
|
|
'max': max
|
|
|
|
};
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
|
|
|
|
2014-01-12 01:11:24 +01:00
|
|
|
/*
|
|
|
|
* Contructor
|
|
|
|
*/
|
2014-01-06 01:46:22 +01:00
|
|
|
if (jQuery.type(data)=='object') {
|
|
|
|
try {
|
2014-01-12 01:11:24 +01:00
|
|
|
this.name=data.name;
|
|
|
|
if (jQuery.type(data.contributors) == 'array') {
|
|
|
|
for (idx in data.contributors) {
|
|
|
|
this.contributors.push(new Contributor(
|
2014-01-12 23:04:03 +01:00
|
|
|
decodeURIComponent(data.contributors[idx].name),
|
|
|
|
decodeURIComponent(data.contributors[idx].email),
|
2014-01-12 01:11:24 +01:00
|
|
|
idx
|
|
|
|
));
|
|
|
|
}
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
2014-01-14 00:29:49 +01:00
|
|
|
if (jQuery.type(data.contributions) == 'object') {
|
|
|
|
for (uuid in data.contributions) {
|
|
|
|
this.contributions[uuid]=new Contribution(
|
|
|
|
this.contributorByName(data.contributions[uuid].contributor),
|
|
|
|
data.contributions[uuid].cost,
|
|
|
|
decodeURIComponent(data.contributions[uuid].title),
|
|
|
|
data.contributions[uuid].date,
|
|
|
|
uuid,
|
|
|
|
data.contributions[uuid].lastChange
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (jQuery.type(data.deletedContributions) == 'object') {
|
|
|
|
for (uuid in data.deletedContributions) {
|
|
|
|
this.deletedContributions[uuid]=data.deletedContributions[uuid];
|
2014-01-12 01:11:24 +01:00
|
|
|
}
|
2014-01-06 01:46:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
alert('Une erreur est survenue en chargeant le groupe '+this.name+' depuis le cache');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-06 03:04:57 +01:00
|
|
|
function Contributor(name,email,id) {
|
2014-01-06 01:46:22 +01:00
|
|
|
this.name=name;
|
|
|
|
this.email=email;
|
2014-01-06 03:04:57 +01:00
|
|
|
this.id=id;
|
2014-01-06 01:46:22 +01:00
|
|
|
this.export=function() {
|
|
|
|
return {
|
2014-01-12 23:04:03 +01:00
|
|
|
'name': encodeURIComponent(this.name),
|
|
|
|
'email': encodeURIComponent(this.email)
|
2014-01-06 01:46:22 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 00:29:49 +01:00
|
|
|
function Contribution(contributor,cost,title,date,uuid,lastChange) {
|
2014-01-06 01:46:22 +01:00
|
|
|
this.contributor=contributor;
|
|
|
|
this.cost=cost;
|
|
|
|
this.title=title;
|
|
|
|
this.date=date;
|
2014-01-14 00:29:49 +01:00
|
|
|
this.uuid=uuid || generate_uuid();
|
2014-01-12 01:13:30 +01:00
|
|
|
this.lastChange=lastChange || new Date().getTime();
|
2014-01-06 01:46:22 +01:00
|
|
|
this.export=function() {
|
|
|
|
return {
|
2014-01-12 23:04:03 +01:00
|
|
|
'contributor': encodeURIComponent(this.contributor.name),
|
2014-01-14 00:29:49 +01:00
|
|
|
'uuid': this.uuid,
|
2014-01-06 01:46:22 +01:00
|
|
|
'cost': this.cost,
|
2014-01-12 23:04:03 +01:00
|
|
|
'title': encodeURIComponent(this.title),
|
2014-01-06 01:46:22 +01:00
|
|
|
'date': this.date,
|
2014-01-12 01:13:30 +01:00
|
|
|
'lastChange': this.lastChange,
|
2014-01-06 01:46:22 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getTitle=function() {
|
|
|
|
if (jQuery.type(this.title)=='string') {
|
|
|
|
return this.title;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2014-01-12 01:24:16 +01:00
|
|
|
|
|
|
|
function SyncServer() {
|
|
|
|
this.url=false;
|
|
|
|
this.email=false;
|
|
|
|
this.password=false;
|
|
|
|
this.logged=false;
|
|
|
|
|
|
|
|
this.login=function(url,email,password,onsuccess,onerror) {
|
|
|
|
this.url=url;
|
|
|
|
this.email=email;
|
|
|
|
this.password=password;
|
|
|
|
|
|
|
|
try {
|
|
|
|
jQuery.getJSON(
|
|
|
|
this.url+'/login',
|
|
|
|
{'email':email,'password':password},
|
|
|
|
function(data, textStatus) {
|
|
|
|
console.log(data);
|
|
|
|
if (textStatus=='success') {
|
|
|
|
if(jQuery.type(data.email) && jQuery.type(data.name)) {
|
|
|
|
console.log('Login success return');
|
|
|
|
console.log(onsuccess);
|
|
|
|
onsuccess(data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onerror(data);
|
|
|
|
}
|
|
|
|
).fail(onerror);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
if(jQuery.type(onerror)=='function') {
|
|
|
|
onerror();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sync=function(url,email,password,groups,onsuccess,onerror) {
|
|
|
|
this.url=url;
|
|
|
|
this.email=email;
|
|
|
|
this.password=password;
|
|
|
|
try {
|
|
|
|
jQuery.getJSON(
|
|
|
|
this.url+'/sync',
|
|
|
|
{
|
|
|
|
'email':email,
|
|
|
|
'password':password,
|
|
|
|
'groups': JSON.stringify(groups)
|
|
|
|
},
|
|
|
|
function(data, textStatus) {
|
|
|
|
console.log(data);
|
|
|
|
if (textStatus=='success') {
|
|
|
|
if(jQuery.type(data.groups)) {
|
|
|
|
console.log('Sync success return');
|
|
|
|
onsuccess(data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onerror(data);
|
|
|
|
}
|
|
|
|
).fail(onerror);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
if(jQuery.type(onerror)=='function') {
|
|
|
|
onerror();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|