From 5e52f230b1ea97e0acd67fa1b4bce989f59e208e Mon Sep 17 00:00:00 2001 From: LouisLam Date: Tue, 17 Aug 2021 16:41:12 +0800 Subject: [PATCH] create datetime mixin --- src/components/Datetime.vue | 12 +++------ src/main.js | 4 ++- src/mixins/datetime.js | 53 +++++++++++++++++++++++++++++++++++++ src/mixins/socket.js | 11 -------- 4 files changed, 60 insertions(+), 20 deletions(-) create mode 100644 src/mixins/datetime.js diff --git a/src/components/Datetime.vue b/src/components/Datetime.vue index 84093afb1..1df982cb1 100644 --- a/src/components/Datetime.vue +++ b/src/components/Datetime.vue @@ -22,15 +22,11 @@ export default { computed: { displayText() { - if (this.value !== undefined && this.value !== "") { - let format = "YYYY-MM-DD HH:mm:ss"; - if (this.dateOnly) { - format = "YYYY-MM-DD"; - } - return dayjs.utc(this.value).tz(this.$root.timezone).format(format); + if (this.dateOnly) { + return this.$root.date(this.value); + } else { + return this.$root.datetime(this.value); } - - return ""; }, }, } diff --git a/src/main.js b/src/main.js index f66d22c26..19da50b97 100644 --- a/src/main.js +++ b/src/main.js @@ -11,6 +11,7 @@ import Layout from "./layouts/Layout.vue"; import socket from "./mixins/socket"; import theme from "./mixins/theme"; import mobile from "./mixins/mobile"; +import datetime from "./mixins/datetime"; import Dashboard from "./pages/Dashboard.vue"; import DashboardHome from "./pages/DashboardHome.vue"; import Details from "./pages/Details.vue"; @@ -80,7 +81,8 @@ const app = createApp({ mixins: [ socket, theme, - mobile + mobile, + datetime ], data() { return { diff --git a/src/mixins/datetime.js b/src/mixins/datetime.js new file mode 100644 index 000000000..e98fc1bd4 --- /dev/null +++ b/src/mixins/datetime.js @@ -0,0 +1,53 @@ +import dayjs from "dayjs"; +import utc from "dayjs/plugin/utc"; +import timezone from "dayjs/plugin/timezone"; +import relativeTime from "dayjs/plugin/relativeTime"; +dayjs.extend(utc); +dayjs.extend(timezone); +dayjs.extend(relativeTime); + +export default { + data() { + return { + userTimezone: localStorage.timezone || "auto", + } + }, + + methods: { + datetime(value) { + return this.datetimeFormat(value, "YYYY-MM-DD HH:mm:ss"); + }, + + date(value) { + return this.datetimeFormat(value, "YYYY-MM-DD"); + }, + + time(value, second = true) { + let secondString; + if (second) { + secondString = ":ss"; + } else { + secondString = ""; + } + return this.datetimeFormat(value, "HH:mm" + secondString); + }, + + datetimeFormat(value, format) { + if (value !== undefined && value !== "") { + return dayjs.utc(value).tz(this.timezone).format(format); + } + return ""; + } + }, + + computed: { + timezone() { + if (this.userTimezone === "auto") { + return dayjs.tz.guess() + } + + return this.userTimezone + }, + } + +} diff --git a/src/mixins/socket.js b/src/mixins/socket.js index 0857fd5f6..69931c92a 100644 --- a/src/mixins/socket.js +++ b/src/mixins/socket.js @@ -1,4 +1,3 @@ -import dayjs from "dayjs"; import { io } from "socket.io-client"; import { useToast } from "vue-toastification"; const toast = useToast() @@ -17,7 +16,6 @@ export default { connectCount: 0, }, remember: (localStorage.remember !== "0"), - userTimezone: localStorage.timezone || "auto", allowLoginDialog: false, // Allowed to show login dialog, but "loggedIn" have to be true too. This exists because prevent the login dialog show 0.1s in first before the socket server auth-ed. loggedIn: false, monitorList: { }, @@ -265,15 +263,6 @@ export default { computed: { - timezone() { - - if (this.userTimezone === "auto") { - return dayjs.tz.guess() - } - - return this.userTimezone - }, - lastHeartbeatList() { let result = {}