0b8d66fbf5bf74ec0d5aabf69810e133bc1ca034
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 Improve GroupsList each() m...

Benjamin Renard authored 10 years ago

31)     return this.each(function(idx,group) {
32)       return group.export();
33)     });
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

34)   }
35) 
Benjamin Renard Add login and sync support

Benjamin Renard authored 10 years ago

36)   this.import=function(groups) {
37)     ret={};
38)     for (el in this) {
39)       if (this.isGroup(this[el])) {
40)         delete ret[el];
41)       }
42)     }
43)     for (el in groups) {
Benjamin Renard Add uuid to group and refer...

Benjamin Renard authored 10 years ago

44)       this[el]=new Group(el,false,groups[el]);
Benjamin Renard Add login and sync support

Benjamin Renard authored 10 years ago

45)     }
46)     return true;
47)   }
48) 
49) 
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

50)   this.save=function() {
Benjamin Renard Add lastChange field in obj...

Benjamin Renard authored 10 years ago

51)     localStorage.groups=JSON.stringify({
52)       'lastChange': new Date().getTime(),
53)       'groups': this.export()
54)     });
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

65)     return ret;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

197)     return ret;
198)   }
199) 
200)   this.addContribution=function(c) {
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

228)       }
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

236)     }
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

253)   }
254) 
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

268)         }
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

285)         }
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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