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-05-12 14:17:27 +00:00
< div class = 'current' data -unblur -department >
< span class = 'create' @ click = '!department.create?(department.newLabel="")+(department.create=true):d_create()' > < / span >
< span class = 'remove' @ click = 'd_remove()' > < / span >
< input v -if = ' department.create ' type = 'text' placeholder = 'Nouveau nom' v -model = ' department.newLabel ' size = '' >
< span v -if = ' ! department.create ' @ click = 'department.dialog=!department.dialog' data -unblur -department > { { get _dcurrent ( ) . label } } < / span >
< / div >
< div class = 'department-dialog' v -show = ' department.dialog ' data -unblur -department >
< span v -for = ' d in department.list ' v -show = ' d.id ! = department.current ' @ 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-05-09 15:43:34 +00:00
< div class = 'current' : data -id = ' get_vcurrent ( ) .id ' data -unblur -version >
< span class = 'remove' @ click = 'v_remove()' > < / span >
< span class = 'edit' @ click = '!version.edit?(version.newName="")+(version.edit=true):v_edit()' > < / span >
< input v -if = ' version.edit ' type = 'text' : placeholder = 'get_vcurrent().name' v -model = ' version.newName ' size = '' >
< span v -if = ' ! version.edit ' @ click = 'version.dialog=!version.dialog' data -unblur -version > { { get _vcurrent ( ) . name } } < / span >
< / div >
2018-05-09 13:57:47 +00:00
2018-05-09 10:36:12 +00:00
< div class = 'version-dialog' v -show = ' version.dialog ' data -unblur -version >
2018-05-09 15:43:34 +00:00
< span v -for = ' v in version.list ' @ click = 'v_switch(v.id)' v -show = ' v.id ! = version.current ' : data -id = ' v.id ' data -unblur -version > { { v . name } } < / span >
< span @ click = 'v_create()' data -unblur -version data -id = ' -1 ' > Créer < / span >
2018-03-12 18:50:56 +00:00
< / div >
2018-02-20 14:53:31 +00:00
< / div >
2018-05-12 14:42:46 +00:00
<!-- Export all -- >
< div class = 'global-export' >
< div class = 'current export' @ click = 'global_export()' >
< span class = 'export' > < / span >
exporter
< / div >
< / 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-05-12 14:17:27 +00:00
department : {
dialog : false ,
current : _SERVER . session . department _id ,
list : _SERVER . session . departments ,
create : false ,
newLabel : ''
} ,
2018-03-15 16:38:06 +00:00
2018-05-09 10:36:12 +00:00
version : {
dialog : false ,
current : - 1 ,
2018-05-09 15:43:34 +00:00
list : [ ] ,
edit : false ,
newName : ''
2018-05-09 10:36:12 +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 ) {
2018-05-12 14:17:27 +00:00
// use @current, if invalid argument @id
( isNaN ( id ) ) && ( id = this . department . current ) ;
2018-03-15 16:38:06 +00:00
2018-05-12 14:17:27 +00:00
// search in @list where id is @current
for ( var d in this . department . list )
if ( this . department . list [ d ] . id == id )
return this . department . list [ d ] ;
2018-03-15 16:38:06 +00:00
2018-05-12 14:17:27 +00:00
return { id : - 2 , name : null } ;
2018-03-15 16:38:06 +00:00
} ,
2018-05-09 10:36:12 +00:00
/ * ( 2 ) G e t c u r r e n t v e r s i o n d a t a
2018-03-15 16:38:06 +00:00
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
2018-03-17 18:07:44 +00:00
get _vcurrent ( id ) {
2018-05-09 10:36:12 +00:00
// use @version.current, if invalid argument @id
( isNaN ( id ) ) && ( id = this . version . current ) ;
2018-03-15 16:38:06 +00:00
2018-05-09 10:36:12 +00:00
// search in @ist where id is @id
for ( var v in this . version . list )
if ( this . version . list [ v ] . id == id )
return this . version . list [ v ] ;
2018-03-15 16:38:06 +00:00
2018-05-09 15:43:34 +00:00
return { id : - 2 , name : '-' } ;
2018-03-15 16:38:06 +00:00
} ,
/ * ( 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
2018-05-12 14:17:27 +00:00
this . department . dialog = false ;
2018-05-09 10:36:12 +00:00
this . version . dialog = false ;
2018-03-15 16:38:06 +00:00
2018-03-17 18:07:44 +00:00
// 2. Do nothing if no change
2018-05-12 14:17:27 +00:00
if ( this . department . current == id )
2018-03-17 18:07:44 +00:00
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-05-12 14:17:27 +00:00
this . department . current = id ;
2018-03-15 16:38:06 +00:00
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
2018-05-12 14:17:27 +00:00
this . department . dialog = false ;
2018-05-09 10:36:12 +00:00
this . version . dialog = false ;
2018-03-15 16:38:06 +00:00
2018-03-17 18:07:44 +00:00
// 2. Do nothing if no change
2018-05-09 10:36:12 +00:00
if ( this . version . current == id )
2018-03-17 18:07:44 +00:00
return ;
2018-05-09 10:36:12 +00:00
// 3. Ask for department change
api . call ( ` GET department/version/switch/ ${ id } ` , { } , function ( rs ) {
2018-03-17 18:07:44 +00:00
// 1. error -> do nothing
2018-05-09 10:36:12 +00:00
if ( rs . error !== 0 )
2018-03-17 18:07:44 +00:00
return ;
// 2. Update GUI
2018-05-09 10:36:12 +00:00
this . version . current = id ;
2018-03-17 18:07:44 +00:00
// 3. Reload page if needed
setTimeout ( ( ) => { document . location = '' ; } , 200 ) ;
} . bind ( this ) ) ;
} ,
2018-05-12 14:17:27 +00:00
/ * ( 5 ) C r e a t e a n e w e m p t y d e p a r t m e n t
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
d _create ( ) {
// 1. De-activate dialogs
this . department . dialog = false ;
this . version . dialog = false ;
// get current department
var cur = this . get _dcurrent ( ) ;
if ( cur . id < 0 || this . department . newLabel . length < 1 ) {
this . department . create = false ;
return ;
}
var newlabel = this . department . newLabel ;
// 2. Popup confirm
( new Promise ( ( resolve , reject ) => {
popup . ask ( {
title : 'Confirmation de création de département' ,
content : ` Le nouveau département <b> ${ newlabel } </b> va être créé; il ne contiendra aucune donnée, il permet de gérer plusieurs départements ne partageant pas les mêmes UEs, enseignants, formations, etc<br><br>Voulez-vous créer un nouveau département vide ? ` ,
action : 'Créer' ,
type : 'valid'
} , ( popup _rs ) => { popup _rs && resolve ( ) } ) ;
// 3. On popup confirm
} ) ) . then ( ( ) => {
// Call API to create a new department
api . call ( ` POST department/ ` , { name : newlabel } , function ( rs ) {
// 1. error -> popup
if ( rs . error !== 0 || ! rs . hasOwnProperty ( 'created_id' ) ) {
return popup . ask ( {
title : 'Erreur (' + rs . error + ')' ,
content : 'La création de département a échoué.' ,
action : 'OK' ,
type : 'neutral'
} , ( ) => { } ) ;
}
// 3. Update GUI
this . department . list . push ( { id : parseInt ( rs . created _id ) , name : newlabel } ) ;
} . bind ( this ) ) ;
} ) ;
} ,
2018-03-17 18:07:44 +00:00
/ * ( 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
2018-05-12 14:17:27 +00:00
this . department . dialog = false ;
2018-05-09 10:36:12 +00:00
this . version . dialog = false ;
2018-03-17 18:07:44 +00:00
// 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 ( ( ) => {
2018-05-09 13:57:47 +00:00
let newVersionName = ` ${ this . get _vcurrent ( ) . name } * ` ;
2018-03-17 18:07:44 +00:00
// Call API to create a new version
2018-05-09 13:57:47 +00:00
api . call ( ` POST department/version/ ` , { label : newVersionName } , function ( rs ) {
2018-03-17 18:07:44 +00:00
// 1. error -> popup
if ( rs . error !== 0 || ! rs . hasOwnProperty ( 'created_id' ) ) {
return popup . ask ( {
2018-05-09 15:43:34 +00:00
title : 'Erreur (' + rs . error + ')' ,
2018-05-09 13:12:26 +00:00
content : 'La création de version à échoué.' ,
2018-03-17 18:07:44 +00:00
action : 'OK' ,
type : 'neutral'
} , ( ) => { } ) ;
}
// 3. Update GUI
2018-05-12 14:17:27 +00:00
this . version . list . push ( { id : parseInt ( rs . created _id ) , name : newVersionName } ) ;
2018-05-09 13:57:47 +00:00
} . bind ( this ) ) ;
} ) ;
} ,
/ * ( 6 ) R e n a m e a v e r s i o n
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
2018-05-09 15:43:34 +00:00
v _edit ( ) {
2018-05-09 13:57:47 +00:00
2018-05-09 15:43:34 +00:00
// get current version
var cur = this . get _vcurrent ( ) ;
2018-05-09 15:45:52 +00:00
if ( cur . id < 0 || this . version . newName . length < 1 ) {
this . version . edit = false ;
2018-05-09 13:57:47 +00:00
return ;
2018-05-09 15:45:52 +00:00
}
2018-05-09 13:57:47 +00:00
2018-05-09 15:43:34 +00:00
var newname = this . version . newName ;
2018-05-09 13:57:47 +00:00
// 2. Popup confirm
( new Promise ( ( resolve , reject ) => {
popup . ask ( {
title : 'Confirmation de modification de version' ,
2018-05-09 15:43:34 +00:00
content : ` La version <b> ${ cur . name } </b> va être renommée en <b> ${ newname } </b><br><br>Voulez-vous valider cette modification ? ` ,
2018-05-09 13:57:47 +00:00
action : 'Valider' ,
2018-05-09 15:43:34 +00:00
type : 'search'
2018-05-09 13:57:47 +00:00
} , ( popup _rs ) => { popup _rs && resolve ( ) } ) ;
// 3. On popup confirm
} ) ) . then ( ( ) => {
// Call API to create a new version
2018-05-09 15:43:34 +00:00
api . call ( ` PUT department/version/ ${ cur . id } ` , { label : newname } , function ( rs ) {
2018-05-09 13:57:47 +00:00
// 1. error -> popup
if ( rs . error !== 0 || ! rs . hasOwnProperty ( 'updated' ) ) {
return popup . ask ( {
2018-05-09 15:43:34 +00:00
title : 'Erreur (' + rs . error + ')' ,
2018-05-09 13:57:47 +00:00
content : 'La modification a échoué.' ,
action : 'OK' ,
type : 'neutral'
} , ( ) => { } ) ;
}
// 3. Update GUI
2018-05-09 15:43:34 +00:00
cur . name = newname ;
2018-03-17 18:07:44 +00:00
} . bind ( this ) ) ;
2018-05-09 15:43:34 +00:00
} ) . finally ( ( ) => {
this . version . edit = false ;
2018-03-17 18:07:44 +00:00
} )
2018-03-15 16:38:06 +00:00
2018-05-09 15:43:34 +00:00
} ,
2018-05-12 14:17:27 +00:00
/ * ( 7 ) R e m o v e a d e p a r t m e n t
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
d _remove ( ) {
// get current department
var cur = this . get _dcurrent ( ) ;
if ( cur . id < 0 )
return ;
// if last department -> forbid
if ( this . department . list . length < 2 ) {
return popup . ask ( {
title : 'Dernier départment' ,
content : ` Le département <b> ${ cur . label } </b> ne peut être supprimé car il est le dernier disponible ` ,
action : 'OK' ,
type : 'invalid'
} ) ;
}
// 2. Popup confirm
( new Promise ( ( resolve , reject ) => {
popup . ask ( {
title : 'Confirmation de suppression' ,
content : ` Le département <b> ${ cur . label } </b> va être supprimé. Toutes les données seront perdues de manière définitive</b><br><br>Voulez-vous supprimer ce département ? ` ,
action : 'Supprimer' ,
type : 'invalid'
} , ( popup _rs ) => { popup _rs && resolve ( ) } ) ;
// 3. On popup confirm
} ) ) . then ( ( ) => {
// Call API to delete the current department
api . call ( ` DELETE department/ ${ cur . id } ` , { } , function ( rs ) {
// 1. error -> popup
if ( rs . error !== 0 || ! rs . hasOwnProperty ( 'deleted' ) ) {
return popup . ask ( {
title : 'Erreur (' + rs . error + ')' ,
content : 'La suppression a échoué.' ,
action : 'OK' ,
type : 'neutral'
} , ( ) => { } ) ;
}
// 3. Reload page
document . location = '' ;
} . bind ( this ) ) ;
} ) ;
} ,
2018-05-09 15:43:34 +00:00
/ * ( 7 ) R e m o v e a v e r s i o n
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
v _remove ( ) {
// get current version
var cur = this . get _vcurrent ( ) ;
if ( cur . id < 0 )
return ;
// if last version -> forbid
if ( this . version . list . length < 2 ) {
return popup . ask ( {
title : 'Dernière version' ,
content : ` La version <b> ${ cur . name } </b> ne peut être supprimée car il ne reste aucune autre version pour ce département ` ,
action : 'OK' ,
type : 'invalid'
} ) ;
}
// 2. Popup confirm
( new Promise ( ( resolve , reject ) => {
popup . ask ( {
title : 'Confirmation de suppression' ,
content : ` La version <b> ${ cur . name } </b> va être supprimée. Toutes les données seront perdues de manière définitive</b><br><br>Voulez-vous supprimer cette version ? ` ,
action : 'Supprimer' ,
type : 'invalid'
} , ( popup _rs ) => { popup _rs && resolve ( ) } ) ;
// 3. On popup confirm
} ) ) . then ( ( ) => {
// Call API to create a new version
api . call ( ` DELETE department/version/ ${ cur . id } ` , { } , function ( rs ) {
// 1. error -> popup
if ( rs . error !== 0 || ! rs . hasOwnProperty ( 'deleted' ) ) {
return popup . ask ( {
title : 'Erreur (' + rs . error + ')' ,
content : 'La suppression a échoué.' ,
action : 'OK' ,
type : 'neutral'
} , ( ) => { } ) ;
}
// 3. Reload page
document . location = '' ;
} . bind ( this ) ) ;
} ) ;
2018-05-12 14:42:46 +00:00
} ,
/ * ( x ) E x p o r t s a l l d a t a a b o u t t h i s d e p a r t m e n t ' s v e r s i o n
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
global _export ( ) {
api . call ( ` GET department/export ` , { } , function ( rs ) {
console . log ( rs ) ;
// 1. error -> popup
if ( rs . error !== 0 || ! rs . hasOwnProperty ( 'link' ) ) {
return popup . ask ( {
title : 'Erreur (' + rs . error + ')' ,
content : 'L\'export a échoué.' ,
action : 'OK' ,
type : 'neutral'
} , ( ) => { } ) ;
}
// 2. Launch download
document . location = rs . link ;
} . bind ( this ) ) ;
2018-03-15 16:38:06 +00:00
}
2018-03-17 18:07:44 +00:00
2018-05-12 14:42:46 +00:00
2018-03-17 17:26:09 +00:00
} ,
2018-05-09 13:57:47 +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
2018-05-09 10:36:12 +00:00
this . version . list = [ ] ;
2018-03-17 18:07:44 +00:00
// 3. Store versions
for ( var ver of rs . versions ) {
2018-05-09 10:36:12 +00:00
// if current version -> set @version.current
if ( _SERVER . session . version . current === ver . iddatabase )
this . version . current = ver . iddatabase
2018-03-17 18:07:44 +00:00
// add version to list
2018-05-09 13:57:47 +00:00
this . version . list . push ( { id : ver . iddatabase , name : ver . label , new _name : ver . label } ) ;
2018-03-17 18:07:44 +00:00
}
2018-05-09 10:36:12 +00:00
this . version
2018-03-17 18:07:44 +00:00
} . 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 )
2018-05-12 14:17:27 +00:00
this . department . dialog = false ;
2018-03-17 17:26:09 +00:00
// only hide not [data-unblur-version] elements
if ( e . target . getAttribute ( 'data-unblur-version' ) === null )
2018-05-09 10:36:12 +00:00
this . version . dialog = false ;
2018-03-17 17:26:09 +00:00
} ) ;
2018-02-20 14:53:31 +00:00
}
}
< / script >