diff --git a/crm/api/doc.py b/crm/api/doc.py index 8fc1f705..199c99b1 100644 --- a/crm/api/doc.py +++ b/crm/api/doc.py @@ -240,7 +240,8 @@ def get_list_data( "views": get_views(doctype), "total_count": len(frappe.get_list(doctype, filters=filters)), "row_count": len(data), - "form_script": get_form_script(doctype) + "form_script": get_form_script(doctype), + "list_script": get_form_script(doctype, "List"), } diff --git a/crm/fcrm/doctype/crm_form_script/crm_form_script.js b/crm/fcrm/doctype/crm_form_script/crm_form_script.js index 7f6f150e..087714dc 100644 --- a/crm/fcrm/doctype/crm_form_script/crm_form_script.js +++ b/crm/fcrm/doctype/crm_form_script/crm_form_script.js @@ -1,8 +1,38 @@ // Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors // For license information, please see license.txt -// frappe.ui.form.on("CRM Form Script", { -// refresh(frm) { +frappe.ui.form.on("CRM Form Script", { + refresh(frm) { + frm.set_query("dt", { + filters: { + istable: 0, + }, + }); + }, + view(frm) { + let has_form_boilerplate = frm.doc.script.includes( + "function setupForm(" + ); + let has_list_boilerplate = frm.doc.script.includes( + "function setupList(" + ); -// }, -// }); + if (frm.doc.view == "Form" && !has_form_boilerplate) { + frm.doc.script = ` +function setupForm({ doc }) { + return { + actions: [], + } +}`.trim(); + } + if (frm.doc.view == "List" && !has_list_boilerplate) { + frm.doc.script = ` +function setupList({ docs }) { + return { + actions: [], + bulk_actions: [], + } +}`.trim(); + } + }, +}); diff --git a/crm/fcrm/doctype/crm_form_script/crm_form_script.json b/crm/fcrm/doctype/crm_form_script/crm_form_script.json index b883e804..9a026098 100644 --- a/crm/fcrm/doctype/crm_form_script/crm_form_script.json +++ b/crm/fcrm/doctype/crm_form_script/crm_form_script.json @@ -7,6 +7,7 @@ "engine": "InnoDB", "field_order": [ "dt", + "view", "column_break_gboh", "enabled", "section_break_xeox", @@ -36,16 +37,26 @@ "label": "Enabled" }, { - "default": "function setupForm({ doc }) {\n return {\n actions: []\n }\n}", + "default": "function setupForm({ doc }) {\n return {\n actions: [],\n }\n}", + "documentation_url": "https://docs.frappe.io/crm/custom-actions", "fieldname": "script", "fieldtype": "Code", "label": "Script", "options": "JS" + }, + { + "default": "Form", + "fieldname": "view", + "fieldtype": "Select", + "in_list_view": 1, + "label": "Apply To", + "options": "Form\nList", + "set_only_once": 1 } ], "index_web_pages_for_search": 1, "links": [], - "modified": "2024-01-19 21:52:46.078626", + "modified": "2024-04-10 18:27:17.617602", "modified_by": "Administrator", "module": "FCRM", "name": "CRM Form Script", diff --git a/crm/fcrm/doctype/crm_form_script/crm_form_script.py b/crm/fcrm/doctype/crm_form_script/crm_form_script.py index 32649d62..ff93ff4f 100644 --- a/crm/fcrm/doctype/crm_form_script/crm_form_script.py +++ b/crm/fcrm/doctype/crm_form_script/crm_form_script.py @@ -14,6 +14,7 @@ class CRMFormScript(Document): if self.dt and self.enabled: filters = { "dt": self.dt, + "view": self.view, "enabled": 1, } if self.name: @@ -27,13 +28,14 @@ class CRMFormScript(Document): frappe.DuplicateEntryError, ) -def get_form_script(dt): - """Returns the script for the given doctype""" +def get_form_script(dt, view="Form"): + """Returns the form script for the given doctype""" FormScript = frappe.qb.DocType("CRM Form Script") query = ( frappe.qb.from_(FormScript) .select("script") .where(FormScript.dt == dt) + .where(FormScript.view == view) .where(FormScript.enabled == 1) .limit(1) )