Merge pull request #7097 from netchampfaris/refactor-custom-script

fix: Custom Script form and UX
This commit is contained in:
Suraj Shetty 2019-03-22 10:17:09 +05:30 committed by GitHub
commit fda05cab83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 47 deletions

View file

@ -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);
}
});

View file

@ -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": "<h3>Custom Script Help</h3>\n<p>Custom Scripts are executed only on the client-side (i.e. in Forms). Here are some examples to get you started</p>\n<pre><code>\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 &lt; 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' &amp;&amp; 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 &gt; 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</code></pre>",
"options": "<h3>Custom Script Help</h3>\n<p>Custom Scripts are executed only on the client-side (i.e. in Forms). Here are some examples to get you started</p>\n<pre><code>\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 &lt; 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' &amp;&amp; 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 &gt; 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</code></pre>",
"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",

View file

@ -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)

View file

@ -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)

View file

@ -46,7 +46,6 @@ def import_custom_scripts(app):
frappe.get_doc({
"doctype":"Custom Script",
"dt": doctype,
"script_type": "Client",
"script": script
}).insert()