fix: added this.doc.trigger & this.doc.getRow with row.trigger
This commit is contained in:
parent
47262761fe
commit
6f04b85663
@ -51,7 +51,7 @@ export function useDocument(doctype, docname) {
|
|||||||
if (!c) return
|
if (!c) return
|
||||||
|
|
||||||
if (row) {
|
if (row) {
|
||||||
c.row = () => row
|
c.currentRowIdx = row.idx
|
||||||
c.value = row[fieldname]
|
c.value = row[fieldname]
|
||||||
c.oldValue = getOldValue(fieldname, row)
|
c.oldValue = getOldValue(fieldname, row)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -74,15 +74,23 @@ export function getScript(doctype, view = 'Form') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const FormClass = evaluateFormClass(script, className, helpers)
|
const FormClass = evaluateFormClass(script, className, helpers)
|
||||||
if (FormClass) {
|
if (!FormClass) return
|
||||||
controllers[className] = new FormClass()
|
|
||||||
|
let parentInstance = null
|
||||||
|
let doctypeName = doctype.replace(/\s+/g, '')
|
||||||
|
|
||||||
|
// if className is not doctype name, then it is a child doctype
|
||||||
|
let isChildDoctype = className !== doctypeName
|
||||||
|
if (isChildDoctype) {
|
||||||
|
parentInstance = controllers[doctypeName]
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
|
||||||
const instances = Object.values(controllers)
|
controllers[className] = setupFormController(
|
||||||
|
FormClass,
|
||||||
instances.forEach((instance) => {
|
document,
|
||||||
setupFormController(instance, document, instances)
|
parentInstance,
|
||||||
|
isChildDoctype,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to load form controller:', err)
|
console.error('Failed to load form controller:', err)
|
||||||
@ -92,26 +100,58 @@ export function getScript(doctype, view = 'Form') {
|
|||||||
return controllers
|
return controllers
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupFormController(instance, document, allInstances) {
|
function setupFormController(
|
||||||
|
FormClass,
|
||||||
|
document,
|
||||||
|
parentInstance = null,
|
||||||
|
isChildDoctype = false,
|
||||||
|
) {
|
||||||
|
let instance = new FormClass()
|
||||||
|
|
||||||
for (const key in document) {
|
for (const key in document) {
|
||||||
if (document.hasOwnProperty(key)) {
|
if (document.hasOwnProperty(key)) {
|
||||||
instance[key] = document[key]
|
instance[key] = document[key]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
instance.trigger = function (methodName, ...args) {
|
if (isChildDoctype) {
|
||||||
for (const i of allInstances) {
|
setupHelperMethods(FormClass, instance, document)
|
||||||
if (typeof i[methodName] === 'function') {
|
instance.doc = createDocProxy(document.doc, parentInstance)
|
||||||
return i[methodName].apply(i, args)
|
} else {
|
||||||
}
|
instance.doc = createDocProxy(document.doc, instance)
|
||||||
}
|
|
||||||
|
|
||||||
console.warn(`Method '${methodName}' not found in any instance`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
instance.actions = (instance.actions || []).filter(
|
instance.actions = (instance.actions || []).filter(
|
||||||
(action) => typeof action.condition !== 'function' || action.condition(),
|
(action) => typeof action.condition !== 'function' || action.condition(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return instance
|
||||||
|
}
|
||||||
|
|
||||||
|
function setupHelperMethods(FormClass, instance, document) {
|
||||||
|
FormClass.prototype.getRow = (parentField, idx) =>
|
||||||
|
getRow(parentField, idx, document.doc, instance)
|
||||||
|
|
||||||
|
exposeHiddenMethods(document.doc, instance, ['getRow'])
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRow(parentField, idx, data, instance) {
|
||||||
|
idx = idx || instance.currentRowIdx
|
||||||
|
|
||||||
|
if (!data[parentField]) {
|
||||||
|
console.warn(`⚠️ No data found for parent field: ${parentField}`)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
const row = data[parentField].find((r) => r.idx === idx)
|
||||||
|
|
||||||
|
if (!row) {
|
||||||
|
console.warn(
|
||||||
|
`⚠️ No row found for idx: ${idx} in parent field: ${parentField}`,
|
||||||
|
)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return createDocProxy(row, instance)
|
||||||
}
|
}
|
||||||
|
|
||||||
// utility function to setup a form controller
|
// utility function to setup a form controller
|
||||||
@ -138,6 +178,49 @@ export function getScript(doctype, view = 'Form') {
|
|||||||
return FormClass
|
return FormClass
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createDocProxy(data, instance) {
|
||||||
|
return new Proxy(data, {
|
||||||
|
get(target, prop) {
|
||||||
|
if (prop === 'trigger') {
|
||||||
|
if ('trigger' in data) {
|
||||||
|
console.warn(
|
||||||
|
`⚠️ Avoid using "trigger" as a field name — it conflicts with the built-in trigger() method.`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (methodName, ...args) => {
|
||||||
|
const method = instance[methodName]
|
||||||
|
if (typeof method === 'function') {
|
||||||
|
return method.apply(instance, args)
|
||||||
|
} else {
|
||||||
|
console.warn(`⚠️ Method "${methodName}" not found in class.`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return target[prop]
|
||||||
|
},
|
||||||
|
set(target, prop, value) {
|
||||||
|
target[prop] = value
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function exposeHiddenMethods(doc, instance, methodNames = []) {
|
||||||
|
for (const name of methodNames) {
|
||||||
|
if (typeof instance[name] === 'function') {
|
||||||
|
// Show as actual method on doc, bound to instance
|
||||||
|
Object.defineProperty(doc, name, {
|
||||||
|
value: (...args) => instance[name](...args),
|
||||||
|
writable: false,
|
||||||
|
enumerable: false,
|
||||||
|
configurable: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
scripts,
|
scripts,
|
||||||
setupScript,
|
setupScript,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user