@ternarytech-io/gears-ui 0.2.1 → 0.3.1
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 +162 -161
- package/package.json +113 -111
- package/src/components/TAccentPicker.vue +41 -41
- package/src/components/TChart.vue +188 -0
- package/src/components/TContainerPicker.vue +22 -22
- package/src/components/TDataGrid.vue +41 -0
- package/src/components/TDataGridItem.vue +23 -0
- package/src/components/TDensityPicker.vue +19 -19
- package/src/components/TEditableTable.vue +203 -0
- package/src/components/TFontPicker.vue +23 -23
- package/src/components/TNavPositionPicker.vue +22 -22
- package/src/components/TRadiusPicker.vue +21 -21
- package/src/components/TScaledFrame.vue +36 -36
- package/src/components/TShell.vue +82 -72
- package/src/components/TSortableTree.vue +73 -73
- package/src/components/TSortableTreeList.vue +60 -60
- package/src/components/TSparkline.vue +34 -0
- package/src/components/TStatCard.vue +56 -26
- package/src/components/TTable.vue +138 -134
- package/src/components/TThemeBasePicker.vue +22 -22
- package/src/components/sortableTree.ts +19 -19
- package/src/index.ts +227 -215
- package/src/plugins/charts.ts +4 -0
- package/src/plugins/index.ts +55 -52
- package/src/styles/gears.css +666 -625
- package/src/theme.ts +50 -45
- package/src/types.ts +38 -1
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
// A theme's font, as tiles showing "Aa" in each typeface. `fonts` limits the choices; Comic is left
|
|
3
|
-
// out unless asked for.
|
|
4
|
-
import { computed } from 'vue'
|
|
5
|
-
import TRadioGroup from './TRadioGroup.vue'
|
|
6
|
-
import { themeFonts } from '../theme'
|
|
7
|
-
import type { ThemeFont } from '../types'
|
|
8
|
-
|
|
9
|
-
const props = withDefaults(defineProps<{ fonts?: ThemeFont[]; disabled?: boolean; legend?: string }>(),
|
|
10
|
-
{ fonts: () => ['sans-serif', 'serif', 'monospace'] })
|
|
11
|
-
|
|
12
|
-
const model = defineModel<ThemeFont | null | undefined>()
|
|
13
|
-
const items = computed(() => themeFonts.filter(f => props.fonts.includes(f.value)))
|
|
14
|
-
const family = (font: ThemeFont) => `var(--tblr-font-${font})`
|
|
15
|
-
</script>
|
|
16
|
-
|
|
17
|
-
<template>
|
|
18
|
-
<TRadioGroup v-model="model" :items="items" variant="tiles" :disabled="disabled" :legend="legend">
|
|
19
|
-
<template #preview="{ item }">
|
|
20
|
-
<span class="t-theme-font" :style="{ fontFamily: family(item.value as ThemeFont) }">Aa</span>
|
|
21
|
-
</template>
|
|
22
|
-
</TRadioGroup>
|
|
23
|
-
</template>
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// A theme's font, as tiles showing "Aa" in each typeface. `fonts` limits the choices; Comic is left
|
|
3
|
+
// out unless asked for.
|
|
4
|
+
import { computed } from 'vue'
|
|
5
|
+
import TRadioGroup from './TRadioGroup.vue'
|
|
6
|
+
import { themeFonts } from '../theme'
|
|
7
|
+
import type { ThemeFont } from '../types'
|
|
8
|
+
|
|
9
|
+
const props = withDefaults(defineProps<{ fonts?: ThemeFont[]; disabled?: boolean; legend?: string }>(),
|
|
10
|
+
{ fonts: () => ['sans-serif', 'serif', 'monospace'] })
|
|
11
|
+
|
|
12
|
+
const model = defineModel<ThemeFont | null | undefined>()
|
|
13
|
+
const items = computed(() => themeFonts.filter(f => props.fonts.includes(f.value)))
|
|
14
|
+
const family = (font: ThemeFont) => `var(--tblr-font-${font})`
|
|
15
|
+
</script>
|
|
16
|
+
|
|
17
|
+
<template>
|
|
18
|
+
<TRadioGroup v-model="model" :items="items" variant="tiles" :disabled="disabled" :legend="legend">
|
|
19
|
+
<template #preview="{ item }">
|
|
20
|
+
<span class="t-theme-font" :style="{ fontFamily: family(item.value as ThemeFont) }">Aa</span>
|
|
21
|
+
</template>
|
|
22
|
+
</TRadioGroup>
|
|
23
|
+
</template>
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
// Where an application's navigation goes: down the left side or across the top. Each tile is a
|
|
3
|
-
// sketch of the page.
|
|
4
|
-
import TRadioGroup from './TRadioGroup.vue'
|
|
5
|
-
import { themeNavs } from '../theme'
|
|
6
|
-
import type { ThemeNav } from '../types'
|
|
7
|
-
|
|
8
|
-
defineProps<{ disabled?: boolean; legend?: string }>()
|
|
9
|
-
|
|
10
|
-
const model = defineModel<ThemeNav | null | undefined>()
|
|
11
|
-
</script>
|
|
12
|
-
|
|
13
|
-
<template>
|
|
14
|
-
<TRadioGroup v-model="model" :items="themeNavs" variant="tiles" tile-width="8rem" :disabled="disabled" :legend="legend">
|
|
15
|
-
<template #preview="{ item }">
|
|
16
|
-
<span :class="['t-sketch', item.value === 'top' ? 't-sketch-top' : 't-sketch-left']">
|
|
17
|
-
<span class="t-sketch-nav" />
|
|
18
|
-
<span class="t-sketch-page"><span /><span /><span /></span>
|
|
19
|
-
</span>
|
|
20
|
-
</template>
|
|
21
|
-
</TRadioGroup>
|
|
22
|
-
</template>
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// Where an application's navigation goes: down the left side or across the top. Each tile is a
|
|
3
|
+
// sketch of the page.
|
|
4
|
+
import TRadioGroup from './TRadioGroup.vue'
|
|
5
|
+
import { themeNavs } from '../theme'
|
|
6
|
+
import type { ThemeNav } from '../types'
|
|
7
|
+
|
|
8
|
+
defineProps<{ disabled?: boolean; legend?: string }>()
|
|
9
|
+
|
|
10
|
+
const model = defineModel<ThemeNav | null | undefined>()
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<template>
|
|
14
|
+
<TRadioGroup v-model="model" :items="themeNavs" variant="tiles" tile-width="8rem" :disabled="disabled" :legend="legend">
|
|
15
|
+
<template #preview="{ item }">
|
|
16
|
+
<span :class="['t-sketch', item.value === 'top' ? 't-sketch-top' : 't-sketch-left']">
|
|
17
|
+
<span class="t-sketch-nav" />
|
|
18
|
+
<span class="t-sketch-page"><span /><span /><span /></span>
|
|
19
|
+
</span>
|
|
20
|
+
</template>
|
|
21
|
+
</TRadioGroup>
|
|
22
|
+
</template>
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
// A theme's corner radius,
|
|
3
|
-
// shape
|
|
4
|
-
import TRadioGroup from './TRadioGroup.vue'
|
|
5
|
-
import { themeRadii } from '../theme'
|
|
6
|
-
import type { ThemeRadius } from '../types'
|
|
7
|
-
|
|
8
|
-
defineProps<{ disabled?: boolean; legend?: string }>()
|
|
9
|
-
|
|
10
|
-
const model = defineModel<ThemeRadius | null | undefined>()
|
|
11
|
-
//
|
|
12
|
-
const radius = (
|
|
13
|
-
</script>
|
|
14
|
-
|
|
15
|
-
<template>
|
|
16
|
-
<TRadioGroup v-model="model" :items="themeRadii" variant="tiles" :disabled="disabled" :legend="legend">
|
|
17
|
-
<template #preview="{ item }">
|
|
18
|
-
<span class="t-theme-radius" :style="{ borderRadius: radius(item.value as ThemeRadius) }" />
|
|
19
|
-
</template>
|
|
20
|
-
</TRadioGroup>
|
|
21
|
-
</template>
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// A theme's corner radius, from 0 (square corners) to 2.5 (Tabler's extra large), as tiles showing
|
|
3
|
+
// a button-sized shape with each one's base radius.
|
|
4
|
+
import TRadioGroup from './TRadioGroup.vue'
|
|
5
|
+
import { themeRadii, themeRadiusSizes } from '../theme'
|
|
6
|
+
import type { ThemeRadius } from '../types'
|
|
7
|
+
|
|
8
|
+
defineProps<{ disabled?: boolean; legend?: string }>()
|
|
9
|
+
|
|
10
|
+
const model = defineModel<ThemeRadius | null | undefined>()
|
|
11
|
+
// Drawn at its real size, so the tile shows the corner a button or a field gets.
|
|
12
|
+
const radius = (value: ThemeRadius) => themeRadiusSizes[value]
|
|
13
|
+
</script>
|
|
14
|
+
|
|
15
|
+
<template>
|
|
16
|
+
<TRadioGroup v-model="model" :items="themeRadii" variant="tiles" :disabled="disabled" :legend="legend">
|
|
17
|
+
<template #preview="{ item }">
|
|
18
|
+
<span class="t-theme-radius" :style="{ borderRadius: radius(item.value as ThemeRadius) }" />
|
|
19
|
+
</template>
|
|
20
|
+
</TRadioGroup>
|
|
21
|
+
</template>
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
// Shows its content at a screen's width (`width`, in pixels), scaled down to fit: a preview of a
|
|
3
|
-
// whole page, such as an application's layout, in a smaller space. It fills its parent's height;
|
|
4
|
-
// the content scrolls inside it, and fixed or sticky parts stay inside the frame.
|
|
5
|
-
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
|
|
6
|
-
|
|
7
|
-
const props = withDefaults(defineProps<{ width?: number }>(), { width: 1440 })
|
|
8
|
-
|
|
9
|
-
const frame = ref<HTMLElement>()
|
|
10
|
-
const size = ref({ width: 0, height: 0 })
|
|
11
|
-
let observer: ResizeObserver | undefined
|
|
12
|
-
|
|
13
|
-
onMounted(() => {
|
|
14
|
-
observer = new ResizeObserver(([entry]) => { size.value = { width: entry.contentRect.width, height: entry.contentRect.height } })
|
|
15
|
-
observer.observe(frame.value!)
|
|
16
|
-
})
|
|
17
|
-
onBeforeUnmount(() => observer?.disconnect())
|
|
18
|
-
|
|
19
|
-
// Never larger than life.
|
|
20
|
-
const scale = computed(() => size.value.width ? Math.min(1, size.value.width / props.width) : 1)
|
|
21
|
-
const screen = computed(() => ({
|
|
22
|
-
width: `${props.width}px`,
|
|
23
|
-
height: `${size.value.height / scale.value}px`,
|
|
24
|
-
transform: `scale(${scale.value})`
|
|
25
|
-
}))
|
|
26
|
-
</script>
|
|
27
|
-
|
|
28
|
-
<template>
|
|
29
|
-
<div ref="frame" class="t-scaled-frame">
|
|
30
|
-
<div class="t-scaled-screen" :style="screen">
|
|
31
|
-
<div class="t-scaled-scroll">
|
|
32
|
-
<slot :scale="scale" />
|
|
33
|
-
</div>
|
|
34
|
-
</div>
|
|
35
|
-
</div>
|
|
36
|
-
</template>
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// Shows its content at a screen's width (`width`, in pixels), scaled down to fit: a preview of a
|
|
3
|
+
// whole page, such as an application's layout, in a smaller space. It fills its parent's height;
|
|
4
|
+
// the content scrolls inside it, and fixed or sticky parts stay inside the frame.
|
|
5
|
+
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
|
|
6
|
+
|
|
7
|
+
const props = withDefaults(defineProps<{ width?: number }>(), { width: 1440 })
|
|
8
|
+
|
|
9
|
+
const frame = ref<HTMLElement>()
|
|
10
|
+
const size = ref({ width: 0, height: 0 })
|
|
11
|
+
let observer: ResizeObserver | undefined
|
|
12
|
+
|
|
13
|
+
onMounted(() => {
|
|
14
|
+
observer = new ResizeObserver(([entry]) => { size.value = { width: entry.contentRect.width, height: entry.contentRect.height } })
|
|
15
|
+
observer.observe(frame.value!)
|
|
16
|
+
})
|
|
17
|
+
onBeforeUnmount(() => observer?.disconnect())
|
|
18
|
+
|
|
19
|
+
// Never larger than life.
|
|
20
|
+
const scale = computed(() => size.value.width ? Math.min(1, size.value.width / props.width) : 1)
|
|
21
|
+
const screen = computed(() => ({
|
|
22
|
+
width: `${props.width}px`,
|
|
23
|
+
height: `${size.value.height / scale.value}px`,
|
|
24
|
+
transform: `scale(${scale.value})`
|
|
25
|
+
}))
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
<template>
|
|
29
|
+
<div ref="frame" class="t-scaled-frame">
|
|
30
|
+
<div class="t-scaled-screen" :style="screen">
|
|
31
|
+
<div class="t-scaled-scroll">
|
|
32
|
+
<slot :scale="scale" />
|
|
33
|
+
</div>
|
|
34
|
+
</div>
|
|
35
|
+
</div>
|
|
36
|
+
</template>
|
|
@@ -1,72 +1,82 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
// An app's frame: the sidebar (dark in both themes) with the brand, the links and a footer, and
|
|
3
|
-
// the page beside it. The sidebar folds to icons (remembered per `storageKey`); on small screens
|
|
4
|
-
// it's a top bar whose menu opens below it.
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// An app's frame: the sidebar (dark in both themes) with the brand, the links and a footer, and
|
|
3
|
+
// the page beside it. The sidebar folds to icons (remembered per `storageKey`); on small screens
|
|
4
|
+
// it's a top bar whose menu opens below it. `fold` folds it for a while (a full-screen editor, say)
|
|
5
|
+
// without changing what's remembered; the toggle still opens it for the moment.
|
|
6
|
+
import { computed, ref, watch } from 'vue'
|
|
7
|
+
import { RouterLink, useRoute } from 'vue-router'
|
|
8
|
+
import TIcon from './TIcon.vue'
|
|
9
|
+
import TNav from './TNav.vue'
|
|
10
|
+
import type { NavItem, Target } from '../types'
|
|
11
|
+
|
|
12
|
+
const props = withDefaults(defineProps<{
|
|
13
|
+
nav: NavItem[]
|
|
14
|
+
footerNav?: NavItem[]
|
|
15
|
+
brand?: string
|
|
16
|
+
// A line under the brand name, in small capitals.
|
|
17
|
+
tagline?: string
|
|
18
|
+
logo?: string
|
|
19
|
+
brandTo?: Target
|
|
20
|
+
foldable?: boolean
|
|
21
|
+
storageKey?: string
|
|
22
|
+
fold?: boolean
|
|
23
|
+
}>(), { brandTo: '/', foldable: true, storageKey: 'gears-sidebar', fold: false })
|
|
24
|
+
|
|
25
|
+
function storedFold() {
|
|
26
|
+
try { return localStorage.getItem(props.storageKey) === 'folded' } catch { return false }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const remembered = ref(props.foldable && storedFold())
|
|
30
|
+
// While `fold` is on: folded unless the toggle opened it; null when `fold` is off.
|
|
31
|
+
const temporary = ref<boolean | null>(props.foldable && props.fold ? true : null)
|
|
32
|
+
const folded = computed(() => temporary.value ?? remembered.value)
|
|
33
|
+
const menuOpen = ref(false)
|
|
34
|
+
const route = useRoute()
|
|
35
|
+
|
|
36
|
+
watch(remembered, value => {
|
|
37
|
+
try { localStorage.setItem(props.storageKey, value ? 'folded' : 'open') } catch { /* storage blocked */ }
|
|
38
|
+
})
|
|
39
|
+
watch(() => props.fold, fold => { temporary.value = props.foldable && fold ? true : null })
|
|
40
|
+
function toggle() {
|
|
41
|
+
if (temporary.value !== null) temporary.value = !temporary.value
|
|
42
|
+
else remembered.value = !remembered.value
|
|
43
|
+
}
|
|
44
|
+
watch(() => route.fullPath, () => { menuOpen.value = false })
|
|
45
|
+
</script>
|
|
46
|
+
|
|
47
|
+
<template>
|
|
48
|
+
<div class="page t-shell">
|
|
49
|
+
<aside :class="['navbar navbar-vertical navbar-expand-lg', { 'navbar-folded': folded }]" data-bs-theme="dark">
|
|
50
|
+
<div class="container-fluid">
|
|
51
|
+
<button type="button" class="navbar-toggler" :aria-expanded="menuOpen" aria-label="Menu" @click="menuOpen = !menuOpen">
|
|
52
|
+
<span class="navbar-toggler-icon" />
|
|
53
|
+
</button>
|
|
54
|
+
<div class="navbar-brand">
|
|
55
|
+
<slot name="brand" :folded="folded">
|
|
56
|
+
<RouterLink :to="brandTo" class="d-flex align-items-center gap-2 text-reset text-decoration-none">
|
|
57
|
+
<img v-if="logo" :src="logo" alt="" class="navbar-brand-image" width="32" height="32">
|
|
58
|
+
<span v-if="!folded && brand" class="text-start lh-sm">
|
|
59
|
+
<span class="d-block fw-bold">{{ brand }}</span>
|
|
60
|
+
<span v-if="tagline" class="d-block text-secondary text-uppercase fw-medium" style="font-size: 0.625rem; letter-spacing: 0.18em">{{ tagline }}</span>
|
|
61
|
+
</span>
|
|
62
|
+
</RouterLink>
|
|
63
|
+
</slot>
|
|
64
|
+
</div>
|
|
65
|
+
<button v-if="foldable" type="button" class="t-fold-toggle d-none d-lg-grid"
|
|
66
|
+
:aria-label="folded ? 'Expand the menu' : 'Fold the menu'" @click="toggle">
|
|
67
|
+
<TIcon :name="folded ? 'chevron-right' : 'chevron-left'" />
|
|
68
|
+
</button>
|
|
69
|
+
<div :class="['collapse navbar-collapse', { show: menuOpen }]">
|
|
70
|
+
<TNav :items="nav" :folded="folded" class="pt-lg-3" />
|
|
71
|
+
<div v-if="footerNav || $slots['sidebar-footer']" class="navbar-footer">
|
|
72
|
+
<TNav v-if="footerNav" :items="footerNav" :folded="folded" />
|
|
73
|
+
<slot name="sidebar-footer" :folded="folded" />
|
|
74
|
+
</div>
|
|
75
|
+
</div>
|
|
76
|
+
</div>
|
|
77
|
+
</aside>
|
|
78
|
+
<div class="page-wrapper">
|
|
79
|
+
<slot />
|
|
80
|
+
</div>
|
|
81
|
+
</div>
|
|
82
|
+
</template>
|
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
<script setup lang="ts" generic="T extends object">
|
|
2
|
-
// A tree whose items can be dragged into a new order, and into or out of each other, with
|
|
3
|
-
// SortableJS: a menu of pages and sub-pages, say. v-model is the top level; each item keeps its
|
|
4
|
-
// children in `childrenKey` ("children"). The `item` slot draws an item's row (it gets the item,
|
|
5
|
-
// its depth from 1, and its parent). `maxDepth` limits how deep the tree goes (2: items and their
|
|
6
|
-
// children); a drop that would go deeper is refused. With `handle`, rows drag only by an element
|
|
7
|
-
// with the `sortable-handle` class. `move` reports each move. SortableJS loads on first use.
|
|
8
|
-
import { provide, ref, useId } from 'vue'
|
|
9
|
-
import TSortableTreeList from './TSortableTreeList.vue'
|
|
10
|
-
import { treeKey, type TreeContext } from './sortableTree'
|
|
11
|
-
|
|
12
|
-
const props = withDefaults(defineProps<{
|
|
13
|
-
itemKey: string | ((item: T) => string | number)
|
|
14
|
-
childrenKey?: string
|
|
15
|
-
maxDepth?: number
|
|
16
|
-
handle?: boolean
|
|
17
|
-
disabled?: boolean
|
|
18
|
-
}>(), { childrenKey: 'children', maxDepth: 2 })
|
|
19
|
-
|
|
20
|
-
const model = defineModel<T[]>({ default: () => [] })
|
|
21
|
-
const emit = defineEmits<{ move: [move: { item: T; from: { parent: T | null; index: number }; to: { parent: T | null; index: number } }] }>()
|
|
22
|
-
const slots = defineSlots<{ item?(scope: { item: T; depth: number; parent: T | null }): unknown }>()
|
|
23
|
-
const dragging = ref(false)
|
|
24
|
-
|
|
25
|
-
const keyOf = (item: T) => String(typeof props.itemKey === 'function' ? props.itemKey(item) : (item as Record<string, unknown>)[props.itemKey])
|
|
26
|
-
const childrenOf = (item: T): T[] => ((item as Record<string, unknown>)[props.childrenKey] as T[] | undefined) ?? []
|
|
27
|
-
|
|
28
|
-
function find(key: string, items: T[] = model.value): T | null {
|
|
29
|
-
for (const item of items) {
|
|
30
|
-
if (keyOf(item) === key) return item
|
|
31
|
-
const found = find(key, childrenOf(item))
|
|
32
|
-
if (found) return found
|
|
33
|
-
}
|
|
34
|
-
return null
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// The list a parent's children are in, made when it has none yet.
|
|
38
|
-
function listOf(parent: T | null): T[] {
|
|
39
|
-
if (!parent) return model.value
|
|
40
|
-
const record = parent as Record<string, unknown>
|
|
41
|
-
if (!Array.isArray(record[props.childrenKey])) record[props.childrenKey] = []
|
|
42
|
-
return record[props.childrenKey] as T[]
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function move(fromKey: string, oldIndex: number, toKey: string, newIndex: number) {
|
|
46
|
-
const from = fromKey ? find(fromKey) : null
|
|
47
|
-
const to = toKey ? find(toKey) : null
|
|
48
|
-
const [item] = listOf(from).splice(oldIndex, 1)
|
|
49
|
-
if (!item) return
|
|
50
|
-
listOf(to).splice(newIndex, 0, item)
|
|
51
|
-
model.value = [...model.value]
|
|
52
|
-
emit('move', { item, from: { parent: from, index: oldIndex }, to: { parent: to, index: newIndex } })
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const context: TreeContext<T> = {
|
|
56
|
-
group: useId(),
|
|
57
|
-
get handle() { return props.handle },
|
|
58
|
-
get disabled() { return props.disabled },
|
|
59
|
-
get maxDepth() { return props.maxDepth },
|
|
60
|
-
dragging,
|
|
61
|
-
keyOf,
|
|
62
|
-
childrenOf,
|
|
63
|
-
move,
|
|
64
|
-
render: scope => slots.item?.(scope as { item: T; depth: number; parent: T | null }) as ReturnType<TreeContext<T>['render']>
|
|
65
|
-
}
|
|
66
|
-
provide(treeKey, context as unknown as TreeContext<object>)
|
|
67
|
-
</script>
|
|
68
|
-
|
|
69
|
-
<template>
|
|
70
|
-
<div :class="['t-tree', { 't-tree-dragging': dragging, 't-sortable-handles': handle }]">
|
|
71
|
-
<TSortableTreeList :items="model" :parent="null" :depth="1" />
|
|
72
|
-
</div>
|
|
73
|
-
</template>
|
|
1
|
+
<script setup lang="ts" generic="T extends object">
|
|
2
|
+
// A tree whose items can be dragged into a new order, and into or out of each other, with
|
|
3
|
+
// SortableJS: a menu of pages and sub-pages, say. v-model is the top level; each item keeps its
|
|
4
|
+
// children in `childrenKey` ("children"). The `item` slot draws an item's row (it gets the item,
|
|
5
|
+
// its depth from 1, and its parent). `maxDepth` limits how deep the tree goes (2: items and their
|
|
6
|
+
// children); a drop that would go deeper is refused. With `handle`, rows drag only by an element
|
|
7
|
+
// with the `sortable-handle` class. `move` reports each move. SortableJS loads on first use.
|
|
8
|
+
import { provide, ref, useId } from 'vue'
|
|
9
|
+
import TSortableTreeList from './TSortableTreeList.vue'
|
|
10
|
+
import { treeKey, type TreeContext } from './sortableTree'
|
|
11
|
+
|
|
12
|
+
const props = withDefaults(defineProps<{
|
|
13
|
+
itemKey: string | ((item: T) => string | number)
|
|
14
|
+
childrenKey?: string
|
|
15
|
+
maxDepth?: number
|
|
16
|
+
handle?: boolean
|
|
17
|
+
disabled?: boolean
|
|
18
|
+
}>(), { childrenKey: 'children', maxDepth: 2 })
|
|
19
|
+
|
|
20
|
+
const model = defineModel<T[]>({ default: () => [] })
|
|
21
|
+
const emit = defineEmits<{ move: [move: { item: T; from: { parent: T | null; index: number }; to: { parent: T | null; index: number } }] }>()
|
|
22
|
+
const slots = defineSlots<{ item?(scope: { item: T; depth: number; parent: T | null }): unknown }>()
|
|
23
|
+
const dragging = ref(false)
|
|
24
|
+
|
|
25
|
+
const keyOf = (item: T) => String(typeof props.itemKey === 'function' ? props.itemKey(item) : (item as Record<string, unknown>)[props.itemKey])
|
|
26
|
+
const childrenOf = (item: T): T[] => ((item as Record<string, unknown>)[props.childrenKey] as T[] | undefined) ?? []
|
|
27
|
+
|
|
28
|
+
function find(key: string, items: T[] = model.value): T | null {
|
|
29
|
+
for (const item of items) {
|
|
30
|
+
if (keyOf(item) === key) return item
|
|
31
|
+
const found = find(key, childrenOf(item))
|
|
32
|
+
if (found) return found
|
|
33
|
+
}
|
|
34
|
+
return null
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// The list a parent's children are in, made when it has none yet.
|
|
38
|
+
function listOf(parent: T | null): T[] {
|
|
39
|
+
if (!parent) return model.value
|
|
40
|
+
const record = parent as Record<string, unknown>
|
|
41
|
+
if (!Array.isArray(record[props.childrenKey])) record[props.childrenKey] = []
|
|
42
|
+
return record[props.childrenKey] as T[]
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function move(fromKey: string, oldIndex: number, toKey: string, newIndex: number) {
|
|
46
|
+
const from = fromKey ? find(fromKey) : null
|
|
47
|
+
const to = toKey ? find(toKey) : null
|
|
48
|
+
const [item] = listOf(from).splice(oldIndex, 1)
|
|
49
|
+
if (!item) return
|
|
50
|
+
listOf(to).splice(newIndex, 0, item)
|
|
51
|
+
model.value = [...model.value]
|
|
52
|
+
emit('move', { item, from: { parent: from, index: oldIndex }, to: { parent: to, index: newIndex } })
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const context: TreeContext<T> = {
|
|
56
|
+
group: useId(),
|
|
57
|
+
get handle() { return props.handle },
|
|
58
|
+
get disabled() { return props.disabled },
|
|
59
|
+
get maxDepth() { return props.maxDepth },
|
|
60
|
+
dragging,
|
|
61
|
+
keyOf,
|
|
62
|
+
childrenOf,
|
|
63
|
+
move,
|
|
64
|
+
render: scope => slots.item?.(scope as { item: T; depth: number; parent: T | null }) as ReturnType<TreeContext<T>['render']>
|
|
65
|
+
}
|
|
66
|
+
provide(treeKey, context as unknown as TreeContext<object>)
|
|
67
|
+
</script>
|
|
68
|
+
|
|
69
|
+
<template>
|
|
70
|
+
<div :class="['t-tree', { 't-tree-dragging': dragging, 't-sortable-handles': handle }]">
|
|
71
|
+
<TSortableTreeList :items="model" :parent="null" :depth="1" />
|
|
72
|
+
</div>
|
|
73
|
+
</template>
|