2018-02-20 14:53:31 +00:00
< template >
< div id = 'HEADER' >
2018-03-15 16:38:06 +00:00
<!-- Department management -- >
2018-03-17 17:26:09 +00:00
< div class = 'departments' data -unblur -department >
2018-03-15 16:38:06 +00:00
2018-03-17 17:26:09 +00:00
< div class = 'current' @ click = 'd_dialog=!d_dialog' data -unblur -department > { { get _dcurrent ( ) . label || 'département à jour' } } < / div >
< div class = 'department-dialog' v -show = ' d_dialog ' data -unblur -department >
2018-03-17 18:07:44 +00:00
< span v -for = ' d in dpts ' v -show = ' d.id ! = dep_id ' @ click = 'd_switch(d.id)' data -unblur -department > { { d . label } } < / span >
2018-03-15 16:38:06 +00:00
< / div >
< / div >
2018-03-12 18:50:56 +00:00
<!-- Version management -- >
2018-03-17 17:26:09 +00:00
< div class = 'versions' data -unblur -version >
2018-03-12 18:50:56 +00:00
2018-03-17 17:26:09 +00:00
< div class = 'current' @ click = 'v_dialog=!v_dialog' : data -id = ' get_vcurrent ( ) .id ' data -unblur -version > { { get _vcurrent ( ) . date || 'version à jour' } } < / div >
< div class = 'version-dialog' v -show = ' v_dialog ' data -unblur -version >
< span v -for = ' v in vers ' v -show = ' v.id ! = ver_id ' @ click = 'v_switch(v.id)' : data -id = ' v.id ' data -unblur -version > { { v . date || 'version à jour' } } < / span >
2018-03-17 18:07:44 +00:00
< span @ click = 'v_create()' data -unblur -version data -id = ' -1 ' > Nouvelle version < / span >
2018-03-12 18:50:56 +00:00
< / div >
2018-02-20 14:53:31 +00:00
< / div >
2018-03-12 18:50:56 +00:00
<!-- < div class = 'header-title' > { { gstore . header _title } } < / div > -- >
2018-02-20 14:53:31 +00:00
< / div >
< / template >
< script >
export default {
name : 'HEADER' ,
data ( ) {
return {
gstore : gstore . get ,
2018-03-12 18:50:56 +00:00
is _connected : _SERVER . session . connected ,
2018-03-15 16:38:06 +00:00
d _dialog : false ,
dep _id : _SERVER . session . department _id ,
dpts : _SERVER . session . departments ,
v _dialog : false ,
ver _id : - 1 ,
vers : [
{ id : - 1 , date : null } ,
{ id : 0 , date : '01-02-2017' } ,
{ id : 1 , date : '23-03-2017' }
2018-03-12 18:50:56 +00:00
]
2018-02-20 14:53:31 +00:00
} ;
2018-03-15 16:38:06 +00:00
} ,
methods : {
/ * ( 1 ) G e t c u r r e n t d e p a r t m e n t d a t a
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
2018-03-17 18:07:44 +00:00
get _dcurrent ( id ) {
// use @dep_id, if invalid argument @id
( isNaN ( id ) ) && ( id = this . dep _id ) ;
2018-03-15 16:38:06 +00:00
// search in @dpts where id is @dep_id
for ( var d in this . dpts )
2018-03-17 18:07:44 +00:00
if ( this . dpts [ d ] . id == id )
2018-03-15 16:38:06 +00:00
return this . dpts [ d ] ;
return { id : null , label : null } ;
} ,
/ * ( 2 ) G e t c u r r e n t v e r s o i n d a t a
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
2018-03-17 18:07:44 +00:00
get _vcurrent ( id ) {
// use @dep_id, if invalid argument @id
( isNaN ( id ) ) && ( id = this . ver _id ) ;
2018-03-15 16:38:06 +00:00
// search in @vers where id is @ver_id
for ( var v in this . vers )
2018-03-17 18:07:44 +00:00
if ( this . vers [ v ] . id == id )
2018-03-15 16:38:06 +00:00
return this . vers [ v ] ;
return { date : null } ;
} ,
/ * ( 3 ) S w i t c h t o o t h e r d e p a r t m e n t
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
d _switch ( id ) {
// 1. De-activate dialogs
this . d _dialog = false ;
this . v _dialog = false ;
2018-03-17 18:07:44 +00:00
// 2. Do nothing if no change
if ( this . dep _id == id )
return ;
// 3. Ask for department change
2018-03-15 16:38:06 +00:00
api . call ( ` PUT department/ ${ id } ` , { } , function ( rs ) {
2018-03-17 18:07:44 +00:00
// 1. error -> do nothing
if ( rs . error !== 0 || rs . switched !== true )
2018-03-15 16:38:06 +00:00
return ;
2018-03-17 18:07:44 +00:00
// 2. Update GUI
2018-03-15 16:38:06 +00:00
this . dep _id = id ;
2018-03-17 18:07:44 +00:00
// 3. Reload page if needed
2018-03-15 16:38:06 +00:00
setTimeout ( ( ) => { document . location = '' ; } , 200 ) ;
} . bind ( this ) ) ;
} ,
/ * ( 4 ) S w i t c h t o o t h e r v e r s i o n
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
v _switch ( id ) {
// 1. De-activate dialogs
this . d _dialog = false ;
this . v _dialog = false ;
2018-03-17 18:07:44 +00:00
// 2. Do nothing if no change
if ( this . ver _id == id )
return ;
// 3. Get version date
var verdate = this . get _vcurrent ( id ) . date ;
// 4. If null date -> go to current version
( verdate === null ) && ( verdate = '' ) ;
// 5. Ask for department change
api . call ( ` PUT department/version/0/ ${ verdate } ` , { } , function ( rs ) {
// 1. error -> do nothing
if ( rs . error !== 0 || rs . updated !== true )
return ;
// 2. Update GUI
this . ver _id = id ;
// 3. Reload page if needed
setTimeout ( ( ) => { document . location = '' ; } , 200 ) ;
} . bind ( this ) ) ;
} ,
/ * ( 5 ) C r e a t e a n e w v e r s i o n f r o m n o w
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
v _create ( ) {
// 1. De-activate dialogs
this . d _dialog = false ;
this . v _dialog = false ;
// 2. Popup confirm
( new Promise ( ( resolve , reject ) => {
popup . ask ( {
title : 'Confirmation de création de version' ,
content : ` Une sauvegarde (ou version) va être crée à partir de l'état actuel des données de tout le département<br><br>Voulez-vous créer cette sauvegarde ? ` ,
action : 'Créer' ,
type : 'valid'
} , ( popup _rs ) => { popup _rs && resolve ( ) } ) ;
// 3. On popup confirm
} ) ) . then ( ( ) => {
// Call API to create a new version
api . call ( ` POST department/version/ ` , { } , function ( rs ) {
// 1. error -> popup
if ( rs . error !== 0 || ! rs . hasOwnProperty ( 'created_id' ) ) {
return popup . ask ( {
title : 'Error (' + err _code + ')' ,
content : 'La création de sauvegarde à échoué.' ,
action : 'OK' ,
type : 'neutral'
} , ( ) => { } ) ;
}
// 2. Get last version id
var last _id = this . vers [ this . vers . length - 1 ] . id ;
// 3. Update GUI
this . vers . push ( { id : last _id + 1 , date : rs . created _id } ) ;
} . bind ( this ) ) ;
} )
2018-03-15 16:38:06 +00:00
}
2018-03-17 18:07:44 +00:00
2018-03-17 17:26:09 +00:00
} ,
beforeMount ( ) {
2018-03-17 18:07:44 +00:00
/* (1) Try to fetch versions from API */
api . call ( 'GET department/version' , { } , function ( rs ) {
// 1. Manage error
if ( rs . error !== 0 || ! rs . hasOwnProperty ( 'versions' ) )
return ;
// 2. Init version list
this . vers = [ { id : - 1 , date : null } ] ;
var idv = 0 ;
// 3. Store versions
for ( var ver of rs . versions ) {
// if current version -> set @ver_id
if ( _SERVER . session . version === ver )
this . ver _id = idv
// add version to list
this . vers . push ( { id : idv ++ , date : ver } ) ;
}
} . bind ( this ) ) ;
/* (2) Set onblur to hide department lists */
2018-03-17 17:26:09 +00:00
window . onblur . link ( 'header.department' , ( e ) => {
// only hide not [data-unblur-department] elements
if ( e . target . getAttribute ( 'data-unblur-department' ) === null )
this . d _dialog = false ;
// only hide not [data-unblur-version] elements
if ( e . target . getAttribute ( 'data-unblur-version' ) === null )
this . v _dialog = false ;
} ) ;
2018-02-20 14:53:31 +00:00
}
}
< / script >