🚧 Adding and removing of all form items

This commit is contained in:
Alicia Sykes 2021-10-17 14:42:42 +01:00
parent 307d3719aa
commit 8d84dbddb3

View File

@ -3,12 +3,16 @@
:name="modalName" :name="modalName"
:resizable="true" :resizable="true"
width="50%" width="50%"
height="50%" height="85%"
classes="dashy-modal edit-item" classes="dashy-modal edit-item"
@closed="modalClosed" @closed="modalClosed"
> >
<div class="edit-item-inner">
<h3 class="title">Edit Item</h3> <h3 class="title">Edit Item</h3>
<p class="sub-title">Editing {{item.title}} (ID: {{itemId}})</p> <p class="sub-title">Editing {{item.title}} (ID: {{itemId}})</p>
<p class="warning-note" v-if="formData.length === 0">
No data configured yet. Click an attribute in the list below to add the field to the form.
</p>
<div class="row" v-for="(row, index) in formData" :key="row.name"> <div class="row" v-for="(row, index) in formData" :key="row.name">
<Input <Input
v-model="formData[index].value" v-model="formData[index].value"
@ -17,9 +21,9 @@
:type="row.type" :type="row.type"
layout="horizontal" layout="horizontal"
/> />
<BinIcon /> <BinIcon @click="() => removeField(row.name)" />
</div> </div>
<div class="add-more-inputs"> <div class="add-more-inputs" v-if="additionalFormData.length > 0">
<h4>More Fields</h4> <h4>More Fields</h4>
<div class="more-fields"> <div class="more-fields">
<span <span
@ -32,14 +36,15 @@
</div> </div>
</div> </div>
<Button :click="saveItem">Save</Button> <Button :click="saveItem">Save</Button>
</div>
</modal> </modal>
</template> </template>
<script> <script>
import Input from '@/components/FormElements/Input';
import Button from '@/components/FormElements/Button';
import AddIcon from '@/assets/interface-icons/interactive-editor-add.svg'; import AddIcon from '@/assets/interface-icons/interactive-editor-add.svg';
import BinIcon from '@/assets/interface-icons/interactive-editor-remove.svg'; import BinIcon from '@/assets/interface-icons/interactive-editor-remove.svg';
import Input from '@/components/FormElements/Input';
import Button from '@/components/FormElements/Button';
import StoreKeys from '@/utils/StoreMutations'; import StoreKeys from '@/utils/StoreMutations';
import DashySchema from '@/utils/ConfigSchema'; import DashySchema from '@/utils/ConfigSchema';
import { modalNames } from '@/utils/defaults'; import { modalNames } from '@/utils/defaults';
@ -50,8 +55,8 @@ export default {
return { return {
modalName: modalNames.EDIT_ITEM, modalName: modalNames.EDIT_ITEM,
schema: DashySchema.properties.sections.items.properties.items.items.properties, schema: DashySchema.properties.sections.items.properties.items.items.properties,
formData: [], formData: [], // Array of form fields
additionalFormData: [], additionalFormData: [], // Array of not-yet-used form fields
item: {}, item: {},
}; };
}, },
@ -71,17 +76,20 @@ export default {
this.$modal.show(modalNames.EDIT_ITEM); this.$modal.show(modalNames.EDIT_ITEM);
}, },
methods: { methods: {
/* For a given item ID, return the item obj from store */
getItemFromState(id) {
return this.$store.getters.getItemById(id);
},
/* Using the schema and item obj, generate data to be rendered in the form */
makeInitialFormData() { makeInitialFormData() {
const formData = []; const formData = [];
const requiredFields = ['title', 'description', 'url', 'icon', 'target', 'hotkey', 'provider', 'tags']; const requiredFields = ['title', 'description', 'url', 'icon', 'target', 'hotkey', 'provider', 'tags'];
const acceptedTypes = ['text'];
const getType = (origType) => (acceptedTypes.includes(origType) ? origType : 'text');
Object.keys(this.schema).forEach((property) => { Object.keys(this.schema).forEach((property) => {
const singleRow = { const singleRow = {
name: property, name: property,
description: this.schema[property].description, description: this.schema[property].description,
value: this.item[property] || '', value: this.item[property],
type: getType(this.schema[property].type), type: this.getInputType(this.schema[property]),
}; };
if (this.item[property] || requiredFields.includes(property)) { if (this.item[property] || requiredFields.includes(property)) {
formData.push(singleRow); formData.push(singleRow);
@ -91,9 +99,7 @@ export default {
}); });
return formData; return formData;
}, },
getItemFromState(id) { /* Saves the updated item to VueX Store */
return this.$store.getters.getItemById(id);
},
saveItem() { saveItem() {
const newItem = {}; const newItem = {};
this.formData.forEach((row) => { this.formData.forEach((row) => {
@ -102,19 +108,43 @@ export default {
newItem.id = this.itemId; newItem.id = this.itemId;
this.$store.commit(StoreKeys.UPDATE_ITEM, { newItem, itemId: this.itemId }); this.$store.commit(StoreKeys.UPDATE_ITEM, { newItem, itemId: this.itemId });
}, },
appendNewField(filedId) { /* Adds filed from extras list to main form, then removes from extras list */
appendNewField(fieldId) {
Object.keys(this.schema).forEach((property) => { Object.keys(this.schema).forEach((property) => {
if (property === filedId) { if (property === fieldId) {
this.formData.push({ this.formData.push({
name: property, name: property,
description: this.schema[property].description, description: this.schema[property].description,
value: this.item[property] || '', value: this.item[property],
type: 'text', type: this.getInputType(this.schema[property]),
}); });
} }
}); });
this.additionalFormData.forEach((elem, index) => {
if (elem.name === fieldId) {
this.additionalFormData.splice(index, 1);
}
});
}, },
// removeSomeField(filedId) {}, /* Removes filed from main form, adds back into extras list */
removeField(fieldId) {
this.formData.forEach((elem, index) => {
if (elem.name === fieldId) {
this.formData.splice(index, 1);
this.additionalFormData.push(elem);
}
});
},
/* For a given attribute, determine type from schema */
getInputType(schemaItem) {
if (schemaItem.type === 'text') {
return 'text';
} else if (schemaItem.type === 'number') {
return 'number';
}
return 'text';
},
/* Clean up work, triggered when modal closed */
modalClosed() { modalClosed() {
this.$store.commit(StoreKeys.SET_MODAL_OPEN, false); this.$store.commit(StoreKeys.SET_MODAL_OPEN, false);
this.$emit('closeEditMenu'); this.$emit('closeEditMenu');
@ -127,7 +157,7 @@ export default {
@import '@/styles/style-helpers.scss'; @import '@/styles/style-helpers.scss';
@import '@/styles/media-queries.scss'; @import '@/styles/media-queries.scss';
.edit-item { .edit-item-inner {
padding: 1rem; padding: 1rem;
background: var(--config-settings-background); background: var(--config-settings-background);
color: var(--config-settings-color); color: var(--config-settings-color);
@ -144,6 +174,9 @@ export default {
font-style: italic; font-style: italic;
opacity: var(--dimming-factor); opacity: var(--dimming-factor);
} }
p.warning-note {
color: var(--warning);
}
.row { .row {
display: flex; display: flex;
padding: 0.5rem 0.25rem; padding: 0.5rem 0.25rem;