2021-03-20 00:00:49 +01:00
import BasicWidget from "./basic_widget.js" ;
2021-03-21 22:43:41 +01:00
import toastService from "../services/toast.js" ;
import ws from "../services/ws.js" ;
import options from "../services/options.js" ;
import syncService from "../services/sync.js" ;
2021-03-20 00:00:49 +01:00
const TPL = `
2021-03-21 22:43:41 +01:00
< div class = "sync-status-widget" >
2021-03-20 00:00:49 +01:00
< style >
2021-03-21 22:43:41 +01:00
. sync - status - widget {
2021-03-20 00:00:49 +01:00
height : 35 px ;
box - sizing : border - box ;
}
. sync - status {
height : 34 px ;
box - sizing : border - box ;
}
2021-03-21 22:43:41 +01:00
. sync - status . sync - status - icon {
height : 34 px ;
font - size : 180 % ;
display : inline - block ;
2021-03-20 00:00:49 +01:00
position : relative ;
2021-03-21 22:46:45 +01:00
padding - left : 10 px ;
padding - right : 10 px ;
2021-03-20 00:00:49 +01:00
}
2021-03-21 23:45:15 +01:00
. sync - status . sync - status - sub - icon {
font - size : 40 % ;
position : absolute ;
left : 7 px ;
top : 20 px ;
}
2021-03-21 22:43:41 +01:00
. sync - status . sync - status - icon span {
border : none ! important ;
2021-03-20 00:00:49 +01:00
}
2021-03-21 22:43:41 +01:00
. sync - status - icon : not ( . sync - status - in - progress ) : hover {
background - color : var ( -- hover - item - background - color ) ;
cursor : pointer ;
2021-03-20 00:00:49 +01:00
}
< / s t y l e >
< div class = "sync-status" >
2021-04-13 21:54:07 +02:00
< span class = "sync-status-icon sync-status-unknown"
data - toggle = "tooltip"
title = "<p>Sync status will be known once the next sync attempt starts.</p><p>Click to trigger sync now.</p>" >
< span class = "bx bx-time" > < / s p a n >
< / s p a n >
2021-03-30 22:53:23 +02:00
< span class = "sync-status-icon sync-status-connected-with-changes"
data - toggle = "tooltip"
title = "<p>Connected to the sync server. <br>There are some outstanding changes yet to be synced.</p><p>Click to trigger sync.</p>" >
2021-03-21 22:43:41 +01:00
< span class = "bx bx-wifi" > < / s p a n >
2021-03-21 23:45:15 +01:00
< span class = "bx bxs-star sync-status-sub-icon" > < / s p a n >
2021-03-21 22:43:41 +01:00
< / s p a n >
< span class = "sync-status-icon sync-status-connected-no-changes"
data - toggle = "tooltip"
title = "<p>Connected to the sync server.<br>All changes have been already synced.</p><p>Click to trigger sync.</p>" >
< span class = "bx bx-wifi" > < / s p a n >
< / s p a n >
< span class = "sync-status-icon sync-status-disconnected-with-changes"
data - toggle = "tooltip"
title = "<p>Establishing the connection to the sync server was unsuccessful.<br>There are some outstanding changes yet to be synced.</p><p>Click to trigger sync.</p>" >
< span class = "bx bx-wifi-off" > < / s p a n >
2021-03-21 23:45:15 +01:00
< span class = "bx bxs-star sync-status-sub-icon" > < / s p a n >
2021-03-21 22:43:41 +01:00
< / s p a n >
< span class = "sync-status-icon sync-status-disconnected-no-changes"
data - toggle = "tooltip"
title = "<p>Establishing the connection to the sync server was unsuccessful.<br>All known changes have been synced.</p><p>Click to trigger sync.</p>" >
< span class = "bx bx-wifi-off" > < / s p a n >
< / s p a n >
< span class = "sync-status-icon sync-status-in-progress"
data - toggle = "tooltip"
title = "Sync with the server is in progress." >
< span class = "bx bx-analyse bx-spin" > < / s p a n >
< / s p a n >
2021-03-20 00:00:49 +01:00
< / d i v >
< / d i v >
` ;
export default class SyncStatusWidget extends BasicWidget {
2021-03-21 22:43:41 +01:00
constructor ( ) {
super ( ) ;
ws . subscribeToMessages ( message => this . processMessage ( message ) ) ;
2021-04-13 21:54:07 +02:00
this . syncState = 'unknown' ;
2021-03-21 22:43:41 +01:00
this . allChangesPushed = false ;
}
2021-03-20 00:00:49 +01:00
doRender ( ) {
this . $widget = $ ( TPL ) ;
2021-03-21 00:01:28 +01:00
this . $widget . hide ( ) ;
2021-03-21 22:43:41 +01:00
this . $widget . find ( '[data-toggle="tooltip"]' ) . tooltip ( {
html : true
} ) ;
2021-03-21 00:01:28 +01:00
2021-03-21 22:43:41 +01:00
this . $widget . find ( '.sync-status-icon:not(.sync-status-in-progress)' )
. on ( 'click' , ( ) => syncService . syncNow ( ) )
2021-03-21 00:01:28 +01:00
2021-03-21 22:43:41 +01:00
this . overflowing ( ) ;
2021-03-21 00:01:28 +01:00
}
showIcon ( className ) {
2021-03-21 22:43:41 +01:00
if ( ! options . get ( 'syncServerHost' ) ) {
2021-05-15 13:44:12 +02:00
this . toggleInt ( false ) ;
2021-03-21 22:43:41 +01:00
return ;
}
2021-03-21 00:01:28 +01:00
this . $widget . show ( ) ;
this . $widget . find ( '.sync-status-icon' ) . hide ( ) ;
this . $widget . find ( '.sync-status-' + className ) . show ( ) ;
}
2021-03-21 22:43:41 +01:00
processMessage ( message ) {
if ( message . type === 'sync-pull-in-progress' ) {
toastService . showPersistent ( {
id : 'sync' ,
title : "Sync status" ,
message : "Sync update in progress" ,
icon : "refresh"
} ) ;
this . syncState = 'in-progress' ;
2021-05-15 22:00:53 +02:00
this . lastSyncedPush = message . lastSyncedPush ;
2021-03-21 22:43:41 +01:00
}
else if ( message . type === 'sync-push-in-progress' ) {
this . syncState = 'in-progress' ;
2021-05-15 22:00:53 +02:00
this . lastSyncedPush = message . lastSyncedPush ;
2021-03-21 22:43:41 +01:00
}
else if ( message . type === 'sync-finished' ) {
// this gives user a chance to see the toast in case of fast sync finish
setTimeout ( ( ) => toastService . closePersistent ( 'sync' ) , 1000 ) ;
this . syncState = 'connected' ;
2021-05-15 22:00:53 +02:00
this . lastSyncedPush = message . lastSyncedPush ;
2021-03-21 22:43:41 +01:00
}
else if ( message . type === 'sync-failed' ) {
this . syncState = 'disconnected' ;
2021-05-15 22:00:53 +02:00
this . lastSyncedPush = message . lastSyncedPush ;
2021-03-21 22:43:41 +01:00
}
else if ( message . type === 'frontend-update' ) {
2021-05-15 22:00:53 +02:00
this . lastSyncedPush = message . data . lastSyncedPush ;
2021-03-21 22:43:41 +01:00
}
2021-05-15 22:00:53 +02:00
this . allChangesPushed = this . lastSyncedPush === ws . getMaxKnownEntityChangeSyncId ( ) ;
2021-05-15 13:44:12 +02:00
if ( [ 'unknown' , 'in-progress' ] . includes ( this . syncState ) ) {
this . showIcon ( this . syncState ) ;
2021-04-13 21:54:07 +02:00
} else {
2021-03-21 22:43:41 +01:00
this . showIcon ( this . syncState + '-' + ( this . allChangesPushed ? 'no-changes' : 'with-changes' ) ) ;
}
}
2021-03-20 00:00:49 +01:00
}