fe92325853b716262368ea16b96c118849b11369
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) 
201)   this.addContribution=function(c) {
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

203)   }
204) 
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

205)   this.updateContribution=function(uuid,c) {
206)     c.uuid=uuid;
207)     this.contributions[uuid]=c;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

208)   }
209) 
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

210)   this.deleteContribution=function(uuid) {
211)     this.contributions[uuid].lastChange=new Date().getTime();
212)     this.deletedContributions[uuid]=this.contributions[uuid].export();
213)     delete this.contributions[uuid];
214)   }
215) 
216) 
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

217)   /*
218)    * Balance
219)    */
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

221)     total={}
222)     min=-1;
223)     max=0;
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

229)       }
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

230)       if (min==-1 || min>sum) {
231)           min=sum;
232)         }
233)       if(max<sum) {
234)         max=sum;
235)       }
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

237)     }
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

240)     for (email in total) {
241)       balance[email]={
242)         'name': this.contributors[email].name,
243)         'total': total[email],
244)         'diff': total[email]-max,
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

247)     }
248)     return {
249)       'balance': balance,
250)       'sum': sum,
251)       'min': min,
252)       'max': max
253)     };
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

254)   }
255) 
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

256)   /*
257)    * Contructor
258)    */
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

263)       if (jQuery.type(data.contributors) == 'object') {
264)         for (email in data.contributors) {
265)           this.contributors[email]=new Contributor(
266)             decodeURIComponent(data.contributors[email].name),
267)             data.contributors[email].email
268)           );
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

269)         }
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

271)       if (jQuery.type(data.contributions) == 'object') {
272)         for (uuid in data.contributions) {
273)           this.contributions[uuid]=new Contribution(
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

274)             this.contributorByEmail(data.contributions[uuid].contributor),
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

275)             data.contributions[uuid].cost,
276)             decodeURIComponent(data.contributions[uuid].title),
277)             data.contributions[uuid].date,
278)             uuid,
279)             data.contributions[uuid].lastChange
280)           );
281)         }
282)       }
283)       if (jQuery.type(data.deletedContributions) == 'object') {
284)         for (uuid in data.deletedContributions) {
285)           this.deletedContributions[uuid]=data.deletedContributions[uuid];
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

286)         }
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

287)       }
288)     }
289)     catch (e) {
290)       alert('Une erreur est survenue en chargeant le groupe '+this.name+' depuis le cache');
291)     }
292)   }
293) }
294) 
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

296)   this.name=name;
297)   this.email=email;
298)   this.export=function() {
299)     return {
Benjamin Renard Encode/Decode on import/exp...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

302)     };
303)   }
304) }
305) 
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

307)   this.contributor=contributor;
308)   this.cost=cost;
309)   this.title=title;
Benjamin Renard Transform date of contribut...

Benjamin Renard authored 10 years ago

310)   if (jQuery.type(date)!='date') {
311)     date=moment(date).toDate();
312)   }
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

316)   this.export=function() {
317)     return {
Benjamin Renard Manage contributor by their...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

324)     };
325)   }
326) 
327)   this.getTitle=function() {
328)     if (jQuery.type(this.title)=='string') {
329)       return this.title;
330)     }
331)     return '';
332)   }
333) }
Benjamin Renard Add login and sync support

Benjamin Renard authored 10 years ago

334) 
335) function SyncServer() {
336)   this.url=false;
337)   this.email=false;
338)   this.password=false;
339)   this.logged=false;
340) 
341)   this.login=function(url,email,password,onsuccess,onerror) {
342)     this.url=url;
343)     this.email=email;
344)     this.password=password;
345) 
346)     try {
347)       jQuery.getJSON(
348)         this.url+'/login',
349)         {'email':email,'password':password},
350)         function(data, textStatus) {
351)           console.log(data);
352)           if (textStatus=='success') {
Benjamin Renard Fixed login error display

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

354)               console.log('Login success return');
355)               console.log(onsuccess);
356)               onsuccess(data);
357)               return true;
358)             }
359)           }
360)           onerror(data);
361)         }
362)       ).fail(onerror);
363)     }
364)     catch(e) {
365)       if(jQuery.type(onerror)=='function') {
366)         onerror();
367)       }
368)     }
369)   }
370) 
Benjamin Renard Add subscribe feature

Benjamin Renard authored 10 years ago

371)   this.subscribe=function(url,email,name,password,onsuccess,onerror) {
372)     this.url=url;
373)     this.email=email;
374)     this.name=name;
375)     this.password=password;
376) 
377)     try {
378)       jQuery.getJSON(
379)         this.url+'/subscribe',
380)         {'email':email,'name': name,'password':password},
381)         function(data, textStatus) {
382)           console.log(data);
383)           if (textStatus=='success') {
384)             if(jQuery.type(data.email) != 'undefined' && jQuery.type(data.name) != 'undefined') {
385)               onsuccess(data);
386)               return true;
387)             }
388)           }
389)           onerror(data);
390)           return false;
391)         }
392)       ).fail(onerror);
393)     }
394)     catch(e) {
395)       if(jQuery.type(onerror)=='function') {
396)         onerror();
397)       }
398)     }
399)   }
400) 
401) 
Benjamin Renard Add login and sync support

Benjamin Renard authored 10 years ago

402)   this.sync=function(url,email,password,groups,onsuccess,onerror) {
403)     this.url=url;
404)     this.email=email;
405)     this.password=password;
406)     try {
Benjamin Renard Use POST instead of GET req...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

408)         this.url+'/sync',
409)         {
410)           'email':email,
411)           'password':password,
412)           'groups': JSON.stringify(groups)
413)         },
414)         function(data, textStatus) {
415)           console.log(data);
416)           if (textStatus=='success') {
417)             if(jQuery.type(data.groups)) {
418)               console.log('Sync success return');
419)               onsuccess(data);
420)               return true;
421)             }
422)           }
423)           onerror(data);
Benjamin Renard Use POST instead of GET req...

Benjamin Renard authored 10 years ago

424)         },
425)         'json'