|
| 1 | +<script setup lang="ts"> |
| 2 | + import { |
| 3 | + getNestedKeys, |
| 4 | + hasNested, |
| 5 | + type PermissionItem, |
| 6 | + } from '@/utils/permissions'; |
| 7 | + import CraftCheckbox from '@craftcms/cp/vue/CraftCheckbox.vue'; |
| 8 | +
|
| 9 | + const emit = defineEmits<{ |
| 10 | + (e: 'update:modelValue', value: Array<string>): void; |
| 11 | + }>(); |
| 12 | + const props = withDefaults( |
| 13 | + defineProps<{ |
| 14 | + modelValue: Array<string>; |
| 15 | + permissions?: Record<string, PermissionItem>; |
| 16 | + disabled?: boolean; |
| 17 | + level?: number; |
| 18 | + }>(), |
| 19 | + {permissions: () => ({}), modelValue: () => [], disabled: false, level: 0} |
| 20 | + ); |
| 21 | +
|
| 22 | + function toggleItem(key: string) { |
| 23 | + const lowerKey = key.toLowerCase(); |
| 24 | + const index = props.modelValue.indexOf(lowerKey); |
| 25 | + if (index === -1) { |
| 26 | + emit('update:modelValue', [...props.modelValue, lowerKey]); |
| 27 | + } else { |
| 28 | + const keysToRemove = new Set([ |
| 29 | + lowerKey, |
| 30 | + ...getNestedKeys(props.permissions[key]), |
| 31 | + ]); |
| 32 | + emit( |
| 33 | + 'update:modelValue', |
| 34 | + props.modelValue.filter((v) => !keysToRemove.has(v)) |
| 35 | + ); |
| 36 | + } |
| 37 | + } |
| 38 | +</script> |
| 39 | + |
| 40 | +<template> |
| 41 | + <ul |
| 42 | + class="group" |
| 43 | + v-for="(item, key) in permissions" |
| 44 | + :key="key" |
| 45 | + :style="{ |
| 46 | + '--gap-x': `calc((${level} * 1lh) + var(--c-spacing-md))`, |
| 47 | + }" |
| 48 | + > |
| 49 | + <li> |
| 50 | + <CraftCheckbox |
| 51 | + :label="item.label" |
| 52 | + :model-value="modelValue.includes(key.toLowerCase())" |
| 53 | + :value="key" |
| 54 | + :disabled="disabled" |
| 55 | + @update:model-value="toggleItem(key)" |
| 56 | + :class="{ |
| 57 | + 'cp-checkbox-indentation': level! > 0, |
| 58 | + }" |
| 59 | + > |
| 60 | + <div v-if="item.info || item.warning" slot="help-text"> |
| 61 | + <template v-if="item.info"> |
| 62 | + {{ item.info }} |
| 63 | + </template> |
| 64 | + |
| 65 | + <template v-if="item.warning"> |
| 66 | + <div class="flex gap-1 items-center" data-color="warning"> |
| 67 | + <craft-icon name="triangle-exclamation"></craft-icon> |
| 68 | + {{ item.warning }} |
| 69 | + </div> |
| 70 | + </template> |
| 71 | + </div> |
| 72 | + </CraftCheckbox> |
| 73 | + |
| 74 | + <PermissionList |
| 75 | + v-if="hasNested(item)" |
| 76 | + :permissions="item.nested" |
| 77 | + :model-value="modelValue" |
| 78 | + :disabled="disabled || !modelValue.includes(item.key.toLowerCase())" |
| 79 | + @update:model-value="emit('update:modelValue', $event)" |
| 80 | + :level="level! + 1" |
| 81 | + /> |
| 82 | + </li> |
| 83 | + </ul> |
| 84 | +</template> |
| 85 | + |
| 86 | +<style scoped lang="scss"> |
| 87 | + .label { |
| 88 | + display: flex; |
| 89 | + } |
| 90 | +
|
| 91 | + .group { |
| 92 | + margin-block: var(--c-spacing-sm); |
| 93 | + } |
| 94 | +
|
| 95 | + .cp-checkbox-indentation { |
| 96 | + position: relative; |
| 97 | + } |
| 98 | +
|
| 99 | + .cp-checkbox-indentation::before { |
| 100 | + content: ''; |
| 101 | + position: absolute; |
| 102 | + // Position the indicator halfway from the top of the checkbox |
| 103 | + inset-block-start: calc(1lh / 2); |
| 104 | + inset-inline-start: calc( |
| 105 | + var(--c-size-control-2xs) + (var(--c-spacing) * 2) |
| 106 | + ); |
| 107 | + width: calc(var(--gap-x) - (var(--c-spacing) * 3.5)); |
| 108 | + height: 1px; |
| 109 | + background-color: var(--c-color-neutral-border-quiet); |
| 110 | + } |
| 111 | +</style> |
0 commit comments