From dbd478dea485890d0fff063847e21440bbe301e0 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Thu, 21 Mar 2019 14:29:57 +0530 Subject: [PATCH 1/4] fix(Custom Script): Remove script_type field --- .../doctype/custom_script/custom_script.json | 47 ++++--------------- .../doctype/custom_script/custom_script.py | 4 +- 2 files changed, 9 insertions(+), 42 deletions(-) diff --git a/frappe/custom/doctype/custom_script/custom_script.json b/frappe/custom/doctype/custom_script/custom_script.json index 85cb759102..fc086e4b0b 100644 --- a/frappe/custom/doctype/custom_script/custom_script.json +++ b/frappe/custom/doctype/custom_script/custom_script.json @@ -4,11 +4,11 @@ "allow_guest_to_view": 0, "allow_import": 1, "allow_rename": 0, - "autoname": "CustomScript.####", + "autoname": "", "beta": 0, "creation": "2013-01-10 16:34:01", "custom": 0, - "description": "Adds a custom script (client or server) to a DocType", + "description": "Adds a client custom script to a DocType", "docstatus": 0, "doctype": "DocType", "document_type": "Document", @@ -22,6 +22,7 @@ "bold": 0, "collapsible": 0, "columns": 0, + "fetch_if_empty": 0, "fieldname": "dt", "fieldtype": "Link", "hidden": 0, @@ -56,41 +57,7 @@ "bold": 0, "collapsible": 0, "columns": 0, - "default": "Client", - "fieldname": "script_type", - "fieldtype": "Select", - "hidden": 1, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_global_search": 0, - "in_list_view": 1, - "in_standard_filter": 1, - "label": "Script Type", - "length": 0, - "no_copy": 0, - "oldfieldname": "script_type", - "oldfieldtype": "Select", - "options": "Client", - "permlevel": 0, - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 1, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "translatable": 0, - "unique": 0 - }, - { - "allow_bulk_edit": 0, - "allow_in_quick_entry": 0, - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, + "fetch_if_empty": 0, "fieldname": "script", "fieldtype": "Code", "hidden": 0, @@ -125,6 +92,7 @@ "bold": 0, "collapsible": 0, "columns": 0, + "fetch_if_empty": 0, "fieldname": "sample", "fieldtype": "HTML", "hidden": 0, @@ -137,7 +105,7 @@ "label": "Sample", "length": 0, "no_copy": 0, - "options": "

Custom Script Help

\n

Custom Scripts are executed only on the client-side (i.e. in Forms). Here are some examples to get you started

\n
\n\n// fetch local_tax_no on selection of customer \n// cur_frm.add_fetch(link_field, source_fieldname, target_fieldname); \ncur_frm.add_fetch('customer', 'local_tax_no', 'local_tax_no');\n\n// additional validation on dates \nfrappe.ui.form.on('Task', 'validate', function(frm) {\n    if (frm.doc.from_date < get_today()) {\n        msgprint('You can not select past date in From Date');\n        validated = false;\n    } \n});\n\n// make a field read-only after saving \nfrappe.ui.form.on('Task', {\n    refresh: function(frm) {\n        // use the __islocal value of doc, to check if the doc is saved or not\n        frm.set_df_property('myfield', 'read_only', frm.doc.__islocal ? 0 : 1);\n    } \n});\n\n// additional permission check\nfrappe.ui.form.on('Task', {\n    validate: function(frm) {\n        if(user=='user1@example.com' && frm.doc.purpose!='Material Receipt') {\n            msgprint('You are only allowed Material Receipt');\n            validated = false;\n        }\n    } \n});\n\n// calculate sales incentive\nfrappe.ui.form.on('Sales Invoice', {\n    validate: function(frm) {\n        // calculate incentives for each person on the deal\n        total_incentive = 0\n        $.each(frm.doc.sales_team, function(i, d) {\n            // calculate incentive\n            var incentive_percent = 2;\n            if(frm.doc.base_grand_total > 400) incentive_percent = 4;\n            // actual incentive\n            d.incentives = flt(frm.doc.base_grand_total) * incentive_percent / 100;\n            total_incentive += flt(d.incentives)\n        });\n        frm.doc.total_incentive = total_incentive;\n    } \n})\n\n
", + "options": "

Custom Script Help

\n

Custom Scripts are executed only on the client-side (i.e. in Forms). Here are some examples to get you started

\n
\n\n// fetch local_tax_no on selection of customer \n// cur_frm.add_fetch(link_field,  source_fieldname,  target_fieldname); \ncur_frm.add_fetch('customer',  'local_tax_no',  'local_tax_no');\n\n// additional validation on dates \nfrappe.ui.form.on('Task',  'validate',  function(frm) {\n    if (frm.doc.from_date < get_today()) {\n        msgprint('You can not select past date in From Date');\n        validated = false;\n    } \n});\n\n// make a field read-only after saving \nfrappe.ui.form.on('Task',  {\n    refresh: function(frm) {\n        // use the __islocal value of doc,  to check if the doc is saved or not\n        frm.set_df_property('myfield',  'read_only',  frm.doc.__islocal ? 0 : 1);\n    } \n});\n\n// additional permission check\nfrappe.ui.form.on('Task',  {\n    validate: function(frm) {\n        if(user=='user1@example.com' && frm.doc.purpose!='Material Receipt') {\n            msgprint('You are only allowed Material Receipt');\n            validated = false;\n        }\n    } \n});\n\n// calculate sales incentive\nfrappe.ui.form.on('Sales Invoice',  {\n    validate: function(frm) {\n        // calculate incentives for each person on the deal\n        total_incentive = 0\n        $.each(frm.doc.sales_team,  function(i,  d) {\n            // calculate incentive\n            var incentive_percent = 2;\n            if(frm.doc.base_grand_total > 400) incentive_percent = 4;\n            // actual incentive\n            d.incentives = flt(frm.doc.base_grand_total) * incentive_percent / 100;\n            total_incentive += flt(d.incentives)\n        });\n        frm.doc.total_incentive = total_incentive;\n    } \n})\n\n
", "permlevel": 0, "print_hide": 0, "print_hide_if_no_value": 0, @@ -162,7 +130,8 @@ "issingle": 0, "istable": 0, "max_attachments": 0, - "modified": "2018-11-06 12:06:43.378396", + "menu_index": 0, + "modified": "2019-03-21 14:26:57.402994", "modified_by": "Administrator", "module": "Custom", "name": "Custom Script", diff --git a/frappe/custom/doctype/custom_script/custom_script.py b/frappe/custom/doctype/custom_script/custom_script.py index d3f5c100c6..e15819de65 100644 --- a/frappe/custom/doctype/custom_script/custom_script.py +++ b/frappe/custom/doctype/custom_script/custom_script.py @@ -7,9 +7,7 @@ from frappe.model.document import Document class CustomScript(Document): def autoname(self): - if not self.script_type: - self.script_type = 'Client' - self.name = self.dt + "-" + self.script_type + self.name = self.dt + "-Client" def on_update(self): frappe.clear_cache(doctype=self.dt) From 9d1b8f896ab5ad8a768263cc37d8410f042a4bd6 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Thu, 21 Mar 2019 14:30:43 +0530 Subject: [PATCH 2/4] fix: Custom Script form helpers and UX --- .../doctype/custom_script/custom_script.js | 71 ++++++++++++++++++- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/frappe/custom/doctype/custom_script/custom_script.js b/frappe/custom/doctype/custom_script/custom_script.js index f6beefa4d9..a1bef3d33f 100644 --- a/frappe/custom/doctype/custom_script/custom_script.js +++ b/frappe/custom/doctype/custom_script/custom_script.js @@ -2,9 +2,76 @@ // For license information, please see license.txt frappe.ui.form.on('Custom Script', { - refresh: function(frm) { + refresh(frm) { if (frm.doc.dt && frm.doc.script) { - frm.add_web_link("/desk#List/" + encodeURIComponent(frm.doc.dt) + "/List", "Test Script"); + frm.add_custom_button(__('Go to {0}', [frm.doc.dt]), + () => frappe.set_route('List', frm.doc.dt, 'List')); } + + frm.add_custom_button(__('Add script for Child Table'), () => { + frappe.model.with_doctype(frm.doc.dt, () => { + const child_tables = frappe.meta.get_docfields(frm.doc.dt, null, { + fieldtype: 'Table' + }).map(df => df.options); + + const d = new frappe.ui.Dialog({ + title: __('Select Child Table'), + fields: [ + { + label: __('Select Child Table'), + fieldtype: 'Link', + fieldname: 'cdt', + options: 'DocType', + get_query: () => { + return { + filters: { + istable: 1, + name: ['in', child_tables] + } + } + } + } + ], + primary_action: ({ cdt }) => { + frm.events.add_script_for_doctype(frm, cdt); + d.hide(); + } + }); + + d.show(); + }); + }); + + frm.set_query('dt', { + filters: { + istable: 0 + } + }); + }, + + dt(frm) { + if (!frm.doc.script) { + frm.events.add_script_for_doctype(frm, frm.doc.dt); + } + + if (frm.doc.script && !frm.doc.script.includes(frm.doc.dt)) { + frm.doc.script = ''; + frm.events.add_script_for_doctype(frm, frm.doc.dt); + } + }, + + add_script_for_doctype(frm, doctype) { + let boilerplate = ` +frappe.ui.form.on('${doctype}', { + refresh(frm) { + // your code here + } +}) + `.trim(); + let script = (frm.doc.script || ''); + if (script) { + script += '\n\n'; + } + frm.set_value('script', script + boilerplate); } }); From 13512a4444f12ce64e4da1f98683440a210b1a54 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Thu, 21 Mar 2019 14:43:21 +0530 Subject: [PATCH 3/4] fix: Missing semicolon --- frappe/custom/doctype/custom_script/custom_script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/custom/doctype/custom_script/custom_script.js b/frappe/custom/doctype/custom_script/custom_script.js index a1bef3d33f..dc449c506a 100644 --- a/frappe/custom/doctype/custom_script/custom_script.js +++ b/frappe/custom/doctype/custom_script/custom_script.js @@ -28,7 +28,7 @@ frappe.ui.form.on('Custom Script', { istable: 1, name: ['in', child_tables] } - } + }; } } ], From f724cd84a101e847b610b55ba3781ec8da4a0d6a Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Fri, 22 Mar 2019 00:32:50 +0530 Subject: [PATCH 4/4] fix: Remove usage of script_type --- frappe/desk/form/meta.py | 3 +-- frappe/utils/fixtures.py | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/frappe/desk/form/meta.py b/frappe/desk/form/meta.py index cd87ffaaee..132bfd0523 100644 --- a/frappe/desk/form/meta.py +++ b/frappe/desk/form/meta.py @@ -128,8 +128,7 @@ class FormMeta(Meta): def add_custom_script(self): """embed all require files""" # custom script - custom = frappe.db.get_value("Custom Script", {"dt": self.name, - "script_type": "Client"}, "script") or "" + custom = frappe.db.get_value("Custom Script", {"dt": self.name}, "script") or "" self.set("__custom_js", custom) diff --git a/frappe/utils/fixtures.py b/frappe/utils/fixtures.py index 8445450f94..4009204c52 100644 --- a/frappe/utils/fixtures.py +++ b/frappe/utils/fixtures.py @@ -46,7 +46,6 @@ def import_custom_scripts(app): frappe.get_doc({ "doctype":"Custom Script", "dt": doctype, - "script_type": "Client", "script": script }).insert()