ad5ed6fd1e18f71e01745ce4b06b42862ccd8231
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 Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

183)     var ret={};
184)     for (uuid in this.contributions) {
185)       if (this.contributions[uuid].contributor.name==name) {
186)         ret[uuid]=this.contributions[uuid];
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

187)       }
188)     }
189)     return ret;
190)   }
191) 
192)   this.addContribution=function(c) {
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

194)   }
195) 
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

196)   this.updateContribution=function(uuid,c) {
197)     c.uuid=uuid;
198)     this.contributions[uuid]=c;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

199)   }
200) 
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

201)   this.deleteContribution=function(uuid) {
202)     this.contributions[uuid].lastChange=new Date().getTime();
203)     this.deletedContributions[uuid]=this.contributions[uuid].export();
204)     delete this.contributions[uuid];
205)   }
206) 
207) 
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

208)   /*
209)    * Balance
210)    */
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

212)     total={}
213)     min=-1;
214)     max=0;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

216)       var sum=0;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

217)       c=this.contributors[idx].name;
218)       cl=this.contributionsByContributorName(c);
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

219)       for (uuid in cl) {
220)         sum+=cl[uuid].cost;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

221)       }
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

222)       if (min==-1 || min>sum) {
223)           min=sum;
224)         }
225)       if(max<sum) {
226)         max=sum;
227)       }
228)       total[c]=sum;
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

229)     }
Benjamin Renard Improve balance display

Benjamin Renard authored 10 years ago

230)     balance={}
231)     var sum=0;
232)     for (c in total) {
233)       balance[c]={
234)         'total': total[c],
235)         'diff': total[c]-max,
236)       }
237)       sum=sum+total[c];
238)     }
239)     return {
240)       'balance': balance,
241)       'sum': sum,
242)       'min': min,
243)       'max': max
244)     };
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

245)   }
246) 
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

247)   /*
248)    * Contructor
249)    */
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

253)       this.name=data.name;
254)       if (jQuery.type(data.contributors) == 'array') {
255)         for (idx in data.contributors) {
256)           this.contributors.push(new Contributor(
Benjamin Renard Encode/Decode on import/exp...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

259)             idx
260)           ));
261)         }
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

263)       if (jQuery.type(data.contributions) == 'object') {
264)         for (uuid in data.contributions) {
265)           this.contributions[uuid]=new Contribution(
266)             this.contributorByName(data.contributions[uuid].contributor),
267)             data.contributions[uuid].cost,
268)             decodeURIComponent(data.contributions[uuid].title),
269)             data.contributions[uuid].date,
270)             uuid,
271)             data.contributions[uuid].lastChange
272)           );
273)         }
274)       }
275)       if (jQuery.type(data.deletedContributions) == 'object') {
276)         for (uuid in data.deletedContributions) {
277)           this.deletedContributions[uuid]=data.deletedContributions[uuid];
Benjamin Renard Clean code

Benjamin Renard authored 10 years ago

278)         }
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

279)       }
280)     }
281)     catch (e) {
282)       alert('Une erreur est survenue en chargeant le groupe '+this.name+' depuis le cache');
283)     }
284)   }
285) }
286) 
Benjamin Renard Add edit contributor feature

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

288)   this.name=name;
289)   this.email=email;
Benjamin Renard Add edit contributor feature

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

293)       'name': encodeURIComponent(this.name),
294)       'email': encodeURIComponent(this.email)
Benjamin Renard Initial commit

Benjamin Renard authored 10 years ago

295)     };
296)   }
297) }
298) 
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

300)   this.contributor=contributor;
301)   this.cost=cost;
302)   this.title=title;
303)   this.date=date;
Benjamin Renard Use UUID as Contribution id...

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 10 years ago

314)     };
315)   }
316) 
317)   this.getTitle=function() {
318)     if (jQuery.type(this.title)=='string') {
319)       return this.title;
320)     }
321)     return '';
322)   }
323) }