bb84db00c0806ebcb5478e6838089ee01d6da6c9
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

1) function GroupList() {
Benjamin Renard Add lastChange field in obj...

Benjamin Renard authored 10 years ago

2) 
3)   lastChange=0;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

4)   
5)   this.loadFromLocalStorage=function() {
Benjamin Renard Manage local data loading e...

Benjamin Renard authored 10 years ago

6)     if (jQuery.type(localStorage.groups)!='undefined') {
7)       try {
8)         var data=JSON.parse(localStorage.groups);
9)         this.lastChange=data.lastChange;
10)         for (el in data.groups) {
Benjamin Renard Add uuid to group and refer...

Benjamin Renard authored 10 years ago

11)           this[el]=new Group(el,false,data.groups[el]);
Benjamin Renard Manage local data loading e...

Benjamin Renard authored 10 years ago

12)         }
13)       }
14)       catch(e) {
15)         for (el in this) {
16)           if (this.isGroup(this[el])) {
17)             delete this[el];
18)           }
19)         }
20)         myconfirm('Erreur en chargeant les données locales. On les purges ?',
21)           function(data) {
22)             delete localStorage.groups;
23)             location.reload();
24)           }
25)         );
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

26)       }
27)     }
28)   }
29) 
30)   this.export=function() {
Benjamin Renard Add lastChange in GroupList...

Benjamin Renard authored 10 years ago

31)     return {
32)       'lastChange': this.lastChange,
33)       'groups': this.each(function(idx,group) {
34)         return group.export();
35)       })
36)     };
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

37)   }
38) 
Benjamin Renard Add login and sync support

Benjamin Renard authored 10 years ago

39)   this.import=function(groups) {
40)     ret={};
41)     for (el in this) {
42)       if (this.isGroup(this[el])) {
43)         delete ret[el];
44)       }
45)     }
Benjamin Renard Add lastChange in GroupList...

Benjamin Renard authored 10 years ago

46)     this.lastChange=groups.lastChange;
47)     for (el in groups.groups) {
48)       this[el]=new Group(el,false,groups.groups[el]);
Benjamin Renard Add login and sync support

Benjamin Renard authored 10 years ago

49)     }
50)     return true;
51)   }
52) 
53) 
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

54)   this.save=function() {
Benjamin Renard Add lastChange in GroupList...

Benjamin Renard authored 10 years ago

55)     localStorage.groups=JSON.stringify(this.export());
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

56)   }
57) 
58)   this.each=function(fct) {
59)     var idx=0;
Benjamin Renard Improve GroupsList each() m...

Benjamin Renard authored 10 years ago

60)     var ret={};
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

61)     for (el in this) {
62)       if(this.isGroup(this[el])) {
Benjamin Renard Improve GroupsList each() m...

Benjamin Renard authored 10 years ago

63)         ret[el]=fct(idx++,this[el]);
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

64)       }
65)     }
Benjamin Renard Improve GroupsList each() m...

Benjamin Renard authored 10 years ago

66)     return ret;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

67)   }
68) 
69)   this.count=function() {
70)     len=0;
Benjamin Renard Improve GroupsList each() m...

Benjamin Renard authored 10 years ago

71)     this.each(function(idx,group) {
72)       len=len+1;
73)     });
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

74)     return len;
75)   }
76) 
77)   this.isGroup=function(el) {
Benjamin Renard Improve GroupList isGroup()...

Benjamin Renard authored 10 years ago

78)     return (jQuery.type(el)=='object' && jQuery.type(el.isGroup)=='function' && el.isGroup());
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

79)   }
80) 
Benjamin Renard Add uuid to group and refer...

Benjamin Renard authored 10 years ago

81)   this.byName=function(name) {
82)     for (el in this) {
83)       if(this.isGroup(this[el])) {
84)         if (this[el].name==name) {
85)           return this[el];
86)         }
87)       }
88)     }
89)     return false;
90)   }
91) 
92) 
93)   this.removeGroup=function(uuid) {
94)     if (this.isGroup(this[uuid])) {
95)       delete this[uuid];
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

96)       return true;
97)     }
98)     return false;
99)   }
Benjamin Renard Add GroupList balances() me...

Benjamin Renard authored 10 years ago

100) 
101)   this.balances=function(fct) {
102)     return this.each(function(idx,group) {
Benjamin Renard Add uuid to group and refer...

Benjamin Renard authored 10 years ago

103)       bal=group.balance();
104)       bal.name=group.name;
105)       return bal;
Benjamin Renard Add GroupList balances() me...

Benjamin Renard authored 10 years ago

106)     });
107)   }
108) 
Benjamin Renard Add uuid to group and refer...

Benjamin Renard authored 10 years ago

109)   this.newGroup=function(name,uuid) {
110)     var uuid=uuid||generate_uuid();
111)     this[uuid]=new Group(uuid,name);
112)     return uuid;
113)   }
114) 
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

115) }
116) 
Benjamin Renard Add uuid to group and refer...

Benjamin Renard authored 10 years ago

117) function Group(uuid,name,data) {
118)   this.uuid=uuid || generate_uuid();
119)   this.name=name || false;
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

120)   this.contributors={};
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

121)   this.contributions={};
122)   this.deletedContributions={};
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

123) 
124) 
125)   this.isGroup=function() {
126)     return true;
127)   }
128) 
129)   this.export=function() {
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

130)     var contributors={};
131)     for (email in this.contributors) {
132)       contributors[email]=this.contributors[email].export();
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

133)     }
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

134)     var contributions={}
135)     for (uuid in this.contributions) {
136)       contributions[uuid]=this.contributions[uuid].export();
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

137)     }
138)     return {
Benjamin Renard Add uuid to group and refer...

Benjamin Renard authored 10 years ago

139)       'uuid': this.uuid,
Benjamin Renard Encode/Decode on import/exp...

Benjamin Renard authored 10 years ago

140)       'name': encodeURIComponent(this.name),
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

141)       'contributors': contributors,
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

142)       'contributions': contributions,
143)       'deletedContributions': this.deletedContributions
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

144)     };
145)   }
146) 
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

147)   /*
148)    * Contributors
149)    */
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

150)   this.removeContributor=function(c) {
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

151)     delete this.contributors[c.email];
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

152)   }
153) 
154)   this.contributorByName=function(name) {
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

155)     for (email in this.contributors) {
156)       if (this.contributors[email].name == name) return this.contributors[email];
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

157)     }
158)     return undefined;
159)   }
160) 
161)   this.contributorByEmail=function(email) {
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

162)     if (jQuery.type(this.contributors[email])!='undefined') {
163)       return this.contributors[email];
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

164)     }
165)     return undefined;
166)   }
167) 
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

168)   this.addContributor=function(c) {
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

169)     this.contributors[c.email]=c;
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

170)   }
171) 
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

172)   this.replaceContributor=function(email,c) {
173)     delete this.contributors[email];
174)     this.contributors[c.email]=c;
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

175)   }
176) 
177)   /*
178)    * Contributions
179)    */
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

180)   this.contributionsByContributorEmail=function(email) {
Benjamin Renard Sort contribution by date o...

Benjamin Renard authored 10 years ago

181)     var ret=[];
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

182)     for (uuid in this.contributions) {
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

183)       if (this.contributions[uuid].contributor.email==email) {
Benjamin Renard Sort contribution by date o...

Benjamin Renard authored 10 years ago

184)         ret.push(this.contributions[uuid]);
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

185)       }
186)     }
Benjamin Renard Sort contribution by date o...

Benjamin Renard authored 10 years ago

187)     ret.sort(function(a,b) {
188)       if (a.date==b.date) {
189)         return 0;
190)       }
191)       else if(a.date<b.date) {
192)         return -1;
193)       }
194)       else {
195)         return 1;
196)       }
197)     });
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

198)     return ret;
199)   }
200) 
Benjamin Renard Add possibility to view trash

Benjamin Renard authored 10 years ago

201)   this.deletedContributionsByContributorEmail=function(email) {
202)     var ret=[];
203)     for (uuid in this.deletedContributions) {
204)       if (this.deletedContributions[uuid].contributor==email) {
205)         ret.push(new Contribution(
206)           this.contributorByEmail(email),
207)           this.deletedContributions[uuid].cost,
208)           decodeURIComponent(this.deletedContributions[uuid].title),
209)           this.deletedContributions[uuid].date,
210)           uuid,
211)           this.deletedContributions[uuid].lastChange
212)         ));
213)       }
214)     }
215)     ret.sort(function(a,b) {
216)       if (a.lastChange==b.lastChange) {
217)         return 0;
218)       }
219)       else if(a.lastChange<b.lastChange) {
220)         return -1;
221)       }
222)       else {
223)         return 1;
224)       }
225)     });
226)     return ret;
227)   }
228) 
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

229)   this.addContribution=function(c) {
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

230)     this.contributions[c.uuid]=c;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

231)   }
232) 
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

233)   this.updateContribution=function(uuid,c) {
234)     c.uuid=uuid;
235)     this.contributions[uuid]=c;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

236)   }
237) 
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

238)   this.deleteContribution=function(uuid) {
239)     this.contributions[uuid].lastChange=new Date().getTime();
240)     this.deletedContributions[uuid]=this.contributions[uuid].export();
241)     delete this.contributions[uuid];
242)   }
243) 
Benjamin Renard Add possibility to view trash

Benjamin Renard authored 10 years ago

244)   this.restoreContribution=function(uuid) {
245)     this.deletedContributions[uuid].lastChange=new Date().getTime();
246)     this.contributions[uuid]=this.importContribution(this.deletedContributions[uuid]);
247)     delete this.deletedContributions[uuid];
248)   }
249) 
250)   this.importContribution=function(data) {
251)     return new Contribution(
252)       this.contributorByEmail(data.contributor),
253)       data.cost,
254)       decodeURIComponent(data.title),
255)       data.date,
256)       data.uuid,
257)       data.lastChange
258)     );
259)   }
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

260) 
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

261)   /*
262)    * Balance
263)    */
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

264)   this.balance=function() {
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

265)     total={}
266)     min=-1;
267)     max=0;
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

268)     for (email in this.contributors) {
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

269)       var sum=0;
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

270)       cl=this.contributionsByContributorEmail(email);
Benjamin Renard Sort contribution by date o...

Benjamin Renard authored 10 years ago

271)       for (idx in cl) {
272)         sum+=cl[idx].cost;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

273)       }
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

274)       if (min==-1 || min>sum) {
275)           min=sum;
276)         }
277)       if(max<sum) {
278)         max=sum;
279)       }
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

280)       total[email]=sum;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

281)     }
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

282)     balance={}
283)     var sum=0;
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

284)     for (email in total) {
285)       balance[email]={
286)         'name': this.contributors[email].name,
287)         'total': total[email],
288)         'diff': total[email]-max,
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

289)       }
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

290)       sum=sum+total[email];
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

291)     }
292)     return {
293)       'balance': balance,
294)       'sum': sum,
295)       'min': min,
296)       'max': max
297)     };
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

298)   }
299) 
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

300)   /*
301)    * Contructor
302)    */
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

303)   if (jQuery.type(data)=='object') {
304)     try {
Benjamin Renard Add uuid to group and refer...

Benjamin Renard authored 10 years ago

305)       this.uuid=data.uuid;
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

306)       this.name=data.name;
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

307)       if (jQuery.type(data.contributors) == 'object') {
308)         for (email in data.contributors) {
309)           this.contributors[email]=new Contributor(
310)             decodeURIComponent(data.contributors[email].name),
311)             data.contributors[email].email
312)           );
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

313)         }
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

314)       }
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

315)       if (jQuery.type(data.contributions) == 'object') {
316)         for (uuid in data.contributions) {
Benjamin Renard Add possibility to view trash

Benjamin Renard authored 10 years ago

317)           this.contributions[uuid]=this.importContribution(data.contributions[uuid]);
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

318)         }
319)       }
320)       if (jQuery.type(data.deletedContributions) == 'object') {
321)         for (uuid in data.deletedContributions) {
322)           this.deletedContributions[uuid]=data.deletedContributions[uuid];
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

323)         }
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

324)       }
325)     }
326)     catch (e) {
327)       alert('Une erreur est survenue en chargeant le groupe '+this.name+' depuis le cache');
328)     }
329)   }
330) }
331) 
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

332) function Contributor(name,email) {
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

333)   this.name=name;
334)   this.email=email;
335)   this.export=function() {
336)     return {
Benjamin Renard Encode/Decode on import/exp...

Benjamin Renard authored 10 years ago

337)       'name': encodeURIComponent(this.name),
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

338)       'email': this.email
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

339)     };
340)   }
341) }
342) 
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

343) function Contribution(contributor,cost,title,date,uuid,lastChange) {
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

344)   this.contributor=contributor;
345)   this.cost=cost;
346)   this.title=title;
Benjamin Renard Transform date of contribut...

Benjamin Renard authored 10 years ago

347)   if (jQuery.type(date)!='date') {
348)     date=moment(date).toDate();
349)   }
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

350)   this.date=date;
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

351)   this.uuid=uuid || generate_uuid();
Benjamin Renard Add lastChange field in obj...

Benjamin Renard authored 10 years ago

352)   this.lastChange=lastChange || new Date().getTime();
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

353)   this.export=function() {
354)     return {
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

355)       'contributor': this.contributor.email,
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

356)       'uuid': this.uuid,
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

357)       'cost': this.cost,
Benjamin Renard Encode/Decode on import/exp...

Benjamin Renard authored 10 years ago

358)       'title': encodeURIComponent(this.title),
Benjamin Renard Transform date of contribut...

Benjamin Renard authored 10 years ago

359)       'date': this.date.getTime(),
Benjamin Renard Add lastChange field in obj...

Benjamin Renard authored 10 years ago

360)       'lastChange': this.lastChange,
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

361)     };
362)   }
363) 
364)   this.getTitle=function() {
365)     if (jQuery.type(this.title)=='string') {
366)       return this.title;
367)     }
368)     return '';
369)   }
370) }
Benjamin Renard Add login and sync support

Benjamin Renard authored 10 years ago

371) 
372) function SyncServer() {
373)   this.url=false;
374)   this.email=false;
375)   this.password=false;
376)   this.logged=false;
377) 
378)   this.login=function(url,email,password,onsuccess,onerror) {
379)     this.url=url;
380)     this.email=email;
381)     this.password=password;
382) 
383)     try {
384)       jQuery.getJSON(
385)         this.url+'/login',
386)         {'email':email,'password':password},
387)         function(data, textStatus) {
388)           console.log(data);
389)           if (textStatus=='success') {
Benjamin Renard Fixed login error display

Benjamin Renard authored 10 years ago

390)             if(jQuery.type(data.email) != 'undefined' && jQuery.type(data.name) != 'undefined') {
Benjamin Renard Add login and sync support

Benjamin Renard authored 10 years ago

391)               console.log('Login success return');
392)               console.log(onsuccess);
393)               onsuccess(data);
394)               return true;
395)             }
396)           }
397)           onerror(data);
398)         }
399)       ).fail(onerror);
400)     }
401)     catch(e) {
402)       if(jQuery.type(onerror)=='function') {
403)         onerror();
404)       }
405)     }
406)   }
407) 
Benjamin Renard Add subscribe feature

Benjamin Renard authored 10 years ago

408)   this.subscribe=function(url,email,name,password,onsuccess,onerror) {
409)     this.url=url;
410)     this.email=email;
411)     this.name=name;
412)     this.password=password;
413) 
414)     try {
415)       jQuery.getJSON(
416)         this.url+'/subscribe',
417)         {'email':email,'name': name,'password':password},
418)         function(data, textStatus) {
419)           console.log(data);
420)           if (textStatus=='success') {
421)             if(jQuery.type(data.email) != 'undefined' && jQuery.type(data.name) != 'undefined') {
422)               onsuccess(data);
423)               return true;
424)             }
425)           }
426)           onerror(data);
427)           return false;
428)         }
429)       ).fail(onerror);
430)     }
431)     catch(e) {
432)       if(jQuery.type(onerror)=='function') {
433)         onerror();
434)       }
435)     }
436)   }
437) 
438) 
Benjamin Renard Add login and sync support

Benjamin Renard authored 10 years ago

439)   this.sync=function(url,email,password,groups,onsuccess,onerror) {
440)     this.url=url;
441)     this.email=email;
442)     this.password=password;
443)     try {
Benjamin Renard Use POST instead of GET req...

Benjamin Renard authored 10 years ago

444)       jQuery.post(
Benjamin Renard Add login and sync support

Benjamin Renard authored 10 years ago

445)         this.url+'/sync',
446)         {
447)           'email':email,
448)           'password':password,
449)           'groups': JSON.stringify(groups)
450)         },
451)         function(data, textStatus) {
452)           console.log(data);
453)           if (textStatus=='success') {
454)             if(jQuery.type(data.groups)) {
455)               console.log('Sync success return');
456)               onsuccess(data);
457)               return true;
458)             }
459)           }
460)           onerror(data);
Benjamin Renard Use POST instead of GET req...

Benjamin Renard authored 10 years ago

461)         },
462)         'json'