2024-09-13 17:43:01 +02:00
// AlertifyJS default settings
alertify . defaults = {
// dialogs defaults
modal : true ,
movable : true ,
resizable : true ,
closable : true ,
maximizable : true ,
pinnable : true ,
pinned : true ,
padding : true ,
overflow : true ,
maintainFocus : true ,
transition : "fade" ,
// notifier defaults
notifier : {
// auto-dismiss wait time (in seconds)
delay : 0 ,
// default position
position : "top-right" ,
} ,
// language resources
glossary : {
// Dialog title text
title : "Confirmation" ,
// ok button text
ok : "OK" ,
// cancel button text
cancel : "Annuler" ,
} ,
theme : {
// class name attached to prompt dialog input textbox.
input : "ajs-input form-control" ,
// class name attached to ok button
ok : "ajs-ok btn btn-primary" ,
// class name attached to cancel button
cancel : "ajs-cancel btn btn-default" ,
} ,
// global hooks
hooks : {
// invoked before initializing any dialog
preinit : function ( instance ) { } ,
// invoked after initializing any dialog
postinit : function ( instance ) { } ,
} ,
} ;
2016-09-06 01:12:46 +02:00
$ ( '#cats' ) . collapse ( {
toggle : false
} ) ;
on _title _click = function ( event ) {
if ( event . target . tagName == 'BUTTON' ) {
return ;
}
var title = $ ( this ) ;
var panel = title . parent ( ) . parent ( ) ;
var panel _collapse = panel . find ( '.panel-collapse' ) ;
var show = ! panel _collapse . hasClass ( 'in' ) ;
$ ( '.panel-collapse' ) . each ( function ( idx , div ) {
$ ( div ) . collapse ( 'hide' ) ;
} ) ;
2024-09-21 17:07:52 +02:00
if ( show )
2016-09-06 01:12:46 +02:00
panel _collapse . collapse ( 'show' ) ;
2024-09-21 17:07:52 +02:00
set _location (
title . data ( 'scase' ) . name ,
show ? title . data ( 'cat' ) . name : null ,
title . data ( 'trash' ) ? 'trash' : null
) ;
2016-09-06 01:12:46 +02:00
}
/ * * * * * * * * * * * * * * * * * * * * * * *
* Add scase
* * * * * * * * * * * * * * * * * * * * * * /
on _add _scase _btn _click = function ( event ) {
navbar _collapse _hide ( ) ;
$ ( '#add_scase_modal' ) . modal ( 'show' ) ;
}
on _valid _add _scase _modal = function ( e ) {
e . preventDefault ( ) ;
var name = $ ( '#add_scase_name' ) [ 0 ] . value ;
if ( name == '' ) {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Vous devez saisir le nom de la valise !" , "error" , 5 ) ;
2016-09-06 01:12:46 +02:00
return ;
}
2017-09-17 18:55:25 +02:00
var nameshake = scases . byName ( name ) ;
if ( nameshake ) {
if ( nameshake . removed ) {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Une valise de ce nom existe dans la corbeille !" , "error" , 5 ) ;
2017-09-17 18:55:25 +02:00
}
else {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Cette valise existe déjà !" , "error" , 5 ) ;
2017-09-17 18:55:25 +02:00
}
2016-09-06 01:12:46 +02:00
return ;
}
var scase = scases . newSCase ( name ) ;
if ( scase ) {
scases . save ( ) ;
show _scase ( scase ) ;
2024-09-21 17:07:52 +02:00
auto _sync _local _data ( ) ;
2016-09-06 01:12:46 +02:00
}
$ ( '#add_scase_modal' ) . modal ( 'hide' ) ;
}
on _show _add _scase _modal = function ( ) {
$ ( '#add_scase_name' ) . focus ( ) ;
}
on _close _add _scase _modal = function ( ) {
$ ( '#add_scase_modal form' ) [ 0 ] . reset ( ) ;
}
/ * * * * * * * * * * * * * * * * * * * * * * *
* Rename scase
* * * * * * * * * * * * * * * * * * * * * * /
on _rename _scase _btn _click = function ( event ) {
navbar _collapse _hide ( ) ;
$ ( '#rename_scase_name' ) [ 0 ] . value = $ ( '#cats' ) . data ( 'scase' ) ;
$ ( '#rename_scase_modal' ) . modal ( 'show' ) ;
}
on _valid _rename _scase _modal = function ( e ) {
e . preventDefault ( ) ;
var name = $ ( '#rename_scase_name' ) [ 0 ] . value ;
if ( name == '' ) {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Vous devez saisir le nouveau nom de la valise !" , "error" , 5 ) ;
2016-09-06 01:12:46 +02:00
return ;
}
if ( $ ( '#cats' ) . data ( 'scase' ) != name ) {
2017-09-17 18:55:25 +02:00
var nameshake = scases . byName ( name ) ;
if ( nameshake ) {
if ( nameshake . removed ) {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Une valise portant ce nom existe dans la corbeille !" , "error" , 5 ) ;
2017-09-17 18:55:25 +02:00
}
else {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Une valise portant ce nom existe déjà !" , "error" , 5 ) ;
2017-09-17 18:55:25 +02:00
}
2016-09-06 01:12:46 +02:00
return ;
}
var scase = scases . renameSCase ( $ ( '#cats' ) . data ( 'scase' ) , name ) ;
if ( scase ) {
scases . save ( ) ;
show _scase ( scase ) ;
2024-09-21 17:07:52 +02:00
auto _sync _local _data ( ) ;
2016-09-06 01:12:46 +02:00
}
else {
2024-09-13 17:43:01 +02:00
alertify . notify ( 'Une erreur est survenue en renomant la valise...' , "error" , 5 ) ;
2016-09-06 01:12:46 +02:00
}
}
$ ( '#rename_scase_modal' ) . modal ( 'hide' ) ;
}
on _show _rename _scase _modal = function ( ) {
$ ( '#rename_scase_name' ) . focus ( ) ;
}
on _close _rename _scase _modal = function ( ) {
$ ( '#rename_scase_modal form' ) [ 0 ] . reset ( ) ;
}
/ * * * * * * * * * * * * * * * * * * * * * * *
* Copy scase
* * * * * * * * * * * * * * * * * * * * * * /
on _copy _scase _btn _click = function ( event ) {
navbar _collapse _hide ( ) ;
$ ( '#copy_scase_modal' ) . modal ( 'show' ) ;
}
on _valid _copy _scase _modal = function ( e ) {
e . preventDefault ( ) ;
var name = $ ( '#copy_scase_name' ) [ 0 ] . value ;
if ( name == '' ) {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Vous devez saisir le nom de la nouvelle valise !" , "error" , 5 ) ;
2016-09-06 01:12:46 +02:00
return ;
}
2017-09-17 18:55:25 +02:00
var nameshake = scases . byName ( name ) ;
if ( nameshake ) {
if ( nameshake . removed ) {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Une valise portant ce nom existe dans la corbeille !" , "error" , 5 ) ;
2017-09-17 18:55:25 +02:00
}
else {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Une valise portant ce nom existe déjà !" , "error" , 5 ) ;
2017-09-17 18:55:25 +02:00
}
2016-09-06 01:12:46 +02:00
return ;
}
var scase = scases . copySCase ( $ ( '#cats' ) . data ( 'scase' ) , name ) ;
if ( scase ) {
scases . save ( ) ;
show _scase ( scase ) ;
2024-09-21 17:07:52 +02:00
auto _sync _local _data ( ) ;
2016-09-06 01:12:46 +02:00
}
else {
2024-09-13 17:43:01 +02:00
alertify . notify ( 'Une erreur est survenue en copiant la valise...' , "error" , 5 ) ;
2016-09-06 01:12:46 +02:00
}
$ ( '#copy_scase_modal' ) . modal ( 'hide' ) ;
}
on _show _copy _scase _modal = function ( ) {
$ ( '#copy_scase_name' ) . focus ( ) ;
}
on _close _copy _scase _modal = function ( ) {
$ ( '#copy_scase_modal form' ) [ 0 ] . reset ( ) ;
}
2016-09-07 00:30:25 +02:00
/ * * * * * * * * * * * * * * * * * * * * * * *
* Reset scase
* * * * * * * * * * * * * * * * * * * * * * /
on _reset _scase _btn _click = function ( event ) {
navbar _collapse _hide ( ) ;
var scase = scases . byName ( $ ( '#cats' ) . data ( 'scase' ) ) ;
if ( scase ) {
2024-09-13 17:43:01 +02:00
alertify . confirm (
` Réinitialisation de la valise ${ scase . name } ` ,
` Voulez-vous vraiment réinitialiser la valise ${ scase . name } ? ` ,
function ( data ) {
scases . resetSCase ( scase . name ) ;
scases . save ( ) ;
show _scase ( scase ) ;
2024-09-21 17:07:52 +02:00
auto _sync _local _data ( ) ;
2024-09-13 17:43:01 +02:00
} ,
null
) ;
2016-09-07 00:30:25 +02:00
}
}
2016-09-06 01:12:46 +02:00
/ * * * * * * * * * * * * * * * * * * * * * * *
* Delete scase
* * * * * * * * * * * * * * * * * * * * * * /
on _delete _scase _btn _click = function ( event ) {
navbar _collapse _hide ( ) ;
var scase = scases . byName ( $ ( '#cats' ) . data ( 'scase' ) ) ;
if ( scase ) {
2024-09-13 17:43:01 +02:00
alertify . confirm (
` Suppression de la valise ${ scase . name } ` ,
` Voulez-vous vraiment supprimer la valise ${ scase . name } ? ` ,
function ( data ) {
scases . removeSCase ( scase . name ) ;
scases . save ( ) ;
show _scases ( ) ;
2024-09-21 17:07:52 +02:00
auto _sync _local _data ( ) ;
2024-09-13 17:43:01 +02:00
} ,
null
) ;
2016-09-06 01:12:46 +02:00
}
}
2017-09-17 18:55:25 +02:00
on _restore _scase _btn _click = function ( event ) {
navbar _collapse _hide ( ) ;
var scase = event . data . scase ;
if ( scase ) {
2024-09-13 17:43:01 +02:00
alertify . confirm (
` Restauration de la valise ${ scase . name } ` ,
` Voulez-vous vraiment restaurer la valise ${ scase . name } ? ` ,
function ( data ) {
scase . restore ( ) ;
scases . save ( ) ;
show _scases ( ) ;
} ,
null
) ;
2017-09-17 18:55:25 +02:00
}
}
on _scase _trash _btn _click = function ( event ) {
event . preventDefault ( ) ;
navbar _collapse _hide ( ) ;
var scase = scases . byName ( $ ( '#cats' ) . data ( 'scase' ) ) ;
if ( scase ) {
show _scase _trash ( scase ) ;
}
}
2016-09-06 01:12:46 +02:00
/ * * * * * * * * * * * * * * * * * * * * * * *
* Add cat
* * * * * * * * * * * * * * * * * * * * * * /
on _add _cat _btn _click = function ( event ) {
navbar _collapse _hide ( ) ;
$ ( '#add_cat_modal' ) . modal ( 'show' ) ;
}
on _valid _add _cat _modal = function ( e ) {
e . preventDefault ( ) ;
var name = $ ( '#add_cat_name' ) [ 0 ] . value ;
if ( name == '' ) {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Vous devez saisir le nom de la catégorie !" , "error" , 5 ) ;
2016-09-06 01:12:46 +02:00
return ;
}
var scase = scases . byName ( $ ( '#cats' ) . data ( 'scase' ) ) ;
if ( scase ) {
2017-09-17 18:55:25 +02:00
var nameshake = scase . cats . byName ( name ) ;
if ( nameshake ) {
if ( nameshake . removed ) {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Une catégorie portant ce nom existe dans la corbeille !" , "error" , 5 ) ;
2017-09-17 18:55:25 +02:00
}
else {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Une catégorie portant ce nom existe déjà !" , "error" , 5 ) ;
2017-09-17 18:55:25 +02:00
}
2016-09-06 01:12:46 +02:00
return ;
}
var cat = scase . cats . newCat ( name ) ;
if ( cat ) {
scases . save ( ) ;
show _scase ( scase , cat . name ) ;
2024-09-21 17:07:52 +02:00
auto _sync _local _data ( ) ;
2016-09-06 01:12:46 +02:00
}
}
$ ( '#add_cat_modal' ) . modal ( 'hide' ) ;
}
on _show _add _cat _modal = function ( ) {
$ ( '#add_cat_name' ) . focus ( ) ;
}
on _close _add _cat _modal = function ( ) {
$ ( '#add_cat_modal form' ) [ 0 ] . reset ( ) ;
}
/ * * * * * * * * * * * * * * * * * * * * * * *
* Rename cat
* * * * * * * * * * * * * * * * * * * * * * /
on _rename _cat _btn _click = function ( event ) {
navbar _collapse _hide ( ) ;
$ ( '#rename_cat_modal' ) . data ( 'cat' , event . data . cat . name ) ;
$ ( '#rename_cat_name' ) [ 0 ] . value = event . data . cat . name ;
$ ( '#rename_cat_modal' ) . modal ( 'show' ) ;
}
on _valid _rename _cat _modal = function ( e ) {
e . preventDefault ( ) ;
var name = $ ( '#rename_cat_name' ) [ 0 ] . value ;
if ( name == '' ) {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Vous devez saisir le nouveau nom de la catégorie !" , "error" , 5 ) ;
2016-09-06 01:12:46 +02:00
return ;
}
var scase = scases . byName ( $ ( '#cats' ) . data ( 'scase' ) ) ;
if ( scase ) {
if ( scase . cats . byName ( name ) ) {
2017-09-17 18:55:25 +02:00
var namesake = scase . cats . byName ( name ) ;
if ( namesake . removed ) {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Une catégorie de se nom existe dans la corbeille !" , "error" , 5 ) ;
2017-09-17 18:55:25 +02:00
}
else {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Cette catégorie existe déjà !" , "error" , 5 ) ;
2017-09-17 18:55:25 +02:00
}
2016-09-06 01:12:46 +02:00
return ;
}
var cat = scase . cats . renameCat ( $ ( '#rename_cat_modal' ) . data ( 'cat' ) , name ) ;
if ( cat ) {
scases . save ( ) ;
show _scase ( scase , cat . name ) ;
2024-09-21 17:07:52 +02:00
auto _sync _local _data ( ) ;
2016-09-06 01:12:46 +02:00
}
}
$ ( '#rename_cat_modal' ) . modal ( 'hide' ) ;
}
on _show _rename _cat _modal = function ( ) {
$ ( '#rename_cat_name' ) . focus ( ) ;
}
on _close _rename _cat _modal = function ( ) {
$ ( '#rename_cat_modal form' ) [ 0 ] . reset ( ) ;
}
/ * * * * * * * * * * * * * * * * * * * * * * *
* Delete cat
* * * * * * * * * * * * * * * * * * * * * * /
on _delete _cat _btn _click = function ( event ) {
navbar _collapse _hide ( ) ;
var scase = scases . byName ( $ ( '#cats' ) . data ( 'scase' ) ) ;
if ( scase ) {
var cat = event . data . cat . name ;
2024-09-13 17:43:01 +02:00
alertify . confirm (
` Suppression de la catégorie ${ cat } ` ,
` Voulez-vous vraiment supprimer la catégorie ${ cat } ? ` ,
function ( data ) {
scase . cats . removeCat ( cat ) ;
scases . save ( ) ;
show _scase ( scase ) ;
2024-09-21 17:07:52 +02:00
auto _sync _local _data ( ) ;
2024-09-13 17:43:01 +02:00
} ,
null
) ;
2016-09-06 01:12:46 +02:00
}
}
2017-09-17 18:55:25 +02:00
on _restore _cat _btn _click = function ( event ) {
navbar _collapse _hide ( ) ;
var scase = scases . byName ( $ ( '#cats' ) . data ( 'scase' ) ) ;
if ( scase ) {
var cat = event . data . cat . name ;
2024-09-13 17:43:01 +02:00
alertify . confirm (
` Restauration de la catégorie ${ cat } ` ,
` Voulez-vous vraiment restaurer la catégorie ${ cat } ? ` ,
function ( data ) {
scase . cats . restoreCat ( cat ) ;
scases . save ( ) ;
show _scase ( scase ) ;
2024-09-21 17:07:52 +02:00
auto _sync _local _data ( ) ;
2024-09-13 17:43:01 +02:00
} ,
null
) ;
2017-09-17 18:55:25 +02:00
}
}
2016-09-06 01:12:46 +02:00
/ * * * * * * * * * * * * * * * * * * * * * * * *
* Check / Uncheck thing
* * * * * * * * * * * * * * * * * * * * * * * /
on _li _click = function ( event ) {
2024-09-21 17:07:52 +02:00
if ( event . target . tagName != 'LI' ) return ;
2016-09-06 01:12:46 +02:00
var li = $ ( this ) ;
var ul = li . parent ( ) ;
var scase = scases . byName ( $ ( '#cats' ) . data ( 'scase' ) ) ;
if ( scase ) {
var cat = scase . cats . byName ( ul . data ( 'cat' ) ) ;
if ( cat ) {
var thing = cat . byLabel ( li . data ( 'label' ) ) ;
if ( thing ) {
2024-09-21 17:07:52 +02:00
li . toggleClass ( 'checked' ) ;
2017-09-17 23:37:44 +02:00
thing . setChecked ( li . hasClass ( 'checked' ) ) ;
2016-09-06 01:12:46 +02:00
scases . save ( ) ;
2024-09-21 17:07:52 +02:00
auto _sync _local _data ( ) ;
2016-09-06 01:12:46 +02:00
}
show _scase ( scase , cat . name ) ;
}
}
}
/ * * * * * * * * * * * * * * * * * * * * * * *
* Add thing
* * * * * * * * * * * * * * * * * * * * * * /
on _li _add _click = function ( event ) {
var li = $ ( this ) ;
var cat = li . parent ( ) . data ( 'cat' ) ;
2017-09-17 12:18:45 +02:00
var modal = $ ( '#add_thing_modal' ) ;
2016-09-06 01:12:46 +02:00
modal . data ( 'cat' , cat ) ;
modal . modal ( 'show' ) ;
}
on _valid _add _thing _modal = function ( e ) {
e . preventDefault ( ) ;
2017-09-17 12:18:45 +02:00
2016-09-06 01:12:46 +02:00
var modal = $ ( '#add_thing_modal' ) ;
var scase = scases . byName ( $ ( '#cats' ) . data ( 'scase' ) ) ;
if ( scase ) {
var cat = scase . cats . byName ( modal . data ( 'cat' ) ) ;
if ( cat ) {
2017-09-17 19:43:57 +02:00
var things = [ ] ;
2017-09-17 12:18:45 +02:00
var labels = [ ] ;
var error = false ;
2017-09-17 19:43:57 +02:00
var add _thing _nbs = $ ( 'input.add_thing_nb' ) ;
2017-09-17 12:18:45 +02:00
$ ( 'input.add_thing_label' ) . each ( function ( idx , input ) {
2017-09-17 19:43:57 +02:00
var label = $ ( input ) . val ( ) ;
if ( label && label != '' ) {
if ( labels . indexOf ( label ) > - 1 ) {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Deux élements ne peuvent porter le même nom !" , "error" , 5 ) ;
2017-09-17 12:18:45 +02:00
error = true ;
return ;
}
2017-09-17 19:43:57 +02:00
if ( cat . byLabel ( label ) ) {
2024-09-13 17:43:01 +02:00
alertify . notify ( "L'élément '" + label + "' existe déjà !" , "error" , 5 ) ;
2017-09-17 12:18:45 +02:00
error = true ;
return ;
}
2017-09-17 19:43:57 +02:00
var nb = 1 ;
if ( add _thing _nbs [ idx ] ) {
nb = parseInt ( $ ( add _thing _nbs [ idx ] ) . val ( ) ) ;
if ( ! nb || nb == 0 ) {
nb = 1 ;
}
}
things . push ( {
'label' : label ,
'nb' : nb
} ) ;
labels . push ( label ) ;
2017-09-17 12:18:45 +02:00
}
} ) ;
if ( error ) {
2016-09-06 01:12:46 +02:00
return ;
}
2017-09-17 19:43:57 +02:00
if ( things . length == 0 ) {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Vous devez saisir au moins un nom d'élément !" , "error" , 5 ) ;
2017-09-17 12:18:45 +02:00
return ;
}
2017-09-17 19:43:57 +02:00
for ( idx in things ) {
cat . newThing ( things [ idx ] [ 'label' ] , things [ idx ] [ 'nb' ] ) ;
2017-09-17 12:18:45 +02:00
}
2016-09-06 01:12:46 +02:00
scases . save ( ) ;
show _scase ( scase , cat . name ) ;
2024-09-21 17:07:52 +02:00
auto _sync _local _data ( ) ;
2016-09-06 01:12:46 +02:00
}
}
modal . modal ( 'hide' ) ;
}
on _show _add _thing _modal = function ( ) {
2017-09-17 19:43:57 +02:00
$ ( '.add_thing_other' ) . remove ( ) ;
2017-09-17 12:18:45 +02:00
$ ( 'input.add_thing_label' ) . val ( '' ) ;
2017-09-17 19:43:57 +02:00
$ ( 'input.add_thing_nb' ) . val ( '' ) ;
2017-09-17 12:18:45 +02:00
$ ( 'input.add_thing_label' ) . first ( ) . focus ( ) ;
2016-09-06 01:12:46 +02:00
}
on _close _add _thing _modal = function ( ) {
$ ( '#add_thing_modal form' ) [ 0 ] . reset ( ) ;
}
2017-09-17 12:18:45 +02:00
on _add _thing _label _focus = function ( event ) {
if ( $ ( 'input.add_thing_label' ) . last ( ) [ 0 ] == event . target ) {
2017-09-17 19:43:57 +02:00
var new _input _group = $ ( '<div class="form-group add_thing_other"></div>' ) ;
var new _input _label = $ ( '<input type="text" class="form-control add_thing_label" placeholder="Encore un ?"/>' ) ;
var new _input _nb = $ ( '<input type="number" class="form-control add_thing_nb" placeholder="Nb"/>' ) ;
new _input _group . append ( new _input _label ) ;
new _input _group . append ( ' ' ) ;
new _input _group . append ( new _input _nb ) ;
new _input _label . bind ( 'focus' , on _add _thing _label _focus ) ;
2017-09-17 12:18:45 +02:00
$ ( event . target ) . parent ( ) . after ( new _input _group ) ;
}
}
2016-09-06 01:12:46 +02:00
/ * * * * * * * * * * * * * * * * * * * * * * *
2017-09-17 19:43:57 +02:00
* Edit thing
2016-09-06 01:12:46 +02:00
* * * * * * * * * * * * * * * * * * * * * * /
2017-09-17 19:43:57 +02:00
on _edit _thing _btn _click = function ( event ) {
2016-09-06 01:12:46 +02:00
navbar _collapse _hide ( ) ;
2017-09-17 19:43:57 +02:00
$ ( '#edit_thing_modal' ) . data ( 'cat' , event . data . cat . name ) ;
$ ( '#edit_thing_modal' ) . data ( 'thing' , event . data . thing . label ) ;
$ ( '#edit_thing_label' ) . val ( event . data . thing . label ) ;
$ ( '#edit_thing_nb' ) . val ( event . data . thing . nb ) ;
$ ( '#edit_thing_modal' ) . modal ( 'show' ) ;
2016-09-06 01:12:46 +02:00
}
2017-09-17 19:43:57 +02:00
on _valid _edit _thing _modal = function ( e ) {
2016-09-06 01:12:46 +02:00
e . preventDefault ( ) ;
2017-09-17 19:43:57 +02:00
var label = $ ( '#edit_thing_label' ) . val ( ) ;
2016-09-06 01:12:46 +02:00
if ( label == '' ) {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Vous devez saisir le nouveau nom de l'élément !" , "error" , 5 ) ;
2016-09-06 01:12:46 +02:00
return ;
}
2017-09-17 19:43:57 +02:00
var nb = parseInt ( $ ( '#edit_thing_nb' ) . val ( ) ) ;
if ( ! nb || nb == 0 ) {
nb = 1 ;
}
2016-09-06 01:12:46 +02:00
var scase = scases . byName ( $ ( '#cats' ) . data ( 'scase' ) ) ;
if ( scase ) {
2017-09-17 19:43:57 +02:00
var cat = scase . cats . byName ( $ ( '#edit_thing_modal' ) . data ( 'cat' ) ) ;
2016-09-06 01:12:46 +02:00
if ( cat ) {
2017-09-17 19:43:57 +02:00
if ( label != $ ( '#edit_thing_modal' ) . data ( 'thing' ) ) {
var namesake = cat . byLabel ( label ) ;
if ( namesake ) {
if ( namesake . removed ) {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Un élément de ce nom existe dans la corbeille !" , "error" , 5 ) ;
2017-09-17 19:43:57 +02:00
}
else {
2024-09-13 17:43:01 +02:00
alertify . notify ( "Un élément de ce nom existe déjà !" , "error" , 5 ) ;
2017-09-17 19:43:57 +02:00
}
return ;
2017-09-17 18:55:25 +02:00
}
2017-09-17 19:43:57 +02:00
var thing = cat . renameThing ( $ ( '#edit_thing_modal' ) . data ( 'thing' ) , label ) ;
}
else {
var thing = cat . byLabel ( label ) ;
2016-09-06 01:12:46 +02:00
}
if ( thing ) {
2017-09-17 19:43:57 +02:00
thing . setNb ( nb ) ;
2016-09-06 01:12:46 +02:00
scases . save ( ) ;
show _scase ( scase , cat . name ) ;
2024-09-21 17:07:52 +02:00
auto _sync _local _data ( ) ;
2016-09-06 01:12:46 +02:00
}
}
}
2017-09-17 19:43:57 +02:00
$ ( '#edit_thing_modal' ) . modal ( 'hide' ) ;
2016-09-06 01:12:46 +02:00
}
2017-09-17 19:43:57 +02:00
on _show _edit _thing _modal = function ( ) {
$ ( '#edit_thing_label' ) . focus ( ) ;
2016-09-06 01:12:46 +02:00
}
2017-09-17 19:43:57 +02:00
on _close _edit _thing _modal = function ( ) {
$ ( '#edit_thing_modal form' ) [ 0 ] . reset ( ) ;
2016-09-06 01:12:46 +02:00
}
/ * * * * * * * * * * * * * * * * * * * * * * *
* Delete thing
* * * * * * * * * * * * * * * * * * * * * * /
on _delete _thing _btn _click = function ( event ) {
navbar _collapse _hide ( ) ;
var scase = scases . byName ( $ ( '#cats' ) . data ( 'scase' ) ) ;
if ( scase ) {
var cat = scase . cats . byName ( event . data . cat . name ) ;
if ( cat ) {
var thing = event . data . thing . label ;
2024-09-13 17:43:01 +02:00
alertify . confirm (
` Suppression de l'élément ${ thing } ` ,
` Voulez-vous vraiment supprimer l'élément ${ thing } ? ` ,
function ( data ) {
cat . removeThing ( thing ) ;
scases . save ( ) ;
show _scase ( scase , cat . name ) ;
2024-09-21 17:07:52 +02:00
auto _sync _local _data ( ) ;
2024-09-13 17:43:01 +02:00
} ,
null
) ;
2016-09-06 01:12:46 +02:00
}
}
}
2017-09-17 18:55:25 +02:00
on _restore _thing _btn _click = function ( event ) {
navbar _collapse _hide ( ) ;
var scase = scases . byName ( $ ( '#cats' ) . data ( 'scase' ) ) ;
if ( scase ) {
var cat = scase . cats . byName ( event . data . cat . name ) ;
if ( cat ) {
var thing = event . data . thing . label ;
2024-09-13 17:43:01 +02:00
alertify . confirm (
` Restauration de l'élément ${ thing } ` ,
` Voulez-vous vraiment restaurer l'élément ${ thing } ? ` ,
function ( data ) {
cat . restoreThing ( thing ) ;
scases . save ( ) ;
show _scase ( scase , cat . name ) ;
2024-09-21 17:07:52 +02:00
auto _sync _local _data ( ) ;
2024-09-13 17:43:01 +02:00
} ,
null
) ;
2017-09-17 18:55:25 +02:00
}
}
}
2016-09-06 01:12:46 +02:00
/ * * * * * * * * * * * * * * * * * * * *
* Show one scase
* * * * * * * * * * * * * * * * * * * /
2024-09-21 17:07:52 +02:00
show _cat = function ( scase , cat , displayed ) {
2016-09-06 01:12:46 +02:00
var panel = $ ( '<div class="panel panel-default"></div>' ) ;
var panel _heading = $ ( '<div class="panel-heading" role="tab"></div>' ) ;
var panel _title = $ ( '<h4 class="panel-title">' + cat . name + ' </h4>' ) ;
2024-09-21 17:07:52 +02:00
panel _title . data ( 'scase' , scase ) ;
panel _title . data ( 'cat' , cat ) ;
2016-09-06 01:12:46 +02:00
panel _title . bind ( 'click' , on _title _click ) ;
2016-09-08 01:23:15 +02:00
var stats = cat . stats ( ) ;
2016-09-06 01:12:46 +02:00
var tag = $ ( '<span class="count-tag pull-right"></span>' ) ;
2016-09-08 01:23:15 +02:00
if ( stats . things == stats . done ) {
2016-09-06 01:12:46 +02:00
tag . append ( $ ( '<span class="label label-success"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></span>' ) ) ;
}
else {
2016-09-08 01:23:15 +02:00
tag . append ( $ ( '<span class="badge">' + stats . done + ' / ' + stats . things + '</span>' ) ) ;
2016-09-06 01:12:46 +02:00
}
var delete _btn = $ ( '<button class="btn btn-default btn-xs pull-right"><span class="glyphicon glyphicon-trash"></button>' ) ;
delete _btn . bind ( 'click' , { 'cat' : cat } , on _delete _cat _btn _click ) ;
tag . append ( delete _btn ) ;
var rename _btn = $ ( '<button class="btn btn-default btn-xs pull-right"><span class="glyphicon glyphicon-edit"></button>' ) ;
rename _btn . bind ( 'click' , { 'cat' : cat } , on _rename _cat _btn _click ) ;
tag . append ( rename _btn ) ;
panel _title . append ( tag ) ;
2024-09-21 17:07:52 +02:00
2016-09-06 01:12:46 +02:00
panel _heading . append ( panel _title ) ;
panel . append ( panel _heading ) ;
var panel _collapse = $ ( '<div class="panel-collapse collapse" role="tabpanel"></div>' ) ;
if ( displayed ) {
panel _collapse . addClass ( 'in' ) ;
}
var ul = $ ( '<ul class="list-group" data-cat="' + cat . name + '"></ul>' ) ;
for ( idx in cat . things ) {
2017-09-17 18:55:25 +02:00
if ( cat . things [ idx ] . removed ) {
continue ;
}
2017-09-17 23:37:44 +02:00
var li = $ ( '<li class="list-group-item checkable" data-label="' + cat . things [ idx ] . label + '">' + cat . things [ idx ] . label + '</li>' ) ;
2017-09-17 19:43:57 +02:00
if ( cat . things [ idx ] . nb > 1 ) {
li . append ( ' <em>(' + cat . things [ idx ] . nb + ')</em>' ) ;
}
2016-09-06 01:12:46 +02:00
if ( cat . things [ idx ] . checked ) {
2017-09-17 23:37:44 +02:00
li . addClass ( 'checked' ) ;
2016-09-06 01:12:46 +02:00
}
li . bind ( 'click' , on _li _click ) ;
var li _actions = $ ( '<span class="actions pull-right"></span>' ) ;
var delete _el _btn = $ ( '<button class="btn btn-default btn-xs pull-right"><span class="glyphicon glyphicon-trash"></button>' ) ;
delete _el _btn . bind ( 'click' , { 'cat' : cat , 'thing' : cat . things [ idx ] } , on _delete _thing _btn _click ) ;
li _actions . append ( delete _el _btn ) ;
2017-09-17 19:43:57 +02:00
var edit _el _btn = $ ( '<button class="btn btn-default btn-xs pull-right"><span class="glyphicon glyphicon-edit"></button>' ) ;
edit _el _btn . bind ( 'click' , { 'cat' : cat , 'thing' : cat . things [ idx ] } , on _edit _thing _btn _click ) ;
li _actions . append ( edit _el _btn ) ;
2016-09-06 01:12:46 +02:00
li . append ( li _actions ) ;
ul . append ( li ) ;
}
var li = $ ( '<li class="list-group-item"><span class="glyphicon glyphicon-plus-sign"></span> Ajouter un élément</li>' ) ;
li . bind ( 'click' , on _li _add _click ) ;
ul . append ( li ) ;
panel _collapse . append ( ul ) ;
panel . append ( panel _collapse ) ;
$ ( '#cats' ) . append ( panel ) ;
}
show _scase = function ( scase , display _cat ) {
2016-09-07 19:06:36 +02:00
clear _page ( '<h3><span class="glyphicon glyphicon-briefcase" aria-hidden="true"></span> ' + scase . name + '</h3><div class="panel-group" id="cats" role="tablist" aria-multiselectable="true" data-scase="' + scase . name + '"></div>' ) ;
2016-09-06 01:12:46 +02:00
scase . cats . each ( function ( idx , cat ) {
2017-09-17 18:55:25 +02:00
if ( cat . removed ) {
return ;
}
2024-09-21 17:07:52 +02:00
show _cat ( scase , cat , ( cat . name == display _cat ) ) ;
2016-09-06 01:12:46 +02:00
} ) ;
2024-09-21 17:07:52 +02:00
set _location ( scase . name , display _cat ) ;
2016-09-06 01:12:46 +02:00
show _menu ( 'scase' ) ;
}
on _back _to _scases _btn _click = function ( e ) {
e . preventDefault ( ) ;
navbar _collapse _hide ( ) ;
show _scases ( ) ;
}
2017-09-17 18:55:25 +02:00
/ * * * * * * * * * * * * * * * * * * * *
* Show scase trash
* * * * * * * * * * * * * * * * * * * /
show _scase _trash = function ( scase , display _cat ) {
clear _page ( '<h3><span class="glyphicon glyphicon-trash" aria-hidden="true"></span>' + scase . name + ' : Corbeille <button class="btn btn-default btn-xs" id="back_btn"><span class="glyphicon glyphicon-arrow-left"></button></h3><div class="panel-group" id="cats" role="tablist" aria-multiselectable="true" data-scase="' + scase . name + '"></div>' ) ;
$ ( '#content h3 #back_btn' ) . bind ( 'click' , { 'scase' : scase } , function ( event ) {
show _scase ( event . data . scase ) ;
} ) ;
scase . cats . each ( function ( idx , cat ) {
2024-09-21 17:07:52 +02:00
show _cat _trash ( scase , cat , ( cat . name == display _cat ) ) ;
2017-09-17 18:55:25 +02:00
} ) ;
if ( $ ( '#cats .panel' ) . length == 0 ) {
$ ( '#content' ) . append ( '<p class="center">La corbeille est vide.</p>' ) ;
}
2024-09-21 17:07:52 +02:00
set _location ( scase . name , display _cat , 'trash' ) ;
2017-09-17 18:55:25 +02:00
show _menu ( 'scase' ) ;
}
2024-09-21 17:07:52 +02:00
show _cat _trash = function ( scase , cat , displayed ) {
2017-09-17 18:55:25 +02:00
var panel = $ ( '<div class="panel panel-default"></div>' ) ;
var panel _heading = $ ( '<div class="panel-heading" role="tab"></div>' ) ;
var panel _title = $ ( '<h4 class="panel-title">' + cat . name + ' </h4>' ) ;
2024-09-21 17:07:52 +02:00
panel _title . data ( 'scase' , scase ) ;
panel _title . data ( 'cat' , cat ) ;
panel _title . data ( 'trash' , true ) ;
2017-09-17 18:55:25 +02:00
var tag = $ ( '<span class="count-tag pull-right"></span>' ) ;
panel _title . append ( tag ) ;
panel _heading . append ( panel _title ) ;
panel . append ( panel _heading ) ;
if ( cat . removed ) {
var stats = cat . stats ( ) ;
tag . append ( $ ( '<span class="badge">' + stats . things + '</span>' ) ) ;
var restore _btn = $ ( '<button class="btn btn-default btn-xs pull-right"><span class="glyphicon glyphicon-ok"></button>' ) ;
restore _btn . bind ( 'click' , { 'cat' : cat } , on _restore _cat _btn _click ) ;
tag . append ( restore _btn ) ;
}
else {
var deleted _things = [ ] ;
for ( idx in cat . things ) {
if ( cat . things [ idx ] . removed ) {
deleted _things . push ( cat . things [ idx ] ) ;
}
}
if ( deleted _things . length == 0 ) {
return true ;
}
panel _title . bind ( 'click' , on _title _click ) ;
tag . append ( $ ( '<span class="badge">' + deleted _things . length + '</span>' ) ) ;
var panel _collapse = $ ( '<div class="panel-collapse collapse" role="tabpanel"></div>' ) ;
if ( displayed ) {
panel _collapse . addClass ( 'in' ) ;
}
var ul = $ ( '<ul class="list-group" data-cat="' + cat . name + '"></ul>' ) ;
for ( idx in deleted _things ) {
var li = $ ( '<li class="list-group-item" data-label="' + deleted _things [ idx ] . label + '">' + deleted _things [ idx ] . label + '</li>' ) ;
var li _actions = $ ( '<span class="actions pull-right"></span>' ) ;
var restore _el _btn = $ ( '<button class="btn btn-default btn-xs pull-right"><span class="glyphicon glyphicon-ok"></button>' ) ;
restore _el _btn . bind ( 'click' , { 'cat' : cat , 'thing' : deleted _things [ idx ] } , on _restore _thing _btn _click ) ;
li _actions . append ( restore _el _btn ) ;
li . append ( li _actions ) ;
ul . append ( li ) ;
}
panel _collapse . append ( ul ) ;
panel . append ( panel _collapse ) ;
}
$ ( '#cats' ) . append ( panel ) ;
}
on _back _to _scase _btn _click = function ( e ) {
e . preventDefault ( ) ;
navbar _collapse _hide ( ) ;
show _scase ( e . data . scase ) ;
}
2016-09-06 01:12:46 +02:00
/ * * * * * * * * * * * * * * * * * * * *
* Show scases
* * * * * * * * * * * * * * * * * * * /
show _scases = function ( ) {
2016-09-07 19:06:11 +02:00
clear _page ( '<h3>Vos valises</h3><ul class="list-group" id="scases"></ul>' ) ;
2016-09-06 01:12:46 +02:00
scases . each ( function ( idx , scase ) {
2017-09-17 18:55:25 +02:00
if ( scase . removed ) {
return ;
}
2016-09-07 19:06:11 +02:00
var stats = scase . stats ( ) ;
var tag = '<span class="count-tag pull-right">' ;
if ( stats . things == stats . done ) {
tag += '<span class="label label-success"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></span>' ;
}
else {
tag += '<span class="badge">' + stats . done + ' / ' + stats . things + '</span>' ;
}
tag += '</span>' ;
var li = $ ( '<li class="list-group-item" data-name="' + scase . name + '"><span class="scase-name"><span class="glyphicon glyphicon-briefcase" aria-hidden="true"></span> ' + scase . name + '</span>' + tag + '</li>' ) ;
2016-09-06 01:12:46 +02:00
li . bind ( 'click' , on _scase _click ) ;
$ ( '#scases' ) . append ( li ) ;
} ) ;
2024-09-21 17:07:52 +02:00
set _location ( ) ;
2016-09-06 01:12:46 +02:00
show _menu ( 'scases' ) ;
}
on _scase _click = function ( event ) {
var li = $ ( this ) ;
var scase = scases . byName ( li . data ( 'name' ) ) ;
show _scase ( scase ) ;
}
2017-09-17 18:55:25 +02:00
/ * * * * * * * * * * * * * * * * * * * *
* Show scases trash
* * * * * * * * * * * * * * * * * * * /
show _scases _trash = function ( ) {
clear _page ( '<h3>Corbeille <button class="btn btn-default btn-xs" id="back_btn"><span class="glyphicon glyphicon-arrow-left"></button></h3><ul class="list-group" id="scases"></ul>' ) ;
$ ( '#content h3 #back_btn' ) . bind ( 'click' , function ( event ) {
show _scases ( ) ;
} ) ;
scases . each ( function ( idx , scase ) {
if ( ! scase . removed ) {
return ;
}
var stats = scase . stats ( ) ;
var tags = $ ( '<span class="count-tag pull-right"></span>' ) ;
tags . append ( '<span class="badge">' + stats . things + '</span>' ) ;
var restore _btn = $ ( '<button class="btn btn-default btn-xs pull-right"><span class="glyphicon glyphicon-ok"></button>' ) ;
restore _btn . bind ( 'click' , { 'scase' : scase } , on _restore _scase _btn _click ) ;
tags . append ( restore _btn ) ;
var li = $ ( '<li class="list-group-item" data-name="' + scase . name + '"><span class="scase-name"><span class="glyphicon glyphicon-briefcase" aria-hidden="true"></span> ' + scase . name + '</span></li>' ) ;
li . append ( tags ) ;
$ ( '#scases' ) . append ( li ) ;
} ) ;
if ( $ ( '#scases li' ) . length == 0 ) {
$ ( '#content' ) . append ( '<p class="center">Aucune valise dans la corbeille.</p>' ) ;
}
show _menu ( 'scases' ) ;
}
2017-09-17 20:04:30 +02:00
on _scases _trash _btn = function ( e ) {
e . preventDefault ( ) ;
navbar _collapse _hide ( ) ;
show _scases _trash ( ) ;
}
2016-09-06 01:12:46 +02:00
clear _page = function ( new _content ) {
if ( new _content ) {
$ ( '#content' ) . html ( new _content ) ;
}
else {
$ ( '#content' ) . html ( '' ) ;
}
}
/ * * * * * * * * * * * * * * * * * * * * * * * *
* Show menu
* * * * * * * * * * * * * * * * * * * * * * * /
show _menu = function ( menu ) {
$ ( '.menu' ) . css ( 'display' , 'none' ) ;
$ ( '.menu-' + menu ) . css ( 'display' , 'block' ) ;
}
/ * * * * * * * * * * * * * * * * * * *
* pleaseWaitDialog
* * * * * * * * * * * * * * * * * * * /
pleaseWaitShow = function ( ) {
$ ( '#please_wait_modal' ) . modal ( 'show' ) ;
}
pleaseWaitHide = function ( ) {
$ ( '#please_wait_modal' ) . modal ( 'hide' ) ;
}
/ * * * * * * * * * * * * * * * *
* Nav bars
* * * * * * * * * * * * * * * * /
navbar _collapse _hide = function ( ) {
if ( $ ( '#navbar-top-collapse' ) . hasClass ( 'in' ) ) {
$ ( '#navbar-top-collapse' ) . collapse ( 'hide' ) ;
}
}
/ * * * * * * * * * * * * * * * * * * * *
* Clear local data
* * * * * * * * * * * * * * * * * * * * /
clear _local _data = function ( ) {
navbar _collapse _hide ( ) ;
2024-09-13 17:43:01 +02:00
alertify . confirm (
"Suppression de toutes les données locales" ,
"Etes-vous sûre de vouloir supprimer les données locales (action irréversible) ?" ,
on _confirm _clear _local _data ,
null
) ;
2016-09-06 01:12:46 +02:00
}
on _confirm _clear _local _data = function ( data ) {
delete localStorage . scases ;
location . reload ( ) ;
}
2024-09-21 17:07:52 +02:00
/ * * * * * * * * * * * * * * * * * * * *
* Clear local data
* * * * * * * * * * * * * * * * * * * * /
load _example _data = function ( ) {
navbar _collapse _hide ( ) ;
alertify . confirm (
"Chargement des données d'exemple" ,
"Etes-vous sûre de vouloir charger les données d'exemple à la place de vos propres données (action irréversible) ?" ,
function ( ) {
delete localStorage . scases ;
scases = new SCaseList ( ) ;
scases . importExampleData ( ) ;
scases . save ( ) ;
show _scases ( ) ;
} ,
null
) ;
}
2017-10-25 01:31:23 +02:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Import / Export local data
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
export _local _data = function ( ) {
navbar _collapse _hide ( ) ;
$ ( '#export_local_data' ) . attr ( 'href' , 'data:application/json;base64,' + btoa ( JSON . stringify ( scases . export ( ) ) ) ) ;
}
import _local _data = function ( ) {
navbar _collapse _hide ( ) ;
var input = $ ( '<input type="file" accept="application/json">' ) ;
input . css ( 'display' , 'none' ) ;
input . bind ( 'change' , { 'input' : input } , function ( e ) {
pleaseWaitShow ( ) ;
var input = e . data . input ;
var file = input . prop ( 'files' ) [ 0 ] ;
if ( file ) {
var reader = new FileReader ( ) ;
2024-09-21 17:07:52 +02:00
$ ( reader ) . bind ( 'load' , function ( e ) {
if (
$ . type ( e . target . result ) != 'string'
|| ! e . target . result . startsWith ( 'data:application/json;base64,' )
) {
pleaseWaitHide ( ) ;
alertify . notify ( 'Fichier.' , "error" , 5 ) ;
return ;
}
try {
json _data = atob ( e . target . result . replace ( 'data:application/json;base64,' , '' ) ) ;
data = JSON . parse ( json _data ) ;
}
catch ( e ) {
pleaseWaitHide ( ) ;
alertify . notify ( 'Impossible de décodé le fichier.' , "error" , 5 ) ;
return ;
}
pleaseWaitHide ( ) ;
alertify . confirm (
"Importation depuis un fichier" ,
"Etes-vous sûre de vouloir écraser vos données locales par celle issues de ce fichier ?" ,
function ( ) {
var backData = scases . export ( ) ;
scases = new SCaseList ( ) ;
if ( scases . loadFromJsonData ( data ) ) {
alertify . notify ( "Le fichier a bien été importé." , "success" , 3 ) ;
2017-10-25 01:31:23 +02:00
}
2024-09-21 17:07:52 +02:00
else {
alertify . notify ( "Une erreur est survenue en chargeant ce fichier. Restauration des données précédentes..." , "error" , 5 ) ;
if ( scases . loadFromJsonData ( backData ) ) {
alertify . notify ( "Les données précédentes ont bien été restaurées." , "success" , 5 ) ;
}
else {
alertify . notify ( "Une erreur est survenue en restaurant les données précédentes." , "error" , 5 ) ;
}
2017-10-25 01:31:23 +02:00
}
2024-09-21 17:07:52 +02:00
show _scases ( ) ;
} ,
null
) ;
2017-10-25 01:31:23 +02:00
} ) ;
reader . readAsDataURL ( file ) ;
}
} ) ;
$ ( 'body' ) . append ( input ) ;
input [ 0 ] . click ( ) ;
}
2024-09-21 17:07:52 +02:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Authentication
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
show _user = function ( ) {
if ( user . connected ( ) ) {
$ ( '#login' ) . parent ( ) . css ( 'display' , 'none' ) ;
$ ( '#logout' ) . parent ( ) . css ( 'display' , 'block' ) ;
$ ( '#username' ) . html ( user . name ) ;
}
else {
$ ( '#login' ) . parent ( ) . css ( 'display' , 'block' ) ;
$ ( '#logout' ) . parent ( ) . css ( 'display' , 'none' ) ;
$ ( '#username' ) . html ( "" ) ;
}
}
on _login _button _click = function ( ) {
if ( user . connected ( ) ) return ;
navbar _collapse _hide ( ) ;
$ ( '#login_modal' ) . modal ( 'show' ) ;
}
on _valid _login _modal = function ( e ) {
e . preventDefault ( ) ;
var username = $ ( '#login_username' ) . val ( ) ;
var password = $ ( '#login_password' ) . val ( ) ;
if ( ! username || ! password ) {
alertify . notify ( "Vous devez saisir votre nom d'utilisateur et votre mot de passe !" , "error" , 5 ) ;
return ;
}
$ . post (
'login' ,
{
username : username ,
password : password ,
}
) . done ( function ( data ) {
if ( data . username && data . token ) {
user . token = data . token ;
user . username = data . username ;
user . name = data . name ? data . name : username ;
user . save ( ) ;
$ ( '#login_modal' ) . modal ( 'hide' ) ;
show _user ( ) ;
alertify . notify ( "Connecté." , "success" , 3 ) ;
propose _sync _local _data ( ) ;
}
else {
alertify . notify ( "Nom d'utilisateur ou mot de passe." , "error" , 5 ) ;
}
} ) . fail ( function ( ) {
alertify . notify ( 'Une erreur est survenue en vous identifiant. Merci de réessayer ultèrieument.' , "error" , 5 ) ;
} ) ;
}
on _show _login _modal = function ( ) {
$ ( '#login_username' ) . val ( user . username ? user . username : "" ) ;
if ( user . username )
$ ( '#login_password' ) . focus ( ) ;
else
$ ( '#login_username' ) . focus ( ) ;
}
on _close _login _modal = function ( ) {
$ ( '#login_modal form' ) [ 0 ] . reset ( ) ;
}
on _logout _button _click = function ( ) {
if ( ! user . connected ( ) ) return ;
navbar _collapse _hide ( ) ;
alertify . confirm (
"Déconnexion" ,
"Voulez-vous vraiment vous déconnecter ?" ,
function ( data ) {
$ . post (
'logout' ,
{
username : user . username ,
token : user . token ,
}
) . done ( function ( data ) {
if ( data . success ) {
user . reset ( ) ;
user . save ( ) ;
show _user ( ) ;
alertify . notify ( "Déconnecté." , "success" , 3 ) ;
}
else {
alertify . notify ( "Une erreur est survenue en vous déconnectant. Merci de réessayer ultèrieument." , "error" , 5 ) ;
}
} ) . fail ( function ( ) {
alertify . notify ( 'Une erreur est survenue en vous déconnectant. Merci de réessayer ultèrieument.' , "error" , 5 ) ;
} ) ;
} ,
null
) ;
}
is _connected = function ( ) {
return user && user . token ;
}
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Sync local data with server
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
sync _local _data = function ( callback ) {
if ( ! is _connected ( ) ) {
if ( callback ) callback ( false ) ;
return ;
}
$ . post (
'sync' ,
{
username : user . username ,
token : user . token ,
data : JSON . stringify ( scases . export ( ) ) ,
}
) . done ( function ( data ) {
if ( data . scases ) {
var backData = scases . export ( ) ;
scases = new SCaseList ( ) ;
if ( scases . loadFromJsonData ( data ) ) {
scases . save ( ) ;
alertify . notify ( "Données synchronisées." , "success" , 3 ) ;
if ( callback ) callback ( true ) ;
}
else {
alertify . notify ( "Une erreur est survenue en chargeant les données issues du serveur. Restauration des données précédentes..." , "error" , 5 ) ;
if ( scases . loadFromJsonData ( backData ) ) {
alertify . notify ( "Les données précédentes ont bien été restaurées." , "success" , 5 ) ;
}
else {
alertify . notify ( "Une erreur est survenue en restaurant les données précédentes." , "error" , 5 ) ;
}
if ( callback ) callback ( false ) ;
}
}
else {
alertify . notify ( "Une erreur est survenue en synchronisant vos données. Merci de réessayer ultèrieument." , "error" , 5 ) ;
if ( callback ) callback ( false ) ;
}
} ) . fail ( function ( xhr , status , error ) {
if ( xhr . status == 401 ) {
user . token = null ;
user . save ( ) ;
show _user ( ) ;
alertify . notify ( "Votre session semble avoir expirée, merci de vous réauthentifier." , "error" , 8 ) ;
$ ( '#login_modal' ) . modal ( 'show' ) ;
}
else {
alertify . notify ( "Une erreur est survenue en synchronisant vos données. Merci de réessayer ultèrieument." , "error" , 5 ) ;
}
if ( callback ) callback ( false ) ;
} ) ;
}
_auto _sync _timeout = false ;
auto _sync _local _data = function ( ) {
if ( _auto _sync _timeout ) clearTimeout ( _auto _sync _timeout ) ;
if ( ! is _connected ( ) || ! navigator . onLine ) return ;
_auto _sync _timeout = setTimeout ( sync _local _data , 3000 ) ;
}
on _sync _local _data _btn _click = function ( ) {
navbar _collapse _hide ( ) ;
if ( ! is _connected ( ) ) {
alertify . notify ( "Vous devez vous connecter avant de pouvoir synchroniser vos données." , "error" , 5 ) ;
$ ( '#login_modal' ) . modal ( 'show' ) ;
return ;
}
pleaseWaitShow ( ) ;
sync _local _data ( function ( success ) {
pleaseWaitHide ( ) ;
} ) ;
}
propose _sync _local _data = function ( ) {
alertify . confirm (
"Synchronisation de vos valises depuis le serveur" ,
"Voulez-vous synchroniser vos valises depuis le serveur ?" ,
function ( ) {
pleaseWaitShow ( ) ;
sync _local _data (
function ( success ) {
pleaseWaitHide ( ) ;
if ( ! success ) return ;
if ( scases . count ( ) == 0 )
propose _example _data ( ) ;
else
refresh _location ( ) ;
}
) ;
} ,
refresh _location
) ;
}
/ * * * * * * * * * * * * * * * * * * * * * * *
* Manage location hash
* * * * * * * * * * * * * * * * * * * * * * * /
set _location = function ( scase , cat , detail ) {
console . log ( ` set_location( ${ scase } , ${ cat } , ${ detail } ) ` ) ;
var parts = [ ] ;
if ( scase ) parts [ 0 ] = scase ;
if ( cat ) parts [ 1 ] = cat ;
if ( detail ) parts [ 2 ] = detail ;
location . hash = parts . join ( "|" ) ;
}
parse _location = function ( value ) {
value = typeof value == "undefined" ? location . hash : value ;
console . log ( ` parse_location( ${ value } ) ` ) ;
parts = (
typeof value == "undefined" ? location . hash : value
) . split ( "|" ) ;
return {
scase : parts [ 0 ] ? decodeURI ( parts [ 0 ] . substring ( 1 ) ) : null ,
cat : parts [ 0 ] && parts [ 1 ] ? decodeURI ( parts [ 1 ] ) : null ,
detail : parts [ 2 ] ? decodeURI ( parts [ 2 ] ) : null ,
} ;
}
refresh _location = function ( ) {
var info = parse _location ( ) ;
console . log ( ` refresh_location( ${ info . scase } , ${ info . cat } , ${ info . detail } ) ` ) ;
var scase = info . scase ? scases . byName ( info . scase ) : null ;
if ( ! scase )
show _scases ( ) ;
else if ( info . detail == 'trash' )
show _scase _trash ( scase , info . cat ) ;
else
show _scase ( scase , info . cat ) ;
}
/ * * * * * * * * * * * * * * * * * * * * * * *
* Welcome modal
* * * * * * * * * * * * * * * * * * * * * * * /
on _welcome _connect _click = function ( ) {
$ ( '#welcome_modal' ) . modal ( 'hide' ) ;
$ ( '#login_modal' ) . modal ( 'show' ) ;
}
on _welcome _annonymous _click = function ( ) {
$ ( '#welcome_modal' ) . modal ( 'hide' ) ;
if ( scases . count ( ) == 0 ) propose _example _data ( ) ;
}
propose _example _data = function ( ) {
alertify . confirm (
"Un exemple de valise ?" ,
"Souhaitez-vous charger un exemple de valise pour commencer ?" ,
function ( ) {
scases . importExampleData ( ) ;
scases . save ( ) ;
refresh _location ( ) ;
} ,
null ,
) ;
}
2016-09-06 01:12:46 +02:00
/ * * * * * * * * * * * * * * * * * * * * *
* Activate
* * * * * * * * * * * * * * * * * * * * * /
$ ( document ) . ready ( function ( ) {
2024-09-21 17:07:52 +02:00
if ( typeof ( localStorage ) === "undefined" ) {
alertify . notify (
'Votre navigateur internet ne support pas le stockage de données locale. Vous ne pouvez donc malheureusment pas utiliser cette application.' , "error" , 5 ) ;
2016-09-06 01:12:46 +02:00
return ;
}
2024-09-21 17:07:52 +02:00
pleaseWaitShow ( ) ;
2016-09-06 01:12:46 +02:00
$ ( '#clear_local_data' ) . bind ( 'click' , clear _local _data ) ;
2024-09-21 17:07:52 +02:00
$ ( '#load_example_data' ) . bind ( 'click' , load _example _data ) ;
2017-10-25 01:31:23 +02:00
$ ( '#import_local_data' ) . bind ( 'click' , import _local _data ) ;
$ ( '#export_local_data' ) . bind ( 'click' , export _local _data ) ;
2024-09-21 17:07:52 +02:00
$ ( '#sync_local_data' ) . bind ( 'click' , on _sync _local _data _btn _click ) ;
2016-09-06 01:12:46 +02:00
$ ( '#add_scase_btn' ) . bind ( 'click' , on _add _scase _btn _click ) ;
$ ( '#add_scase_submit' ) . bind ( 'click' , on _valid _add _scase _modal ) ;
$ ( "#add_scase_modal" ) . on ( 'shown.bs.modal' , on _show _add _scase _modal ) ;
$ ( "#add_scase_modal" ) . on ( 'hidden.bs.modal' , on _close _add _scase _modal ) ;
$ ( "#add_scase_modal form" ) . bind ( 'submit' , on _valid _add _scase _modal ) ;
2017-09-17 20:04:30 +02:00
$ ( '#scases_trash_btn' ) . bind ( 'click' , on _scases _trash _btn ) ;
2017-09-17 18:55:25 +02:00
2016-09-06 01:12:46 +02:00
$ ( '#rename_scase_btn' ) . bind ( 'click' , on _rename _scase _btn _click ) ;
$ ( '#rename_scase_submit' ) . bind ( 'click' , on _valid _rename _scase _modal ) ;
$ ( "#rename_scase_modal" ) . on ( 'shown.bs.modal' , on _show _rename _scase _modal ) ;
$ ( "#rename_scase_modal" ) . on ( 'hidden.bs.modal' , on _close _rename _scase _modal ) ;
$ ( "#rename_scase_modal form" ) . bind ( 'submit' , on _valid _rename _scase _modal ) ;
$ ( '#copy_scase_btn' ) . bind ( 'click' , on _copy _scase _btn _click ) ;
$ ( '#copy_scase_submit' ) . bind ( 'click' , on _valid _copy _scase _modal ) ;
$ ( "#copy_scase_modal" ) . on ( 'shown.bs.modal' , on _show _copy _scase _modal ) ;
$ ( "#copy_scase_modal" ) . on ( 'hidden.bs.modal' , on _close _copy _scase _modal ) ;
$ ( "#copy_scase_modal form" ) . bind ( 'submit' , on _valid _copy _scase _modal ) ;
2016-09-07 00:30:25 +02:00
$ ( '#reset_scase_btn' ) . bind ( 'click' , on _reset _scase _btn _click ) ;
2016-09-06 01:12:46 +02:00
$ ( '#delete_scase_btn' ) . bind ( 'click' , on _delete _scase _btn _click ) ;
2017-09-17 18:55:25 +02:00
$ ( '#scase_trash_btn' ) . bind ( 'click' , on _scase _trash _btn _click ) ;
2016-09-06 01:12:46 +02:00
$ ( '#add_cat_btn' ) . bind ( 'click' , on _add _cat _btn _click ) ;
$ ( '#add_cat_submit' ) . bind ( 'click' , on _valid _add _cat _modal ) ;
$ ( "#add_cat_modal" ) . on ( 'shown.bs.modal' , on _show _add _cat _modal ) ;
$ ( "#add_cat_modal" ) . on ( 'hidden.bs.modal' , on _close _add _cat _modal ) ;
$ ( "#add_cat_modal form" ) . bind ( 'submit' , on _valid _add _cat _modal ) ;
$ ( '#rename_cat_submit' ) . bind ( 'click' , on _valid _rename _cat _modal ) ;
$ ( "#rename_cat_modal" ) . on ( 'shown.bs.modal' , on _show _rename _cat _modal ) ;
$ ( "#rename_cat_modal" ) . on ( 'hidden.bs.modal' , on _close _rename _cat _modal ) ;
$ ( "#rename_cat_modal form" ) . bind ( 'submit' , on _valid _rename _cat _modal ) ;
$ ( '#back_to_scases' ) . bind ( 'click' , on _back _to _scases _btn _click ) ;
2024-09-21 17:07:52 +02:00
2017-09-17 12:18:45 +02:00
$ ( 'input.add_thing_label' ) . bind ( 'focus' , on _add _thing _label _focus ) ;
2016-09-06 01:12:46 +02:00
$ ( '#add_thing_submit' ) . bind ( 'click' , on _valid _add _thing _modal ) ;
$ ( "#add_thing_modal" ) . on ( 'shown.bs.modal' , on _show _add _thing _modal ) ;
$ ( "#add_thing_modal" ) . on ( 'hidden.bs.modal' , on _close _add _thing _modal ) ;
$ ( "#add_thing_modal form" ) . bind ( 'submit' , on _valid _add _thing _modal ) ;
2017-09-17 19:43:57 +02:00
$ ( '#edit_thing_submit' ) . bind ( 'click' , on _valid _edit _thing _modal ) ;
$ ( "#edit_thing_modal" ) . on ( 'shown.bs.modal' , on _show _edit _thing _modal ) ;
$ ( "#edit_thing_modal" ) . on ( 'hidden.bs.modal' , on _close _edit _thing _modal ) ;
$ ( "#edit_thing_modal form" ) . bind ( 'submit' , on _valid _edit _thing _modal ) ;
2016-09-06 01:12:46 +02:00
2024-09-21 17:07:52 +02:00
$ ( '#login' ) . bind ( 'click' , on _login _button _click ) ;
$ ( '#logout' ) . bind ( 'click' , on _logout _button _click ) ;
$ ( '#login_submit' ) . bind ( 'click' , on _valid _login _modal ) ;
$ ( "#login_modal" ) . on ( 'shown.bs.modal' , on _show _login _modal ) ;
$ ( "#login_modal" ) . on ( 'hidden.bs.modal' , on _close _login _modal ) ;
$ ( "#login_modal form" ) . bind ( 'submit' , on _valid _login _modal ) ;
$ ( '#welcome_connect' ) . bind ( 'click' , on _welcome _connect _click ) ;
$ ( '#welcome_annonymous' ) . bind ( 'click' , on _welcome _annonymous _click ) ;
2016-09-07 19:07:05 +02:00
$ ( '#app-name' ) . bind ( 'click' , show _scases ) ;
2016-09-06 01:12:46 +02:00
2024-09-21 17:07:52 +02:00
user = new User ( ) ;
user . loadFromLocalStorage ( ) ;
show _user ( ) ;
scases = new SCaseList ( ) ;
switch ( scases . loadFromLocalStorage ( ) ) {
case null :
pleaseWaitHide ( ) ;
if ( is _connected ( ) )
propose _sync _local _data ( ) ;
else
$ ( '#welcome_modal' ) . modal ( 'show' ) ;
break ;
case false :
alertify . confirm (
"Erreur en chargeant les données locales" ,
'Une erreur est survenue en chargeant les données locales. On les purges ?' ,
function ( data ) {
delete localStorage . scases ;
location . reload ( ) ;
} ,
function ( data ) {
pleaseWaitHide ( ) ;
location . reload ( ) ;
} ,
) ;
break ;
case true :
refresh _location ( ) ;
pleaseWaitHide ( ) ;
break ;
}
2016-09-06 01:12:46 +02:00
} ) ;