MyCo/inc/myco_objects.js

711 lines
17 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() {
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,false,data.groups[el]);
2014-01-12 03:05:14 +01:00
}
}
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() {
return {
'lastChange': this.lastChange,
'groups': 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];
}
}
this.lastChange=groups.lastChange;
for (el in groups.groups) {
this[el]=new Group(el,false,groups.groups[el]);
2014-01-12 01:24:16 +01:00
}
return true;
}
2014-01-06 01:46:22 +01:00
this.save=function() {
localStorage.groups=JSON.stringify(this.export());
2014-01-06 01:46:22 +01:00
}
this.each=function(fct) {
var idx=0;
var ret={};
2014-01-06 01:46:22 +01:00
for (el in this) {
if(this.isGroup(this[el])) {
ret[el]=fct(idx++,this[el]);
2014-01-06 01:46:22 +01:00
}
}
return ret;
2014-01-06 01:46:22 +01:00
}
this.count=function() {
len=0;
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.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];
2014-01-06 01:46:22 +01:00
return true;
}
return false;
}
2014-01-12 20:09:47 +01:00
this.balances=function(fct) {
return this.each(function(idx,group) {
bal=group.balance();
bal.name=group.name;
return bal;
2014-01-12 20:09:47 +01:00
});
}
this.newGroup=function(name,uuid) {
var uuid=uuid||generate_uuid();
this[uuid]=new Group(uuid,name);
return uuid;
}
2014-01-06 01:46:22 +01:00
}
function Group(uuid,name,data) {
this.uuid=uuid || generate_uuid();
this.name=name || false;
2014-01-19 14:48:41 +01:00
this.contributors={};
2014-01-31 00:41:22 +01:00
this.deletedContributors={};
this.contributions={};
this.deletedContributions={};
2014-07-19 20:06:57 +02:00
this.categories={};
2014-07-19 23:58:59 +02:00
this.deletedCategories={};
2014-01-06 01:46:22 +01:00
this.isGroup=function() {
return true;
}
this.export=function() {
2014-01-19 14:48:41 +01:00
var contributors={};
for (email in this.contributors) {
contributors[email]=this.contributors[email].export();
2014-01-06 01:46:22 +01:00
}
var contributions={}
for (uuid in this.contributions) {
contributions[uuid]=this.contributions[uuid].export();
2014-01-06 01:46:22 +01:00
}
2014-07-19 23:58:59 +02:00
var categories={}
for (uuid in this.categories) {
categories[uuid]=this.categories[uuid].export();
}
2014-01-06 01:46:22 +01:00
return {
'uuid': this.uuid,
'name': encodeURIComponent(this.name),
2014-01-06 01:46:22 +01:00
'contributors': contributors,
2014-01-31 00:41:22 +01:00
'deletedContributors': this.deletedContributors,
'contributions': contributions,
2014-07-19 20:06:57 +02:00
'deletedContributions': this.deletedContributions,
2014-07-19 23:58:59 +02:00
'categories': categories,
'deletedCategories': this.deletedCategories
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) {
2014-01-31 00:41:22 +01:00
this.deletedContributors[c.email]=c.export();
time=new Date().getTime();
this.deletedContributors[c.email].deletionTime=time;
contributions=this.contributionsByContributorEmail(c.email);
for (idx in contributions) {
this.deleteContribution(contributions[idx].uuid,time);
}
2014-01-19 14:48:41 +01:00
delete this.contributors[c.email];
2014-01-31 00:41:22 +01:00
return true;
}
this.restoreContributor=function(email) {
if (email in this.contributors) return;
if (! email in this.deletedContributors) return;
this.contributors[email]=this.importContributor(this.deletedContributors[email]);
contributions=this.deletedContributionsByContributorEmail(email);
for (idx in contributions) {
if (contributions[idx].lastChange==this.deletedContributors[email].deletionTime) {
this.restoreContribution(contributions[idx].uuid);
}
}
delete this.deletedContributors[email];
return true;
}
this.getDeletedContributors=function() {
var ret=[];
for (email in this.deletedContributors) {
ret.push(this.importContributor(this.deletedContributors[email]));
}
return ret;
2014-01-06 01:46:22 +01:00
}
this.contributorByName=function(name) {
2014-01-19 14:48:41 +01:00
for (email in this.contributors) {
if (this.contributors[email].name == name) return this.contributors[email];
2014-01-06 01:46:22 +01:00
}
return undefined;
}
this.contributorByEmail=function(email) {
2014-01-19 14:48:41 +01:00
if (jQuery.type(this.contributors[email])!='undefined') {
return this.contributors[email];
2014-01-06 01:46:22 +01:00
}
return undefined;
}
2014-01-12 01:11:24 +01:00
this.addContributor=function(c) {
2014-01-19 14:48:41 +01:00
this.contributors[c.email]=c;
2014-01-12 01:11:24 +01:00
}
2014-01-19 14:48:41 +01:00
this.replaceContributor=function(email,c) {
delete this.contributors[email];
this.contributors[c.email]=c;
2014-01-12 01:11:24 +01:00
}
2014-07-19 23:58:59 +02:00
this.importContributor=function(data) {
return new Contributor(
decodeURIComponent(data.name),
data.email
);
}
2014-01-12 01:11:24 +01:00
/*
* Contributions
*/
2014-01-19 14:48:41 +01:00
this.contributionsByContributorEmail=function(email) {
2014-01-16 20:40:05 +01:00
var ret=[];
for (uuid in this.contributions) {
2014-01-19 14:48:41 +01:00
if (this.contributions[uuid].contributor.email==email) {
2014-01-16 20:40:05 +01:00
ret.push(this.contributions[uuid]);
2014-01-06 01:46:22 +01:00
}
}
2014-01-16 20:40:05 +01:00
ret.sort(function(a,b) {
if (a.date==b.date) {
return 0;
}
else if(a.date<b.date) {
return -1;
}
else {
return 1;
}
});
2014-01-06 01:46:22 +01:00
return ret;
}
2014-01-30 00:30:05 +01:00
this.deletedContributionsByContributorEmail=function(email) {
var ret=[];
for (uuid in this.deletedContributions) {
if (this.deletedContributions[uuid].contributor==email) {
ret.push(new Contribution(
this.contributorByEmail(email),
this.deletedContributions[uuid].cost,
decodeURIComponent(this.deletedContributions[uuid].title),
this.deletedContributions[uuid].date,
2014-07-19 23:58:59 +02:00
this.deletedContributions[uuid].category,
2014-01-30 00:30:05 +01:00
uuid,
this.deletedContributions[uuid].lastChange
));
}
}
ret.sort(function(a,b) {
if (a.lastChange==b.lastChange) {
return 0;
}
else if(a.lastChange<b.lastChange) {
return -1;
}
else {
return 1;
}
});
return ret;
}
2014-01-06 01:46:22 +01:00
this.addContribution=function(c) {
this.contributions[c.uuid]=c;
2014-01-06 01:46:22 +01:00
}
this.updateContribution=function(uuid,c) {
c.uuid=uuid;
this.contributions[uuid]=c;
2014-01-06 01:46:22 +01:00
}
2014-01-31 00:41:22 +01:00
this.deleteContribution=function(uuid,time) {
this.contributions[uuid].lastChange=time || new Date().getTime();
this.deletedContributions[uuid]=this.contributions[uuid].export();
delete this.contributions[uuid];
}
2014-01-30 00:30:05 +01:00
this.restoreContribution=function(uuid) {
this.deletedContributions[uuid].lastChange=new Date().getTime();
this.contributions[uuid]=this.importContribution(this.deletedContributions[uuid]);
delete this.deletedContributions[uuid];
}
this.importContribution=function(data) {
return new Contribution(
this.contributorByEmail(data.contributor),
data.cost,
decodeURIComponent(data.title),
data.date,
2014-07-19 23:58:59 +02:00
data.category,
2014-01-30 00:30:05 +01:00
data.uuid,
data.lastChange
);
}
2014-07-19 23:58:59 +02:00
this.findContributionByTitleMatches=function() {
var contributions=this.contributions;
return function(q, cb) {
var matches, substrRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
var titles=[];
for (uuid in contributions) {
if (substrRegex.test(contributions[uuid].title)) {
var title=String(contributions[uuid].title).replace(/^\s+|\s+$/g, '');
if (titles.indexOf(title.toLowerCase())!=-1) {
continue;
}
titles.push(title.toLowerCase());
matches.push({
value: title,
category: contributions[uuid].category
});
}
}
cb(matches);
};
}
2014-10-13 00:20:28 +02:00
this.searchContributions=function(pattern) {
var ret=[];
substrRegex = new RegExp(pattern, 'i');
for (uuid in this.contributions) {
if (substrRegex.test(this.contributions[uuid].title) || substrRegex.test(this.contributions[uuid].cost)) {
ret.push(this.contributions[uuid]);
}
}
return ret;
}
this.getContributionsByCategory=function (category) {
var ret={};
for (uuid in this.contributions) {
if (this.contributions[uuid].category==category.uuid) {
ret[uuid]=this.contributions[uuid];
}
}
return ret;
}
this.findUnusedCategories=function() {
var cats={};
for (uuid in this.contributions) {
cid=this.contributions[uuid].category;
if ($.type(cats[cid])=='undefined') {
cats[cid]=1;
}
else {
cats[cid]++;
}
}
var ret={};
for (cid in this.categories) {
if ($.type(cats[cid])=='undefined') {
ret[cid]=this.categories[cid];
}
}
return ret;
}
2014-07-19 23:58:59 +02:00
/*
* Categories
*/
this.addCategory=function(c) {
this.categories[c.uuid]=c;
}
this.updateCategory=function(uuid,c) {
c.uuid=uuid;
this.categories[uuid]=c;
}
this.deleteCategory=function(uuid,time) {
this.categories[uuid].lastChange=time || new Date().getTime();
this.deletedCategories[uuid]=this.categories[uuid].export();
2014-07-19 23:58:59 +02:00
delete this.categories[uuid];
}
this.restoreCategory=function(uuid) {
this.deletedCategories[uuid].lastChange=new Date().getTime();
this.categories[uuid]=this.importCategory(this.deletedCategories[uuid]);
delete this.deletedCategories[uuid];
}
this.getCategoryByName=function(name,approx) {
if(approx) {
name=String(name).replace(/^\s+|\s+$/g, '').toLowerCase();
}
for (uuid in this.categories) {
if (approx) {
if (String(this.categories[uuid].name).replace(/^\s+|\s+$/g, '').toLowerCase()==name) {
return this.categories[uuid];
}
}
else if(this.categories[uuid].name==name) {
return this.categories[uuid]
}
}
return false;
2014-07-19 23:58:59 +02:00
}
this.importCategory=function(data) {
return new Category(
decodeURIComponent(data.name),
data.color,
data.lastChange,
data.uuid
2014-07-19 23:58:59 +02:00
);
}
2014-07-20 00:37:25 +02:00
this.getSortedCategories=function() {
uuids=[];
for (uuid in this.categories) {
uuids.push(uuid);
}
tmp_cats=this.categories;
uuids.sort(function(a,b) {
return tmp_cats[a]['name'].localeCompare(tmp_cats[b]['name']);
});
var ret={};
for (idx in uuids) {
ret[uuids[idx]]=this.categories[uuids[idx]];
}
return ret;
}
2014-07-23 01:59:56 +02:00
this.getCategoriesStats=function() {
var cats={};
for (uuid in this.contributions) {
if (jQuery.type(cats[this.contributions[uuid].category])=='undefined') {
cats[this.contributions[uuid].category]=0;
}
cats[this.contributions[uuid].category]+=this.contributions[uuid].cost;
}
var data=[];
for (cid in cats) {
if (jQuery.type(this.categories[cid])!='undefined') {
data.push({
'label': this.categories[cid].name,
2014-07-23 02:04:28 +02:00
'data': cats[cid],
'color': this.categories[cid].color
2014-07-23 01:59:56 +02:00
});
}
}
return data;
}
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-19 14:48:41 +01:00
for (email in this.contributors) {
2014-01-12 20:07:01 +01:00
var sum=0;
2014-01-19 14:48:41 +01:00
cl=this.contributionsByContributorEmail(email);
2014-01-16 20:40:05 +01:00
for (idx in cl) {
sum+=cl[idx].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;
}
2014-01-19 14:48:41 +01:00
total[email]=sum;
2014-01-06 01:46:22 +01:00
}
2014-01-12 20:07:01 +01:00
balance={}
var sum=0;
2014-01-19 14:48:41 +01:00
for (email in total) {
balance[email]={
'name': this.contributors[email].name,
'total': total[email],
'diff': total[email]-max,
2014-01-12 20:07:01 +01:00
}
2014-01-19 14:48:41 +01:00
sum=sum+total[email];
2014-01-12 20:07:01 +01:00
}
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 {
this.uuid=data.uuid;
2014-01-12 01:11:24 +01:00
this.name=data.name;
2014-01-19 14:48:41 +01:00
if (jQuery.type(data.contributors) == 'object') {
for (email in data.contributors) {
2014-01-31 00:41:22 +01:00
this.contributors[email]=this.importContributor(data.contributors[email]);
}
}
if (jQuery.type(data.deletedContributors) == 'object') {
for (email in data.deletedContributors) {
this.deletedContributors[email]=data.deletedContributors[email];
2014-01-12 01:11:24 +01:00
}
2014-01-06 01:46:22 +01:00
}
if (jQuery.type(data.contributions) == 'object') {
for (uuid in data.contributions) {
2014-01-30 00:30:05 +01:00
this.contributions[uuid]=this.importContribution(data.contributions[uuid]);
}
}
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
}
2014-07-19 20:06:57 +02:00
if (jQuery.type(data.categories) == 'object') {
2014-07-19 23:58:59 +02:00
for (uuid in data.categories) {
this.categories[uuid]=this.importCategory(data.categories[uuid]);
2014-07-19 20:06:57 +02:00
}
}
2014-07-19 23:58:59 +02:00
if (jQuery.type(data.deletedCategories) == 'object') {
for (uuid in data.deletedCategories) {
this.deletedCategories[uuid]=data.deletedCategories[uuid];
}
}
2014-01-06 01:46:22 +01:00
}
catch (e) {
2014-07-19 23:58:59 +02:00
console.log(e);
2014-01-06 01:46:22 +01:00
alert('Une erreur est survenue en chargeant le groupe '+this.name+' depuis le cache');
}
}
}
2014-01-19 14:48:41 +01:00
function Contributor(name,email) {
2014-01-06 01:46:22 +01:00
this.name=name;
this.email=email;
this.export=function() {
return {
'name': encodeURIComponent(this.name),
2014-01-19 14:48:41 +01:00
'email': this.email
2014-01-06 01:46:22 +01:00
};
}
}
function Category(name,color,lastChange,uuid) {
2014-07-19 23:58:59 +02:00
this.name=name;
this.color=color || '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
this.lastChange=lastChange || new Date().getTime();
this.uuid=uuid || generate_uuid();
2014-07-19 23:58:59 +02:00
this.export=function() {
return {
'name': encodeURIComponent(this.name),
'color': this.color,
'lastChange': this.lastChange,
'uuid': this.uuid
2014-07-19 23:58:59 +02:00
};
}
}
2014-07-19 20:06:57 +02:00
function Contribution(contributor,cost,title,date,cat,uuid,lastChange) {
2014-01-06 01:46:22 +01:00
this.contributor=contributor;
this.cost=cost;
this.title=title;
if (jQuery.type(date)!='date') {
date=moment(date).toDate();
}
2014-01-06 01:46:22 +01:00
this.date=date;
this.uuid=uuid || generate_uuid();
2014-07-19 23:58:59 +02:00
this.category=cat;
this.lastChange=lastChange || new Date().getTime();
2014-01-06 01:46:22 +01:00
this.export=function() {
return {
2014-01-19 14:48:41 +01:00
'contributor': this.contributor.email,
'uuid': this.uuid,
2014-01-06 01:46:22 +01:00
'cost': this.cost,
'title': encodeURIComponent(this.title),
'date': this.date.getTime(),
2014-07-19 23:58:59 +02:00
'category': this.category,
'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',
2014-02-03 00:10:42 +01:00
{'email':email,'password':password, 'time': new Date().getTime()},
2014-01-12 01:24:16 +01:00
function(data, textStatus) {
console.log(data);
if (textStatus=='success') {
2014-01-18 22:42:28 +01:00
if(jQuery.type(data.email) != 'undefined' && jQuery.type(data.name) != 'undefined') {
2014-01-12 01:24:16 +01:00
console.log('Login success return');
console.log(onsuccess);
onsuccess(data);
return true;
}
}
onerror(data);
}
).fail(onerror);
}
catch(e) {
if(jQuery.type(onerror)=='function') {
onerror();
}
}
}
2014-01-18 22:41:59 +01:00
this.subscribe=function(url,email,name,password,onsuccess,onerror) {
this.url=url;
this.email=email;
this.name=name;
this.password=password;
try {
jQuery.getJSON(
this.url+'/subscribe',
2014-02-03 00:10:42 +01:00
{'email':email,'name': name,'password':password, 'time': new Date().getTime()},
2014-01-18 22:41:59 +01:00
function(data, textStatus) {
console.log(data);
if (textStatus=='success') {
if(jQuery.type(data.email) != 'undefined' && jQuery.type(data.name) != 'undefined') {
onsuccess(data);
return true;
}
}
onerror(data);
return false;
}
).fail(onerror);
}
catch(e) {
if(jQuery.type(onerror)=='function') {
onerror();
}
}
}
2014-01-12 01:24:16 +01:00
this.sync=function(url,email,password,groups,onsuccess,onerror) {
this.url=url;
this.email=email;
this.password=password;
try {
jQuery.post(
2014-01-12 01:24:16 +01:00
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);
},
'json'
2014-01-12 01:24:16 +01:00
).fail(onerror);
}
catch(e) {
if(jQuery.type(onerror)=='function') {
onerror();
}
}
}
}