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,false,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,false,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.byName=function(name) { for (el in this) { 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 false; } this.balances=function(fct) { return this.each(function(idx,group) { 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(uuid,name,data) { this.uuid=uuid || generate_uuid(); this.name=name || false; this.contributors=[]; this.contributions={}; this.deletedContributions={}; this.isGroup=function() { return true; } this.export=function() { var contributors=[]; for (idx in this.contributors) { contributors.push(this.contributors[idx].export()); } var contributions={} for (uuid in this.contributions) { contributions[uuid]=this.contributions[uuid].export(); } return { 'uuid': this.uuid, 'name': encodeURIComponent(this.name), 'contributors': contributors, 'contributions': contributions, 'deletedContributions': this.deletedContributions }; } /* * 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 (uuid in this.contributions) { if (this.contributions[uuid].contributor.name==name) { ret.push(this.contributions[uuid]); } } ret.sort(function(a,b) { if (a.date==b.date) { return 0; } else if(a.datesum) { min=sum; } if(max