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.

275 lines
6.6 KiB
Vue
Raw Normal View History

2019-10-04 20:18:10 +00:00
<template>
<div class="border-l h-full">
<div class="flex items-center justify-between px-4 pt-4">
<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>
<div class="flex items-stretch">
<DropdownWithActions :actions="actions" />
<StatusBadge :status="status" />
<Button
:icon="true"
@click="sync"
type="primary"
2022-04-28 06:34:55 +00:00
v-if="doc && doc.notInserted"
class="ml-2 text-white text-xs"
>
{{ t`Save` }}
</Button>
<Button
:icon="true"
@click="submitDoc"
type="primary"
v-if="
2022-04-28 06:34:55 +00:00
schema?.isSubmittable &&
doc &&
!doc.submitted &&
2022-04-28 06:34:55 +00:00
!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>
2019-12-11 09:09:16 +00:00
<div class="px-4 pt-2 pb-4 flex-center" v-if="doc">
<div class="flex flex-col items-center">
<FormControl
v-if="imageField"
:df="imageField"
:value="doc[imageField.fieldname]"
@change="(value) => valueChange(imageField, value)"
size="small"
class="mb-1"
:letter-placeholder="
2019-12-04 18:40:46 +00:00
// for AttachImage field
doc[titleField.fieldname] ? doc[titleField.fieldname][0] : null
"
/>
<FormControl
input-class="text-center"
ref="titleControl"
v-if="titleField"
:df="titleField"
:value="doc[titleField.fieldname]"
@change="(value) => valueChange(titleField, value)"
@input="setTitleSize"
/>
</div>
2019-10-04 20:18:10 +00:00
</div>
<TwoColumnForm
ref="form"
v-if="doc"
:doc="doc"
:fields="fields"
:autosave="true"
:column-ratio="[1.1, 2]"
/>
2019-12-26 13:45:41 +00:00
<component v-if="doc && quickEditWidget" :is="quickEditWidget" />
2019-10-04 20:18:10 +00:00
</div>
</template>
<script>
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() {
let vm = this;
2019-10-06 12:33:21 +00:00
return {
2022-04-28 06:34:55 +00:00
schemaName: this.schemaName,
name: this.name,
get doc() {
return vm.doc;
},
};
2019-10-06 12:33:21 +00:00
},
mounted() {
2022-04-28 06:34:55 +00:00
if (this.defaults) {
this.values = JSON.parse(this.defaults);
}
2022-04-28 06:34:55 +00:00
if (fyo.store.isDevelopment) {
window.qef = this;
}
},
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
};
},
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() {
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]) {
return;
}
if (readOnly) {
this.doc.set(fieldname, t`New ${this.schema.label}`);
return;
}
this.doc.set(fieldname, '');
setTimeout(() => {
this.$refs.titleControl.focus();
}, 300);
},
async fetchDoc() {
try {
2022-04-28 06:34:55 +00:00
this.doc = await fyo.doc.getDoc(this.schemaName, this.name);
this.doc.once('afterRename', () => {
openQuickEdit({
2022-04-28 06:34:55 +00:00
schemaName: this.schemaName,
name: this.doc.name,
});
});
this.doc.on('beforeSync', () => {
2022-02-09 07:17:54 +00:00
this.statusText = t`Saving...`;
});
this.doc.on('afterSync', () => {
setTimeout(() => {
this.statusText = null;
}, 500);
});
} catch (e) {
this.$router.back();
}
},
2019-12-04 18:40:46 +00:00
valueChange(df, value) {
this.$refs.form.onChange(df, value);
},
sync() {
this.$refs.form.sync();
},
async submitDoc() {
try {
await this.$refs.form.submit();
} catch (e) {
this.statusText = null;
console.error(e);
}
2019-11-08 19:58:58 +00:00
},
routeToPrevious() {
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;
let valueLength = (value || '').length + 1;
if (valueLength < 7) {
valueLength = 7;
}
input.size = valueLength;
},
},
2019-10-04 20:18:10 +00:00
};
</script>