356 lines
7.5 KiB
JavaScript
356 lines
7.5 KiB
JavaScript
function GroupList() {
|
|
|
|
lastChange=0;
|
|
|
|
this.loadFromLocalStorage=function() {
|
|
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();
|
|
}
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
this.export=function() {
|
|
return this.each(function(idx,group) {
|
|
return group.export();
|
|
});
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
this.save=function() {
|
|
localStorage.groups=JSON.stringify({
|
|
'lastChange': new Date().getTime(),
|
|
'groups': this.export()
|
|
});
|
|
}
|
|
|
|
this.each=function(fct) {
|
|
var idx=0;
|
|
var ret={};
|
|
for (el in this) {
|
|
if(this.isGroup(this[el])) {
|
|
ret[el]=fct(idx++,this[el]);
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
this.count=function() {
|
|
len=0;
|
|
this.each(function(idx,group) {
|
|
len=len+1;
|
|
});
|
|
return len;
|
|
}
|
|
|
|
this.isGroup=function(el) {
|
|
return (jQuery.type(el)=='object' && jQuery.type(el.isGroup)=='function' && el.isGroup());
|
|
}
|
|
|
|
this.removeGroup=function(name) {
|
|
if (this.isGroup(this[name])) {
|
|
delete this[name];
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
this.balances=function(fct) {
|
|
return this.each(function(idx,group) {
|
|
return group.balance();
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
function Group(name,data) {
|
|
this.name=name;
|
|
this.contributors=[];
|
|
this.contributions=[];
|
|
|
|
|
|
this.isGroup=function() {
|
|
return true;
|
|
}
|
|
|
|
this.export=function() {
|
|
var contributors=[];
|
|
for (idx in this.contributors) {
|
|
contributors.push(this.contributors[idx].export());
|
|
}
|
|
var contributions=[];
|
|
for (idx in this.contributions) {
|
|
contributions.push(this.contributions[idx].export());
|
|
}
|
|
return {
|
|
'name': this.name,
|
|
'contributors': contributors,
|
|
'contributions': contributions
|
|
};
|
|
}
|
|
|
|
/*
|
|
* Contributors
|
|
*/
|
|
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;
|
|
}
|
|
|
|
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) {
|
|
if (this.contributions[idx].contributor.name==name) {
|
|
ret.push(this.contributions[idx]);
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
this.addContribution=function(c) {
|
|
c.id=this.contributions.length;
|
|
this.contributions.push(c);
|
|
}
|
|
|
|
this.replaceContribution=function(idx,c) {
|
|
c.id=idx;
|
|
this.contributions[idx]=c;
|
|
}
|
|
|
|
/*
|
|
* Balance
|
|
*/
|
|
this.balance=function() {
|
|
total={}
|
|
min=-1;
|
|
max=0;
|
|
for (idx in this.contributors) {
|
|
var sum=0;
|
|
c=this.contributors[idx].name;
|
|
cl=this.contributionsByContributorName(c);
|
|
for (idc in cl) {
|
|
sum+=cl[idc].cost;
|
|
}
|
|
if (min==-1 || min>sum) {
|
|
min=sum;
|
|
}
|
|
if(max<sum) {
|
|
max=sum;
|
|
}
|
|
total[c]=sum;
|
|
}
|
|
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
|
|
};
|
|
}
|
|
|
|
/*
|
|
* 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
|
|
));
|
|
}
|
|
}
|
|
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,
|
|
data.contributions[idx].lastChange
|
|
));
|
|
}
|
|
}
|
|
}
|
|
catch (e) {
|
|
alert('Une erreur est survenue en chargeant le groupe '+this.name+' depuis le cache');
|
|
}
|
|
}
|
|
}
|
|
|
|
function Contributor(name,email,id) {
|
|
this.name=name;
|
|
this.email=email;
|
|
this.id=id;
|
|
this.export=function() {
|
|
return {
|
|
'name': this.name,
|
|
'email': this.email
|
|
};
|
|
}
|
|
}
|
|
|
|
function Contribution(contributor,cost,title,date,id,lastChange) {
|
|
this.contributor=contributor;
|
|
this.cost=cost;
|
|
this.title=title;
|
|
this.date=date;
|
|
this.id=id;
|
|
this.lastChange=lastChange || new Date().getTime();
|
|
this.export=function() {
|
|
return {
|
|
'contributor': this.contributor.name,
|
|
'cost': this.cost,
|
|
'title': this.title,
|
|
'date': this.date,
|
|
'lastChange': this.lastChange,
|
|
};
|
|
}
|
|
|
|
this.getTitle=function() {
|
|
if (jQuery.type(this.title)=='string') {
|
|
return this.title;
|
|
}
|
|
return '';
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|