2
0
mirror of https://github.com/frappe/books.git synced 2025-01-10 18:24:40 +00:00
books/src/pages/QuickEditForm.vue

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

296 lines
7.1 KiB
Vue
Raw Normal View History

2019-10-04 20:18:10 +00:00
<template>
<div class="border-l h-full overflow-auto">
<!-- Quick edit Tool bar -->
<div class="flex items-center justify-between px-4 pt-4">
<!-- Close Button and Status Text -->
<div class="flex items-center">
<Button :icon="true" @click="routeToPrevious">
<feather-icon name="x" class="w-4 h-4" />
</Button>
<span v-if="statusText" class="ml-2 text-base text-gray-600">{{
statusText
}}</span>
</div>
<!-- Actions, Badge and Status Change Buttons -->
<div class="flex items-stretch">
<DropdownWithActions :actions="actions" />
<StatusBadge :status="status" />
<Button
:icon="true"
@click="sync"
type="primary"
v-if="doc?.dirty || doc?.notInserted"
class="ml-2 text-white text-xs"
>
{{ t`Save` }}
</Button>
<Button
:icon="true"
@click="submit"
type="primary"
v-if="
2022-04-28 06:34:55 +00:00
schema?.isSubmittable &&
!doc?.submitted &&
!doc?.notInserted &&
!(doc?.cancelled || false)
"
class="ml-2 text-white text-xs"
>
{{ t`Submit` }}
</Button>
</div>
2019-10-04 20:18:10 +00:00
</div>
<!-- Name and image -->
<div class="p-4 gap-2 flex-center flex flex-col items-center" v-if="doc">
<FormControl
v-if="imageField"
:df="imageField"
:value="doc[imageField.fieldname]"
@change="(value) => valueChange(imageField, value)"
size="small"
:letter-placeholder="doc[titleField.fieldname]?.[0] ?? null"
/>
<FormControl
input-class="text-center"
ref="titleControl"
v-if="titleField"
:df="titleField"
:value="doc[titleField.fieldname]"
2022-05-18 14:58:35 +00:00
:read-only="doc.inserted"
@change="(value) => valueChange(titleField, value)"
@input="setTitleSize"
/>
2019-10-04 20:18:10 +00:00
</div>
<!-- Rest of the form -->
<TwoColumnForm
ref="form"
v-if="doc"
:doc="doc"
:fields="fields"
:autosave="false"
:column-ratio="[1.1, 2]"
/>
<!-- QuickEdit Widgets -->
<component v-if="quickEditWidget" :is="quickEditWidget" />
2019-10-04 20:18:10 +00:00
</div>
</template>
<script>
2022-04-29 19:04:08 +00:00
import { computed } from '@vue/reactivity';
import { t } from 'fyo';
2022-04-28 06:34:55 +00:00
import Button from 'src/components/Button.vue';
2022-04-22 11:02:03 +00:00
import FormControl from 'src/components/Controls/FormControl.vue';
2022-04-28 06:34:55 +00:00
import DropdownWithActions from 'src/components/DropdownWithActions.vue';
import StatusBadge from 'src/components/StatusBadge.vue';
import TwoColumnForm from 'src/components/TwoColumnForm.vue';
import { fyo } from 'src/initFyo';
2022-04-28 06:34:55 +00:00
import { getQuickEditWidget } from 'src/utils/quickEditWidgets';
import { getActionsForDocument, openQuickEdit } from 'src/utils/ui';
2019-10-04 20:18:10 +00:00
export default {
name: 'QuickEditForm',
2022-04-28 06:34:55 +00:00
props: {
name: String,
schemaName: String,
defaults: String,
hideFields: { type: Array, default: () => [] },
showFields: { type: Array, default: () => [] },
},
2019-10-04 20:18:10 +00:00
components: {
Button,
FormControl,
StatusBadge,
TwoColumnForm,
DropdownWithActions,
2019-10-04 20:18:10 +00:00
},
2019-10-06 12:33:21 +00:00
provide() {
return {
2022-04-28 06:34:55 +00:00
schemaName: this.schemaName,
name: this.name,
2022-04-29 19:04:08 +00:00
doc: computed(() => this.doc),
};
2019-10-06 12:33:21 +00:00
},
2019-10-04 20:18:10 +00:00
data() {
return {
doc: null,
values: null,
titleField: null,
imageField: null,
statusText: null,
2019-10-04 20:18:10 +00:00
};
},
2022-04-29 19:04:08 +00:00
mounted() {
if (this.defaults) {
this.values = JSON.parse(this.defaults);
}
if (fyo.store.isDevelopment) {
window.qef = this;
}
},
async created() {
2022-04-28 06:34:55 +00:00
await this.fetchFieldsAndDoc();
},
computed: {
2022-04-28 06:34:55 +00:00
schema() {
return fyo.schemaMap[this.schemaName] ?? null;
},
status() {
2022-04-28 06:34:55 +00:00
if (this.doc && this.doc.notInserted) {
return 'Draft';
}
2022-04-28 06:34:55 +00:00
return '';
},
fields() {
2022-04-28 06:34:55 +00:00
if (!this.schema) {
return [];
}
const fieldnames = (this.schema.quickEditFields ?? ['name']).filter(
(f) => !this.hideFields.includes(f)
);
if (this.showFields?.length) {
fieldnames.push(
...this.schema.fields
.map((f) => f.fieldname)
.filter((f) => this.showFields.includes(f))
);
}
2022-04-28 06:34:55 +00:00
return fieldnames.map((f) => fyo.getField(this.schemaName, f));
},
actions() {
return getActionsForDocument(this.doc);
2019-12-26 13:45:41 +00:00
},
quickEditWidget() {
if (this.doc?.notInserted ?? true) {
return null;
}
2022-04-28 06:34:55 +00:00
const widget = getQuickEditWidget(this.schemaName);
if (widget === null) {
2019-12-26 13:45:41 +00:00
return null;
}
2022-04-28 06:34:55 +00:00
return widget(this.doc);
},
},
methods: {
2022-04-28 06:34:55 +00:00
async fetchFieldsAndDoc() {
if (!this.schema) {
return;
}
const titleField = this.schema.titleField;
this.titleField = fyo.getField(this.schemaName, titleField);
this.imageField = fyo.getField(this.schemaName, 'image');
await this.fetchDoc();
// setup the title field
2022-04-28 06:34:55 +00:00
this.setTitleField();
// set default values
if (this.values) {
this.doc?.set(this.values);
}
// set title size
this.setTitleSize();
},
2022-04-28 06:34:55 +00:00
setTitleField() {
const { fieldname, readOnly } = this.titleField;
if (!this.doc?.notInserted || !this?.doc[fieldname]) {
2022-04-28 06:34:55 +00:00
return;
}
if (readOnly) {
this.doc.set(fieldname, t`New ${this.schema.label}`);
return;
}
this.doc.set(fieldname, '');
setTimeout(() => {
this.$refs.titleControl?.focus();
2022-04-28 06:34:55 +00:00
}, 300);
},
async fetchDoc() {
if (!this.schemaName) {
this.$router.back();
}
if (this.name) {
try {
this.doc = await fyo.doc.getDoc(this.schemaName, this.name);
} catch (e) {
this.$router.back();
}
} else {
this.doc = fyo.doc.getNewDoc(this.schemaName);
}
if (this.doc === null) {
return;
}
this.doc.once('afterRename', () => {
openQuickEdit({
schemaName: this.schemaName,
name: this.doc.name,
});
});
},
2019-12-04 18:40:46 +00:00
valueChange(df, value) {
this.$refs.form.onChange(df, value);
},
async sync() {
this.statusText = t`Saving`;
try {
await this.$refs.form.sync();
setTimeout(() => {
this.statusText = null;
}, 300);
} catch (err) {
this.statusText = null;
console.error(err);
}
},
async submit() {
this.statusText = t`Submitting`;
try {
await this.$refs.form.submit();
setTimeout(() => {
this.statusText = null;
}, 300);
} catch (err) {
this.statusText = null;
console.error(err);
}
2019-11-08 19:58:58 +00:00
},
routeToPrevious() {
if (this.doc.dirty && !this.doc.notInserted) {
this.doc.load();
}
this.$router.back();
},
setTitleSize() {
2022-04-28 06:34:55 +00:00
if (!this.$refs.titleControl) {
return;
}
2022-04-28 06:34:55 +00:00
const input = this.$refs.titleControl.getInput();
const value = input.value;
const valueLength = (value || '').length + 1;
input.size = Math.max(valueLength, 15);
},
},
2019-10-04 20:18:10 +00:00
};
</script>