@ternarytech-io/gears-ui 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -4
- package/package.json +122 -117
- package/src/components/TButtonGroup.vue +48 -0
- package/src/components/TDropdown.vue +5 -35
- package/src/components/TLightbox.vue +101 -0
- package/src/components/TPdfViewer.vue +255 -0
- package/src/components/TSeparator.vue +37 -5
- package/src/components/TSplitButton.vue +54 -0
- package/src/components/TStatCard.vue +69 -56
- package/src/components/TTimeInput.vue +159 -0
- package/src/components/TToaster.vue +38 -7
- package/src/components/internal/DropdownItems.vue +49 -0
- package/src/composables/useToast.ts +26 -4
- package/src/index.ts +249 -239
- package/src/plugins/index.ts +62 -55
- package/src/plugins/pdf.ts +12 -0
- package/src/styles/gears.css +5 -0
- package/src/types.ts +234 -222
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// A PDF, drawn by PDF.js (loaded on first use): its pages one under another, each drawn when it
|
|
3
|
+
// scrolls into view, at a zoom that fits the width (default), fits a whole page, or a percent.
|
|
4
|
+
// The toolbar has the page (v-model:page; typing one goes there), previous and next, zoom, and
|
|
5
|
+
// download and open in a new tab. A PDF that can't be read says so, with a link to download it.
|
|
6
|
+
import { computed, nextTick, onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue'
|
|
7
|
+
import type { PDFDocumentProxy, PDFPageProxy, RenderTask } from 'pdfjs-dist'
|
|
8
|
+
import { loadPdfJs } from '../plugins/pdf'
|
|
9
|
+
import TIcon from './TIcon.vue'
|
|
10
|
+
|
|
11
|
+
export type PdfZoom = 'width' | 'page' | number
|
|
12
|
+
|
|
13
|
+
const props = withDefaults(defineProps<{
|
|
14
|
+
src: string
|
|
15
|
+
// Fit the width, fit a page, or a percent (100 is the PDF's own size).
|
|
16
|
+
zoom?: PdfZoom
|
|
17
|
+
toolbar?: boolean
|
|
18
|
+
download?: boolean
|
|
19
|
+
// The downloaded file's name; by default the address's last part.
|
|
20
|
+
fileName?: string
|
|
21
|
+
height?: number | string
|
|
22
|
+
label?: string
|
|
23
|
+
}>(), { zoom: 'width', toolbar: true, download: true, height: 480, label: 'PDF document' })
|
|
24
|
+
|
|
25
|
+
const page = defineModel<number>('page', { default: 1 })
|
|
26
|
+
const emit = defineEmits<{ load: [info: { pages: number }]; error: [message: string] }>()
|
|
27
|
+
|
|
28
|
+
const scroller = ref<HTMLElement>()
|
|
29
|
+
const doc = shallowRef<PDFDocumentProxy | null>(null)
|
|
30
|
+
const pages = shallowRef<PDFPageProxy[]>([])
|
|
31
|
+
const loading = ref(true)
|
|
32
|
+
const problem = ref('')
|
|
33
|
+
const zoom = ref<PdfZoom>(props.zoom)
|
|
34
|
+
const scale = ref(1)
|
|
35
|
+
watch(() => props.zoom, z => { zoom.value = z })
|
|
36
|
+
|
|
37
|
+
const cssHeight = computed(() => (typeof props.height === 'number' ? `${props.height}px` : props.height))
|
|
38
|
+
const name = computed(() => props.fileName || decodeURIComponent(props.src.split(/[?#]/)[0]!.split('/').pop() || 'document.pdf'))
|
|
39
|
+
const count = computed(() => pages.value.length)
|
|
40
|
+
|
|
41
|
+
// ---- Loading ----
|
|
42
|
+
let loadingTask: { destroy: () => Promise<void> } | null = null
|
|
43
|
+
async function open() {
|
|
44
|
+
close()
|
|
45
|
+
loading.value = true
|
|
46
|
+
problem.value = ''
|
|
47
|
+
try {
|
|
48
|
+
const pdfjs = await loadPdfJs()
|
|
49
|
+
const task = pdfjs.getDocument({ url: props.src })
|
|
50
|
+
loadingTask = task
|
|
51
|
+
const opened = await task.promise
|
|
52
|
+
doc.value = opened
|
|
53
|
+
pages.value = await Promise.all(Array.from({ length: opened.numPages }, (_, i) => opened.getPage(i + 1)))
|
|
54
|
+
fit()
|
|
55
|
+
loading.value = false
|
|
56
|
+
emit('load', { pages: opened.numPages })
|
|
57
|
+
await nextTick()
|
|
58
|
+
observe()
|
|
59
|
+
if (page.value > 1) goTo(page.value)
|
|
60
|
+
} catch (e) {
|
|
61
|
+
loading.value = false
|
|
62
|
+
problem.value = e instanceof Error && /password/i.test(e.message) ? 'This PDF needs a password.' : 'This PDF couldn\'t be shown.'
|
|
63
|
+
emit('error', e instanceof Error ? e.message : problem.value)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function close() {
|
|
67
|
+
for (const task of tasks.values()) task.cancel()
|
|
68
|
+
tasks.clear()
|
|
69
|
+
drawn.clear()
|
|
70
|
+
observer?.disconnect()
|
|
71
|
+
void loadingTask?.destroy()
|
|
72
|
+
loadingTask = null
|
|
73
|
+
doc.value = null
|
|
74
|
+
pages.value = []
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// ---- Zoom ----
|
|
78
|
+
const sizes = computed(() => pages.value.map(p => p.getViewport({ scale: scale.value })))
|
|
79
|
+
/** The zoom as a scale: a fit works it out from the box and the first page. */
|
|
80
|
+
function fit() {
|
|
81
|
+
const first = pages.value[0]
|
|
82
|
+
const box = scroller.value
|
|
83
|
+
if (!first || !box) return
|
|
84
|
+
const natural = first.getViewport({ scale: 1 })
|
|
85
|
+
const width = (box.clientWidth - 32) / natural.width
|
|
86
|
+
const whole = Math.min(width, (box.clientHeight - 32) / natural.height)
|
|
87
|
+
scale.value = Math.max(0.1, zoom.value === 'width' ? width : zoom.value === 'page' ? whole : zoom.value / 100)
|
|
88
|
+
}
|
|
89
|
+
watch(zoom, () => { fit(); redraw() })
|
|
90
|
+
const steps = [50, 75, 100, 125, 150, 200, 300]
|
|
91
|
+
function zoomBy(direction: 1 | -1) {
|
|
92
|
+
const now = Math.round(scale.value * 100)
|
|
93
|
+
const next = direction > 0 ? steps.find(s => s > now) ?? 400 : [...steps].reverse().find(s => s < now) ?? 25
|
|
94
|
+
zoom.value = next
|
|
95
|
+
}
|
|
96
|
+
const zoomItems = [{ label: 'Fit width', value: 'width' }, { label: 'Fit page', value: 'page' }, ...steps.map(s => ({ label: `${s}%`, value: String(s) }))]
|
|
97
|
+
const zoomChoice = computed({
|
|
98
|
+
get: () => (typeof zoom.value === 'number' ? String(zoom.value) : zoom.value),
|
|
99
|
+
set: (v: string) => { zoom.value = v === 'width' || v === 'page' ? v : Number(v) }
|
|
100
|
+
})
|
|
101
|
+
const zoomShown = computed(() => (typeof zoom.value === 'number' || !zoomItems.some(z => z.value === zoomChoice.value) ? `${Math.round(scale.value * 100)}%` : ''))
|
|
102
|
+
|
|
103
|
+
// ---- Drawing pages as they come into view ----
|
|
104
|
+
const canvases = ref<HTMLCanvasElement[]>([])
|
|
105
|
+
const tasks = new Map<number, RenderTask>()
|
|
106
|
+
const drawn = new Map<number, number>()
|
|
107
|
+
let observer: IntersectionObserver | null = null
|
|
108
|
+
function observe() {
|
|
109
|
+
observer?.disconnect()
|
|
110
|
+
observer = new IntersectionObserver(entries => {
|
|
111
|
+
for (const e of entries) if (e.isIntersecting) void draw(Number((e.target as HTMLElement).dataset.page))
|
|
112
|
+
}, { root: scroller.value, rootMargin: '400px 0px' })
|
|
113
|
+
scroller.value?.querySelectorAll('.t-pdf-page').forEach(el => observer!.observe(el))
|
|
114
|
+
}
|
|
115
|
+
async function draw(index: number) {
|
|
116
|
+
const p = pages.value[index]
|
|
117
|
+
const canvas = canvases.value[index]
|
|
118
|
+
if (!p || !canvas || drawn.get(index) === scale.value) return
|
|
119
|
+
tasks.get(index)?.cancel()
|
|
120
|
+
const ratio = window.devicePixelRatio || 1
|
|
121
|
+
const viewport = p.getViewport({ scale: scale.value * ratio })
|
|
122
|
+
canvas.width = Math.floor(viewport.width)
|
|
123
|
+
canvas.height = Math.floor(viewport.height)
|
|
124
|
+
const task = p.render({ canvas, viewport })
|
|
125
|
+
tasks.set(index, task)
|
|
126
|
+
try {
|
|
127
|
+
await task.promise
|
|
128
|
+
drawn.set(index, scale.value)
|
|
129
|
+
} catch {
|
|
130
|
+
// Cancelled by a newer drawing.
|
|
131
|
+
} finally {
|
|
132
|
+
if (tasks.get(index) === task) tasks.delete(index)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function redraw() {
|
|
136
|
+
drawn.clear()
|
|
137
|
+
void nextTick(() => observe())
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// ---- Pages ----
|
|
141
|
+
function goTo(n: number) {
|
|
142
|
+
const target = Math.min(Math.max(1, Math.round(n) || 1), Math.max(1, count.value))
|
|
143
|
+
page.value = target
|
|
144
|
+
const el = scroller.value?.querySelector<HTMLElement>(`[data-page="${target - 1}"]`)
|
|
145
|
+
if (el && scroller.value) scroller.value.scrollTop = el.offsetTop - 12
|
|
146
|
+
}
|
|
147
|
+
// The page shown is the one nearest the top as it scrolls.
|
|
148
|
+
function scrolled() {
|
|
149
|
+
const box = scroller.value
|
|
150
|
+
if (!box) return
|
|
151
|
+
const els = [...box.querySelectorAll<HTMLElement>('.t-pdf-page')]
|
|
152
|
+
const top = box.scrollTop + box.clientHeight * 0.25
|
|
153
|
+
let current = 1
|
|
154
|
+
for (const el of els) if (el.offsetTop <= top) current = Number(el.dataset.page) + 1
|
|
155
|
+
if (current !== page.value) page.value = current
|
|
156
|
+
}
|
|
157
|
+
const typed = ref('')
|
|
158
|
+
watch(page, p => { typed.value = String(p) }, { immediate: true })
|
|
159
|
+
watch(page, p => {
|
|
160
|
+
const box = scroller.value
|
|
161
|
+
const el = box?.querySelector<HTMLElement>(`[data-page="${p - 1}"]`)
|
|
162
|
+
if (!box || !el) return
|
|
163
|
+
// Changed from outside (not by scrolling): go there.
|
|
164
|
+
if (el.offsetTop > box.scrollTop + box.clientHeight * 0.25 || el.offsetTop + el.offsetHeight < box.scrollTop) box.scrollTop = el.offsetTop - 12
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
function downloadFile() {
|
|
168
|
+
const a = document.createElement('a')
|
|
169
|
+
a.href = props.src
|
|
170
|
+
a.download = name.value
|
|
171
|
+
document.body.appendChild(a)
|
|
172
|
+
a.click()
|
|
173
|
+
a.remove()
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
let sized: ResizeObserver | null = null
|
|
177
|
+
onMounted(() => {
|
|
178
|
+
void open()
|
|
179
|
+
sized = new ResizeObserver(() => {
|
|
180
|
+
if (zoom.value === 'width' || zoom.value === 'page') {
|
|
181
|
+
const before = scale.value
|
|
182
|
+
fit()
|
|
183
|
+
if (Math.abs(before - scale.value) > 0.01) redraw()
|
|
184
|
+
}
|
|
185
|
+
})
|
|
186
|
+
if (scroller.value) sized.observe(scroller.value)
|
|
187
|
+
})
|
|
188
|
+
watch(() => props.src, () => void open())
|
|
189
|
+
onBeforeUnmount(() => {
|
|
190
|
+
sized?.disconnect()
|
|
191
|
+
close()
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
defineExpose({ setPage: goTo, download: downloadFile })
|
|
195
|
+
</script>
|
|
196
|
+
|
|
197
|
+
<template>
|
|
198
|
+
<div class="t-pdf card" :style="{ height: cssHeight }" role="document" :aria-label="label">
|
|
199
|
+
<div v-if="toolbar" class="t-pdf-toolbar">
|
|
200
|
+
<button type="button" class="btn btn-sm btn-ghost-secondary btn-icon" :disabled="page <= 1 || !count" aria-label="Previous page" @click="goTo(page - 1)">
|
|
201
|
+
<TIcon name="chevron-up" />
|
|
202
|
+
</button>
|
|
203
|
+
<button type="button" class="btn btn-sm btn-ghost-secondary btn-icon" :disabled="page >= count || !count" aria-label="Next page" @click="goTo(page + 1)">
|
|
204
|
+
<TIcon name="chevron-down" />
|
|
205
|
+
</button>
|
|
206
|
+
<label class="t-pdf-page-number">
|
|
207
|
+
<input v-model="typed" class="form-control form-control-sm" inputmode="numeric" aria-label="Page" :disabled="!count"
|
|
208
|
+
@keydown.enter="goTo(Number(typed))" @blur="goTo(Number(typed))">
|
|
209
|
+
<span class="text-secondary small text-nowrap">of {{ count || '…' }}</span>
|
|
210
|
+
</label>
|
|
211
|
+
<span class="ms-auto" />
|
|
212
|
+
<button type="button" class="btn btn-sm btn-ghost-secondary btn-icon" :disabled="!count" aria-label="Zoom out" @click="zoomBy(-1)"><TIcon name="zoom-out" /></button>
|
|
213
|
+
<select v-model="zoomChoice" class="form-select form-select-sm t-pdf-zoom" aria-label="Zoom" :disabled="!count">
|
|
214
|
+
<option v-if="zoomShown" :value="zoomChoice" hidden>{{ zoomShown }}</option>
|
|
215
|
+
<option v-for="z in zoomItems" :key="z.value" :value="z.value">{{ z.label }}</option>
|
|
216
|
+
</select>
|
|
217
|
+
<button type="button" class="btn btn-sm btn-ghost-secondary btn-icon" :disabled="!count" aria-label="Zoom in" @click="zoomBy(1)"><TIcon name="zoom-in" /></button>
|
|
218
|
+
<template v-if="download">
|
|
219
|
+
<button type="button" class="btn btn-sm btn-ghost-secondary btn-icon" aria-label="Download" title="Download" @click="downloadFile"><TIcon name="download" /></button>
|
|
220
|
+
<a class="btn btn-sm btn-ghost-secondary btn-icon" :href="src" target="_blank" rel="noopener" aria-label="Open in a new tab" title="Open in a new tab">
|
|
221
|
+
<TIcon name="external-link" />
|
|
222
|
+
</a>
|
|
223
|
+
</template>
|
|
224
|
+
</div>
|
|
225
|
+
<div ref="scroller" class="t-pdf-pages" @scroll.passive="scrolled">
|
|
226
|
+
<div v-if="loading" class="t-pdf-note"><span class="spinner-border spinner-border-sm" role="status" /> Loading the PDF…</div>
|
|
227
|
+
<div v-else-if="problem" class="t-pdf-note flex-column">
|
|
228
|
+
<TIcon name="file-alert" class="text-secondary" />
|
|
229
|
+
<span>{{ problem }}</span>
|
|
230
|
+
<a v-if="download" :href="src" :download="name">Download it</a>
|
|
231
|
+
</div>
|
|
232
|
+
<template v-else>
|
|
233
|
+
<div v-for="(size, i) in sizes" :key="i" class="t-pdf-page" :data-page="i" :style="{ width: `${size.width}px`, height: `${size.height}px` }">
|
|
234
|
+
<canvas :ref="el => { canvases[i] = el as HTMLCanvasElement }" :style="{ width: `${size.width}px`, height: `${size.height}px` }"
|
|
235
|
+
:aria-label="`Page ${i + 1}`" role="img" />
|
|
236
|
+
</div>
|
|
237
|
+
</template>
|
|
238
|
+
</div>
|
|
239
|
+
</div>
|
|
240
|
+
</template>
|
|
241
|
+
|
|
242
|
+
<style scoped>
|
|
243
|
+
.t-pdf { display: flex; flex-direction: column; overflow: hidden; margin: 0; container-type: inline-size; }
|
|
244
|
+
/* A narrow viewer drops its zoom choice, then its zoom buttons, so the page and download stay. */
|
|
245
|
+
@container (max-width: 440px) { .t-pdf-zoom { display: none; } }
|
|
246
|
+
@container (max-width: 340px) { .t-pdf-toolbar button[aria-label^="Zoom"] { display: none; } }
|
|
247
|
+
.t-pdf-toolbar { display: flex; align-items: center; gap: 0.25rem; padding: 0.375rem 0.5rem; border-bottom: 1px solid var(--tblr-border-color); }
|
|
248
|
+
.t-pdf-page-number { display: inline-flex; align-items: center; gap: 0.375rem; margin: 0 0 0 0.25rem; }
|
|
249
|
+
.t-pdf-page-number input { width: 3.25rem; text-align: center; }
|
|
250
|
+
.t-pdf-zoom { width: 7rem; }
|
|
251
|
+
.t-pdf-pages { position: relative; flex: 1; min-height: 0; overflow: auto; padding: 16px; background: var(--tblr-bg-surface-secondary); }
|
|
252
|
+
.t-pdf-page { margin: 0 auto 12px; background: #fff; box-shadow: var(--tblr-box-shadow-card, 0 1px 3px rgba(0, 0, 0, 0.15)); }
|
|
253
|
+
.t-pdf-page canvas { display: block; }
|
|
254
|
+
.t-pdf-note { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; gap: 0.5rem; color: var(--tblr-secondary); font-size: 0.875rem; }
|
|
255
|
+
</style>
|
|
@@ -1,10 +1,42 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
// A
|
|
3
|
-
|
|
2
|
+
// A line between things: across (with an optional label at the start, center or end), or upright
|
|
3
|
+
// inside a row. Its color is the border color unless given (a named color such as 'primary', one
|
|
4
|
+
// of Tabler's, or any CSS color), with a style (solid, dashed, dotted) and a thickness in pixels.
|
|
5
|
+
// `spacing` 'none' drops the space above and below.
|
|
6
|
+
import { computed } from 'vue'
|
|
7
|
+
import { toTabler, type Color } from '../types'
|
|
8
|
+
|
|
9
|
+
const props = withDefaults(defineProps<{
|
|
10
|
+
label?: string
|
|
11
|
+
orientation?: 'horizontal' | 'vertical'
|
|
12
|
+
position?: 'start' | 'center' | 'end'
|
|
13
|
+
color?: Color | string
|
|
14
|
+
variant?: 'solid' | 'dashed' | 'dotted'
|
|
15
|
+
thickness?: number
|
|
16
|
+
spacing?: 'normal' | 'none'
|
|
17
|
+
}>(), { orientation: 'horizontal', position: 'center', variant: 'solid', thickness: 1, spacing: 'normal' })
|
|
18
|
+
|
|
19
|
+
const named = new Set(['primary', 'neutral', 'success', 'warning', 'error', 'info'])
|
|
20
|
+
const tabler = new Set(['blue', 'azure', 'indigo', 'purple', 'pink', 'red', 'orange', 'yellow', 'lime', 'green', 'teal', 'cyan', 'secondary', 'muted'])
|
|
21
|
+
const line = computed(() => {
|
|
22
|
+
const c = props.color
|
|
23
|
+
const color = !c ? 'var(--tblr-border-color)' : named.has(c) ? `var(--tblr-${toTabler(c as Color)})` : tabler.has(c) ? `var(--tblr-${c})` : c
|
|
24
|
+
return { '--t-separator-color': color, '--t-separator-style': props.variant, '--t-separator-width': `${Math.max(1, Math.min(props.thickness, 12))}px` }
|
|
25
|
+
})
|
|
4
26
|
</script>
|
|
5
27
|
|
|
6
28
|
<template>
|
|
7
|
-
<div v-if="orientation === 'vertical'" class="
|
|
8
|
-
<div v-else-if="label" class="hr-text
|
|
9
|
-
|
|
29
|
+
<div v-if="orientation === 'vertical'" class="t-separator t-separator-vertical" role="separator" aria-orientation="vertical" :style="line" />
|
|
30
|
+
<div v-else-if="label" :class="['t-separator hr-text', { 'hr-text-start': position === 'start', 'hr-text-end': position === 'end', 't-separator-flush': spacing === 'none' }]"
|
|
31
|
+
role="separator" :style="line"><span>{{ label }}</span></div>
|
|
32
|
+
<hr v-else :class="['t-separator', spacing === 'none' ? 'my-0' : 'my-3']" :style="line">
|
|
10
33
|
</template>
|
|
34
|
+
|
|
35
|
+
<style scoped>
|
|
36
|
+
hr.t-separator { border: 0; border-top: var(--t-separator-width) var(--t-separator-style) var(--t-separator-color); opacity: 1; }
|
|
37
|
+
.t-separator.hr-text::before, .t-separator.hr-text::after {
|
|
38
|
+
height: 0; background: none; border-top: var(--t-separator-width) var(--t-separator-style) var(--t-separator-color);
|
|
39
|
+
}
|
|
40
|
+
.t-separator-flush { margin: 0.5rem 0; }
|
|
41
|
+
.t-separator-vertical { align-self: stretch; width: 0; min-height: 1em; border-inline-start: var(--t-separator-width) var(--t-separator-style) var(--t-separator-color); }
|
|
42
|
+
</style>
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// A main action with a menu of others beside it (Tabler's split dropdown): the button runs `click`;
|
|
3
|
+
// the arrow opens the menu, whose items are TDropdown's (groups split by dividers, icons,
|
|
4
|
+
// Disabled, a color such as error for a dangerous one, `onSelect`, `to`, `href`). `select` reports
|
|
5
|
+
// the item chosen.
|
|
6
|
+
import { onBeforeUnmount, onMounted, ref } from 'vue'
|
|
7
|
+
import { Dropdown } from '@tabler/core'
|
|
8
|
+
import TButton from './TButton.vue'
|
|
9
|
+
import DropdownItems from './internal/DropdownItems.vue'
|
|
10
|
+
import type { Color, DropdownItem, Size } from '../types'
|
|
11
|
+
|
|
12
|
+
withDefaults(defineProps<{
|
|
13
|
+
label?: string
|
|
14
|
+
icon?: string
|
|
15
|
+
items: DropdownItem[] | DropdownItem[][]
|
|
16
|
+
color?: Color
|
|
17
|
+
variant?: 'solid' | 'outline' | 'ghost' | 'soft'
|
|
18
|
+
size?: Size
|
|
19
|
+
disabled?: boolean
|
|
20
|
+
/** Only the menu is off. */
|
|
21
|
+
menuDisabled?: boolean
|
|
22
|
+
loading?: boolean
|
|
23
|
+
block?: boolean
|
|
24
|
+
align?: 'start' | 'end'
|
|
25
|
+
/** The arrow's name, for screen readers. */
|
|
26
|
+
menuLabel?: string
|
|
27
|
+
}>(), { color: 'primary', variant: 'solid', size: 'md', align: 'end', menuLabel: 'More actions' })
|
|
28
|
+
|
|
29
|
+
const emit = defineEmits<{ click: [event: MouseEvent]; select: [item: DropdownItem, event: Event]; 'update:open': [open: boolean] }>()
|
|
30
|
+
|
|
31
|
+
const toggle = ref<InstanceType<typeof TButton>>()
|
|
32
|
+
let dropdown: Dropdown | null = null
|
|
33
|
+
|
|
34
|
+
onMounted(() => {
|
|
35
|
+
const el = toggle.value!.$el as HTMLElement
|
|
36
|
+
// A fixed popper escapes containers that clip (cards, tables, the sidebar).
|
|
37
|
+
dropdown = new Dropdown(el, { popperConfig: (config: object) => ({ ...config, strategy: 'fixed' }) })
|
|
38
|
+
el.addEventListener('show.bs.dropdown', () => emit('update:open', true))
|
|
39
|
+
el.addEventListener('hide.bs.dropdown', () => emit('update:open', false))
|
|
40
|
+
})
|
|
41
|
+
onBeforeUnmount(() => dropdown?.dispose())
|
|
42
|
+
</script>
|
|
43
|
+
|
|
44
|
+
<template>
|
|
45
|
+
<div :class="['btn-group', { 'w-100 d-flex': block }]" role="group">
|
|
46
|
+
<TButton :label="label" :icon="icon" :color="color" :variant="variant" :size="size" :disabled="disabled" :loading="loading"
|
|
47
|
+
:class="{ 'flex-fill': block }" @click="(event: MouseEvent) => emit('click', event)" />
|
|
48
|
+
<TButton ref="toggle" :color="color" :variant="variant" :size="size" :disabled="disabled || menuDisabled" class="dropdown-toggle dropdown-toggle-split flex-grow-0"
|
|
49
|
+
data-bs-toggle="dropdown" aria-haspopup="menu" aria-expanded="false" :aria-label="menuLabel" />
|
|
50
|
+
<div :class="['dropdown-menu', align === 'end' && 'dropdown-menu-end']" role="menu">
|
|
51
|
+
<DropdownItems :items="items" @select="(item, event) => emit('select', item, event)" />
|
|
52
|
+
</div>
|
|
53
|
+
</div>
|
|
54
|
+
</template>
|
|
@@ -1,56 +1,69 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
// A figure on an overview page: label, value, a line under it, and an icon in a tinted square. A
|
|
3
|
-
// `trend` (the change, in percent) shows beside the value, green when it's good: up, unless
|
|
4
|
-
// `trendGood` is 'down' (costs, errors). `sparkline` draws the recent values under it.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
</div>
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
</
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// A figure on an overview page: label, value, a line under it, and an icon in a tinted square. A
|
|
3
|
+
// `trend` (the change, in percent) shows beside the value, green when it's good: up, unless
|
|
4
|
+
// `trendGood` is 'down' (costs, errors). `sparkline` draws the recent values under it. `loading`
|
|
5
|
+
// holds placeholders where the value and trend go; `clickable` makes the card a button that emits
|
|
6
|
+
// `click` (Enter and Space too).
|
|
7
|
+
import { computed } from 'vue'
|
|
8
|
+
import TIcon from './TIcon.vue'
|
|
9
|
+
import TSparkline from './TSparkline.vue'
|
|
10
|
+
import { toTabler, type Color } from '../types'
|
|
11
|
+
|
|
12
|
+
const props = withDefaults(defineProps<{
|
|
13
|
+
label: string
|
|
14
|
+
value: string | number
|
|
15
|
+
detail?: string
|
|
16
|
+
icon?: string
|
|
17
|
+
color?: Color
|
|
18
|
+
trend?: number | null
|
|
19
|
+
trendLabel?: string
|
|
20
|
+
trendGood?: 'up' | 'down'
|
|
21
|
+
sparkline?: number[]
|
|
22
|
+
sparklineType?: 'line' | 'bar'
|
|
23
|
+
loading?: boolean
|
|
24
|
+
clickable?: boolean
|
|
25
|
+
}>(), { color: 'primary', trendGood: 'up', sparklineType: 'line' })
|
|
26
|
+
|
|
27
|
+
const emit = defineEmits<{ click: [event: MouseEvent | KeyboardEvent] }>()
|
|
28
|
+
function press(event: MouseEvent | KeyboardEvent) {
|
|
29
|
+
if (props.clickable) emit('click', event)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const trendText = computed(() => props.trend == null ? '' : `${Math.abs(Math.round(props.trend * 10) / 10)}%`)
|
|
33
|
+
const trendClass = computed(() => {
|
|
34
|
+
if (props.trend == null || props.trend === 0) return 'text-secondary'
|
|
35
|
+
return (props.trend > 0) === (props.trendGood === 'up') ? 'text-success' : 'text-danger'
|
|
36
|
+
})
|
|
37
|
+
const trendIcon = computed(() => !props.trend ? 'minus' : props.trend > 0 ? 'trending-up' : 'trending-down')
|
|
38
|
+
</script>
|
|
39
|
+
|
|
40
|
+
<template>
|
|
41
|
+
<div :class="['card', { 't-stat-clickable': clickable }]" :role="clickable ? 'button' : undefined" :tabindex="clickable ? 0 : undefined"
|
|
42
|
+
:aria-busy="loading || undefined" @click="press" @keydown.enter.prevent="press" @keydown.space.prevent="press">
|
|
43
|
+
<div class="card-body">
|
|
44
|
+
<div class="d-flex align-items-start justify-content-between gap-3">
|
|
45
|
+
<div class="min-w-0">
|
|
46
|
+
<div class="subheader">{{ label }}</div>
|
|
47
|
+
<div v-if="loading" class="mt-2 placeholder-glow" aria-label="Loading">
|
|
48
|
+
<span class="placeholder col-7 t-stat-placeholder" />
|
|
49
|
+
</div>
|
|
50
|
+
<div v-else class="d-flex align-items-baseline gap-2 mt-2">
|
|
51
|
+
<div class="h1 mb-0">{{ value }}</div>
|
|
52
|
+
<span v-if="trend != null" :class="['d-inline-flex align-items-center gap-1 lh-1 small fw-medium', trendClass]"
|
|
53
|
+
:title="trendLabel">
|
|
54
|
+
{{ trendText }}<TIcon :name="trendIcon" />
|
|
55
|
+
</span>
|
|
56
|
+
</div>
|
|
57
|
+
<div v-if="detail || trendLabel || $slots.detail" class="text-secondary small mt-1">
|
|
58
|
+
<slot name="detail">{{ detail ?? trendLabel }}</slot>
|
|
59
|
+
</div>
|
|
60
|
+
</div>
|
|
61
|
+
<div v-if="icon" :class="['t-stat-icon', `bg-${toTabler(color)}-lt`]">
|
|
62
|
+
<TIcon :name="icon" />
|
|
63
|
+
</div>
|
|
64
|
+
</div>
|
|
65
|
+
<TSparkline v-if="sparkline?.length && !loading" :values="sparkline" :type="sparklineType" :color="color" :width="240" :height="36"
|
|
66
|
+
class="t-stat-sparkline mt-3" :label="`${label}: recent values`" />
|
|
67
|
+
</div>
|
|
68
|
+
</div>
|
|
69
|
+
</template>
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// A time of day, typed (masked with IMask: hours, minutes, seconds if wanted, and AM or PM in 12
|
|
3
|
+
// hours) or picked from a list at `step` minutes. v-model is always "HH:mm" (or "HH:mm:ss" with
|
|
4
|
+
// `seconds`), 24-hour, however it's shown; null when empty. `min` and `max` ("HH:mm") limit the
|
|
5
|
+
// list, and a typed time outside them shows as invalid. IMask loads on first use.
|
|
6
|
+
import { computed, onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue'
|
|
7
|
+
import { Dropdown } from '@tabler/core'
|
|
8
|
+
import TInputMask from './TInputMask.vue'
|
|
9
|
+
import TIcon from './TIcon.vue'
|
|
10
|
+
import { useField } from '../composables/useField'
|
|
11
|
+
|
|
12
|
+
const props = withDefaults(defineProps<{
|
|
13
|
+
/** Shown with AM and PM. */
|
|
14
|
+
hour12?: boolean
|
|
15
|
+
seconds?: boolean
|
|
16
|
+
/** The list's step, in minutes (none: no list). */
|
|
17
|
+
step?: 1 | 5 | 10 | 15 | 30 | 60 | null
|
|
18
|
+
min?: string | null
|
|
19
|
+
max?: string | null
|
|
20
|
+
placeholder?: string
|
|
21
|
+
size?: 'sm' | 'md' | 'lg'
|
|
22
|
+
disabled?: boolean
|
|
23
|
+
invalid?: boolean
|
|
24
|
+
id?: string
|
|
25
|
+
}>(), { hour12: false, seconds: false, step: 30, min: null, max: null, size: 'md' })
|
|
26
|
+
|
|
27
|
+
const model = defineModel<string | null | undefined>()
|
|
28
|
+
const field = useField(props)
|
|
29
|
+
|
|
30
|
+
// ---- Times as seconds since midnight ----
|
|
31
|
+
const parse = (text: string | null | undefined): number | null => {
|
|
32
|
+
const m = /^(\d{1,2}):(\d{2})(?::(\d{2}))?$/.exec(text ?? '')
|
|
33
|
+
if (!m) return null
|
|
34
|
+
const [h, mi, s] = [Number(m[1]), Number(m[2]), Number(m[3] ?? 0)]
|
|
35
|
+
return h < 24 && mi < 60 && s < 60 ? h * 3600 + mi * 60 + s : null
|
|
36
|
+
}
|
|
37
|
+
const pad = (n: number) => String(n).padStart(2, '0')
|
|
38
|
+
/** The model's text: 24-hour, with seconds or not. */
|
|
39
|
+
const toModel = (t: number) => `${pad(Math.floor(t / 3600))}:${pad(Math.floor(t / 60) % 60)}${props.seconds ? `:${pad(t % 60)}` : ''}`
|
|
40
|
+
/** As it's shown and typed. */
|
|
41
|
+
function toText(t: number) {
|
|
42
|
+
const h = Math.floor(t / 3600)
|
|
43
|
+
const rest = `${pad(Math.floor(t / 60) % 60)}${props.seconds ? `:${pad(t % 60)}` : ''}`
|
|
44
|
+
return props.hour12 ? `${pad(h % 12 || 12)}:${rest} ${h < 12 ? 'AM' : 'PM'}` : `${pad(h)}:${rest}`
|
|
45
|
+
}
|
|
46
|
+
/** A typed text's time, when it's complete. */
|
|
47
|
+
function fromText(text: string): number | null {
|
|
48
|
+
const m = props.hour12
|
|
49
|
+
? /^(\d{2}):(\d{2})(?::(\d{2}))? (AM|PM)$/i.exec(text.trim())
|
|
50
|
+
: /^(\d{2}):(\d{2})(?::(\d{2}))?$/.exec(text.trim())
|
|
51
|
+
if (!m || (props.seconds && m[3] === undefined)) return null
|
|
52
|
+
let h = Number(m[1])
|
|
53
|
+
if (props.hour12) {
|
|
54
|
+
if (h < 1 || h > 12) return null
|
|
55
|
+
h = (h % 12) + (m[4]!.toUpperCase() === 'PM' ? 12 : 0)
|
|
56
|
+
}
|
|
57
|
+
const t = h * 3600 + Number(m[2]) * 60 + Number(m[3] ?? 0)
|
|
58
|
+
return h < 24 && Number(m[2]) < 60 && Number(m[3] ?? 0) < 60 ? t : null
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const lowest = computed(() => parse(props.min))
|
|
62
|
+
const highest = computed(() => parse(props.max))
|
|
63
|
+
const outside = (t: number) => (lowest.value !== null && t < lowest.value) || (highest.value !== null && t > highest.value)
|
|
64
|
+
const current = computed(() => parse(model.value))
|
|
65
|
+
|
|
66
|
+
// ---- The masked text ----
|
|
67
|
+
const text = ref('')
|
|
68
|
+
watch([() => model.value, () => props.hour12, () => props.seconds], () => {
|
|
69
|
+
const t = current.value
|
|
70
|
+
if (t === null ? text.value !== '' && fromText(text.value) !== null : fromText(text.value) !== t) text.value = t === null ? '' : toText(t)
|
|
71
|
+
}, { immediate: true })
|
|
72
|
+
function typed(value: string | null | undefined) {
|
|
73
|
+
text.value = value ?? ''
|
|
74
|
+
if (!text.value.trim()) {
|
|
75
|
+
if (model.value != null) model.value = null
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
const t = fromText(text.value)
|
|
79
|
+
if (t !== null && toModel(t) !== model.value) model.value = toModel(t)
|
|
80
|
+
}
|
|
81
|
+
// Left half-typed: back to the time it holds.
|
|
82
|
+
function settle() {
|
|
83
|
+
if (text.value.trim() && fromText(text.value) === null) text.value = current.value === null ? '' : toText(current.value)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// IMask's ranges and AM/PM, once it's loaded.
|
|
87
|
+
const imask = shallowRef<{ MaskedRange: unknown; MaskedEnum: unknown } | null>(null)
|
|
88
|
+
onMounted(async () => { imask.value = (await import('imask')).default as unknown as { MaskedRange: unknown; MaskedEnum: unknown } })
|
|
89
|
+
const options = computed(() => {
|
|
90
|
+
const I = imask.value
|
|
91
|
+
if (!I) return undefined
|
|
92
|
+
const range = (from: number, to: number) => ({ mask: I.MaskedRange, from, to, maxLength: 2, autofix: 'pad' })
|
|
93
|
+
return {
|
|
94
|
+
mask: `HH:MM${props.seconds ? ':SS' : ''}${props.hour12 ? ' aa' : ''}`,
|
|
95
|
+
blocks: {
|
|
96
|
+
HH: props.hour12 ? range(1, 12) : range(0, 23), MM: range(0, 59), SS: range(0, 59),
|
|
97
|
+
aa: { mask: I.MaskedEnum, enum: ['AM', 'PM'], prepareChar: (c: string) => c.toUpperCase() }
|
|
98
|
+
},
|
|
99
|
+
lazy: true, overwrite: true
|
|
100
|
+
}
|
|
101
|
+
})
|
|
102
|
+
const shownPlaceholder = computed(() => props.placeholder ?? `${props.hour12 ? 'hh' : 'HH'}:mm${props.seconds ? ':ss' : ''}${props.hour12 ? ' AM' : ''}`)
|
|
103
|
+
|
|
104
|
+
// ---- The list ----
|
|
105
|
+
const times = computed(() => {
|
|
106
|
+
if (!props.step) return []
|
|
107
|
+
const from = lowest.value ?? 0
|
|
108
|
+
const to = highest.value ?? 24 * 3600 - 1
|
|
109
|
+
const list: number[] = []
|
|
110
|
+
for (let t = Math.ceil(from / (props.step * 60)) * props.step * 60; t <= to && list.length < 1440; t += props.step * 60) list.push(t)
|
|
111
|
+
return list
|
|
112
|
+
})
|
|
113
|
+
const toggle = ref<HTMLButtonElement>()
|
|
114
|
+
const menu = ref<HTMLElement>()
|
|
115
|
+
let dropdown: Dropdown | null = null
|
|
116
|
+
onMounted(() => {
|
|
117
|
+
if (!toggle.value) return
|
|
118
|
+
dropdown = new Dropdown(toggle.value, { popperConfig: (config: object) => ({ ...config, strategy: 'fixed' }) })
|
|
119
|
+
// The chosen time (or the nearest after it) is scrolled into view.
|
|
120
|
+
toggle.value.addEventListener('shown.bs.dropdown', () => {
|
|
121
|
+
const at = current.value ?? 0
|
|
122
|
+
const index = Math.max(0, times.value.findIndex(t => t >= at))
|
|
123
|
+
const item = menu.value?.children[index] as HTMLElement | undefined
|
|
124
|
+
if (item && menu.value) menu.value.scrollTop = item.offsetTop - menu.value.clientHeight / 2 + item.clientHeight / 2
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
onBeforeUnmount(() => dropdown?.dispose())
|
|
128
|
+
function pick(t: number) {
|
|
129
|
+
model.value = toModel(t)
|
|
130
|
+
text.value = toText(t)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const wrong = computed(() => field.invalid.value || (current.value !== null && outside(current.value)))
|
|
134
|
+
</script>
|
|
135
|
+
|
|
136
|
+
<template>
|
|
137
|
+
<div :class="['input-group flex-nowrap t-time-input', size !== 'md' && `input-group-${size}`]">
|
|
138
|
+
<TInputMask :id="field.id.value" :model-value="text" :options="options" :placeholder="shownPlaceholder" :size="size" :disabled="disabled" :invalid="wrong"
|
|
139
|
+
class="flex-fill" @update:model-value="typed" @focusout="settle" />
|
|
140
|
+
<template v-if="step">
|
|
141
|
+
<button ref="toggle" type="button" class="btn btn-icon" data-bs-toggle="dropdown" aria-haspopup="listbox" aria-expanded="false"
|
|
142
|
+
:disabled="disabled" aria-label="Choose a time">
|
|
143
|
+
<TIcon name="clock" />
|
|
144
|
+
</button>
|
|
145
|
+
<div ref="menu" class="dropdown-menu dropdown-menu-end t-time-list" role="listbox">
|
|
146
|
+
<button v-for="t in times" :key="t" type="button" role="option" :aria-selected="t === current"
|
|
147
|
+
:class="['dropdown-item', { active: t === current }]" @click="pick(t)">{{ toText(t) }}</button>
|
|
148
|
+
</div>
|
|
149
|
+
</template>
|
|
150
|
+
</div>
|
|
151
|
+
</template>
|
|
152
|
+
|
|
153
|
+
<style scoped>
|
|
154
|
+
.t-time-input :deep(.form-control) { min-width: 0; }
|
|
155
|
+
.t-time-input > :first-child { min-width: 0; }
|
|
156
|
+
/* The field joins the clock button, as an input group's own fields do. */
|
|
157
|
+
.t-time-input:has(> .btn) > :first-child :deep(.form-control) { border-top-right-radius: 0; border-bottom-right-radius: 0; }
|
|
158
|
+
.t-time-list { max-height: 16rem; overflow-y: auto; min-width: 8rem; }
|
|
159
|
+
</style>
|