2024-09-03 17:08:07 +02:00
|
|
|
import { t } from "../services/i18n.js";
|
2021-03-20 00:00:49 +01:00
|
|
|
import BasicWidget from "./basic_widget.js";
|
2021-03-21 22:43:41 +01:00
|
|
|
import ws from "../services/ws.js";
|
|
|
|
import options from "../services/options.js";
|
|
|
|
import syncService from "../services/sync.js";
|
2025-01-28 21:03:39 +02:00
|
|
|
import { escapeQuotes } from "../services/utils.js";
|
2021-03-20 00:00:49 +01:00
|
|
|
|
|
|
|
const TPL = `
|
2022-12-18 20:29:17 +01:00
|
|
|
<div class="sync-status-widget launcher-button">
|
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
|
|
|
}
|
2025-01-28 21:03:39 +02:00
|
|
|
|
2021-03-20 00:00:49 +01:00
|
|
|
.sync-status {
|
|
|
|
box-sizing: border-box;
|
|
|
|
}
|
2025-01-28 21:03:39 +02:00
|
|
|
|
2021-03-21 22:43:41 +01:00
|
|
|
.sync-status .sync-status-icon {
|
|
|
|
display: inline-block;
|
2021-03-20 00:00:49 +01:00
|
|
|
position: relative;
|
2021-07-04 21:24:47 +02:00
|
|
|
top: -5px;
|
|
|
|
font-size: 110%;
|
2021-03-20 00:00:49 +01:00
|
|
|
}
|
2025-01-28 21:03:39 +02:00
|
|
|
|
2021-03-21 23:45:15 +01:00
|
|
|
.sync-status .sync-status-sub-icon {
|
2025-01-28 21:03:39 +02:00
|
|
|
font-size: 40%;
|
|
|
|
position: absolute;
|
2021-07-04 21:24:47 +02:00
|
|
|
left: 0;
|
|
|
|
top: 16px;
|
2021-03-21 23:45:15 +01:00
|
|
|
}
|
2025-01-28 21:03:39 +02:00
|
|
|
|
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
|
|
|
}
|
2025-01-28 21:03:39 +02: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
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<div class="sync-status">
|
2025-01-28 21:03:39 +02:00
|
|
|
<span class="sync-status-icon sync-status-unknown bx bx-time"
|
|
|
|
data-bs-toggle="tooltip"
|
|
|
|
title="${escapeQuotes(t("sync_status.unknown"))}">
|
2021-04-13 21:54:07 +02:00
|
|
|
</span>
|
2021-10-06 18:13:58 +02:00
|
|
|
<span class="sync-status-icon sync-status-connected-with-changes bx bx-wifi"
|
2025-01-28 21:03:39 +02:00
|
|
|
data-bs-toggle="tooltip"
|
|
|
|
title="${escapeQuotes(t("sync_status.connected_with_changes"))}">
|
2021-03-21 23:45:15 +01:00
|
|
|
<span class="bx bxs-star sync-status-sub-icon"></span>
|
2021-03-21 22:43:41 +01:00
|
|
|
</span>
|
2025-01-28 21:03:39 +02:00
|
|
|
<span class="sync-status-icon sync-status-connected-no-changes bx bx-wifi"
|
|
|
|
data-bs-toggle="tooltip"
|
|
|
|
title="${escapeQuotes(t("sync_status.connected_no_changes"))}">
|
2021-03-21 22:43:41 +01:00
|
|
|
</span>
|
2021-05-18 22:49:47 +02:00
|
|
|
<span class="sync-status-icon sync-status-disconnected-with-changes bx bx-wifi-off"
|
2025-01-28 21:03:39 +02:00
|
|
|
data-bs-toggle="tooltip"
|
|
|
|
title="${escapeQuotes(t("sync_status.disconnected_with_changes"))}">
|
2021-03-21 23:45:15 +01:00
|
|
|
<span class="bx bxs-star sync-status-sub-icon"></span>
|
2021-03-21 22:43:41 +01:00
|
|
|
</span>
|
2025-01-28 21:03:39 +02:00
|
|
|
<span class="sync-status-icon sync-status-disconnected-no-changes bx bx-wifi-off"
|
2024-09-03 17:08:07 +02:00
|
|
|
data-bs-toggle="tooltip"
|
2025-01-28 21:03:39 +02:00
|
|
|
title="${escapeQuotes(t("sync_status.disconnected_no_changes"))}">
|
2021-03-21 22:43:41 +01:00
|
|
|
</span>
|
2025-01-28 21:03:39 +02:00
|
|
|
<span class="sync-status-icon sync-status-in-progress bx bx-analyse bx-spin"
|
2024-09-03 17:08:07 +02:00
|
|
|
data-bs-toggle="tooltip"
|
2025-01-28 21:03:39 +02:00
|
|
|
title="${escapeQuotes(t("sync_status.in_progress"))}">
|
2021-03-21 22:43:41 +01:00
|
|
|
</span>
|
2021-03-20 00:00:49 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
|
|
|
|
export default class SyncStatusWidget extends BasicWidget {
|
2021-03-21 22:43:41 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
this.syncState = "unknown";
|
2021-03-21 22:43:41 +01:00
|
|
|
this.allChangesPushed = false;
|
2024-12-13 22:43:58 +02:00
|
|
|
this.settings = {
|
|
|
|
titlePlacement: "right"
|
|
|
|
};
|
2021-03-21 22:43:41 +01:00
|
|
|
}
|
|
|
|
|
2021-03-20 00:00:49 +01:00
|
|
|
doRender() {
|
|
|
|
this.$widget = $(TPL);
|
2021-03-21 00:01:28 +01:00
|
|
|
this.$widget.hide();
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$widget.find(".sync-status-icon:not(.sync-status-in-progress)").on("click", () => syncService.syncNow());
|
2021-03-21 00:01:28 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
ws.subscribeToMessages((message) => this.processMessage(message));
|
2021-03-21 00:01:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
showIcon(className) {
|
2025-01-09 18:07:02 +02: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;
|
|
|
|
}
|
|
|
|
|
2024-09-03 17:08:07 +02:00
|
|
|
bootstrap.Tooltip.getOrCreateInstance(this.$widget.find(`.sync-status-${className}`), {
|
|
|
|
html: true,
|
2024-12-13 22:43:58 +02:00
|
|
|
placement: this.settings.titlePlacement,
|
2025-01-09 18:07:02 +02:00
|
|
|
fallbackPlacements: [this.settings.titlePlacement]
|
2024-09-03 17:08:07 +02:00
|
|
|
});
|
|
|
|
|
2021-03-21 00:01:28 +01:00
|
|
|
this.$widget.show();
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$widget.find(".sync-status-icon").hide();
|
2022-12-21 15:19:05 +01:00
|
|
|
this.$widget.find(`.sync-status-${className}`).show();
|
2021-03-21 00:01:28 +01:00
|
|
|
}
|
2021-03-21 22:43:41 +01:00
|
|
|
|
|
|
|
processMessage(message) {
|
2025-01-09 18:07:02 +02:00
|
|
|
if (message.type === "sync-pull-in-progress") {
|
|
|
|
this.syncState = "in-progress";
|
2021-05-15 22:00:53 +02:00
|
|
|
this.lastSyncedPush = message.lastSyncedPush;
|
2025-01-09 18:07:02 +02: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;
|
2025-01-09 18:07:02 +02:00
|
|
|
} else if (message.type === "sync-finished") {
|
|
|
|
this.syncState = "connected";
|
2021-05-15 22:00:53 +02:00
|
|
|
this.lastSyncedPush = message.lastSyncedPush;
|
2025-01-09 18:07:02 +02:00
|
|
|
} else if (message.type === "sync-failed") {
|
|
|
|
this.syncState = "disconnected";
|
2021-05-15 22:00:53 +02:00
|
|
|
this.lastSyncedPush = message.lastSyncedPush;
|
2025-01-09 18:07:02 +02: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();
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (["unknown", "in-progress"].includes(this.syncState)) {
|
2021-05-15 13:44:12 +02:00
|
|
|
this.showIcon(this.syncState);
|
2021-04-13 21:54:07 +02:00
|
|
|
} else {
|
2025-01-09 18:07:02 +02:00
|
|
|
this.showIcon(`${this.syncState}-${this.allChangesPushed ? "no-changes" : "with-changes"}`);
|
2021-03-21 22:43:41 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-20 00:00:49 +01:00
|
|
|
}
|