fix: streamline trigger functions to use a unified handler for controller actions

This commit is contained in:
Shariq Ansari 2025-05-09 17:30:25 +05:30
parent 53c0706a3a
commit 12c3290f19

View File

@ -49,63 +49,63 @@ export function useDocument(doctype, docname) {
} }
async function triggerOnRefresh() { async function triggerOnRefresh() {
const controllers = getControllers() const handler = async function () {
if (!controllers.length) return await this.refresh()
}
const tasks = controllers.map((c) => async () => await c.refresh()) await trigger(handler)
await runSequentially(tasks)
} }
async function triggerOnChange(fieldname, row) { async function triggerOnChange(fieldname, row) {
const controllers = getControllers(row) const handler = async function () {
if (!controllers.length) return
const tasks = controllers.map((c) => async () => {
if (row) { if (row) {
c.currentRowIdx = row.idx this.currentRowIdx = row.idx
c.value = row[fieldname] this.value = row[fieldname]
c.oldValue = getOldValue(fieldname, row) this.oldValue = getOldValue(fieldname, row)
} else { } else {
c.value = documentsCache[doctype][docname].doc[fieldname] this.value = documentsCache[doctype][docname].doc[fieldname]
c.oldValue = getOldValue(fieldname) this.oldValue = getOldValue(fieldname)
} }
await c[fieldname]?.() await this[fieldname]?.()
}) }
await runSequentially(tasks) await trigger(handler, row)
} }
async function triggerOnRowAdd(row) { async function triggerOnRowAdd(row) {
const controllers = getControllers(row) const handler = async function () {
if (!controllers.length) return this.currentRowIdx = row.idx
this.value = row
await this[row.parentfield + '_add']?.()
}
const tasks = controllers.map((c) => async () => { await trigger(handler, row)
c.currentRowIdx = row.idx
c.value = row
await c[row.parentfield + '_add']?.()
})
await runSequentially(tasks)
} }
async function triggerOnRowRemove(selectedRows, rows) { async function triggerOnRowRemove(selectedRows, rows) {
if (!selectedRows) return const handler = async function () {
const controllers = getControllers(rows[0])
if (!controllers.length) return
const tasks = controllers.map((c) => async () => {
if (selectedRows.size === 1) { if (selectedRows.size === 1) {
const selectedRow = Array.from(selectedRows)[0] const selectedRow = Array.from(selectedRows)[0]
c.currentRowIdx = rows.find((r) => r.name === selectedRow).idx this.currentRowIdx = rows.find((r) => r.name === selectedRow).idx
} else { } else {
delete c.currentRowIdx delete this.currentRowIdx
} }
c.selectedRows = Array.from(selectedRows) this.selectedRows = Array.from(selectedRows)
c.rows = rows this.rows = rows
await c[rows[0].parentfield + '_remove']?.() await this[rows[0].parentfield + '_remove']?.()
}) }
await trigger(handler, rows[0])
}
async function trigger(taskFn, row = null) {
const controllers = getControllers(row)
if (!controllers.length) return
const tasks = controllers.map(
(controller) => async () => await taskFn.call(controller),
)
await runSequentially(tasks) await runSequentially(tasks)
} }