feat: add support for 12-hour time format

This commit is contained in:
Substancia 2023-10-02 19:27:49 +05:30
parent e897a96c17
commit dbaf84fe39
4 changed files with 66 additions and 8 deletions

View file

@ -15,6 +15,10 @@ export default {
type: Boolean, type: Boolean,
default: false, default: false,
}, },
use12HourTimeFormat: {
type: Boolean,
default: false
},
}, },
computed: { computed: {
@ -22,7 +26,7 @@ export default {
if (this.dateOnly) { if (this.dateOnly) {
return this.$root.date(this.value); return this.$root.date(this.value);
} else { } else {
return this.$root.datetime(this.value); return this.$root.datetime(this.value, this.use12HourTimeFormat);
} }
}, },
}, },

View file

@ -1,6 +1,10 @@
<template> <template>
<div> <div>
<div class="period-options"> <div class="period-options">
<span class="time-format-12-hour">
<input id="checkbox12HourTimeFormat" class="time-format-12-hour-check-input form-check-input" type="checkbox" @change="set12HourTimeFormat" />
<label for="checkbox12HourTimeFormat" class="time-format-12-hour-check-label">{{ $t("12-hour format") }}</label>
</span>
<button type="button" class="btn btn-light dropdown-toggle btn-period-toggle" data-bs-toggle="dropdown" aria-expanded="false"> <button type="button" class="btn btn-light dropdown-toggle btn-period-toggle" data-bs-toggle="dropdown" aria-expanded="false">
{{ chartPeriodOptions[chartPeriodHrs] }}&nbsp; {{ chartPeriodOptions[chartPeriodHrs] }}&nbsp;
</button> </button>
@ -36,7 +40,12 @@ export default {
type: Number, type: Number,
required: true, required: true,
}, },
use12HourTimeFormat: {
type: Boolean,
default: false
}
}, },
emits: [ "switch-use-12-hour-time-format" ],
data() { data() {
return { return {
@ -60,6 +69,11 @@ export default {
}, },
computed: { computed: {
chartOptions() { chartOptions() {
const minuteTimeFormat = `${
this.use12HourTimeFormat ? "hh" : "HH"
}:mm${
this.use12HourTimeFormat ? " A" : ""
}`;
return { return {
responsive: true, responsive: true,
maintainAspectRatio: false, maintainAspectRatio: false,
@ -99,8 +113,8 @@ export default {
round: "second", round: "second",
tooltipFormat: "YYYY-MM-DD HH:mm:ss", tooltipFormat: "YYYY-MM-DD HH:mm:ss",
displayFormats: { displayFormats: {
minute: "HH:mm", minute: minuteTimeFormat,
hour: "MM-DD HH:mm", hour: `MM-DD ${minuteTimeFormat}`,
} }
}, },
ticks: { ticks: {
@ -177,7 +191,7 @@ export default {
) )
) )
.map((beat) => { .map((beat) => {
const x = this.$root.datetime(beat.time); const x = this.$root.datetime(beat.time, this.use12HourTimeFormat);
pingData.push({ pingData.push({
x, x,
y: beat.ping, y: beat.ping,
@ -265,6 +279,12 @@ export default {
if (period != null) { if (period != null) {
this.chartPeriodHrs = Math.min(period, 6); this.chartPeriodHrs = Math.min(period, 6);
} }
},
methods: {
set12HourTimeFormat() {
console.log(this.use12HourTimeFormat, "->", !this.use12HourTimeFormat);
this.$emit("switch-use-12-hour-time-format");
}
} }
}; };
</script> </script>
@ -280,10 +300,29 @@ export default {
.period-options { .period-options {
padding: 0.1em 1em; padding: 0.1em 1em;
margin-bottom: -1.2em; margin-bottom: -1.2em;
display: flex;
flex-direction: row;
align-items: center;
float: right; float: right;
position: relative; position: relative;
z-index: 10; z-index: 10;
.time-format-12-hour {
.time-format-12-hour-check-input {
margin-top: 0;
vertical-align: middle;
background-color: #070a10;
border-color: #1d2634;
}
.time-format-12-hour-check-label {
padding: 2px 5px;
background: transparent;
border: 0;
opacity: 0.7;
font-size: 0.9em;
}
}
.dropdown-menu { .dropdown-menu {
padding: 0; padding: 0;
min-width: 50px; min-width: 50px;

View file

@ -35,10 +35,18 @@ export default {
/** /**
* Return a given value in the format YYYY-MM-DD HH:mm:ss * Return a given value in the format YYYY-MM-DD HH:mm:ss
* @param {any} value Value to format as date time * @param {any} value Value to format as date time
* @param {boolean} use12HourFormat Whether to use 12-hour format
* @returns {string} Formatted string * @returns {string} Formatted string
*/ */
datetime(value) { datetime(value, use12HourFormat = false) {
return this.datetimeFormat(value, "YYYY-MM-DD HH:mm:ss"); const timeFormat =
`YYYY-MM-DD ${
use12HourFormat ? "hh" : "HH"
}:mm:ss${
use12HourFormat ? " A" : ""
}`;
console.log(timeFormat);
return this.datetimeFormat(value, timeFormat);
}, },
/** /**

View file

@ -176,7 +176,7 @@
<div v-if="showPingChartBox" class="shadow-box big-padding text-center ping-chart-wrapper"> <div v-if="showPingChartBox" class="shadow-box big-padding text-center ping-chart-wrapper">
<div class="row"> <div class="row">
<div class="col"> <div class="col">
<PingChart :monitor-id="monitor.id" /> <PingChart :monitor-id="monitor.id" :use12HourTimeFormat="use12HourTimeFormat" @switch-use-12-hour-time-format="set12HourTimeFormat" />
</div> </div>
</div> </div>
</div> </div>
@ -219,7 +219,7 @@
<tbody> <tbody>
<tr v-for="(beat, index) in displayedRecords" :key="index" style="padding: 10px;"> <tr v-for="(beat, index) in displayedRecords" :key="index" style="padding: 10px;">
<td><Status :status="beat.status" /></td> <td><Status :status="beat.status" /></td>
<td :class="{ 'border-0':! beat.msg}"><Datetime :value="beat.time" /></td> <td :class="{ 'border-0':! beat.msg}"><Datetime :value="beat.time" :use12HourTimeFormat="use12HourTimeFormat" /></td>
<td class="border-0">{{ beat.msg }}</td> <td class="border-0">{{ beat.msg }}</td>
</tr> </tr>
@ -317,6 +317,7 @@ export default {
currentExample: "javascript-fetch", currentExample: "javascript-fetch",
code: "", code: "",
}, },
use12HourTimeFormat: false,
}; };
}, },
computed: { computed: {
@ -642,7 +643,13 @@ export default {
.replace("https://example.com/api/push/key?status=up&msg=OK&ping=", this.pushURL); .replace("https://example.com/api/push/key?status=up&msg=OK&ping=", this.pushURL);
this.pushMonitor.code = code; this.pushMonitor.code = code;
}); });
},
set12HourTimeFormat() {
console.log(this.use12HourTimeFormat, "->", !this.use12HourTimeFormat);
this.use12HourTimeFormat = !this.use12HourTimeFormat;
} }
}, },
}; };
</script> </script>