MyCo/inc/myco_objects.js

231 lines
4.8 KiB
JavaScript
Raw Normal View History

2014-01-06 01:46:22 +01:00
function GroupList() {
lastChange=0;
2014-01-06 01:46:22 +01:00
this.loadFromLocalStorage=function() {
if (localStorage.groups!==undefined) {
var data=JSON.parse(localStorage.groups);
this.lastChange=data.lastChange;
for (el in data.groups) {
this[el]=new Group(el,data.groups[el]);
2014-01-06 01:46:22 +01:00
}
}
}
this.export=function() {
ret={};
for (el in this) {
if (this.isGroup(this[el])) {
ret[el]=this[el].export();
}
}
return ret;
}
this.save=function() {
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;
for (el in this) {
if(this.isGroup(this[el])) {
fct(idx++,this[el]);
}
}
}
this.count=function() {
len=0;
for (el in this) {
if (this.isGroup(this[el])) len=len+1;
}
return len;
}
this.isGroup=function(el) {
return (el.isGroup!==undefined);
}
this.removeGroup=function(name) {
if (this.isGroup(this[name])) {
delete this[name];
return true;
}
return false;
}
}
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
};
}
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) {
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;
}
2014-01-12 01:11:24 +01:00
/*
* Balance
*/
2014-01-06 01:46:22 +01:00
this.balance=function() {
ret={}
for (idx in this.contributors) {
sum=0;
c=this.contributors[idx].name;
cl=this.contributionsByContributorName(c);
for (idc in cl) {
sum+=cl[idc].cost;
}
ret[c]=sum;
}
return ret;
}
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(
data.contributors[idx].name,
data.contributors[idx].email,
idx
));
}
2014-01-06 01:46:22 +01:00
}
2014-01-12 01:11:24 +01:00
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
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 {
'name': this.name,
'email': this.email
};
}
}
function Contribution(contributor,cost,title,date,id,lastChange) {
2014-01-06 01:46:22 +01:00
this.contributor=contributor;
this.cost=cost;
this.title=title;
this.date=date;
this.id=id;
this.lastChange=lastChange || new Date().getTime();
2014-01-06 01:46:22 +01:00
this.export=function() {
return {
'contributor': this.contributor.name,
'cost': this.cost,
'title': this.title,
'date': this.date,
'lastChange': this.lastChange,
2014-01-06 01:46:22 +01:00
};
}
this.getTitle=function() {
if (jQuery.type(this.title)=='string') {
return this.title;
}
return '';
}
}