+

Last commit for inc/myco.js: 5b1ed81828c47895644575c7a005e44be423f8c9

Add Export/Import feature

Benjamin Renard [2016-09-12 22:17:02]
Add Export/Import feature
  1. refresh_group_list=function() {
  2. grouplist=$('#group-choice')[0];
  3. $('a.group-choice').each(function(idx,a) {
  4. $(a).parent().remove();
  5. });
  6.  
  7. if (groups.count()==0) {
  8. $(grouplist).prepend('<li><a class="group-choice">Aucune groupe</a></li>');
  9. }
  10. else {
  11. groups.each(function(idx,group) {
  12. $(grouplist).prepend('<li><a class="group-choice" data-uuid="'+group.uuid+'">'+group.name+'</a></li>');
  13. });
  14.  
  15. $('a.group-choice').each(function(idx,a) {
  16. $(a).bind('click',on_group_choice_click);
  17. });
  18. }
  19. }
  20.  
  21.  
  22. /****************
  23.  * Add group
  24.  ****************/
  25.  
  26. on_click_add_group_btn=function(e) {
  27. navbar_collapse_hide();
  28. $('#add_group_modal').modal('show');
  29. }
  30.  
  31. on_show_add_group_modal=function(e) {
  32. $('#add_group_modal #add_group_name').focus();
  33. }
  34.  
  35. on_valid_add_group_modal=function (e) {
  36. e.preventDefault();
  37. var name=$('#add_group_name')[0].value;
  38. if (name=='') {
  39. alert('Vous devez saisir un nom !');
  40. return;
  41. }
  42. if (groups[name]!==undefined) {
  43. alert('Ce groupe exite déjà !');
  44. return;
  45. }
  46. var uuid=groups.newGroup(name);
  47. refresh_group_list();
  48. $('#add_group_modal').modal('hide');
  49. groups.save();
  50. view_group(groups[uuid]);
  51. }
  52.  
  53. on_close_add_group_modal=function () {
  54. $('#add_group_modal form')[0].reset();
  55. }
  56.  
  57. /****************
  58.  * Nav bars
  59.  ****************/
  60. on_group_choice_click=function(e) {
  61. view_group(groups[$(e.target).data('uuid')]);
  62. navbar_collapse_hide();
  63. }
  64.  
  65. navbar_collapse_hide=function() {
  66. if ($('#navbar-groupe-collapse').hasClass('in')) {
  67. $('#navbar-groupe-collapse').collapse('hide');
  68. }
  69. }
  70.  
  71. bottom_navbar_collapse_hide=function() {
  72. if ($('#bottom-navbar-collapse-1').hasClass('in')) {
  73. $('#bottom-navbar-collapse-1').collapse('hide');
  74. }
  75. }
  76.  
  77. /****************
  78.  * Parts
  79.  ****************/
  80.  
  81. view_part=function(part) {
  82. $('.part').each(function(idx,div) {
  83. $(div).css('display','none');
  84. });
  85. $(part).css('display','block');
  86. }
  87.  
  88. view_home=function() {
  89. navbar_collapse_hide();
  90. if (user) {
  91. $('#view-home #title').html('Bonjour '+user.name);
  92. }
  93. else {
  94. $('#view-home #title').html('Bonjour');
  95. }
  96. if (groups.count()>0) {
  97. tbody=$('#view-home #mybalances tbody');
  98. tbody.html('');
  99. balances=groups.balances();
  100. var sum=0;
  101. for (g in balances) {
  102. if (jQuery.type(balances[g]['balance'][user.email])!='undefined') {
  103. if (balances[g]['balance'][user.email]['diff']<0) {
  104. sum+=balances[g]['balance'][user.email]['diff'];
  105. diff='<td class="negative">'+balances[g]['balance'][user.email]['diff'].toFixed(2)+'</td>';
  106. }
  107. else {
  108. value=balances[g]['balance'][user.email]['total']-balances[g]['min'];
  109. sum+=value;
  110. diff='<td class="positive">+'+value.toFixed(2)+' €</td>';
  111. }
  112. tbody.html(tbody.html()+'<tr><td><a class="group-link" data-uuid="'+g+'">'+balances[g].name+'</a></td>'+diff+'</tr>');
  113. }
  114. }
  115. $('#view-home #mybalances a.group-link').bind('click',function(e) {
  116. g=$(e.target).data('uuid');
  117. if (jQuery.type(groups[g])!='undefined') {
  118. view_group(groups[g]);
  119. }
  120. });
  121. $('a.home_grp_cat_btn').each(function(idx,a) {
  122. $(a).bind('click',on_home_grp_cat_btn_click);
  123. });
  124. if (sum<0) {
  125. $('#view-home #mybalances #total-value').html('<span class="negative">'+sum.toFixed(2)+' €</span>');
  126. }
  127. else {
  128. $('#view-home #mybalances #total-value').html('<span class="positive">'+(sum==0?'':'+')+sum.toFixed(2)+' €</span>');
  129. }
  130. $('#view-home #mybalances').css('display','table');
  131. }
  132. else {
  133. $('#view-home #mybalances').css('display','none');
  134. }
  135. view_part('#view-home');
  136. }
  137.  
  138. /****************
  139.  * View group
  140.  ****************/
  141. view_group=function(group,contributor_email) {
  142. $('#view-group').data('uuid',group.uuid);
  143. $('#view-group .group-title:first').html(group.name);
  144. set_contributors(group,contributor_email);
  145. on_contributor_change();
  146. view_part('#view-group');
  147. }
  148.  
  149. set_contributors=function(group,contributor_email) {
  150. $('#view-group #contributor option').remove();
  151. if(group.contributors.length==0) {
  152. $('#view-group #contributor').append('<option>Aucun participant</option>');
  153. }
  154. else {
  155. for (email in group.contributors) {
  156. if (email == contributor_email) {
  157. sel=' selected';
  158. }
  159. else {
  160. sel='';
  161. }
  162. $('#view-group #contributor').append('<option value="'+email+'"'+sel+'>'+group.contributors[email].name+'</option>');
  163. }
  164. }
  165. }
  166.  
  167. on_contributor_change=function(e) {
  168. if ($('#group-navbar-collapse-1').hasClass('in')) {
  169. $('#group-navbar-collapse-1').collapse('hide');
  170. }
  171. $('#view-group #group-username').html($('#view-group #contributor option:selected').text());
  172. show_contributions(groups[$('#view-group').data('uuid')],$('#view-group #contributor')[0].value);
  173. }
  174.  
  175. show_contributions=function(group,contributor_email,contributions) {
  176. var tbody=$($('#view-group #contributions tbody')[0]);
  177. tbody.html('');
  178. total=$($('#view-group #total-value')[0]);
  179. if (!contributions) {
  180. contributions=group.contributionsByContributorEmail(contributor_email);
  181. contributions.reverse();
  182. }
  183. if (contributions.length==0) {
  184. tbody.append('<tr><td colspan=3>Aucune contributions</td></tr>');
  185. total.html('0,00 €');
  186. }
  187. else {
  188. sum=0;
  189. for (idx in contributions) {
  190. var cat='';
  191. if (contributions[idx].category && jQuery.type(group.categories[contributions[idx].category])) {
  192. cat='<br/><span class="category"><span class="cat-color" style="background-color: '+group.categories[contributions[idx].category]['color']+'"></span> '+group.categories[contributions[idx].category]['name']+"</span>";
  193. }
  194. col_actions='<td><div class="btn-group" data-uuid="'+contributions[idx].uuid+'"><button type="button" class="btn btn-default btn-sm contribution_edit_btn"><span class="glyphicon glyphicon-edit"></span></button><button type="button" class="btn btn-default btn-sm contribution_delete_btn"><span class="glyphicon glyphicon-trash"></span></button></div></td>';
  195. tbody.append('<tr><td>'+contributions[idx].getTitle()+cat+'</td><td>'+contributions[idx].cost.toFixed(2)+' €<br/><span class="date">'+moment(contributions[idx].date).format('DD/MM/YYYY')+'</span></td>'+col_actions+'</tr>');
  196. sum+=contributions[idx].cost;
  197. }
  198. total.html(sum.toFixed(2)+' €');
  199. }
  200.  
  201. $('.contribution_delete_btn').bind('click',on_contribution_delete_btn_click);
  202. $('.contribution_edit_btn').bind('click',on_contribution_edit_btn_click);
  203. }
  204.  
  205. on_categories_group_btn_click=function(e) {
  206. group=groups[$('#view-group').data('uuid')];
  207. $('#view-group-categories').data('group-uuid',$('#view-group').data('uuid'));
  208. refresh_group_categories(group);
  209. bottom_navbar_collapse_hide();
  210. view_part('#view-group-categories');
  211. }
  212.  
  213. refresh_group_categories=function(group) {
  214. ul=$('#view-group-categories ul');
  215. html="";
  216. for(cid in group.getSortedCategories()) {
  217. html+="<li data-uuid='"+cid+"'><span class='cat-color' style='background-color: "+group.categories[cid]['color']+"'></span> "+group.categories[cid]['name']+"</li>";
  218. }
  219. ul.html(html);
  220. $('#view-group-categories ul li').bind('click',on_categories_group_cat_click);
  221.  
  222. $('#cats-chart').css('width',$(window).width()*0.9+'px');
  223.  
  224. $.plot($('#cats-chart'),group.getCategoriesStats(),{
  225. 'grid': {
  226. 'hoverable': true
  227. },
  228. 'legend': {
  229. 'show': false,
  230. },
  231. 'series': {
  232. 'pie': {
  233. 'show': true,
  234. 'innerRadius': 0.5,
  235. 'radius': .8,
  236. 'tilt': .5,
  237. 'label': {
  238. 'show': true,
  239. 'formatter': function (label, series) {
  240. return '<div class="cats-chart-label">' + label + '<br/><span>' +
  241. Math.round(series.percent) + '% - '+series.data[0][1].toFixed(2)+'€</span></div>';
  242. }
  243. }
  244. }
  245. }
  246. });
  247. }
  248.  
  249. on_categories_group_cat_click=function(e) {
  250. li=$(e.target);
  251. if (li.prop("tagName")!='LI') {
  252. return true;
  253. }
  254. cid=li.data('uuid');
  255. group=groups[$('#view-group-categories').data('group-uuid')];
  256. cat=group.categories[cid];
  257. li.html("<span class='cat-color' style='background-color: "+cat['color']+"'></span> "+
  258. "<input type='text' value=\""+cat.name+"\"/> "+
  259. "<button class='btn btn-default btn-xs cat_edit'><span class='glyphicon glyphicon-ok'></span></button>"+
  260. "<button class='btn btn-default btn-xs cat_delete'><span class='glyphicon glyphicon-trash'></span></button>");
  261. li.children('button.cat_edit').bind('click',{'li': li,'group': group,'cid': cid},on_categories_group_cat_edit_valid_btn_click);
  262. li.children('button.cat_delete').bind('click',{'li': li,'group': group,'cid': cid},on_categories_group_cat_delete_btn_click);
  263. }
  264.  
  265. on_categories_group_cat_edit_valid_btn_click=function(e) {
  266. name=e.data.li.children('input:first').val();
  267. cat=e.data.group.categories[e.data.cid];
  268. e.data.group.updateCategory(e.data.cid,new Category(name,cat.color));
  269. groups.save();
  270. refresh_group_categories(e.data.group);
  271. }
  272.  
  273. on_categories_group_cat_delete_btn_click=function(e) {
  274. e.data.group.deleteCategory(e.data.cid);
  275. groups.save();
  276. refresh_group_categories(e.data.group);
  277. }
  278.  
  279. on_categories_go_back_group_btn_click=function(e) {
  280. view_group(groups[$('#view-group-categories').data('group-uuid')]);
  281. }
  282.  
  283. on_categories_group_add_btn_click=function(e) {
  284. name=$('#add_category input')[0].value;
  285.  
  286. if (jQuery.type(name)!='string' || name=='') {
  287. return;
  288. }
  289. group_uuid=$('#view-group-categories').data('group-uuid');
  290. group=groups[group_uuid];
  291.  
  292. if (group.getCategoryByName(name,true)) {
  293. alert('Cette catégorie existe déjà');
  294. }
  295. else {
  296. group.addCategory(new Category(name));
  297. $('#add_category input')[0].value='';
  298. refresh_group_categories(group);
  299. }
  300. }
  301.  
  302. on_nav_search_form_submit=function(e) {
  303. e.preventDefault();
  304. }
  305.  
  306. on_nav_search_form_input=function(e) {
  307. var ipt=$(e.target);
  308. var pattern=ipt.val();
  309.  
  310. group=groups[$('#view-group').data('uuid')];
  311.  
  312. var found=group.searchContributions(pattern);
  313. show_contributions(group,null,found);
  314. }
  315.  
  316.  
  317. /*****************************
  318.  * Trash
  319.  *****************************/
  320.  
  321. on_trash_group_btn_click=function(e) {
  322. group=groups[$('#view-group').data('uuid')];
  323. contributor_email=$('#view-group #contributor')[0].value;
  324. bottom_navbar_collapse_hide();
  325. view_group_trash(group,contributor_email);
  326. }
  327.  
  328. on_go_back_group_btn_click=function(e) {
  329. group=groups[$('#view-group-trash').data('uuid')];
  330. contributor_email=$('#view-group-trash').data('contributor-email');
  331. view_group(group,contributor_email);
  332. }
  333.  
  334. view_group_trash=function(group,contributor_email) {
  335. $('#view-group-trash').data('uuid',group.uuid);
  336. $('#view-group-trash').data('contributor-email',contributor_email);
  337. var tbody=$('#view-group-trash #trash tbody');
  338. tbody.html('');
  339. contributions=group.deletedContributionsByContributorEmail(contributor_email);
  340. contributions.reverse();
  341. if (contributions.length==0) {
  342. tbody.append('<tr><td colspan=3>Aucune contributions</td></tr>');
  343. }
  344. else {
  345. for (idx in contributions) {
  346. tbody.append('<tr data-uuid="'+contributions[idx].uuid+'"><td>'+contributions[idx].getTitle()+'</td><td>'+contributions[idx].cost.toFixed(2)+' €<br/><span class="date">'+moment(contributions[idx].date).format('DD/MM/YYYY')+'</span></td><td><button type="button" class="btn btn-default contribution_restore_btn"><span class="glyphicon glyphicon-share"></span></button></td></tr>');
  347. }
  348. }
  349.  
  350. $('#view-group-trash .contribution_restore_btn').bind('click',on_contribution_restore_btn_click);
  351.  
  352. view_part('#view-group-trash');
  353. }
  354.  
  355. on_contribution_restore_btn_click=function(e) {
  356. var group=groups[$('#view-group-trash').data('uuid')];
  357. contribution_uuid=$($(e.target).parents('tr')[0]).data('uuid');
  358. contribution=group.importContribution(group.deletedContributions[contribution_uuid]);
  359. myconfirm('Etes-vous sûre de vouloir restaurer la participation '+contribution.getTitle()+' de '+contribution.contributor.name+' ?',on_confirm_contribution_restore,null,{'group':group,'contribution_uuid':contribution_uuid, 'contribution': contribution} );
  360. }
  361.  
  362. on_confirm_contribution_restore=function(data) {
  363. data.group.restoreContribution(data.contribution_uuid);
  364. groups.save();
  365. view_group(data.group,data.contribution.contributor.email);
  366. }
  367.  
  368. on_trash_group_contributors_btn_click=function(e) {
  369. group=groups[$('#view-group').data('uuid')];
  370. view_group_trash_contributors(group);
  371. }
  372.  
  373. on_go_back_group_trash_contributors_btn_click=function(e) {
  374. group=groups[$('#view-group-trash-contributors').data('uuid')];
  375. view_group(group);
  376. }
  377.  
  378. view_group_trash_contributors=function(group) {
  379. $('#view-group-trash-contributors').data('uuid',group.uuid);
  380. var tbody=$('#view-group-trash-contributors #trash tbody');
  381. tbody.html('');
  382. contributors=group.getDeletedContributors();
  383. if (contributors.length==0) {
  384. tbody.append('<tr><td colspan=3>Aucun participant</td></tr>');
  385. }
  386. else {
  387. for (idx in contributors) {
  388. tbody.append('<tr data-email="'+contributors[idx].email+'"><td>'+contributors[idx].name+'</td><td><button type="button" class="btn btn-default contributor_restore_btn"><span class="glyphicon glyphicon-share"></span></button></td></tr>');
  389. }
  390. }
  391.  
  392. $('#view-group-trash-contributors .contributor_restore_btn').bind('click',on_contributor_restore_btn_click);
  393.  
  394. view_part('#view-group-trash-contributors');
  395. }
  396.  
  397. on_contributor_restore_btn_click=function(e) {
  398. var group=groups[$('#view-group-trash-contributors').data('uuid')];
  399. contributor_email=$($(e.target).parents('tr')[0]).data('email');
  400. contributor=group.importContributor(group.deletedContributors[contributor_email]);
  401. myconfirm('Etes-vous sûre de vouloir restaurer '+contributor.name+' ?',on_confirm_contributor_restore,null,{'group':group,'contributor_email':contributor_email, 'contributor': contributor} );
  402. }
  403.  
  404. on_confirm_contributor_restore=function(data) {
  405. data.group.restoreContributor(data.contributor_email);
  406. groups.save();
  407. view_group(data.group,data.contributor_email);
  408. }
  409.  
  410. /******************************
  411.  * Add/Edit/remove contributor
  412.  ******************************/
  413. on_click_add_contributor_btn=function() {
  414. $('#add_contributor_modal').data('group-uuid',$('#view-group').data('uuid'))
  415. $($('#add_contributor_modal h4')[0]).html('Ajouter un participant');
  416. $($('#add_contributor_modal #add_contributor_submit')[0]).html('Ajouter');
  417. $('#view-group #edit_id')[0].value='-1';
  418. $('#add_contributor_modal #add_contributor_email').attr('disabled',false);
  419. $('#add_contributor_modal').modal('show');
  420. }
  421.  
  422. on_show_add_contributor_modal=function(e) {
  423. $('#add_contributor_modal #add_contributor_name').focus();
  424. }
  425.  
  426. validateEmail=function(email) {
  427. var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  428. return re.test(email);
  429. }
  430.  
  431. on_valid_add_contributor_modal=function(e) {
  432. e.preventDefault();
  433. var group=groups[$('#add_contributor_modal').data('group-uuid')];
  434. var name=$('#add_contributor_name')[0].value;
  435. edit_id=$('#view-group #edit_id')[0].value;
  436. if (name=='') {
  437. alert('Vous devez saisir un nom !');
  438. return;
  439. }
  440. var email=$('#add_contributor_email')[0].value;
  441. if (email=='') {
  442. alert('Vous devez saisir un email !');
  443. return;
  444. }
  445. duplicate=group.contributorByEmail(email)
  446. if (duplicate!=undefined && edit=='-1') {
  447. alert('Un contributor utilise déjà cet email !');
  448. return;
  449. }
  450. if (!validateEmail(email)) {
  451. alert('Email invalide !');
  452. return;
  453. }
  454. if(edit_id=='-1') {
  455. group.addContributor(new Contributor(name,email));
  456. }
  457. else {
  458. group.replaceContributor(email,new Contributor(name,email));
  459. }
  460. set_contributors(group);
  461. $('#add_contributor_modal').modal('hide');
  462. groups.save();
  463. view_group(group,email);
  464. }
  465.  
  466. on_close_add_contributor_modal=function () {
  467. $('#add_contributor_name')[0].value='';
  468. $('#add_contributor_email')[0].value='';
  469. }
  470.  
  471. on_edit_contributor_btn_click=function(e) {
  472. var group=groups[$('#view-group').data('uuid')];
  473. $('#add_contributor_modal').data('group-uuid',group.uuid);
  474. contributor_email=$('#view-group #contributor')[0].value;
  475. contributor=group.contributorByEmail(contributor_email);
  476. $('#view-group #edit_id')[0].value=contributor_email;
  477. $('#add_contributor_modal #add_contributor_name')[0].value=contributor.name;
  478. $('#add_contributor_modal #add_contributor_email')[0].value=contributor.email;
  479. $('#add_contributor_modal #add_contributor_email').attr('disabled',true);
  480. $($('#add_contributor_modal h4')[0]).html('Editer un participant');
  481. $($('#add_contributor_modal #add_contributor_submit')[0]).html('Valider');
  482. $('#add_contributor_modal').modal('show');
  483. }
  484.  
  485. on_click_del_contributor_btn=function() {
  486. var group=groups[$('#view-group').data('uuid')];
  487. contributor=group.contributorByEmail($('#view-group #contributor')[0].value);
  488. question='Voulez-vous vraiment supprimer '+contributor.name+' ?';
  489. myconfirm(question,on_confirm_del_contributor,null,contributor);
  490. }
  491.  
  492. on_confirm_del_contributor=function(contributor) {
  493. var group=groups[$('#view-group').data('uuid')];
  494. group.removeContributor(contributor);
  495. view_group(group);
  496. groups.save();
  497. }
  498.  
  499. /********************************
  500.  * Add/edit/delete contribution
  501.  ********************************/
  502. on_show_add_contribution_modal=function(e) {
  503. $('#add_contribution_modal #add_contribution_contributor_email').html($('#view-group #contributor').html());
  504. $('#add_contribution_modal #add_contribution_contributor_email')[0].value=$('#view-group #contributor')[0].value;
  505. gid=$('#add_contribution_modal').data('group-uuid');
  506. group=groups[gid];
  507. current_cat=$('#add_contribution_modal #add_contribution_category')[0].value;
  508. cats="<option value=''>Non définie</option>";
  509. for (u in group.getSortedCategories()) {
  510. cats+="<option value='"+u+"'>"+group.categories[u]['name']+"</option>";
  511. }
  512. $('#add_contribution_modal #add_contribution_category').html(cats);
  513. $('#add_contribution_modal #add_contribution_category')[0].value=current_cat;
  514. $('#add_contribution_modal #add_contribution_title').typeahead('destroy');
  515. $('#add_contribution_modal #add_contribution_title').typeahead({
  516. hint: true,
  517. highlight: true,
  518. minLength: 1
  519. },
  520. {
  521. name: 'titles',
  522. displayKey: 'value',
  523. source: group.findContributionByTitleMatches()
  524. });
  525.  
  526. $('#add_contribution_modal #add_contribution_title').focus();
  527. }
  528.  
  529. on_select_contribution_suggestion=function(event,choice,name) {
  530. if (jQuery.type(choice['category'])=='string') {
  531. $('#add_contribution_modal #add_contribution_category').val(choice['category']);
  532. }
  533. }
  534.  
  535. on_click_add_contribution_btn=function() {
  536. $('#add_contribution_modal').data('group-uuid',$('#view-group').data('uuid'));
  537. $('#add_contribution_modal #edit_uuid')[0].value='-1';
  538. $('#add_contribution_date')[0].value=moment().format('DD/MM/YYYY');
  539. $($('#add_contribution_modal h4')[0]).html('Ajouter une contribution');
  540. $($('#add_contribution_modal #add_contribution_submit')[0]).html('Ajouter');
  541. $('#add_contribution_modal').modal('show');
  542. }
  543.  
  544. on_valid_add_contribution_modal=function(e) {
  545. e.preventDefault();
  546. var group=groups[$('#add_contribution_modal').data('group-uuid')];
  547. var contributor_email=$('#add_contribution_contributor_email')[0].value;
  548. var contributor=group.contributorByEmail(contributor_email);
  549. if (contributor==undefined) {
  550. alert('Participant inconu !');
  551. return;
  552. }
  553. var title=$('#add_contribution_title')[0].value;
  554. if (title=='') {
  555. $('#add_contribution_title')[0].focus();
  556. alert('Vous devez saisir un titre !');
  557. return;
  558. }
  559. var cost=$('#add_contribution_cost')[0].value;
  560. if (cost=='') {
  561. $('#add_contribution_cost')[0].focus();
  562. alert('Vous devez saisir un coût !');
  563. return;
  564. }
  565. else if(!RegExp('^[0-9]*((,|\.)[0-9]{1,2})*$').test(cost)) {
  566. $('#add_contribution_cost')[0].focus();
  567. alert('Coût incorrect !');
  568. return;
  569. }
  570. cost=parseFloat(cost.replace(',','.'));
  571.  
  572. var date=$('#add_contribution_date')[0].value;
  573. date.replace(/ /g,'');
  574. if (date!='') {
  575. if(!RegExp('^[0-3][0-9]\/[0-1][0-9]\/[0-2][0-9][0-9][0-9]$').test(date)) {
  576. $('#add_contribution_date')[0].focus();
  577. alert('Date incorrect !');
  578. return;
  579. }
  580. date=moment(date,'DD-MM-YYYY').toDate();
  581. if (jQuery.type(date)!='date') {
  582. alert('Date non-reconnu !');
  583. return;
  584. }
  585. }
  586. else {
  587. date=new Date();
  588. }
  589.  
  590. var cat=$('#add_contribution_category')[0].value;
  591. if (cat != '' && !jQuery.type(group.categories[cat])) {
  592. alert('Category incorrect');
  593. return;
  594. }
  595.  
  596. if($('#add_contribution_modal #edit_uuid')[0].value=='-1') {
  597. group.addContribution(new Contribution(contributor,cost,title,date,cat));
  598. }
  599. else {
  600. contribution_uuid=$('#add_contribution_modal #edit_uuid')[0].value;
  601. group.updateContribution(contribution_uuid,new Contribution(contributor,cost,title,date,cat));
  602. }
  603. show_contributions(group,contributor_email);
  604. $('#add_contribution_modal').modal('hide');
  605. $('#add_contribution_modal form')[0].reset();
  606. groups.save();
  607. }
  608.  
  609. on_close_add_contribution_modal=function () {
  610. $('#add_contribution_modal form')[0].reset();
  611. }
  612.  
  613. on_contribution_delete_btn_click=function(e) {
  614. var group=groups[$('#view-group').data('uuid')];
  615. contribution_uuid=$($(e.target).parents('div')[0]).data('uuid');
  616. contribution=group.contributions[contribution_uuid];
  617. myconfirm('Etes-vous sûre de vouloir supprimer la participation '+contribution.getTitle()+' de '+contribution.contributor.name+' ?',on_confirm_contribution_delete,null,{'group':group,'contribution_uuid':contribution_uuid, 'contribution': contribution} );
  618. }
  619.  
  620. on_confirm_contribution_delete=function(data) {
  621. data.group.deleteContribution(data.contribution_uuid);
  622. groups.save();
  623. on_contributor_change();
  624. }
  625.  
  626. on_contribution_edit_btn_click=function(e) {
  627. var group=groups[$('#view-group').data('uuid')];
  628. $('#add_contribution_modal').data('group-uuid',$('#view-group').data('uuid'));
  629. $($('#add_contribution_modal h4')[0]).html('Editer une contribution');
  630. $($('#add_contribution_modal #add_contribution_submit')[0]).html('Valider');
  631. contribution_uuid=$($(e.target).parents('div')[0]).data('uuid');
  632. contribution=group.contributions[contribution_uuid];
  633. $('#add_contribution_modal #add_contribution_contributor_email')[0].value=contribution.contributor.email;
  634. $('#add_contribution_modal #add_contribution_title')[0].value=contribution.getTitle();
  635. $('#add_contribution_modal #add_contribution_cost')[0].value=contribution.cost;
  636. $('#add_contribution_modal #add_contribution_date')[0].value=moment(contribution.date).format('DD/MM/YYYY');
  637. if (contribution.category) {
  638. $('#add_contribution_modal #add_contribution_category')[0].value=contribution.category;
  639. }
  640. $('#add_contribution_modal #edit_uuid')[0].value=contribution_uuid;
  641. $('#add_contribution_modal').modal('show');
  642. }
  643.  
  644.  
  645.  
  646. /********************
  647.  * Clear local data
  648.  ********************/
  649. clear_local_data=function() {
  650. navbar_collapse_hide();
  651. myconfirm('Etes-vous sûre de vouloir supprimer les données locales ?',on_confirm_clear_local_data);
  652. }
  653.  
  654. on_confirm_clear_local_data=function(data) {
  655. delete localStorage.groups;
  656. location.reload();
  657. }
  658.  
  659. /*********************
  660.  * View balance
  661.  *********************/
  662.  
  663. on_display_balance_btn_click=function(e) {
  664. group=groups[$('#view-group').data('uuid')];
  665. display_balance(group);
  666. }
  667.  
  668. display_balance=function(group) {
  669. bal=group.balance();
  670. tbody=$($('#display_balance_modal tbody')[0]);
  671. tbody.html('');
  672. for (c in bal['balance']) {
  673. if(bal['balance'][c]['diff']<0) {
  674. diff='<td class="negative">'+bal['balance'][c]['diff'].toFixed(2)+' €</td>';
  675. }
  676. else {
  677. diff='<td><span class="glyphicon glyphicon-thumbs-up"></span></td>';
  678. }
  679. tbody.append('<tr><td>'+bal['balance'][c]['name']+'</td><td>'+bal['balance'][c]['total'].toFixed(2)+' €</td>'+diff+'</tr>');
  680. }
  681. $('#display_balance_modal #total-value').html(bal.sum.toFixed(2)+' €');
  682. $('#display_balance_modal').modal('show');
  683. }
  684.  
  685. /*********************
  686.  * Remove group
  687.  *********************/
  688.  
  689. on_remove_group_btn_click=function(e) {
  690. group=groups[$('#view-group').data('uuid')];
  691. bottom_navbar_collapse_hide();
  692. myconfirm('Etes-vous sûre de vouloir supprimer le group '+group.name+' ?',on_confirm_remove_group,null,group);
  693. }
  694.  
  695. on_confirm_remove_group=function(group) {
  696. if(groups.removeGroup(group.uuid)) {
  697. groups.save();
  698. location.reload();
  699. }
  700. else {
  701. alert('Erreur en supprimant le groupe '+group.name+' :(');
  702. }
  703. }
  704.  
  705. /********************
  706.  * Login
  707.  ********************/
  708. on_close_login_modal=function(e) {
  709. $('#login_modal form')[0].reset();
  710. }
  711.  
  712. sync_server=false;
  713. on_valid_login_modal=function(e) {
  714. e.preventDefault();
  715. email=$('#login_modal #login_email')[0].value;
  716. pass=$('#login_modal #login_pass')[0].value;
  717. server=$('#login_modal #login_server')[0].value;
  718. $('#login_modal').modal('hide');
  719. pleaseWaitShow();
  720. sync_server.login(server,email,pass,
  721. function(data) {
  722. localStorage.user=JSON.stringify({
  723. 'name': data.name,
  724. 'email': email,
  725. 'password': pass,
  726. 'server': server,
  727. 'lastsync': null
  728. });
  729. pleaseWaitHide();
  730. logged_menu();
  731. },
  732. function(data) {
  733. pleaseWaitHide();
  734. if (jQuery.type(data) != 'object' && data.loginerror) {
  735. alert(data.loginerror);
  736. }
  737. else {
  738. console.log(data);
  739. alert('Erreur durant la connexion au serveur');
  740. }
  741. }
  742. );
  743. }
  744.  
  745. on_click_go_to_subscribe_btn=function() {
  746. $('#login_modal').modal('hide');
  747. $('#subscribe_modal').modal('show');
  748. }
  749.  
  750. /********************
  751.  * Subscribe
  752.  ********************/
  753. on_close_subscribe_modal=function(e) {
  754. $('#subscribe_modal form')[0].reset();
  755. }
  756.  
  757. sync_server=false;
  758. on_valid_subscribe_modal=function(e) {
  759. e.preventDefault();
  760. email=$('#subscribe_modal #subscribe_email')[0].value;
  761. name=$('#subscribe_modal #subscribe_name')[0].value;
  762. pass=$('#subscribe_modal #subscribe_pass')[0].value;
  763. server=$('#subscribe_modal #subscribe_server')[0].value;
  764. $('#subscribe_modal').modal('hide');
  765. pleaseWaitShow();
  766. sync_server.subscribe(server,email,name,pass,
  767. function(data) {
  768. localStorage.user=JSON.stringify({
  769. 'name': data.name,
  770. 'email': data.email,
  771. 'password': data.password,
  772. 'server': server,
  773. });
  774. pleaseWaitHide();
  775. logged_menu();
  776. alert('Bienvenu '+data.name+' !');
  777. },
  778. function(data) {
  779. pleaseWaitHide();
  780. if (jQuery.type(data) == 'object' && jQuery.type(data.subscribeerror)!='undefined') {
  781. alert(data.subscribeerror);
  782. }
  783. else {
  784. console.log(data);
  785. alert('Erreur durant la connexion au serveur');
  786. }
  787. }
  788. );
  789. }
  790.  
  791. /********************
  792.  * Sync
  793.  ********************/
  794. on_click_sync_btn=function(e) {
  795. navbar_collapse_hide();
  796. if (user) {
  797. pleaseWaitShow();
  798. onsuccess=function(data) {
  799. console.log(data);
  800. if (data.groups) {
  801. groups.import(data.groups);
  802. groups.save();
  803. user.lastsync=new Date().getTime();
  804. localStorage.user=JSON.stringify(user);
  805. refresh_group_list();
  806. pleaseWaitHide();
  807. view_home();
  808. alert('Groupes synchronisés');
  809. }
  810. else {
  811. onerror(data);
  812. }
  813. }
  814. onerror=function(data) {
  815. pleaseWaitHide();
  816. console.log(data);
  817. if (jQuery.type(data['loginerror'])!='undefined') {
  818. alert(data['loginerror']);
  819. }
  820. else if (jQuery.type(data['syncerror'])!='undefined') {
  821. alert(data['syncerror']);
  822. }
  823. else {
  824. alert('Erreur durant la synchronisation :(');
  825. }
  826. }
  827. sync_server.sync(user.server, user.email, user.password, groups.export(),onsuccess,onerror);
  828. }
  829. else {
  830. alert("Vous devez vous connecter pour commencer");
  831. $('#login_modal').modal('show');
  832. }
  833. }
  834.  
  835. /*
  836.  * User menu
  837.  */
  838. user=false;
  839. logged_menu=function() {
  840. user=JSON.parse(localStorage.user);
  841. $('#user-name').html(user.name);
  842. $('#user-menu li').remove();
  843. menu=$('#user-menu');
  844. menu.html("<li><a id='myaccount_btn'>Mon compte</a></li>"+
  845. "<li><a id='sync_btn'>Synchroniser</a></li>" +
  846. "<li class='divider'></li>" +
  847. "<li><a id='logoff_btn'>Déconnexion</a></li>");
  848. $('#myaccount_btn').bind('click',on_click_myaccount_btn);
  849. $('#sync_btn').bind('click',on_click_sync_btn);
  850. $('#logoff_btn').bind('click',on_click_logoff_btn);
  851. }
  852.  
  853. logged_out_menu=function() {
  854. user=false;
  855. $('#user-name').html('Connexion');
  856. $('#user-menu li').remove();
  857. menu=$('#user-menu');
  858. menu.html("<li><a id='login_btn'>Connexion</a></li>"+
  859. "<li><a id='subscribe_btn'>Inscription</a></li>");
  860. $('#login_btn').bind('click',on_click_login_btn);
  861. $('#subscribe_btn').bind('click',on_click_subscribe_btn);
  862. }
  863.  
  864. on_click_myaccount_btn=function() {
  865. $('#myaccount_modal #myaccount_email').html(user.email);
  866. $('#myaccount_modal #myaccount_name')[0].value=user.name;
  867. $('#myaccount_modal #myaccount_server').html(user.server);
  868. if (user.lastsync) {
  869. $('#myaccount_modal #myaccount_lastsync').html(moment(user.lastsync).format('DD/MM/YYYY HH:mm:ss'));
  870. }
  871. else {
  872. $('#myaccount_modal #myaccount_lastsync').html('Jamais synchronis&eacute;');
  873. }
  874. $('#myaccount_modal').modal('show');
  875. }
  876.  
  877. on_click_login_btn=function() {
  878. $('#login_modal').modal('show');
  879. }
  880.  
  881. on_click_subscribe_btn=function() {
  882. $('#subscribe_modal').modal('show');
  883. }
  884.  
  885. on_click_logoff_btn=function() {
  886. delete localStorage.user;
  887. logged_out_menu();
  888. }
  889.  
  890. /*******************
  891.  * pleaseWaitDialog
  892.  *******************/
  893.  
  894. pleaseWaitShow=function() {
  895. $('#please_wait_modal').modal('show');
  896. }
  897.  
  898. pleaseWaitHide=function() {
  899. $('#please_wait_modal').modal('hide');
  900. }
  901.  
  902. /*********************
  903.  * Activate
  904.  *********************/
  905. $( document ).ready( function() {
  906. pleaseWaitShow();
  907. if(typeof(Storage)!=="undefined"){
  908. groups=new GroupList();
  909. groups.loadFromLocalStorage();
  910. refresh_group_list();
  911. if (jQuery.type(localStorage.user)!='undefined') {
  912. logged_menu();
  913. }
  914. else {
  915. logged_out_menu();
  916. }
  917. }
  918. else {
  919. alert('Local storage not supported !');
  920. pleaseWaitHide();
  921. return;
  922. }
  923. sync_server=new SyncServer();
  924.  
  925. $('#app-name').bind('click',view_home);
  926.  
  927. $('#add_group_btn').bind('click',on_click_add_group_btn);
  928. $('#add_group_submit').bind('click',on_valid_add_group_modal);
  929. $("#add_group_modal").on('shown.bs.modal',on_show_add_group_modal);
  930. $("#add_group_modal").on('hidden.bs.modal',on_close_add_group_modal);
  931. $("#add_group_modal form").bind('submit',on_valid_add_group_modal);
  932.  
  933. $('#clear_local_data').bind('click',clear_local_data);
  934.  
  935. $('#login_modal').on('hidden.bs.modal',on_close_login_modal);
  936. $('#login_modal #login_submit').bind('click',on_valid_login_modal);
  937. $('#login_modal #go_to_subscribe_btn').bind('click',on_click_go_to_subscribe_btn);
  938. $('#login_modal form').bind('submit',on_valid_login_modal);
  939.  
  940. $('#subscribe_modal').on('hidden.bs.modal',on_close_subscribe_modal);
  941. $('#subscribe_modal #subscribe_submit').bind('click',on_valid_subscribe_modal);
  942. $('#subscribe_modal form').bind('submit',on_valid_subscribe_modal);
  943.  
  944. $('#view-group #contributor').bind('change',on_contributor_change);
  945.  
  946. $('#add_contributor_btn').bind('click',on_click_add_contributor_btn);
  947. $('#add_contributor_submit').bind('click',on_valid_add_contributor_modal);
  948. $("#add_contributor_modal").on('shown.bs.modal',on_show_add_contributor_modal);
  949. $("#add_contributor_modal").on('hidden.bs.modal',on_close_add_contributor_modal);
  950. $("#add_contributor_modal form").bind('submit',on_valid_add_contributor_modal);
  951. $("#add_contribution_date").pickadate({'format': 'dd/mm/yyyy','firstDay': 1});
  952.  
  953. $('#edit_contributor_btn').bind('click',on_edit_contributor_btn_click);
  954. $('#trash_contributor_btn').bind('click',on_trash_group_contributors_btn_click);
  955. $('#del_contributor_btn').bind('click',on_click_del_contributor_btn);
  956.  
  957. $('#add_contribution_btn').bind('click',on_click_add_contribution_btn);
  958. $('#add_contribution_submit').bind('click',on_valid_add_contribution_modal);
  959. $("#add_contribution_modal").on('shown.bs.modal',on_show_add_contribution_modal);
  960. $("#add_contribution_modal").on('hidden.bs.modal',on_close_add_contribution_modal);
  961. $("#add_contribution_modal form").on('submit',on_valid_add_contribution_modal);
  962. $('#add_contribution_modal #add_contribution_title').on('typeahead:selected', on_select_contribution_suggestion);
  963.  
  964. $("#display_balance_btn").bind('click',on_display_balance_btn_click);
  965.  
  966. $("#view-group-trash #go-back-group").bind('click',on_go_back_group_btn_click);
  967. $("#view-group-trash-contributors #go-back-group").bind('click',on_go_back_group_trash_contributors_btn_click);
  968.  
  969. $('#categories_group_btn').bind('click',on_categories_group_btn_click);
  970. $('#trash_group_btn').bind('click',on_trash_group_btn_click);
  971. $('#remove_group_btn').bind('click',on_remove_group_btn_click);
  972. $('#nav-search-form').bind('submit',on_nav_search_form_submit);
  973. $('#nav-search-form input').bind('input',on_nav_search_form_input);
  974.  
  975. $('#view-group-categories span.input-group-addon').bind('click',on_categories_group_add_btn_click);
  976. $("#view-group-categories button.go-back-group").bind('click',on_categories_go_back_group_btn_click);
  977.  
  978. view_home();
  979. pleaseWaitHide();
  980. } );