fix: loop through controllers with multiple instances of multiple scripts and run trigger methods

This commit is contained in:
Shariq Ansari 2025-05-08 18:32:56 +05:30
parent a2081da296
commit 07b2d9f792

View File

@ -1,7 +1,6 @@
import { getScript } from '@/data/script' import { getScript } from '@/data/script'
import { createToast } from '@/utils' import { createToast } from '@/utils'
import { createDocumentResource } from 'frappe-ui' import { createDocumentResource } from 'frappe-ui'
import { computed } from 'vue'
const documentsCache = {} const documentsCache = {}
@ -44,73 +43,75 @@ export function useDocument(doctype, docname) {
documentsCache[doctype][docname]['controllers'] = controllers documentsCache[doctype][docname]['controllers'] = controllers
} }
function getController(dt = null) { function getControllers(row = null, dt = null) {
let controllers = documentsCache[doctype][docname]?.controllers || {} let controllers = documentsCache[doctype][docname]?.controllers || {}
if (Object.keys(controllers).length === 0) return if (Object.keys(controllers).length === 0) return
dt = dt || doctype dt = dt || doctype
let doctypeClassName = dt.replace(/\s+/g, '') if (!controllers[dt].length) return []
const c = controllers[doctypeClassName]
return c || null
}
function getActions() { const _dt = row?.doctype ? row.doctype : doctype
let c = getController() || setupFormScript() const _controllers = controllers[dt].filter(
if (!c) return [] (c) => c.constructor.name === _dt.replace(/\s+/g, ''),
return c?.actions || [] )
return _controllers || []
} }
async function triggerOnRefresh() { async function triggerOnRefresh() {
const c = getController() const controllers = getControllers()
if (!c) return if (!controllers.length) return
return await c.refresh() for (const c of controllers) {
await c.refresh()
}
} }
async function triggerOnChange(fieldname, row) { async function triggerOnChange(fieldname, row) {
const dt = row?.doctype ? row.doctype : doctype const controllers = getControllers(row)
const c = getController(dt) if (!controllers.length) return
if (!c) return
if (row) { for (const c of controllers) {
c.currentRowIdx = row.idx if (row) {
c.value = row[fieldname] c.currentRowIdx = row.idx
c.oldValue = getOldValue(fieldname, row) c.value = row[fieldname]
} else { c.oldValue = getOldValue(fieldname, row)
c.value = documentsCache[doctype][docname].doc[fieldname] } else {
c.oldValue = getOldValue(fieldname) c.value = documentsCache[doctype][docname].doc[fieldname]
c.oldValue = getOldValue(fieldname)
}
await c[fieldname]?.()
} }
return await c[fieldname]?.()
} }
async function triggerOnRowAdd(row) { async function triggerOnRowAdd(row) {
const dt = row?.doctype ? row.doctype : doctype const controllers = getControllers(row)
const c = getController(dt) if (!controllers.length) return
if (!c) return
c.currentRowIdx = row.idx for (const c of controllers) {
c.value = row c.currentRowIdx = row.idx
c.value = row
return await c[row.parentfield + '_add']?.() await c[row.parentfield + '_add']?.()
}
} }
async function triggerOnRowRemove(selectedRows, rows) { async function triggerOnRowRemove(selectedRows, rows) {
if (!selectedRows) return if (!selectedRows) return
const dt = rows[0]?.doctype ? rows[0].doctype : doctype const controllers = getControllers(rows[0])
const c = getController(dt) if (!controllers.length) return
if (!c) return
if (selectedRows.size === 1) { for (const c of controllers) {
const selectedRow = Array.from(selectedRows)[0] if (selectedRows.size === 1) {
c.currentRowIdx = rows.find((r) => r.name === selectedRow).idx const selectedRow = Array.from(selectedRows)[0]
} else { c.currentRowIdx = rows.find((r) => r.name === selectedRow).idx
delete c.currentRowIdx } else {
delete c.currentRowIdx
}
c.selectedRows = Array.from(selectedRows)
c.rows = rows
await c[rows[0].parentfield + '_remove']?.()
} }
c.selectedRows = Array.from(selectedRows)
c.rows = rows
return await c[rows[0].parentfield + '_remove']?.()
} }
function getOldValue(fieldname, row) { function getOldValue(fieldname, row) {
@ -130,8 +131,6 @@ export function useDocument(doctype, docname) {
return { return {
document: documentsCache[doctype][docname], document: documentsCache[doctype][docname],
actions: computed(() => getActions()),
getOldValue,
triggerOnChange, triggerOnChange,
triggerOnRowAdd, triggerOnRowAdd,
triggerOnRowRemove, triggerOnRowRemove,