refactor: replace Link field with Select field

This commit is contained in:
Saqib Ansari 2022-01-27 15:21:47 +05:30
parent 3cf3489172
commit b5ebf062cb
4 changed files with 90 additions and 137 deletions

View file

@ -53,73 +53,84 @@ frappe.ui.form.on('Form Tour', {
};
});
frm.set_query("field", "steps", function() {
return {
query: "frappe.desk.doctype.form_tour.form_tour.get_docfield_list",
filters: {
doctype: frm.doc.reference_doctype,
hidden: 0
}
};
});
frm.set_query("parent_field", "steps", function() {
return {
query: "frappe.desk.doctype.form_tour.form_tour.get_docfield_list",
filters: {
doctype: frm.doc.reference_doctype,
fieldtype: "Table",
hidden: 0,
}
};
});
frm.trigger('reference_doctype');
},
reference_doctype(frm) {
if (!frm.doc.reference_doctype) return;
frappe.db.get_list('DocField', {
filters: {
parent: frm.doc.reference_doctype,
parenttype: 'DocType',
fieldtype: 'Table'
},
fields: ['options']
}).then(res => {
if (Array.isArray(res)) {
frm.child_doctypes = res.map(r => r.options);
}
frappe.model.with_doctype(frm.doc.reference_doctype, () => {
let fields = frappe.meta
.get_docfields(frm.doc.reference_doctype, null, {
hidden: 0
})
.map(df => ({
label: `${df.label || 'No Label'} (${df.fieldtype})`,
value: df.fieldname
}));
frm.fields_dict.steps.grid.update_docfield_property(
"fieldname",
"options",
[""].concat(fields)
);
let parent_fields = frappe.meta
.get_docfields(frm.doc.reference_doctype, null, {
fieldtype: "Table",
hidden: 0
})
.map(df => ({
label: `${df.label || 'No Label'} (${df.fieldtype})`,
value: df.fieldname
}));
frm.fields_dict.steps.grid.update_docfield_property(
"parent_fieldname",
"options",
[""].concat(parent_fields)
);
});
}
});
frappe.ui.form.on('Form Tour Step', {
parent_field(frm, cdt, cdn) {
form_render(frm, cdt, cdn) {
if (locals[cdt][cdn].is_table_field) {
frm.trigger('parent_fieldname', cdt, cdn);
}
},
parent_fieldname(frm, cdt, cdn) {
const child_row = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, 'field', '');
const field_control = get_child_field("steps", cdn, "field");
field_control.get_query = function() {
return {
query: "frappe.desk.doctype.form_tour.form_tour.get_docfield_list",
filters: {
doctype: child_row.child_doctype,
const parent_fieldname_df = frappe
.get_meta(frm.doc.reference_doctype)
.fields.find(df => df.fieldname == child_row.parent_fieldname);
frappe.model.with_doctype(parent_fieldname_df.options, () => {
let fields = frappe.meta
.get_docfields(parent_fieldname_df.options, null, {
hidden: 0
}
};
};
})
.map(df => ({
label: `${df.label || 'No Label'} (${df.fieldtype})`,
value: df.fieldname
}));
frm.fields_dict.steps.grid.update_docfield_property(
"fieldname",
"options",
[""].concat(fields)
);
if (child_row.fieldname) {
frappe.model.set_value(cdt, cdn, 'fieldname', child_row.fieldname);
}
});
}
});
function get_child_field(child_table, child_name, fieldname) {
// gets the field from grid row form
const grid = cur_frm.fields_dict[child_table].grid;
const grid_row = grid.grid_rows_by_docname[child_name];
return grid_row.grid_form.fields_dict[fieldname];
}
async function check_if_single(doctype) {
const { message } = await frappe.db.get_value('DocType', doctype, 'issingle');
return message.issingle || 0;

View file

@ -5,58 +5,23 @@ import frappe
from frappe.model.document import Document
from frappe.modules.export_file import export_to_files
class FormTour(Document):
def before_insert(self):
if not self.is_standard:
return
def before_save(self):
meta = frappe.get_meta(self.reference_doctype)
for step in self.steps:
if step.is_table_field and step.parent_fieldname:
parent_field_df = meta.get_field(step.parent_fieldname)
step.child_doctype = parent_field_df.options
# while syncing, set proper docfield reference
for d in self.steps:
if not frappe.db.exists('DocField', d.field):
d.field = frappe.db.get_value('DocField', {
'fieldname': d.fieldname, 'parent': self.reference_doctype, 'fieldtype': d.fieldtype
}, "name")
if d.is_table_field and not frappe.db.exists('DocField', d.parent_field):
d.parent_field = frappe.db.get_value('DocField', {
'fieldname': d.parent_fieldname, 'parent': self.reference_doctype, 'fieldtype': 'Table'
}, "name")
field_df = frappe.get_meta(step.child_doctype).get_field(step.fieldname)
step.label = field_df.label
step.fieldtype = field_df.fieldtype
else:
field_df = meta.get_field(step.fieldname)
step.label = field_df.label
step.fieldtype = field_df.fieldtype
def on_update(self):
if frappe.conf.developer_mode and self.is_standard:
export_to_files([['Form Tour', self.name]], self.module)
def before_export(self, doc):
for d in doc.steps:
d.field = ""
d.parent_field = ""
@frappe.whitelist()
@frappe.validate_and_sanitize_search_inputs
def get_docfield_list(doctype, txt, searchfield, start, page_len, filters):
or_filters = [
['fieldname', 'like', '%' + txt + '%'],
['label', 'like', '%' + txt + '%'],
['fieldtype', 'like', '%' + txt + '%']
]
parent_doctype = filters.get('doctype')
fieldtype = filters.get('fieldtype')
if not fieldtype:
excluded_fieldtypes = ['Column Break']
excluded_fieldtypes += filters.get('excluded_fieldtypes', [])
fieldtype_filter = ['not in', excluded_fieldtypes]
else:
fieldtype_filter = fieldtype
docfields = frappe.get_all(
doctype,
fields=["name as value", "label", "fieldtype"],
filters={'parent': parent_doctype, 'fieldtype': fieldtype_filter},
or_filters=or_filters,
limit_start=start,
limit_page_length=page_len,
order_by="idx",
as_list=1,
)
return docfields
export_to_files([["Form Tour", self.name]], self.module)

View file

@ -6,19 +6,17 @@
"field_order": [
"is_table_field",
"section_break_2",
"parent_field",
"field",
"parent_fieldname",
"fieldname",
"title",
"description",
"column_break_2",
"position",
"label",
"fieldtype",
"has_next_condition",
"next_step_condition",
"section_break_13",
"fieldname",
"parent_fieldname",
"fieldtype",
"child_doctype"
],
"fields": [
@ -38,23 +36,13 @@
"reqd": 1
},
{
"depends_on": "eval: (!doc.is_table_field || (doc.is_table_field && doc.parent_field))",
"fieldname": "field",
"fieldtype": "Link",
"label": "Field",
"options": "DocField",
"depends_on": "eval: (!doc.is_table_field || (doc.is_table_field && doc.parent_fieldname))",
"fieldname": "fieldname",
"fieldtype": "Select",
"label": "Fieldname",
"reqd": 1
},
{
"fetch_from": "field.fieldname",
"fieldname": "fieldname",
"fieldtype": "Data",
"hidden": 1,
"label": "Fieldname",
"read_only": 1
},
{
"fetch_from": "field.label",
"fieldname": "label",
"fieldtype": "Data",
"in_list_view": 1,
@ -88,10 +76,8 @@
},
{
"default": "0",
"fetch_from": "field.fieldtype",
"fieldname": "fieldtype",
"fieldtype": "Data",
"hidden": 1,
"label": "Fieldtype",
"read_only": 1
},
@ -105,14 +91,6 @@
"fieldname": "section_break_2",
"fieldtype": "Section Break"
},
{
"depends_on": "is_table_field",
"fieldname": "parent_field",
"fieldtype": "Link",
"label": "Parent Field",
"mandatory_depends_on": "is_table_field",
"options": "DocField"
},
{
"fieldname": "section_break_13",
"fieldtype": "Section Break",
@ -120,7 +98,6 @@
"label": "Hidden Fields"
},
{
"fetch_from": "parent_field.options",
"fieldname": "child_doctype",
"fieldtype": "Data",
"hidden": 1,
@ -128,18 +105,17 @@
"read_only": 1
},
{
"fetch_from": "parent_field.fieldname",
"depends_on": "is_table_field",
"fieldname": "parent_fieldname",
"fieldtype": "Data",
"hidden": 1,
"label": "Parent Fieldname",
"read_only": 1
"fieldtype": "Select",
"label": "Parent Field",
"mandatory_depends_on": "is_table_field"
}
],
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2021-06-06 20:52:21.076972",
"modified": "2022-01-27 15:18:36.481801",
"modified_by": "Administrator",
"module": "Desk",
"name": "Form Tour Step",
@ -147,5 +123,6 @@
"permissions": [],
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"track_changes": 1
}

View file

@ -151,7 +151,7 @@ frappe.ui.form.FormTour = class FormTour {
const curr_step = step_info;
const next_step = this.tour.steps[curr_step.idx];
const is_next_field_in_curr_table = next_step.parent_field == curr_step.field;
const is_next_field_in_curr_table = next_step.parent_fieldname == curr_step.fieldname;
if (!is_next_field_in_curr_table) return;