903c4f071baa1f7e7e28d13bb265ba8e975062a1
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 Initial commit

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() {
129)     var contributors=[];
130)     for (idx in this.contributors) {
131)       contributors.push(this.contributors[idx].export());
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) {
150)     this.contributors=this.contributors.filter(function(v){
151)       return (v.name!=c);
152)     });
153)   }
154) 
155)   this.contributorByName=function(name) {
156)     for (c in this.contributors) {
157)       if (this.contributors[c].name == name) return this.contributors[c];
158)     }
159)     return undefined;
160)   }
161) 
162)   this.contributorByEmail=function(email) {
163)     for (c in this.contributors) {
164)       if (this.contributors[c].email == email) return this.contributors[c];
165)     }
166)     return undefined;
167)   }
168) 
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

169)   this.addContributor=function(c) {
170)     c.id=this.contributors.length;
171)     this.contributors.push(c);
172)   }
173) 
174)   this.replaceContributor=function(idx,c) {
175)     c.id=idx;
176)     this.contributors[idx]=c;
177)   }
178) 
179)   /*
180)    * Contributions
181)    */
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

182)   this.contributionsByContributorName=function(name) {
Benjamin Renard Sort contribution by date o...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

184)     for (uuid in this.contributions) {
185)       if (this.contributions[uuid].contributor.name==name) {
Benjamin Renard Sort contribution by date o...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

187)       }
188)     }
Benjamin Renard Sort contribution by date o...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

200)     return ret;
201)   }
202) 
203)   this.addContribution=function(c) {
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

205)   }
206) 
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

210)   }
211) 
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

219)   /*
220)    * Balance
221)    */
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

223)     total={}
224)     min=-1;
225)     max=0;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

226)     for (idx in this.contributors) {
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

227)       var sum=0;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

228)       c=this.contributors[idx].name;
229)       cl=this.contributionsByContributorName(c);
Benjamin Renard Sort contribution by date o...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

232)       }
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

233)       if (min==-1 || min>sum) {
234)           min=sum;
235)         }
236)       if(max<sum) {
237)         max=sum;
238)       }
239)       total[c]=sum;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

240)     }
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

241)     balance={}
242)     var sum=0;
243)     for (c in total) {
244)       balance[c]={
245)         'total': total[c],
246)         'diff': total[c]-max,
247)       }
248)       sum=sum+total[c];
249)     }
250)     return {
251)       'balance': balance,
252)       'sum': sum,
253)       'min': min,
254)       'max': max
255)     };
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

256)   }
257) 
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

258)   /*
259)    * Contructor
260)    */
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

264)       this.name=data.name;
265)       if (jQuery.type(data.contributors) == 'array') {
266)         for (idx in data.contributors) {
267)           this.contributors.push(new Contributor(
Benjamin Renard Encode/Decode on import/exp...

Benjamin Renard authored 10 years ago

268)             decodeURIComponent(data.contributors[idx].name),
269)             decodeURIComponent(data.contributors[idx].email),
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

270)             idx
271)           ));
272)         }
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

289)         }
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

290)       }
291)     }
292)     catch (e) {
293)       alert('Une erreur est survenue en chargeant le groupe '+this.name+' depuis le cache');
294)     }
295)   }
296) }
297) 
Benjamin Renard Add edit contributor feature

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

299)   this.name=name;
300)   this.email=email;
Benjamin Renard Add edit contributor feature

Benjamin Renard authored 10 years ago

301)   this.id=id;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

302)   this.export=function() {
303)     return {
Benjamin Renard Encode/Decode on import/exp...

Benjamin Renard authored 10 years ago

304)       'name': encodeURIComponent(this.name),
305)       'email': encodeURIComponent(this.email)
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

306)     };
307)   }
308) }
309) 
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

311)   this.contributor=contributor;
312)   this.cost=cost;
313)   this.title=title;
314)   this.date=date;
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

317)   this.export=function() {
318)     return {
Benjamin Renard Encode/Decode on import/exp...

Benjamin Renard authored 10 years ago

319)       'contributor': encodeURIComponent(this.contributor.name),
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

322)       'title': encodeURIComponent(this.title),
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

323)       'date': this.date,
Benjamin Renard Add lastChange field in obj...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

325)     };
326)   }
327) 
328)   this.getTitle=function() {
329)     if (jQuery.type(this.title)=='string') {
330)       return this.title;
331)     }
332)     return '';
333)   }
334) }