2014-01-06 01:46:22 +01:00
refresh _group _list = function ( ) {
grouplist = $ ( '#group-choice' ) [ 0 ] ;
$ ( 'a.group-choice' ) . each ( function ( idx , a ) {
$ ( a ) . parent ( ) . remove ( ) ;
} ) ;
if ( groups . count ( ) == 0 ) {
$ ( grouplist ) . prepend ( '<li><a class="group-choice">Aucune groupe</a></li>' ) ;
}
else {
groups . each ( function ( idx , group ) {
2014-01-16 20:16:29 +01:00
$ ( grouplist ) . prepend ( '<li><a class="group-choice" data-uuid="' + group . uuid + '">' + group . name + '</a></li>' ) ;
2014-01-06 01:46:22 +01:00
} ) ;
$ ( 'a.group-choice' ) . each ( function ( idx , a ) {
$ ( a ) . bind ( 'click' , on _group _choice _click ) ;
} ) ;
}
}
/ * * * * * * * * * * * * * * * *
* Add group
* * * * * * * * * * * * * * * * /
2014-01-06 17:10:33 +01:00
on _click _add _group _btn = function ( e ) {
2014-01-06 17:22:09 +01:00
navbar _collapse _hide ( ) ;
2014-01-06 17:10:33 +01:00
$ ( '#add_group_modal' ) . modal ( 'show' ) ;
}
2014-01-06 01:46:22 +01:00
on _show _add _group _modal = function ( e ) {
$ ( '#add_group_modal #add_group_name' ) . focus ( ) ;
}
2014-01-14 00:33:19 +01:00
on _valid _add _group _modal = function ( e ) {
e . preventDefault ( ) ;
2014-01-06 01:46:22 +01:00
var name = $ ( '#add_group_name' ) [ 0 ] . value ;
if ( name == '' ) {
alert ( 'Vous devez saisir un nom !' ) ;
return ;
}
if ( groups [ name ] !== undefined ) {
alert ( 'Ce groupe exite déjà !' ) ;
return ;
}
2014-01-16 20:16:29 +01:00
var uuid = groups . newGroup ( name ) ;
2014-01-06 01:46:22 +01:00
refresh _group _list ( ) ;
$ ( '#add_group_modal' ) . modal ( 'hide' ) ;
groups . save ( ) ;
2014-01-16 20:16:29 +01:00
view _group ( groups [ uuid ] ) ;
2014-01-06 01:46:22 +01:00
}
on _close _add _group _modal = function ( ) {
2014-01-06 02:11:09 +01:00
$ ( '#add_group_modal form' ) [ 0 ] . reset ( ) ;
2014-01-06 01:46:22 +01:00
}
/ * * * * * * * * * * * * * * * *
* Nav bars
* * * * * * * * * * * * * * * * /
on _group _choice _click = function ( e ) {
2014-01-16 20:16:29 +01:00
view _group ( groups [ $ ( e . target ) . data ( 'uuid' ) ] ) ;
2014-01-06 17:22:09 +01:00
navbar _collapse _hide ( ) ;
}
navbar _collapse _hide = function ( ) {
if ( $ ( '#navbar-groupe-collapse' ) . hasClass ( 'in' ) ) {
$ ( '#navbar-groupe-collapse' ) . collapse ( 'hide' ) ;
}
2014-01-06 01:46:22 +01:00
}
2014-07-23 02:45:38 +02:00
bottom _navbar _collapse _hide = function ( ) {
if ( $ ( '#bottom-navbar-collapse-1' ) . hasClass ( 'in' ) ) {
$ ( '#bottom-navbar-collapse-1' ) . collapse ( 'hide' ) ;
}
}
2014-01-12 20:11:40 +01:00
/ * * * * * * * * * * * * * * * *
* Parts
* * * * * * * * * * * * * * * * /
view _part = function ( part ) {
$ ( '.part' ) . each ( function ( idx , div ) {
$ ( div ) . css ( 'display' , 'none' ) ;
} ) ;
$ ( part ) . css ( 'display' , 'block' ) ;
}
view _home = function ( ) {
2014-01-18 00:30:06 +01:00
navbar _collapse _hide ( ) ;
2014-01-12 20:11:40 +01:00
if ( user ) {
$ ( '#view-home #title' ) . html ( 'Bonjour ' + user . name ) ;
}
else {
$ ( '#view-home #title' ) . html ( 'Bonjour' ) ;
}
if ( groups . count ( ) > 0 ) {
tbody = $ ( '#view-home #mybalances tbody' ) ;
tbody . html ( '' ) ;
balances = groups . balances ( ) ;
var sum = 0 ;
for ( g in balances ) {
2014-01-19 14:48:41 +01:00
if ( jQuery . type ( balances [ g ] [ 'balance' ] [ user . email ] ) != 'undefined' ) {
if ( balances [ g ] [ 'balance' ] [ user . email ] [ 'diff' ] < 0 ) {
sum += balances [ g ] [ 'balance' ] [ user . email ] [ 'diff' ] ;
diff = '<td class="negative">' + balances [ g ] [ 'balance' ] [ user . email ] [ 'diff' ] . toFixed ( 2 ) + '</td>' ;
2014-01-12 20:11:40 +01:00
}
else {
2014-01-19 14:48:41 +01:00
value = balances [ g ] [ 'balance' ] [ user . email ] [ 'total' ] - balances [ g ] [ 'min' ] ;
2014-01-12 20:11:40 +01:00
sum += value ;
diff = '<td class="positive">+' + value . toFixed ( 2 ) + ' €</td>' ;
}
2014-07-23 00:16:12 +02:00
tbody . html ( tbody . html ( ) + '<tr><td><a class="group-link" data-uuid="' + g + '">' + balances [ g ] . name + '</a></td>' + diff + '</tr>' ) ;
2014-01-12 20:11:40 +01:00
}
}
2014-01-12 23:24:22 +01:00
$ ( '#view-home #mybalances a.group-link' ) . bind ( 'click' , function ( e ) {
2014-01-16 20:22:53 +01:00
g = $ ( e . target ) . data ( 'uuid' ) ;
2014-01-12 23:24:22 +01:00
if ( jQuery . type ( groups [ g ] ) != 'undefined' ) {
view _group ( groups [ g ] ) ;
}
} ) ;
2014-07-19 20:06:57 +02:00
$ ( 'a.home_grp_cat_btn' ) . each ( function ( idx , a ) {
$ ( a ) . bind ( 'click' , on _home _grp _cat _btn _click ) ;
} ) ;
2014-01-12 20:11:40 +01:00
if ( sum < 0 ) {
$ ( '#view-home #mybalances #total-value' ) . html ( '<span class="negative">' + sum . toFixed ( 2 ) + ' €</span>' ) ;
}
else {
$ ( '#view-home #mybalances #total-value' ) . html ( '<span class="positive">' + ( sum == 0 ? '' : '+' ) + sum . toFixed ( 2 ) + ' €</span>' ) ;
}
2014-01-12 23:50:39 +01:00
$ ( '#view-home #mybalances' ) . css ( 'display' , 'table' ) ;
2014-01-12 20:11:40 +01:00
}
else {
$ ( '#view-home #mybalances' ) . css ( 'display' , 'none' ) ;
}
view _part ( '#view-home' ) ;
}
2014-01-06 01:46:22 +01:00
/ * * * * * * * * * * * * * * * *
* View group
* * * * * * * * * * * * * * * * /
2014-01-30 00:30:05 +01:00
view _group = function ( group , contributor _email ) {
2014-01-16 20:16:29 +01:00
$ ( '#view-group' ) . data ( 'uuid' , group . uuid ) ;
2014-07-20 19:50:06 +02:00
$ ( '#view-group .group-title:first' ) . html ( group . name ) ;
2014-01-30 00:30:05 +01:00
set _contributors ( group , contributor _email ) ;
2014-01-06 01:46:22 +01:00
on _contributor _change ( ) ;
2014-01-12 20:11:40 +01:00
view _part ( '#view-group' ) ;
2014-01-06 01:46:22 +01:00
}
2014-01-30 00:30:05 +01:00
set _contributors = function ( group , contributor _email ) {
2014-01-06 01:46:22 +01:00
$ ( '#view-group #contributor option' ) . remove ( ) ;
if ( group . contributors . length == 0 ) {
$ ( '#view-group #contributor' ) . append ( '<option>Aucun participant</option>' ) ;
}
else {
2014-01-19 14:48:41 +01:00
for ( email in group . contributors ) {
2014-01-30 00:30:05 +01:00
if ( email == contributor _email ) {
sel = ' selected' ;
}
else {
sel = '' ;
}
$ ( '#view-group #contributor' ) . append ( '<option value="' + email + '"' + sel + '>' + group . contributors [ email ] . name + '</option>' ) ;
2014-01-19 14:48:41 +01:00
}
2014-01-06 01:46:22 +01:00
}
}
on _contributor _change = function ( e ) {
2014-07-20 19:50:06 +02:00
if ( $ ( '#group-navbar-collapse-1' ) . hasClass ( 'in' ) ) {
$ ( '#group-navbar-collapse-1' ) . collapse ( 'hide' ) ;
}
$ ( '#view-group #group-username' ) . html ( $ ( '#view-group #contributor option:selected' ) . text ( ) ) ;
show _contributions ( groups [ $ ( '#view-group' ) . data ( 'uuid' ) ] , $ ( '#view-group #contributor' ) [ 0 ] . value ) ;
2014-01-06 01:46:22 +01:00
}
2014-11-13 00:40:19 +01:00
_current _displayed _contributions = [ ] ;
_current _displayed _contributions _group = null ;
2014-10-13 00:20:28 +02:00
show _contributions = function ( group , contributor _email , contributions ) {
2014-01-06 01:46:22 +01:00
var tbody = $ ( $ ( '#view-group #contributions tbody' ) [ 0 ] ) ;
tbody . html ( '' ) ;
2014-10-13 00:20:28 +02:00
if ( ! contributions ) {
contributions = group . contributionsByContributorEmail ( contributor _email ) ;
contributions . reverse ( ) ;
}
2014-11-13 00:40:19 +01:00
_current _displayed _contributions = contributions ;
_current _displayed _contributions _group = group ;
show _more _contributions ( ) ;
$ ( '.contribution_delete_btn' ) . bind ( 'click' , on _contribution _delete _btn _click ) ;
$ ( '.contribution_edit_btn' ) . bind ( 'click' , on _contribution _edit _btn _click ) ;
}
show _more _contributions = function ( count ) {
if ( ! _current _displayed _contributions _group ) return ;
var tbody = $ ( $ ( '#view-group #contributions tbody' ) [ 0 ] ) ;
if ( _current _displayed _contributions . length == 0 ) {
tbody . html ( '' ) ;
2014-01-06 01:46:22 +01:00
tbody . append ( '<tr><td colspan=3>Aucune contributions</td></tr>' ) ;
}
else {
2014-11-13 00:40:19 +01:00
var previous = 0 ;
if ( tbody . children ( 'tr' ) . length > 0 ) {
previous = tbody . children ( 'tr' ) . length - 1 ;
}
if ( ! count ) count = 20 ;
for ( var i = 0 ; i < count ; i ++ ) {
var idx = previous + i ;
if ( ! _current _displayed _contributions [ idx ] ) break ;
2014-07-19 20:06:57 +02:00
var cat = '' ;
2014-11-13 00:40:19 +01:00
if ( _current _displayed _contributions [ idx ] . category && jQuery . type ( _current _displayed _contributions _group . categories [ _current _displayed _contributions [ idx ] . category ] ) ) {
cat = '<br/><span class="category"><span class="cat-color" style="background-color: ' + _current _displayed _contributions _group . categories [ _current _displayed _contributions [ idx ] . category ] [ 'color' ] + '"></span> ' + _current _displayed _contributions _group . categories [ _current _displayed _contributions [ idx ] . category ] [ 'name' ] + "</span>" ;
2014-07-19 20:06:57 +02:00
}
2014-11-13 00:40:19 +01:00
col _actions = '<td><div class="btn-group" data-uuid="' + _current _displayed _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>' ;
tbody . append ( '<tr><td>' + _current _displayed _contributions [ idx ] . getTitle ( ) + cat + '</td><td>' + _current _displayed _contributions [ idx ] . cost . toFixed ( 2 ) + ' €<br/><span class="date">' + moment ( _current _displayed _contributions [ idx ] . date ) . format ( 'DD/MM/YYYY' ) + '</span></td>' + col _actions + '</tr>' ) ;
2014-01-06 01:46:22 +01:00
}
2014-11-13 00:40:19 +01:00
}
}
_view _group _scroll _lock = false ;
on _view _group _scroll = function ( event ) {
if ( _view _group _scroll _lock ) return ;
_view _group _scroll _lock = true ;
if ( $ ( '#view-group' ) . css ( 'display' ) == 'block' && is _on _bottom ( ) ) {
show _more _contributions ( 20 ) ;
2014-01-06 01:46:22 +01:00
}
2014-11-13 00:40:19 +01:00
_view _group _scroll _lock = false ;
2014-01-06 01:46:22 +01:00
}
2014-07-23 00:16:12 +02:00
on _categories _group _btn _click = function ( e ) {
group = groups [ $ ( '#view-group' ) . data ( 'uuid' ) ] ;
$ ( '#view-group-categories' ) . data ( 'group-uuid' , $ ( '#view-group' ) . data ( 'uuid' ) ) ;
refresh _group _categories ( group ) ;
2014-07-23 02:45:38 +02:00
bottom _navbar _collapse _hide ( ) ;
2014-07-23 00:16:12 +02:00
view _part ( '#view-group-categories' ) ;
}
refresh _group _categories = function ( group ) {
ul = $ ( '#view-group-categories ul' ) ;
html = "" ;
for ( cid in group . getSortedCategories ( ) ) {
html += "<li data-uuid='" + cid + "'><span class='cat-color' style='background-color: " + group . categories [ cid ] [ 'color' ] + "'></span> " + group . categories [ cid ] [ 'name' ] + "</li>" ;
}
ul . html ( html ) ;
$ ( '#view-group-categories ul li' ) . bind ( 'click' , on _categories _group _cat _click ) ;
2014-07-23 01:59:56 +02:00
$ ( '#cats-chart' ) . css ( 'width' , $ ( window ) . width ( ) * 0.9 + 'px' ) ;
$ . plot ( $ ( '#cats-chart' ) , group . getCategoriesStats ( ) , {
'grid' : {
'hoverable' : true
} ,
'legend' : {
2014-07-23 02:04:28 +02:00
'show' : false ,
2014-07-23 01:59:56 +02:00
} ,
'series' : {
'pie' : {
'show' : true ,
'innerRadius' : 0.5 ,
2014-07-23 02:38:17 +02:00
'radius' : . 8 ,
'tilt' : . 5 ,
2014-07-23 01:59:56 +02:00
'label' : {
'show' : true ,
'formatter' : function ( label , series ) {
2014-07-23 02:38:17 +02:00
return '<div class="cats-chart-label">' + label + '<br/><span>' +
Math . round ( series . percent ) + '% - ' + series . data [ 0 ] [ 1 ] . toFixed ( 2 ) + '€</span></div>' ;
2014-07-23 01:59:56 +02:00
}
}
}
}
} ) ;
2014-07-23 00:16:12 +02:00
}
on _categories _group _cat _click = function ( e ) {
li = $ ( e . target ) ;
if ( li . prop ( "tagName" ) != 'LI' ) {
return true ;
}
cid = li . data ( 'uuid' ) ;
group = groups [ $ ( '#view-group-categories' ) . data ( 'group-uuid' ) ] ;
cat = group . categories [ cid ] ;
li . html ( "<span class='cat-color' style='background-color: " + cat [ 'color' ] + "'></span> " +
"<input type='text' value=\"" + cat . name + "\"/> " +
"<button class='btn btn-default btn-xs cat_edit'><span class='glyphicon glyphicon-ok'></span></button>" +
"<button class='btn btn-default btn-xs cat_delete'><span class='glyphicon glyphicon-trash'></span></button>" ) ;
li . children ( 'button.cat_edit' ) . bind ( 'click' , { 'li' : li , 'group' : group , 'cid' : cid } , on _categories _group _cat _edit _valid _btn _click ) ;
li . children ( 'button.cat_delete' ) . bind ( 'click' , { 'li' : li , 'group' : group , 'cid' : cid } , on _categories _group _cat _delete _btn _click ) ;
}
on _categories _group _cat _edit _valid _btn _click = function ( e ) {
name = e . data . li . children ( 'input:first' ) . val ( ) ;
cat = e . data . group . categories [ e . data . cid ] ;
e . data . group . updateCategory ( e . data . cid , new Category ( name , cat . color ) ) ;
groups . save ( ) ;
refresh _group _categories ( e . data . group ) ;
}
on _categories _group _cat _delete _btn _click = function ( e ) {
e . data . group . deleteCategory ( e . data . cid ) ;
groups . save ( ) ;
refresh _group _categories ( e . data . group ) ;
}
on _categories _go _back _group _btn _click = function ( e ) {
view _group ( groups [ $ ( '#view-group-categories' ) . data ( 'group-uuid' ) ] ) ;
}
on _categories _group _add _btn _click = function ( e ) {
name = $ ( '#add_category input' ) [ 0 ] . value ;
if ( jQuery . type ( name ) != 'string' || name == '' ) {
return ;
}
group _uuid = $ ( '#view-group-categories' ) . data ( 'group-uuid' ) ;
group = groups [ group _uuid ] ;
if ( group . getCategoryByName ( name , true ) ) {
alert ( 'Cette catégorie existe déjà' ) ;
}
else {
group . addCategory ( new Category ( name ) ) ;
$ ( '#add_category input' ) [ 0 ] . value = '' ;
refresh _group _categories ( group ) ;
}
}
2014-10-13 00:24:44 +02:00
on _export _group _btn _click = function ( e ) {
if ( user ) {
group = $ ( '#view-group' ) . data ( 'uuid' ) ;
document . location = user . server + '/export2csv?email=' + user . email + '&password=' + user . password + '&group=' + group ;
}
else {
alert ( 'Vous devez être connecté à un serveur pour cela !' ) ;
}
}
2014-10-13 00:20:28 +02:00
on _nav _search _form _submit = function ( e ) {
e . preventDefault ( ) ;
}
on _nav _search _form _input = function ( e ) {
var ipt = $ ( e . target ) ;
var pattern = ipt . val ( ) ;
group = groups [ $ ( '#view-group' ) . data ( 'uuid' ) ] ;
var found = group . searchContributions ( pattern ) ;
show _contributions ( group , null , found ) ;
}
2014-07-23 00:16:12 +02:00
2014-01-30 00:30:05 +01:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Trash
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
on _trash _group _btn _click = function ( e ) {
group = groups [ $ ( '#view-group' ) . data ( 'uuid' ) ] ;
contributor _email = $ ( '#view-group #contributor' ) [ 0 ] . value ;
2014-07-23 02:45:38 +02:00
bottom _navbar _collapse _hide ( ) ;
2014-01-30 00:30:05 +01:00
view _group _trash ( group , contributor _email ) ;
}
on _go _back _group _btn _click = function ( e ) {
group = groups [ $ ( '#view-group-trash' ) . data ( 'uuid' ) ] ;
contributor _email = $ ( '#view-group-trash' ) . data ( 'contributor-email' ) ;
view _group ( group , contributor _email ) ;
}
view _group _trash = function ( group , contributor _email ) {
$ ( '#view-group-trash' ) . data ( 'uuid' , group . uuid ) ;
$ ( '#view-group-trash' ) . data ( 'contributor-email' , contributor _email ) ;
var tbody = $ ( '#view-group-trash #trash tbody' ) ;
tbody . html ( '' ) ;
contributions = group . deletedContributionsByContributorEmail ( contributor _email ) ;
contributions . reverse ( ) ;
if ( contributions . length == 0 ) {
tbody . append ( '<tr><td colspan=3>Aucune contributions</td></tr>' ) ;
}
else {
for ( idx in contributions ) {
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>' ) ;
}
}
$ ( '#view-group-trash .contribution_restore_btn' ) . bind ( 'click' , on _contribution _restore _btn _click ) ;
view _part ( '#view-group-trash' ) ;
}
on _contribution _restore _btn _click = function ( e ) {
var group = groups [ $ ( '#view-group-trash' ) . data ( 'uuid' ) ] ;
contribution _uuid = $ ( $ ( e . target ) . parents ( 'tr' ) [ 0 ] ) . data ( 'uuid' ) ;
contribution = group . importContribution ( group . deletedContributions [ contribution _uuid ] ) ;
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 } ) ;
}
on _confirm _contribution _restore = function ( data ) {
data . group . restoreContribution ( data . contribution _uuid ) ;
groups . save ( ) ;
view _group ( data . group , data . contribution . contributor . email ) ;
}
2014-01-31 00:41:22 +01:00
on _trash _group _contributors _btn _click = function ( e ) {
group = groups [ $ ( '#view-group' ) . data ( 'uuid' ) ] ;
view _group _trash _contributors ( group ) ;
}
on _go _back _group _trash _contributors _btn _click = function ( e ) {
group = groups [ $ ( '#view-group-trash-contributors' ) . data ( 'uuid' ) ] ;
view _group ( group ) ;
}
view _group _trash _contributors = function ( group ) {
$ ( '#view-group-trash-contributors' ) . data ( 'uuid' , group . uuid ) ;
var tbody = $ ( '#view-group-trash-contributors #trash tbody' ) ;
tbody . html ( '' ) ;
contributors = group . getDeletedContributors ( ) ;
if ( contributors . length == 0 ) {
tbody . append ( '<tr><td colspan=3>Aucun participant</td></tr>' ) ;
}
else {
for ( idx in contributors ) {
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>' ) ;
}
}
$ ( '#view-group-trash-contributors .contributor_restore_btn' ) . bind ( 'click' , on _contributor _restore _btn _click ) ;
view _part ( '#view-group-trash-contributors' ) ;
}
on _contributor _restore _btn _click = function ( e ) {
var group = groups [ $ ( '#view-group-trash-contributors' ) . data ( 'uuid' ) ] ;
contributor _email = $ ( $ ( e . target ) . parents ( 'tr' ) [ 0 ] ) . data ( 'email' ) ;
contributor = group . importContributor ( group . deletedContributors [ contributor _email ] ) ;
myconfirm ( 'Etes-vous sûre de vouloir restaurer ' + contributor . name + ' ?' , on _confirm _contributor _restore , null , { 'group' : group , 'contributor_email' : contributor _email , 'contributor' : contributor } ) ;
}
on _confirm _contributor _restore = function ( data ) {
data . group . restoreContributor ( data . contributor _email ) ;
groups . save ( ) ;
view _group ( data . group , data . contributor _email ) ;
}
2014-01-06 03:04:57 +01:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Add / Edit / remove contributor
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
2014-01-06 01:46:22 +01:00
on _click _add _contributor _btn = function ( ) {
2014-01-18 00:21:05 +01:00
$ ( '#add_contributor_modal' ) . data ( 'group-uuid' , $ ( '#view-group' ) . data ( 'uuid' ) )
2014-01-06 03:04:57 +01:00
$ ( $ ( '#add_contributor_modal h4' ) [ 0 ] ) . html ( 'Ajouter un participant' ) ;
$ ( $ ( '#add_contributor_modal #add_contributor_submit' ) [ 0 ] ) . html ( 'Ajouter' ) ;
$ ( '#view-group #edit_id' ) [ 0 ] . value = '-1' ;
2014-01-19 14:48:41 +01:00
$ ( '#add_contributor_modal #add_contributor_email' ) . attr ( 'disabled' , false ) ;
2014-01-06 01:46:22 +01:00
$ ( '#add_contributor_modal' ) . modal ( 'show' ) ;
}
on _show _add _contributor _modal = function ( e ) {
$ ( '#add_contributor_modal #add_contributor_name' ) . focus ( ) ;
}
2014-01-30 23:44:25 +01:00
validateEmail = function ( email ) {
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,}))$/ ;
return re . test ( email ) ;
}
2014-01-14 00:33:19 +01:00
on _valid _add _contributor _modal = function ( e ) {
e . preventDefault ( ) ;
2014-01-16 20:16:29 +01:00
var group = groups [ $ ( '#add_contributor_modal' ) . data ( 'group-uuid' ) ] ;
2014-01-06 01:46:22 +01:00
var name = $ ( '#add_contributor_name' ) [ 0 ] . value ;
2014-01-06 03:04:57 +01:00
edit _id = $ ( '#view-group #edit_id' ) [ 0 ] . value ;
2014-01-06 01:46:22 +01:00
if ( name == '' ) {
alert ( 'Vous devez saisir un nom !' ) ;
return ;
}
var email = $ ( '#add_contributor_email' ) [ 0 ] . value ;
if ( email == '' ) {
alert ( 'Vous devez saisir un email !' ) ;
return ;
}
2014-01-06 03:04:57 +01:00
duplicate = group . contributorByEmail ( email )
2014-01-19 14:48:41 +01:00
if ( duplicate != undefined && edit == '-1' ) {
2014-01-06 01:46:22 +01:00
alert ( 'Un contributor utilise déjà cet email !' ) ;
return ;
}
2014-01-30 23:44:25 +01:00
if ( ! validateEmail ( email ) ) {
alert ( 'Email invalide !' ) ;
return ;
}
2014-01-06 03:04:57 +01:00
if ( edit _id == '-1' ) {
group . addContributor ( new Contributor ( name , email ) ) ;
}
else {
2014-01-19 14:48:41 +01:00
group . replaceContributor ( email , new Contributor ( name , email ) ) ;
2014-01-06 03:04:57 +01:00
}
2014-01-06 01:46:22 +01:00
set _contributors ( group ) ;
$ ( '#add_contributor_modal' ) . modal ( 'hide' ) ;
groups . save ( ) ;
2014-01-31 00:35:59 +01:00
view _group ( group , email ) ;
2014-01-06 01:46:22 +01:00
}
on _close _add _contributor _modal = function ( ) {
$ ( '#add_contributor_name' ) [ 0 ] . value = '' ;
$ ( '#add_contributor_email' ) [ 0 ] . value = '' ;
}
2014-01-06 03:04:57 +01:00
on _edit _contributor _btn _click = function ( e ) {
2014-01-16 20:16:29 +01:00
var group = groups [ $ ( '#view-group' ) . data ( 'uuid' ) ] ;
$ ( '#add_contributor_modal' ) . data ( 'group-uuid' , group . uuid ) ;
2014-01-19 14:48:41 +01:00
contributor _email = $ ( '#view-group #contributor' ) [ 0 ] . value ;
contributor = group . contributorByEmail ( contributor _email ) ;
$ ( '#view-group #edit_id' ) [ 0 ] . value = contributor _email ;
2014-01-06 03:04:57 +01:00
$ ( '#add_contributor_modal #add_contributor_name' ) [ 0 ] . value = contributor . name ;
$ ( '#add_contributor_modal #add_contributor_email' ) [ 0 ] . value = contributor . email ;
2014-01-19 14:48:41 +01:00
$ ( '#add_contributor_modal #add_contributor_email' ) . attr ( 'disabled' , true ) ;
2014-01-06 03:04:57 +01:00
$ ( $ ( '#add_contributor_modal h4' ) [ 0 ] ) . html ( 'Editer un participant' ) ;
$ ( $ ( '#add_contributor_modal #add_contributor_submit' ) [ 0 ] ) . html ( 'Valider' ) ;
$ ( '#add_contributor_modal' ) . modal ( 'show' ) ;
}
2014-01-06 01:46:22 +01:00
on _click _del _contributor _btn = function ( ) {
2014-01-30 23:45:17 +01:00
var group = groups [ $ ( '#view-group' ) . data ( 'uuid' ) ] ;
contributor = group . contributorByEmail ( $ ( '#view-group #contributor' ) [ 0 ] . value ) ;
question = 'Voulez-vous vraiment supprimer ' + contributor . name + ' ?' ;
2014-01-06 01:46:22 +01:00
myconfirm ( question , on _confirm _del _contributor , null , contributor ) ;
}
on _confirm _del _contributor = function ( contributor ) {
2014-01-16 20:16:29 +01:00
var group = groups [ $ ( '#view-group' ) . data ( 'uuid' ) ] ;
2014-01-06 01:46:22 +01:00
group . removeContributor ( contributor ) ;
2014-01-31 00:38:42 +01:00
view _group ( group ) ;
2014-01-06 01:46:22 +01:00
groups . save ( ) ;
}
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Add / edit / delete contribution
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
on _show _add _contribution _modal = function ( e ) {
2014-01-19 14:48:41 +01:00
$ ( '#add_contribution_modal #add_contribution_contributor_email' ) . html ( $ ( '#view-group #contributor' ) . html ( ) ) ;
$ ( '#add_contribution_modal #add_contribution_contributor_email' ) [ 0 ] . value = $ ( '#view-group #contributor' ) [ 0 ] . value ;
2014-07-19 20:06:57 +02:00
gid = $ ( '#add_contribution_modal' ) . data ( 'group-uuid' ) ;
group = groups [ gid ] ;
2014-07-20 00:37:25 +02:00
current _cat = $ ( '#add_contribution_modal #add_contribution_category' ) [ 0 ] . value ;
2014-07-19 20:06:57 +02:00
cats = "<option value=''>Non définie</option>" ;
2014-07-20 00:37:25 +02:00
for ( u in group . getSortedCategories ( ) ) {
2014-07-19 20:06:57 +02:00
cats += "<option value='" + u + "'>" + group . categories [ u ] [ 'name' ] + "</option>" ;
}
2014-07-19 23:58:59 +02:00
$ ( '#add_contribution_modal #add_contribution_category' ) . html ( cats ) ;
2014-07-20 00:37:25 +02:00
$ ( '#add_contribution_modal #add_contribution_category' ) [ 0 ] . value = current _cat ;
2014-07-27 18:54:13 +02:00
$ ( '#add_contribution_modal #add_contribution_title' ) . typeahead ( 'destroy' ) ;
2014-07-20 17:04:14 +02:00
$ ( '#add_contribution_modal #add_contribution_title' ) . typeahead ( {
hint : true ,
highlight : true ,
minLength : 1
} ,
{
name : 'titles' ,
displayKey : 'value' ,
source : group . findContributionByTitleMatches ( )
} ) ;
2014-01-06 01:46:22 +01:00
$ ( '#add_contribution_modal #add_contribution_title' ) . focus ( ) ;
}
2014-07-20 17:04:14 +02:00
on _select _contribution _suggestion = function ( event , choice , name ) {
if ( jQuery . type ( choice [ 'category' ] ) == 'string' ) {
2014-07-27 18:54:13 +02:00
$ ( '#add_contribution_modal #add_contribution_category' ) . val ( choice [ 'category' ] ) ;
2014-07-20 17:04:14 +02:00
}
}
2014-01-06 01:46:22 +01:00
on _click _add _contribution _btn = function ( ) {
2014-01-16 20:16:29 +01:00
$ ( '#add_contribution_modal' ) . data ( 'group-uuid' , $ ( '#view-group' ) . data ( 'uuid' ) ) ;
2014-01-14 00:29:49 +01:00
$ ( '#add_contribution_modal #edit_uuid' ) [ 0 ] . value = '-1' ;
2014-02-24 00:31:53 +01:00
$ ( '#add_contribution_date' ) [ 0 ] . value = moment ( ) . format ( 'DD/MM/YYYY' ) ;
2014-01-06 03:06:04 +01:00
$ ( $ ( '#add_contribution_modal h4' ) [ 0 ] ) . html ( 'Ajouter une contribution' ) ;
$ ( $ ( '#add_contribution_modal #add_contribution_submit' ) [ 0 ] ) . html ( 'Ajouter' ) ;
2014-01-06 01:46:22 +01:00
$ ( '#add_contribution_modal' ) . modal ( 'show' ) ;
}
on _valid _add _contribution _modal = function ( e ) {
e . preventDefault ( ) ;
2014-01-16 20:16:29 +01:00
var group = groups [ $ ( '#add_contribution_modal' ) . data ( 'group-uuid' ) ] ;
2014-01-19 14:48:41 +01:00
var contributor _email = $ ( '#add_contribution_contributor_email' ) [ 0 ] . value ;
var contributor = group . contributorByEmail ( contributor _email ) ;
2014-01-06 01:46:22 +01:00
if ( contributor == undefined ) {
alert ( 'Participant inconu !' ) ;
return ;
}
var title = $ ( '#add_contribution_title' ) [ 0 ] . value ;
if ( title == '' ) {
$ ( '#add_contribution_title' ) [ 0 ] . focus ( ) ;
alert ( 'Vous devez saisir un titre !' ) ;
return ;
}
var cost = $ ( '#add_contribution_cost' ) [ 0 ] . value ;
if ( cost == '' ) {
$ ( '#add_contribution_cost' ) [ 0 ] . focus ( ) ;
alert ( 'Vous devez saisir un coût !' ) ;
return ;
}
2014-01-06 02:11:43 +01:00
else if ( ! RegExp ( '^[0-9]*((,|\.)[0-9]{1,2})*$' ) . test ( cost ) ) {
2014-01-06 01:46:22 +01:00
$ ( '#add_contribution_cost' ) [ 0 ] . focus ( ) ;
alert ( 'Coût incorrect !' ) ;
return ;
}
cost = parseFloat ( cost . replace ( ',' , '.' ) ) ;
2014-01-12 01:11:24 +01:00
2014-01-06 01:46:22 +01:00
var date = $ ( '#add_contribution_date' ) [ 0 ] . value ;
2014-02-24 00:31:53 +01:00
date . replace ( / /g , '' ) ;
2014-01-06 01:46:22 +01:00
if ( date != '' ) {
if ( ! RegExp ( '^[0-3][0-9]\/[0-1][0-9]\/[0-2][0-9][0-9][0-9]$' ) . test ( date ) ) {
$ ( '#add_contribution_date' ) [ 0 ] . focus ( ) ;
alert ( 'Date incorrect !' ) ;
return ;
}
date = moment ( date , 'DD-MM-YYYY' ) . toDate ( ) ;
if ( jQuery . type ( date ) != 'date' ) {
alert ( 'Date non-reconnu !' ) ;
return ;
}
}
else {
date = new Date ( ) ;
}
2014-01-12 01:11:24 +01:00
2014-07-19 23:58:59 +02:00
var cat = $ ( '#add_contribution_category' ) [ 0 ] . value ;
2014-07-19 20:06:57 +02:00
if ( cat != '' && ! jQuery . type ( group . categories [ cat ] ) ) {
2014-07-19 23:58:59 +02:00
alert ( 'Category incorrect' ) ;
2014-07-19 20:06:57 +02:00
return ;
}
2014-01-14 00:29:49 +01:00
if ( $ ( '#add_contribution_modal #edit_uuid' ) [ 0 ] . value == '-1' ) {
2014-07-19 20:06:57 +02:00
group . addContribution ( new Contribution ( contributor , cost , title , date , cat ) ) ;
2014-01-06 01:46:22 +01:00
}
else {
2014-01-14 00:29:49 +01:00
contribution _uuid = $ ( '#add_contribution_modal #edit_uuid' ) [ 0 ] . value ;
2014-07-19 20:06:57 +02:00
group . updateContribution ( contribution _uuid , new Contribution ( contributor , cost , title , date , cat ) ) ;
2014-01-06 01:46:22 +01:00
}
2014-10-13 00:25:38 +02:00
show _contributions ( group , $ ( '#view-group #contributor' ) [ 0 ] . value ) ;
2014-01-06 01:46:22 +01:00
$ ( '#add_contribution_modal' ) . modal ( 'hide' ) ;
$ ( '#add_contribution_modal form' ) [ 0 ] . reset ( ) ;
groups . save ( ) ;
}
on _close _add _contribution _modal = function ( ) {
$ ( '#add_contribution_modal form' ) [ 0 ] . reset ( ) ;
}
on _contribution _delete _btn _click = function ( e ) {
2014-01-16 20:16:29 +01:00
var group = groups [ $ ( '#view-group' ) . data ( 'uuid' ) ] ;
2014-01-14 00:29:49 +01:00
contribution _uuid = $ ( $ ( e . target ) . parents ( 'div' ) [ 0 ] ) . data ( 'uuid' ) ;
contribution = group . contributions [ contribution _uuid ] ;
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 } ) ;
2014-01-06 01:46:22 +01:00
}
on _confirm _contribution _delete = function ( data ) {
2014-01-14 00:29:49 +01:00
data . group . deleteContribution ( data . contribution _uuid ) ;
2014-01-06 01:46:22 +01:00
groups . save ( ) ;
on _contributor _change ( ) ;
}
on _contribution _edit _btn _click = function ( e ) {
2014-01-16 20:16:29 +01:00
var group = groups [ $ ( '#view-group' ) . data ( 'uuid' ) ] ;
$ ( '#add_contribution_modal' ) . data ( 'group-uuid' , $ ( '#view-group' ) . data ( 'uuid' ) ) ;
2014-01-06 03:06:04 +01:00
$ ( $ ( '#add_contribution_modal h4' ) [ 0 ] ) . html ( 'Editer une contribution' ) ;
$ ( $ ( '#add_contribution_modal #add_contribution_submit' ) [ 0 ] ) . html ( 'Valider' ) ;
2014-01-14 00:29:49 +01:00
contribution _uuid = $ ( $ ( e . target ) . parents ( 'div' ) [ 0 ] ) . data ( 'uuid' ) ;
contribution = group . contributions [ contribution _uuid ] ;
2014-01-19 14:48:41 +01:00
$ ( '#add_contribution_modal #add_contribution_contributor_email' ) [ 0 ] . value = contribution . contributor . email ;
2014-01-06 01:46:22 +01:00
$ ( '#add_contribution_modal #add_contribution_title' ) [ 0 ] . value = contribution . getTitle ( ) ;
$ ( '#add_contribution_modal #add_contribution_cost' ) [ 0 ] . value = contribution . cost ;
$ ( '#add_contribution_modal #add_contribution_date' ) [ 0 ] . value = moment ( contribution . date ) . format ( 'DD/MM/YYYY' ) ;
2014-07-19 23:58:59 +02:00
if ( contribution . category ) {
$ ( '#add_contribution_modal #add_contribution_category' ) [ 0 ] . value = contribution . category ;
2014-07-19 20:06:57 +02:00
}
2014-01-14 00:29:49 +01:00
$ ( '#add_contribution_modal #edit_uuid' ) [ 0 ] . value = contribution _uuid ;
2014-01-06 01:46:22 +01:00
$ ( '#add_contribution_modal' ) . modal ( 'show' ) ;
}
/ * * * * * * * * * * * * * * * * * * * *
* Clear local data
* * * * * * * * * * * * * * * * * * * * /
clear _local _data = function ( ) {
2014-01-06 17:22:09 +01:00
navbar _collapse _hide ( ) ;
2014-01-06 01:46:22 +01:00
myconfirm ( 'Etes-vous sûre de vouloir supprimer les données locales ?' , on _confirm _clear _local _data ) ;
}
on _confirm _clear _local _data = function ( data ) {
2014-01-18 00:21:36 +01:00
delete localStorage . groups ;
2014-01-06 01:46:22 +01:00
location . reload ( ) ;
}
/ * * * * * * * * * * * * * * * * * * * * *
* View balance
* * * * * * * * * * * * * * * * * * * * * /
on _display _balance _btn _click = function ( e ) {
2014-01-16 20:16:29 +01:00
group = groups [ $ ( '#view-group' ) . data ( 'uuid' ) ] ;
2014-01-06 01:46:22 +01:00
display _balance ( group ) ;
}
display _balance = function ( group ) {
bal = group . balance ( ) ;
tbody = $ ( $ ( '#display_balance_modal tbody' ) [ 0 ] ) ;
tbody . html ( '' ) ;
2014-01-12 20:07:01 +01:00
for ( c in bal [ 'balance' ] ) {
if ( bal [ 'balance' ] [ c ] [ 'diff' ] < 0 ) {
diff = '<td class="negative">' + bal [ 'balance' ] [ c ] [ 'diff' ] . toFixed ( 2 ) + ' €</td>' ;
}
else {
diff = '<td><span class="glyphicon glyphicon-thumbs-up"></span></td>' ;
}
2014-01-19 14:48:41 +01:00
tbody . append ( '<tr><td>' + bal [ 'balance' ] [ c ] [ 'name' ] + '</td><td>' + bal [ 'balance' ] [ c ] [ 'total' ] . toFixed ( 2 ) + ' €</td>' + diff + '</tr>' ) ;
2014-01-06 01:46:22 +01:00
}
2014-01-12 20:07:01 +01:00
$ ( '#display_balance_modal #total-value' ) . html ( bal . sum . toFixed ( 2 ) + ' €' ) ;
2014-01-06 01:46:22 +01:00
$ ( '#display_balance_modal' ) . modal ( 'show' ) ;
}
/ * * * * * * * * * * * * * * * * * * * * *
* Remove group
* * * * * * * * * * * * * * * * * * * * * /
on _remove _group _btn _click = function ( e ) {
2014-01-16 20:16:29 +01:00
group = groups [ $ ( '#view-group' ) . data ( 'uuid' ) ] ;
2014-07-23 02:45:38 +02:00
bottom _navbar _collapse _hide ( ) ;
2014-01-06 01:46:22 +01:00
myconfirm ( 'Etes-vous sûre de vouloir supprimer le group ' + group . name + ' ?' , on _confirm _remove _group , null , group ) ;
}
on _confirm _remove _group = function ( group ) {
2014-01-16 20:16:29 +01:00
if ( groups . removeGroup ( group . uuid ) ) {
2014-01-06 01:46:22 +01:00
groups . save ( ) ;
location . reload ( ) ;
}
else {
alert ( 'Erreur en supprimant le groupe ' + group . name + ' :(' ) ;
}
}
2014-01-12 01:24:16 +01:00
/ * * * * * * * * * * * * * * * * * * * *
* Login
* * * * * * * * * * * * * * * * * * * * /
on _close _login _modal = function ( e ) {
$ ( '#login_modal form' ) [ 0 ] . reset ( ) ;
}
sync _server = false ;
on _valid _login _modal = function ( e ) {
2014-01-14 00:33:19 +01:00
e . preventDefault ( ) ;
2014-01-12 01:24:16 +01:00
email = $ ( '#login_modal #login_email' ) [ 0 ] . value ;
pass = $ ( '#login_modal #login_pass' ) [ 0 ] . value ;
server = $ ( '#login_modal #login_server' ) [ 0 ] . value ;
$ ( '#login_modal' ) . modal ( 'hide' ) ;
2014-01-12 20:34:06 +01:00
pleaseWaitShow ( ) ;
2014-01-12 01:24:16 +01:00
sync _server . login ( server , email , pass ,
function ( data ) {
localStorage . user = JSON . stringify ( {
'name' : data . name ,
'email' : email ,
'password' : pass ,
'server' : server ,
2014-02-23 23:51:50 +01:00
'lastsync' : null
2014-01-12 01:24:16 +01:00
} ) ;
2014-01-12 20:34:06 +01:00
pleaseWaitHide ( ) ;
2014-01-12 01:24:16 +01:00
logged _menu ( ) ;
} ,
function ( data ) {
2014-01-12 20:34:06 +01:00
pleaseWaitHide ( ) ;
2014-01-12 01:24:16 +01:00
if ( jQuery . type ( data ) != 'object' && data . loginerror ) {
alert ( data . loginerror ) ;
}
else {
console . log ( data ) ;
alert ( 'Erreur durant la connexion au serveur' ) ;
}
}
) ;
}
2014-01-18 22:41:59 +01:00
on _click _go _to _subscribe _btn = function ( ) {
$ ( '#login_modal' ) . modal ( 'hide' ) ;
$ ( '#subscribe_modal' ) . modal ( 'show' ) ;
}
/ * * * * * * * * * * * * * * * * * * * *
* Subscribe
* * * * * * * * * * * * * * * * * * * * /
on _close _subscribe _modal = function ( e ) {
$ ( '#subscribe_modal form' ) [ 0 ] . reset ( ) ;
}
2014-01-12 01:24:16 +01:00
2014-01-18 22:41:59 +01:00
sync _server = false ;
on _valid _subscribe _modal = function ( e ) {
e . preventDefault ( ) ;
email = $ ( '#subscribe_modal #subscribe_email' ) [ 0 ] . value ;
name = $ ( '#subscribe_modal #subscribe_name' ) [ 0 ] . value ;
pass = $ ( '#subscribe_modal #subscribe_pass' ) [ 0 ] . value ;
server = $ ( '#subscribe_modal #subscribe_server' ) [ 0 ] . value ;
$ ( '#subscribe_modal' ) . modal ( 'hide' ) ;
pleaseWaitShow ( ) ;
sync _server . subscribe ( server , email , name , pass ,
function ( data ) {
localStorage . user = JSON . stringify ( {
'name' : data . name ,
'email' : data . email ,
'password' : data . password ,
'server' : server ,
} ) ;
pleaseWaitHide ( ) ;
logged _menu ( ) ;
alert ( 'Bienvenu ' + data . name + ' !' ) ;
} ,
function ( data ) {
pleaseWaitHide ( ) ;
if ( jQuery . type ( data ) == 'object' && jQuery . type ( data . subscribeerror ) != 'undefined' ) {
alert ( data . subscribeerror ) ;
}
else {
console . log ( data ) ;
alert ( 'Erreur durant la connexion au serveur' ) ;
}
}
) ;
}
2014-01-12 01:24:16 +01:00
/ * * * * * * * * * * * * * * * * * * * *
* Sync
* * * * * * * * * * * * * * * * * * * * /
on _click _sync _btn = function ( e ) {
2014-01-18 00:23:00 +01:00
navbar _collapse _hide ( ) ;
2014-01-12 20:03:12 +01:00
if ( user ) {
2014-01-12 20:34:06 +01:00
pleaseWaitShow ( ) ;
2014-01-19 14:48:41 +01:00
onsuccess = function ( data ) {
console . log ( data ) ;
if ( data . groups ) {
groups . import ( data . groups ) ;
groups . save ( ) ;
2014-02-23 23:51:50 +01:00
user . lastsync = new Date ( ) . getTime ( ) ;
localStorage . user = JSON . stringify ( user ) ;
2014-01-19 14:48:41 +01:00
refresh _group _list ( ) ;
2014-01-12 20:34:06 +01:00
pleaseWaitHide ( ) ;
2014-01-19 14:54:43 +01:00
view _home ( ) ;
2014-01-19 14:48:41 +01:00
alert ( 'Groupes synchronisés' ) ;
}
else {
onerror ( data ) ;
}
}
onerror = function ( data ) {
pleaseWaitHide ( ) ;
console . log ( data ) ;
if ( jQuery . type ( data [ 'loginerror' ] ) != 'undefined' ) {
alert ( data [ 'loginerror' ] ) ;
}
else if ( jQuery . type ( data [ 'syncerror' ] ) != 'undefined' ) {
alert ( data [ 'syncerror' ] ) ;
2014-01-12 01:24:16 +01:00
}
2014-01-19 14:48:41 +01:00
else {
alert ( 'Erreur durant la synchronisation :(' ) ;
}
}
sync _server . sync ( user . server , user . email , user . password , groups . export ( ) , onsuccess , onerror ) ;
2014-01-12 01:24:16 +01:00
}
else {
alert ( "Vous devez vous connecter pour commencer" ) ;
$ ( '#login_modal' ) . modal ( 'show' ) ;
}
}
/ *
* User menu
* /
user = false ;
logged _menu = function ( ) {
user = JSON . parse ( localStorage . user ) ;
$ ( '#user-name' ) . html ( user . name ) ;
$ ( '#user-menu li' ) . remove ( ) ;
menu = $ ( '#user-menu' ) ;
menu . html ( "<li><a id='myaccount_btn'>Mon compte</a></li>" +
"<li><a id='sync_btn'>Synchroniser</a></li>" +
"<li class='divider'></li>" +
"<li><a id='logoff_btn'>Déconnexion</a></li>" ) ;
$ ( '#myaccount_btn' ) . bind ( 'click' , on _click _myaccount _btn ) ;
$ ( '#sync_btn' ) . bind ( 'click' , on _click _sync _btn ) ;
$ ( '#logoff_btn' ) . bind ( 'click' , on _click _logoff _btn ) ;
}
logged _out _menu = function ( ) {
2014-01-12 20:03:12 +01:00
user = false ;
2014-01-12 01:24:16 +01:00
$ ( '#user-name' ) . html ( 'Connexion' ) ;
$ ( '#user-menu li' ) . remove ( ) ;
menu = $ ( '#user-menu' ) ;
2014-01-18 22:41:59 +01:00
menu . html ( "<li><a id='login_btn'>Connexion</a></li>" +
"<li><a id='subscribe_btn'>Inscription</a></li>" ) ;
2014-01-12 01:24:16 +01:00
$ ( '#login_btn' ) . bind ( 'click' , on _click _login _btn ) ;
2014-01-18 22:41:59 +01:00
$ ( '#subscribe_btn' ) . bind ( 'click' , on _click _subscribe _btn ) ;
2014-01-12 01:24:16 +01:00
}
on _click _myaccount _btn = function ( ) {
$ ( '#myaccount_modal #myaccount_email' ) . html ( user . email ) ;
$ ( '#myaccount_modal #myaccount_name' ) [ 0 ] . value = user . name ;
2014-01-12 20:05:08 +01:00
$ ( '#myaccount_modal #myaccount_server' ) . html ( user . server ) ;
2014-02-23 23:51:50 +01:00
if ( user . lastsync ) {
$ ( '#myaccount_modal #myaccount_lastsync' ) . html ( moment ( user . lastsync ) . format ( 'DD/MM/YYYY HH:mm:ss' ) ) ;
}
else {
$ ( '#myaccount_modal #myaccount_lastsync' ) . html ( 'Jamais synchronisé' ) ;
}
2014-01-12 01:24:16 +01:00
$ ( '#myaccount_modal' ) . modal ( 'show' ) ;
}
on _click _login _btn = function ( ) {
$ ( '#login_modal' ) . modal ( 'show' ) ;
}
2014-01-18 22:41:59 +01:00
on _click _subscribe _btn = function ( ) {
$ ( '#subscribe_modal' ) . modal ( 'show' ) ;
}
2014-01-12 01:24:16 +01:00
on _click _logoff _btn = function ( ) {
delete localStorage . user ;
logged _out _menu ( ) ;
}
2014-11-13 00:40:19 +01:00
is _on _bottom = function ( ) {
var totalHeight , currentScroll , visibleHeight ;
if ( document . documentElement . scrollTop ) {
currentScroll = document . documentElement . scrollTop ;
}
else {
currentScroll = document . body . scrollTop ;
}
totalHeight = document . body . offsetHeight ;
visibleHeight = document . documentElement . clientHeight ;
return ( totalHeight <= currentScroll + visibleHeight ) ;
}
2014-01-12 20:33:11 +01:00
/ * * * * * * * * * * * * * * * * * * *
* pleaseWaitDialog
* * * * * * * * * * * * * * * * * * * /
pleaseWaitShow = function ( ) {
2014-01-12 21:09:32 +01:00
$ ( '#please_wait_modal' ) . modal ( 'show' ) ;
2014-01-12 20:33:11 +01:00
}
pleaseWaitHide = function ( ) {
2014-01-12 21:09:32 +01:00
$ ( '#please_wait_modal' ) . modal ( 'hide' ) ;
2014-01-12 20:33:11 +01:00
}
2014-01-06 01:46:22 +01:00
/ * * * * * * * * * * * * * * * * * * * * *
* Activate
* * * * * * * * * * * * * * * * * * * * * /
$ ( document ) . ready ( function ( ) {
2014-01-12 20:34:06 +01:00
pleaseWaitShow ( ) ;
2014-01-06 01:46:22 +01:00
if ( typeof ( Storage ) !== "undefined" ) {
groups = new GroupList ( ) ;
groups . loadFromLocalStorage ( ) ;
refresh _group _list ( ) ;
2014-01-12 01:24:16 +01:00
if ( jQuery . type ( localStorage . user ) != 'undefined' ) {
logged _menu ( ) ;
}
else {
logged _out _menu ( ) ;
}
2014-01-06 01:46:22 +01:00
}
else {
alert ( 'Local storage not supported !' ) ;
2014-01-12 20:34:06 +01:00
pleaseWaitHide ( ) ;
2014-01-12 01:24:16 +01:00
return ;
2014-01-06 01:46:22 +01:00
}
2014-01-12 01:24:16 +01:00
sync _server = new SyncServer ( ) ;
2014-01-06 01:46:22 +01:00
2014-01-12 23:22:37 +01:00
$ ( '#app-name' ) . bind ( 'click' , view _home ) ;
2014-01-06 17:10:33 +01:00
$ ( '#add_group_btn' ) . bind ( 'click' , on _click _add _group _btn ) ;
2014-01-06 01:46:22 +01:00
$ ( '#add_group_submit' ) . bind ( 'click' , on _valid _add _group _modal ) ;
$ ( "#add_group_modal" ) . on ( 'shown.bs.modal' , on _show _add _group _modal ) ;
$ ( "#add_group_modal" ) . on ( 'hidden.bs.modal' , on _close _add _group _modal ) ;
2014-01-14 00:33:19 +01:00
$ ( "#add_group_modal form" ) . bind ( 'submit' , on _valid _add _group _modal ) ;
2014-01-06 01:46:22 +01:00
$ ( '#clear_local_data' ) . bind ( 'click' , clear _local _data ) ;
2014-01-12 01:24:16 +01:00
$ ( '#login_modal' ) . on ( 'hidden.bs.modal' , on _close _login _modal ) ;
$ ( '#login_modal #login_submit' ) . bind ( 'click' , on _valid _login _modal ) ;
2014-01-18 22:41:59 +01:00
$ ( '#login_modal #go_to_subscribe_btn' ) . bind ( 'click' , on _click _go _to _subscribe _btn ) ;
2014-01-12 01:24:16 +01:00
$ ( '#login_modal form' ) . bind ( 'submit' , on _valid _login _modal ) ;
2014-01-18 22:41:59 +01:00
$ ( '#subscribe_modal' ) . on ( 'hidden.bs.modal' , on _close _subscribe _modal ) ;
$ ( '#subscribe_modal #subscribe_submit' ) . bind ( 'click' , on _valid _subscribe _modal ) ;
$ ( '#subscribe_modal form' ) . bind ( 'submit' , on _valid _subscribe _modal ) ;
2014-01-06 01:46:22 +01:00
$ ( '#view-group #contributor' ) . bind ( 'change' , on _contributor _change ) ;
2014-11-13 00:40:19 +01:00
$ ( window ) . bind ( 'scroll' , on _view _group _scroll ) ;
2014-01-06 01:46:22 +01:00
$ ( '#add_contributor_btn' ) . bind ( 'click' , on _click _add _contributor _btn ) ;
$ ( '#add_contributor_submit' ) . bind ( 'click' , on _valid _add _contributor _modal ) ;
$ ( "#add_contributor_modal" ) . on ( 'shown.bs.modal' , on _show _add _contributor _modal ) ;
$ ( "#add_contributor_modal" ) . on ( 'hidden.bs.modal' , on _close _add _contributor _modal ) ;
2014-01-14 00:33:19 +01:00
$ ( "#add_contributor_modal form" ) . bind ( 'submit' , on _valid _add _contributor _modal ) ;
2014-02-24 00:31:53 +01:00
$ ( "#add_contribution_date" ) . pickadate ( { 'format' : 'dd/mm/yyyy' , 'firstDay' : 1 } ) ;
2014-01-06 01:46:22 +01:00
2014-01-06 03:04:57 +01:00
$ ( '#edit_contributor_btn' ) . bind ( 'click' , on _edit _contributor _btn _click ) ;
2014-01-31 00:41:22 +01:00
$ ( '#trash_contributor_btn' ) . bind ( 'click' , on _trash _group _contributors _btn _click ) ;
2014-01-06 01:46:22 +01:00
$ ( '#del_contributor_btn' ) . bind ( 'click' , on _click _del _contributor _btn ) ;
$ ( '#add_contribution_btn' ) . bind ( 'click' , on _click _add _contribution _btn ) ;
$ ( '#add_contribution_submit' ) . bind ( 'click' , on _valid _add _contribution _modal ) ;
$ ( "#add_contribution_modal" ) . on ( 'shown.bs.modal' , on _show _add _contribution _modal ) ;
$ ( "#add_contribution_modal" ) . on ( 'hidden.bs.modal' , on _close _add _contribution _modal ) ;
$ ( "#add_contribution_modal form" ) . on ( 'submit' , on _valid _add _contribution _modal ) ;
2014-07-20 17:04:14 +02:00
$ ( '#add_contribution_modal #add_contribution_title' ) . on ( 'typeahead:selected' , on _select _contribution _suggestion ) ;
2014-01-06 01:46:22 +01:00
$ ( "#display_balance_btn" ) . bind ( 'click' , on _display _balance _btn _click ) ;
2014-01-30 00:30:05 +01:00
$ ( "#view-group-trash #go-back-group" ) . bind ( 'click' , on _go _back _group _btn _click ) ;
2014-01-31 00:41:22 +01:00
$ ( "#view-group-trash-contributors #go-back-group" ) . bind ( 'click' , on _go _back _group _trash _contributors _btn _click ) ;
2014-01-30 00:30:05 +01:00
2014-07-23 00:16:12 +02:00
$ ( '#categories_group_btn' ) . bind ( 'click' , on _categories _group _btn _click ) ;
2014-10-13 00:24:44 +02:00
$ ( '#export_group_btn' ) . bind ( 'click' , on _export _group _btn _click ) ;
2014-01-30 00:30:05 +01:00
$ ( '#trash_group_btn' ) . bind ( 'click' , on _trash _group _btn _click ) ;
2014-01-06 01:46:22 +01:00
$ ( '#remove_group_btn' ) . bind ( 'click' , on _remove _group _btn _click ) ;
2014-10-13 00:20:28 +02:00
$ ( '#nav-search-form' ) . bind ( 'submit' , on _nav _search _form _submit ) ;
$ ( '#nav-search-form input' ) . bind ( 'input' , on _nav _search _form _input ) ;
2014-07-23 00:16:12 +02:00
$ ( '#view-group-categories span.input-group-addon' ) . bind ( 'click' , on _categories _group _add _btn _click ) ;
$ ( "#view-group-categories button.go-back-group" ) . bind ( 'click' , on _categories _go _back _group _btn _click ) ;
2014-01-12 20:11:40 +01:00
view _home ( ) ;
2014-01-12 20:34:06 +01:00
pleaseWaitHide ( ) ;
2014-01-06 01:46:22 +01:00
} ) ;