frequi_origin/src/components/general/EditValue.vue

145 lines
3.2 KiB
Vue
Raw Normal View History

2023-05-07 07:13:18 +00:00
<template>
2024-01-16 17:13:04 +00:00
<form class="d-flex flex-row" @submit.prevent="saveNewName">
2023-05-07 07:19:55 +00:00
<div class="flex-grow-1">
2023-06-02 15:28:22 +00:00
<slot v-if="mode === EditState.None"> </slot>
2023-05-07 07:19:55 +00:00
<b-form-input v-else v-model="localName" size="sm"> </b-form-input>
</div>
<div
class="flex-grow-2 mt-auto d-flex gap-1 ms-1"
:class="alignVertical ? 'flex-column' : 'flex-row'"
>
2023-06-02 15:28:22 +00:00
<template v-if="allowEdit && mode === EditState.None">
2023-05-07 07:19:55 +00:00
<b-button
size="sm"
variant="secondary"
:title="`Edit this ${editableName}.`"
2023-06-02 15:28:22 +00:00
@click="mode = EditState.Editing"
2023-05-07 07:19:55 +00:00
>
2023-05-09 16:25:28 +00:00
<i-mdi-pencil />
2023-05-07 07:19:55 +00:00
</b-button>
2023-06-02 15:28:22 +00:00
<b-button
v-if="allowDuplicate"
size="sm"
variant="secondary"
:title="`Duplicate ${editableName}.`"
@click="duplicate"
>
<i-mdi-content-copy />
</b-button>
2023-05-07 07:19:55 +00:00
<b-button
size="sm"
variant="secondary"
:title="`Delete this ${editableName}.`"
@click="$emit('delete', modelValue)"
>
2023-05-09 16:25:28 +00:00
<i-mdi-delete />
2023-05-07 07:19:55 +00:00
</b-button>
</template>
2023-05-07 07:13:18 +00:00
<b-button
2023-06-02 15:28:22 +00:00
v-if="allowAdd && mode === EditState.None"
2023-05-07 07:13:18 +00:00
size="sm"
2023-05-07 07:19:55 +00:00
:title="`Add new ${editableName}.`"
2023-05-07 07:13:18 +00:00
variant="primary"
2023-05-07 07:19:55 +00:00
@click="addNewClick"
2023-05-09 16:25:28 +00:00
><i-mdi-plus-box-outline />
2023-05-07 07:13:18 +00:00
</b-button>
2023-06-02 15:28:22 +00:00
<template v-if="mode !== EditState.None">
2023-05-07 07:19:55 +00:00
<b-button
size="sm"
2024-01-16 17:13:04 +00:00
:title="`Add new ${editableName}`"
2023-05-07 07:19:55 +00:00
variant="primary"
@click="saveNewName"
>
2023-05-09 16:25:28 +00:00
<i-mdi-check />
2023-05-07 07:19:55 +00:00
</b-button>
2024-01-16 17:13:04 +00:00
<b-button size="sm" title="Abort" variant="secondary" @click="abort">
2023-05-09 16:25:28 +00:00
<i-mdi-close />
2023-05-07 07:19:55 +00:00
</b-button>
</template>
</div>
2024-01-16 17:13:04 +00:00
</form>
2023-05-07 07:13:18 +00:00
</template>
<script setup lang="ts">
const props = defineProps({
modelValue: {
type: String,
required: true,
},
allowEdit: {
type: Boolean,
default: false,
},
2023-05-07 07:16:25 +00:00
allowAdd: {
type: Boolean,
default: false,
},
2023-06-02 15:28:22 +00:00
allowDuplicate: {
type: Boolean,
default: false,
},
2023-05-07 07:13:18 +00:00
editableName: {
type: String,
required: true,
},
alignVertical: {
type: Boolean,
default: false,
},
2023-05-07 07:13:18 +00:00
});
const emit = defineEmits<{
2023-06-02 15:28:22 +00:00
delete: [value: string];
new: [value: string];
duplicate: [oldName: string, newName: string];
rename: [oldName: string, newName: string];
2023-05-07 07:13:18 +00:00
}>();
2023-06-02 15:28:22 +00:00
enum EditState {
None,
Editing,
Adding,
Duplicating,
}
2023-08-02 05:14:40 +00:00
const localName = ref<string>('');
2023-06-02 15:28:22 +00:00
const mode = ref<EditState>(EditState.None);
2023-08-02 05:14:40 +00:00
onMounted(() => {
localName.value = props.modelValue;
});
2023-05-07 07:13:18 +00:00
function abort() {
2023-06-02 15:28:22 +00:00
mode.value = EditState.None;
2023-05-07 07:13:18 +00:00
localName.value = props.modelValue;
}
2023-06-02 15:28:22 +00:00
function duplicate() {
localName.value = localName.value + ' (copy)';
mode.value = EditState.Duplicating;
}
2023-05-07 07:13:18 +00:00
function addNewClick() {
localName.value = '';
2023-06-02 15:28:22 +00:00
mode.value = EditState.Adding;
2023-05-07 07:13:18 +00:00
}
2023-06-02 15:28:22 +00:00
2023-05-07 10:45:02 +00:00
watch(
() => props.modelValue,
() => {
localName.value = props.modelValue;
},
);
2023-05-07 07:13:18 +00:00
function saveNewName() {
2023-06-02 15:28:22 +00:00
if (mode.value === EditState.Adding) {
2023-05-07 07:13:18 +00:00
emit('new', localName.value);
2023-06-02 15:28:22 +00:00
} else if (mode.value === EditState.Duplicating) {
emit('duplicate', props.modelValue, localName.value);
2023-05-07 07:13:18 +00:00
} else {
// Editing
emit('rename', props.modelValue, localName.value);
}
2023-06-02 15:28:22 +00:00
mode.value = EditState.None;
2023-05-07 07:13:18 +00:00
}
</script>