From 98faba837fb252e81646afe7e0e496f7522e36cb Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Fri, 28 Jan 2022 20:30:52 +0530 Subject: [PATCH 1/9] fix: update child table row's linked field on creating new connection --- frappe/public/js/frappe/form/form.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/frappe/public/js/frappe/form/form.js b/frappe/public/js/frappe/form/form.js index 5c0b6b1399..982f9ea120 100644 --- a/frappe/public/js/frappe/form/form.js +++ b/frappe/public/js/frappe/form/form.js @@ -1670,16 +1670,10 @@ frappe.ui.form.Form = class FrappeForm { this.custom_buttons[__(this.custom_make_buttons[doctype])].trigger('click'); } else { frappe.model.with_doctype(doctype, function() { - var new_doc = frappe.model.get_new_doc(doctype); + var new_doc = frappe.model.get_new_doc(doctype, null, null, true); // set link fields (if found) - frappe.get_meta(doctype).fields.forEach(function(df) { - if(df.fieldtype==='Link' && df.options===me.doctype) { - new_doc[df.fieldname] = me.doc.name; - } else if (['Link', 'Dynamic Link'].includes(df.fieldtype) && me.doc[df.fieldname]) { - new_doc[df.fieldname] = me.doc[df.fieldname]; - } - }); + me.set_link_field(doctype, new_doc, me); frappe.ui.form.make_quick_entry(doctype, null, null, new_doc); // frappe.set_route('Form', doctype, new_doc.name); @@ -1687,6 +1681,19 @@ frappe.ui.form.Form = class FrappeForm { } } + set_link_field(doctype, new_doc, me) { + frappe.get_meta(doctype).fields.forEach(function(df) { + if(df.fieldtype==='Link' && df.options===me.doctype) { + new_doc[df.fieldname] = me.doc.name; + } else if (['Link', 'Dynamic Link'].includes(df.fieldtype) && me.doc[df.fieldname]) { + new_doc[df.fieldname] = me.doc[df.fieldname]; + } else if (df.fieldtype==='Table' && df.options) { + let row = new_doc[df.fieldname][0]; + me.set_link_field(df.options, row, me); + } + }); + } + update_in_all_rows(table_fieldname, fieldname, value) { // update the child value in all tables where it is missing if(!value) return; From 80f733d675fe71d192f8f260859977bd5de659ca Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Fri, 28 Jan 2022 21:21:27 +0530 Subject: [PATCH 2/9] chore: improved code --- frappe/public/js/frappe/form/form.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/frappe/public/js/frappe/form/form.js b/frappe/public/js/frappe/form/form.js index 982f9ea120..aef146a569 100644 --- a/frappe/public/js/frappe/form/form.js +++ b/frappe/public/js/frappe/form/form.js @@ -1663,17 +1663,17 @@ frappe.ui.form.Form = class FrappeForm { // make new doctype from the current form // will handover to `make_methods` if defined // or will create and match link fields - var me = this; + let me = this; if(this.make_methods && this.make_methods[doctype]) { return this.make_methods[doctype](this); } else if(this.custom_make_buttons && this.custom_make_buttons[doctype]) { this.custom_buttons[__(this.custom_make_buttons[doctype])].trigger('click'); } else { frappe.model.with_doctype(doctype, function() { - var new_doc = frappe.model.get_new_doc(doctype, null, null, true); + let new_doc = frappe.model.get_new_doc(doctype, null, null, true); // set link fields (if found) - me.set_link_field(doctype, new_doc, me); + me.set_link_field(doctype, new_doc); frappe.ui.form.make_quick_entry(doctype, null, null, new_doc); // frappe.set_route('Form', doctype, new_doc.name); @@ -1681,15 +1681,16 @@ frappe.ui.form.Form = class FrappeForm { } } - set_link_field(doctype, new_doc, me) { + set_link_field(doctype, new_doc) { + let me = this; frappe.get_meta(doctype).fields.forEach(function(df) { - if(df.fieldtype==='Link' && df.options===me.doctype) { + if (df.fieldtype==='Link' && df.options===me.doctype) { new_doc[df.fieldname] = me.doc.name; } else if (['Link', 'Dynamic Link'].includes(df.fieldtype) && me.doc[df.fieldname]) { new_doc[df.fieldname] = me.doc[df.fieldname]; } else if (df.fieldtype==='Table' && df.options) { let row = new_doc[df.fieldname][0]; - me.set_link_field(df.options, row, me); + me.set_link_field(df.options, row); } }); } From c64a42288ddef1ee727de5c8037d615525f6ccf5 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Wed, 2 Feb 2022 12:23:12 +0530 Subject: [PATCH 3/9] chore: keep spaces around operators --- frappe/public/js/frappe/form/form.js | 34 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/frappe/public/js/frappe/form/form.js b/frappe/public/js/frappe/form/form.js index aef146a569..5a09e6eef7 100644 --- a/frappe/public/js/frappe/form/form.js +++ b/frappe/public/js/frappe/form/form.js @@ -248,7 +248,7 @@ frappe.ui.form.Form = class FrappeForm { // on main doc frappe.model.on(me.doctype, "*", function(fieldname, value, doc) { // set input - if(doc.name===me.docname) { + if(doc.name === me.docname) { me.dirty(); let field = me.fields_dict[fieldname]; @@ -271,7 +271,7 @@ frappe.ui.form.Form = class FrappeForm { // using $.each to preserve df via closure $.each(table_fields, function(i, df) { frappe.model.on(df.options, "*", function(fieldname, value, doc) { - if(doc.parent===me.docname && doc.parentfield===df.fieldname) { + if(doc.parent === me.docname && doc.parentfield === df.fieldname) { me.dirty(); me.fields_dict[df.fieldname].grid.set_value(fieldname, value, doc); return me.script_manager.trigger(fieldname, doc.doctype, doc.name); @@ -366,7 +366,7 @@ frappe.ui.form.Form = class FrappeForm { // } if(switched) { - if(this.show_print_first && this.doc.docstatus===1) { + if(this.show_print_first && this.doc.docstatus === 1) { // show print view this.print_doc(); } @@ -374,9 +374,9 @@ frappe.ui.form.Form = class FrappeForm { // set status classes this.$wrapper.removeClass('validated-form') - .toggleClass('editable-form', this.doc.docstatus===0) - .toggleClass('submitted-form', this.doc.docstatus===1) - .toggleClass('cancelled-form', this.doc.docstatus===2); + .toggleClass('editable-form', this.doc.docstatus === 0) + .toggleClass('submitted-form', this.doc.docstatus === 1) + .toggleClass('cancelled-form', this.doc.docstatus === 2); this.show_conflict_message(); } @@ -415,7 +415,7 @@ frappe.ui.form.Form = class FrappeForm { frappe.throw(`Action ${action} not found`); } } - if (action.action_type==='Server Action') { + if (action.action_type === 'Server Action') { return frappe.xcall(action.action, {'doc': this.doc}).then((doc) => { if (doc.doctype) { // document is returned by the method, @@ -430,7 +430,7 @@ frappe.ui.form.Form = class FrappeForm { alert: true }); }); - } else if (action.action_type==='Route') { + } else if (action.action_type === 'Route') { return frappe.set_route(action.action); } } @@ -647,9 +647,9 @@ frappe.ui.form.Form = class FrappeForm { save_or_update() { if(this.save_disabled) return; - if(this.doc.docstatus===0) { + if(this.doc.docstatus === 0) { this.save(); - } else if(this.doc.docstatus===1 && this.doc.__unsaved) { + } else if(this.doc.docstatus === 1 && this.doc.__unsaved) { this.save("Update"); } } @@ -1013,7 +1013,7 @@ frappe.ui.form.Form = class FrappeForm { && !this.is_dirty() && !this.is_new() && !frappe.model.has_workflow(this.doctype) // show only if no workflow - && this.doc.docstatus===0) { + && this.doc.docstatus === 0) { this.dashboard.add_comment(__('Submit this document to confirm'), 'blue', true); } } @@ -1337,7 +1337,7 @@ frappe.ui.form.Form = class FrappeForm { } field_map(fnames, fn) { - if(typeof fnames==='string') { + if(typeof fnames === 'string') { if(fnames == '*') { fnames = Object.keys(this.fields_dict); } else { @@ -1493,7 +1493,7 @@ frappe.ui.form.Form = class FrappeForm { call(opts, args, callback) { var me = this; - if(typeof opts==='string') { + if(typeof opts === 'string') { // called as frm.call('do_this', {with_arg: 'arg'}); opts = { method: opts, @@ -1503,7 +1503,7 @@ frappe.ui.form.Form = class FrappeForm { }; } if(!opts.doc) { - if(opts.method.indexOf(".")===-1) + if(opts.method.indexOf(".") === -1) opts.method = frappe.model.get_server_module_name(me.doctype) + "." + opts.method; opts.original_callback = opts.callback; opts.callback = function(r) { @@ -1514,7 +1514,7 @@ frappe.ui.form.Form = class FrappeForm { var std_field_list = ["doctype"].concat(frappe.model.std_fields_list); for (var key in r.message) { - if (std_field_list.indexOf(key)===-1) { + if (std_field_list.indexOf(key) === -1) { opts.child[key] = r.message[key]; } } @@ -1684,11 +1684,11 @@ frappe.ui.form.Form = class FrappeForm { set_link_field(doctype, new_doc) { let me = this; frappe.get_meta(doctype).fields.forEach(function(df) { - if (df.fieldtype==='Link' && df.options===me.doctype) { + if (df.fieldtype === 'Link' && df.options === me.doctype) { new_doc[df.fieldname] = me.doc.name; } else if (['Link', 'Dynamic Link'].includes(df.fieldtype) && me.doc[df.fieldname]) { new_doc[df.fieldname] = me.doc[df.fieldname]; - } else if (df.fieldtype==='Table' && df.options) { + } else if (df.fieldtype === 'Table' && df.options) { let row = new_doc[df.fieldname][0]; me.set_link_field(df.options, row); } From 434ab616dfcd99ea27906bccc132040cc432e355 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Wed, 2 Feb 2022 12:43:58 +0530 Subject: [PATCH 4/9] fix: sider fix --- frappe/public/js/frappe/form/form.js | 176 +++++++++++++-------------- 1 file changed, 88 insertions(+), 88 deletions(-) diff --git a/frappe/public/js/frappe/form/form.js b/frappe/public/js/frappe/form/form.js index 5a09e6eef7..0f88983c7e 100644 --- a/frappe/public/js/frappe/form/form.js +++ b/frappe/public/js/frappe/form/form.js @@ -44,7 +44,7 @@ frappe.ui.form.Form = class FrappeForm { setup_meta() { this.meta = frappe.get_doc('DocType', this.doctype); - if(this.meta.istable) { + if (this.meta.istable) { this.meta.in_dialog = 1; } @@ -248,7 +248,7 @@ frappe.ui.form.Form = class FrappeForm { // on main doc frappe.model.on(me.doctype, "*", function(fieldname, value, doc) { // set input - if(doc.name === me.docname) { + if (doc.name === me.docname) { me.dirty(); let field = me.fields_dict[fieldname]; @@ -271,7 +271,7 @@ frappe.ui.form.Form = class FrappeForm { // using $.each to preserve df via closure $.each(table_fields, function(i, df) { frappe.model.on(df.options, "*", function(fieldname, value, doc) { - if(doc.parent === me.docname && doc.parentfield === df.fieldname) { + if (doc.parent === me.docname && doc.parentfield === df.fieldname) { me.dirty(); me.fields_dict[df.fieldname].grid.set_value(fieldname, value, doc); return me.script_manager.trigger(fieldname, doc.doctype, doc.name); @@ -299,7 +299,7 @@ frappe.ui.form.Form = class FrappeForm { e.stopPropagation(); e.preventDefault(); - if(me.doc.__islocal) { + if (me.doc.__islocal) { frappe.msgprint(__("Please save before attaching.")); throw "attach error"; } @@ -322,19 +322,19 @@ frappe.ui.form.Form = class FrappeForm { refresh(docname) { var switched = docname ? true : false; - if(docname) { + if (docname) { this.switch_doc(docname); } cur_frm = this; - if(this.docname) { // document to show + if (this.docname) { // document to show this.save_disabled = false; // set the doc this.doc = frappe.get_doc(this.doctype, this.docname); // check permissions - if(!this.has_read_permission()) { + if (!this.has_read_permission()) { frappe.show_not_permitted(__(this.doctype) + " " + __(this.docname)); return; } @@ -353,7 +353,7 @@ frappe.ui.form.Form = class FrappeForm { } // do setup - if(!this.setup_done) { + if (!this.setup_done) { this.setup(); } @@ -361,12 +361,12 @@ frappe.ui.form.Form = class FrappeForm { this.trigger_onload(switched); // if print format is shown, refresh the format - // if(this.print_preview.wrapper.is(":visible")) { + // if (this.print_preview.wrapper.is(":visible")) { // this.print_preview.preview(); // } - if(switched) { - if(this.show_print_first && this.doc.docstatus === 1) { + if (switched) { + if (this.show_print_first && this.doc.docstatus === 1) { // show print view this.print_doc(); } @@ -449,7 +449,7 @@ frappe.ui.form.Form = class FrappeForm { } check_reload() { - if(this.doc && (!this.doc.__unsaved) && this.doc.__last_sync_on && + if (this.doc && (!this.doc.__unsaved) && this.doc.__last_sync_on && (new Date() - this.doc.__last_sync_on) > (this.refresh_if_stale_for * 1000)) { this.reload_doc(); return true; @@ -458,7 +458,7 @@ frappe.ui.form.Form = class FrappeForm { trigger_onload(switched) { this.cscript.is_onload = false; - if(!this.opendocs[this.docname]) { + if (!this.opendocs[this.docname]) { var me = this; this.cscript.is_onload = true; this.initialize_new_doc(); @@ -496,13 +496,13 @@ frappe.ui.form.Form = class FrappeForm { }); // update seen - if(this.meta.track_seen) { + if (this.meta.track_seen) { $('.list-id[data-name="'+ me.docname +'"]').addClass('seen'); } } render_form(switched) { - if(!this.meta.istable) { + if (!this.meta.istable) { this.layout.doc = this.doc; this.layout.attach_doc_and_docfields(); @@ -530,7 +530,7 @@ frappe.ui.form.Form = class FrappeForm { () => this.script_manager.trigger("refresh"), // call onload post render for callbacks to be fired () => { - if(this.cscript.is_onload) { + if (this.cscript.is_onload) { return this.script_manager.trigger("onload_post_render"); } }, @@ -547,7 +547,7 @@ frappe.ui.form.Form = class FrappeForm { this.cscript.is_onload && this.set_first_tab_as_active(); - if(!this.hidden) { + if (!this.hidden) { this.layout.show_empty_form_message(); } @@ -588,7 +588,7 @@ frappe.ui.form.Form = class FrappeForm { } cleanup_refresh() { - if(this.fields_dict['amended_from']) { + if (this.fields_dict['amended_from']) { if (this.doc.amended_from) { unhide_field('amended_from'); if (this.fields_dict['amendment_date']) unhide_field('amendment_date'); @@ -598,15 +598,15 @@ frappe.ui.form.Form = class FrappeForm { } } - if(this.fields_dict['trash_reason']) { - if(this.doc.trash_reason && this.doc.docstatus == 2) { + if (this.fields_dict['trash_reason']) { + if (this.doc.trash_reason && this.doc.docstatus == 2) { unhide_field('trash_reason'); } else { hide_field('trash_reason'); } } - if(this.meta.autoname && this.meta.autoname.substr(0,6)=='field:' && !this.doc.__islocal) { + if (this.meta.autoname && this.meta.autoname.substr(0,6)=='field:' && !this.doc.__islocal) { var fn = this.meta.autoname.substr(6); if (this.doc[fn]) { @@ -614,7 +614,7 @@ frappe.ui.form.Form = class FrappeForm { } } - if(this.meta.autoname=="naming_series:" && !this.doc.__islocal) { + if (this.meta.autoname=="naming_series:" && !this.doc.__islocal) { this.toggle_display("naming_series", false); } } @@ -622,12 +622,12 @@ frappe.ui.form.Form = class FrappeForm { refresh_header(switched) { // set title // main title - if(!this.meta.in_dialog || this.in_form) { + if (!this.meta.in_dialog || this.in_form) { frappe.utils.set_title(this.meta.issingle ? this.doctype : this.docname); } // show / hide buttons - if(this.toolbar) { + if (this.toolbar) { if (switched) { this.toolbar.current_status = undefined; } @@ -645,11 +645,11 @@ frappe.ui.form.Form = class FrappeForm { // SAVE save_or_update() { - if(this.save_disabled) return; + if (this.save_disabled) return; - if(this.doc.docstatus === 0) { + if (this.doc.docstatus === 0) { this.save(); - } else if(this.doc.docstatus === 1 && this.doc.__unsaved) { + } else if (this.doc.docstatus === 1 && this.doc.__unsaved) { this.save("Update"); } } @@ -669,13 +669,13 @@ frappe.ui.form.Form = class FrappeForm { validate_and_save(save_action, callback, btn, on_error, resolve, reject) { var me = this; - if(!save_action) save_action = "Save"; + if (!save_action) save_action = "Save"; this.validate_form_action(save_action, resolve); var after_save = function(r) { // to remove hash from URL to avoid scroll after save history.replaceState(null, null, ' '); - if(!r.exc) { + if (!r.exc) { if (["Save", "Update", "Amend"].indexOf(save_action)!==-1) { frappe.utils.play_sound("click"); } @@ -694,7 +694,7 @@ frappe.ui.form.Form = class FrappeForm { } me.refresh(); } else { - if(on_error) { + if (on_error) { on_error(); reject(); } @@ -708,20 +708,20 @@ frappe.ui.form.Form = class FrappeForm { console.error(e) } btn && $(btn).prop("disabled", false); - if(on_error) { + if (on_error) { on_error(); reject(); } }; - if(save_action != "Update") { + if (save_action != "Update") { // validate frappe.validated = true; frappe.run_serially([ () => this.script_manager.trigger("validate"), () => this.script_manager.trigger("before_save"), () => { - if(!frappe.validated) { + if (!frappe.validated) { fail(); return; } @@ -741,12 +741,12 @@ frappe.ui.form.Form = class FrappeForm { frappe.confirm(__("Permanently Submit {0}?", [this.docname]), function() { frappe.validated = true; me.script_manager.trigger("before_submit").then(function() { - if(!frappe.validated) { + if (!frappe.validated) { return me.handle_save_fail(btn, on_error); } me.save('Submit', function(r) { - if(r.exc) { + if (r.exc) { me.handle_save_fail(btn, on_error); } else { frappe.utils.play_sound("submit"); @@ -939,7 +939,7 @@ frappe.ui.form.Form = class FrappeForm { } if (!this.perm[0][perm_to_check] && !allowed_for_workflow) { - if(resolve) { + if (resolve) { // re-enable buttons resolve(); } @@ -995,8 +995,8 @@ frappe.ui.form.Form = class FrappeForm { } show_conflict_message() { - if(this.doc.__needs_refresh) { - if(this.doc.__unsaved) { + if (this.doc.__needs_refresh) { + if (this.doc.__unsaved) { this.dashboard.clear_headline(); this.dashboard.set_headline_alert(__("This form has been modified after you have loaded it") + '' @@ -1008,7 +1008,7 @@ frappe.ui.form.Form = class FrappeForm { } show_submit_message() { - if(this.meta.is_submittable + if (this.meta.is_submittable && this.perm[0] && this.perm[0].submit && !this.is_dirty() && !this.is_new() @@ -1019,9 +1019,9 @@ frappe.ui.form.Form = class FrappeForm { } show_web_link() { - if(!this.doc.__islocal && this.doc.__onload && this.doc.__onload.is_website_generator) { + if (!this.doc.__islocal && this.doc.__onload && this.doc.__onload.is_website_generator) { this.web_link && this.web_link.remove(); - if(this.doc.__onload.published) { + if (this.doc.__onload.published) { this.add_web_link("/" + this.doc.route); } } @@ -1038,16 +1038,16 @@ frappe.ui.form.Form = class FrappeForm { var dt = this.parent_doctype ? this.parent_doctype : this.doctype; this.perm = frappe.perm.get_perm(dt, this.doc); - if(!this.perm[0].read) { + if (!this.perm[0].read) { return 0; } return 1; } check_doctype_conflict(docname) { - if(this.doctype=='DocType' && docname=='DocType') { + if (this.doctype=='DocType' && docname=='DocType') { frappe.msgprint(__('Allowing DocType, DocType. Be careful!')); - } else if(this.doctype=='DocType') { + } else if (this.doctype=='DocType') { if (frappe.views.formview[docname] || frappe.pages['List/'+docname]) { window.location.reload(); // frappe.msgprint(__("Cannot open {0} when its instance is open", ['DocType'])) @@ -1066,16 +1066,16 @@ frappe.ui.form.Form = class FrappeForm { // notify this form of renamed records rename_notify(dt, old, name) { // from form - if(this.meta.istable) + if (this.meta.istable) return; - if(this.docname == old) + if (this.docname == old) this.docname = name; else return; // cleanup - if(this && this.opendocs[old] && frappe.meta.docfield_copy[dt]) { + if (this && this.opendocs[old] && frappe.meta.docfield_copy[dt]) { // delete docfield copy frappe.meta.docfield_copy[dt][name] = frappe.meta.docfield_copy[dt][old]; delete frappe.meta.docfield_copy[dt][old]; @@ -1084,7 +1084,7 @@ frappe.ui.form.Form = class FrappeForm { delete this.opendocs[old]; this.opendocs[name] = true; - if(this.meta.in_dialog || !this.in_form) { + if (this.meta.in_dialog || !this.in_form) { return; } @@ -1159,7 +1159,7 @@ frappe.ui.form.Form = class FrappeForm { newdoc.idx = null; newdoc.__run_link_triggers = false; - if(onload) { + if (onload) { onload(newdoc); } frappe.set_route('Form', newdoc.doctype, newdoc.name); @@ -1168,7 +1168,7 @@ frappe.ui.form.Form = class FrappeForm { reload_doc() { this.check_doctype_conflict(this.docname); - if(!this.doc.__islocal) { + if (!this.doc.__islocal) { frappe.model.remove_from_locals(this.doctype, this.docname); return frappe.model.with_doc(this.doctype, this.docname, () => { this.refresh(); @@ -1315,9 +1315,9 @@ frappe.ui.form.Form = class FrappeForm { $.each(fields_list, function(i, fname) { var docfield = frappe.meta.docfield_map[doctype][fname]; - if(docfield) { + if (docfield) { var label = __(docfield.label || "").replace(/\([^\)]*\)/g, ""); // eslint-disable-line - if(parentfield) { + if (parentfield) { grid_field_label_map[doctype + "-" + fname] = label.trim() + " (" + __(currency) + ")"; } else { @@ -1337,8 +1337,8 @@ frappe.ui.form.Form = class FrappeForm { } field_map(fnames, fn) { - if(typeof fnames === 'string') { - if(fnames == '*') { + if (typeof fnames === 'string') { + if (fnames == '*') { fnames = Object.keys(this.fields_dict); } else { fnames = [fnames]; @@ -1347,7 +1347,7 @@ frappe.ui.form.Form = class FrappeForm { for (var i=0, l=fnames.length; i { - if(!unique_keys.includes(key)) { + if (!unique_keys.includes(key)) { d[key] = values[key]; } }); @@ -1452,9 +1452,9 @@ frappe.ui.form.Form = class FrappeForm { var me = this; var _set = function(f, v) { var fieldobj = me.fields_dict[f]; - if(fieldobj) { - if(!if_missing || !frappe.model.has_value(me.doctype, me.doc.name, f)) { - if(frappe.model.table_fields.includes(fieldobj.df.fieldtype) && $.isArray(v)) { + if (fieldobj) { + if (!if_missing || !frappe.model.has_value(me.doctype, me.doc.name, f)) { + if (frappe.model.table_fields.includes(fieldobj.df.fieldtype) && $.isArray(v)) { frappe.model.clear_table(me.doc, fieldobj.df.fieldname); @@ -1477,13 +1477,13 @@ frappe.ui.form.Form = class FrappeForm { } }; - if(typeof field=="string") { + if (typeof field=="string") { return _set(field, value); - } else if($.isPlainObject(field)) { + } else if ($.isPlainObject(field)) { let tasks = []; for (let f in field) { let v = field[f]; - if(me.get_field(f)) { + if (me.get_field(f)) { tasks.push(() => _set(f, v)); } } @@ -1493,7 +1493,7 @@ frappe.ui.form.Form = class FrappeForm { call(opts, args, callback) { var me = this; - if(typeof opts === 'string') { + if (typeof opts === 'string') { // called as frm.call('do_this', {with_arg: 'arg'}); opts = { method: opts, @@ -1502,13 +1502,13 @@ frappe.ui.form.Form = class FrappeForm { callback: callback }; } - if(!opts.doc) { - if(opts.method.indexOf(".") === -1) + if (!opts.doc) { + if (opts.method.indexOf(".") === -1) opts.method = frappe.model.get_server_module_name(me.doctype) + "." + opts.method; opts.original_callback = opts.callback; opts.callback = function(r) { - if($.isPlainObject(r.message)) { - if(opts.child) { + if ($.isPlainObject(r.message)) { + if (opts.child) { // update child doc opts.child = locals[opts.child.doctype][opts.child.name]; @@ -1530,7 +1530,7 @@ frappe.ui.form.Form = class FrappeForm { } else { opts.original_callback = opts.callback; opts.callback = function(r) { - if(!r.exc) me.refresh_fields(); + if (!r.exc) me.refresh_fields(); opts.original_callback && opts.original_callback(r); }; @@ -1571,7 +1571,7 @@ frappe.ui.form.Form = class FrappeForm { } get_title() { - if(this.meta.title_field) { + if (this.meta.title_field) { return this.doc[this.meta.title_field]; } else { return this.doc.name; @@ -1585,11 +1585,11 @@ frappe.ui.form.Form = class FrappeForm { // handle TableMultiselect child fields let _selected = []; - if(me.fields_dict[df.fieldname].grid) { + if (me.fields_dict[df.fieldname].grid) { _selected = me.fields_dict[df.fieldname].grid.get_selected(); } - if(_selected.length) { + if (_selected.length) { selected[df.fieldname] = _selected; } }); @@ -1599,11 +1599,11 @@ frappe.ui.form.Form = class FrappeForm { set_indicator_formatter(fieldname, get_color, get_text) { // get doctype from parent var doctype; - if(frappe.meta.docfield_map[this.doctype][fieldname]) { + if (frappe.meta.docfield_map[this.doctype][fieldname]) { doctype = this.doctype; } else { frappe.meta.get_table_fields(this.doctype).every(function(df) { - if(frappe.meta.docfield_map[df.options][fieldname]) { + if (frappe.meta.docfield_map[df.options][fieldname]) { doctype = df.options; return false; } else { @@ -1614,11 +1614,11 @@ frappe.ui.form.Form = class FrappeForm { frappe.meta.docfield_map[doctype][fieldname].formatter = function(value, df, options, doc) { - if(value) { + if (value) { var label; - if(get_text) { + if (get_text) { label = get_text(doc); - } else if(frappe.form.link_formatters[df.options]) { + } else if (frappe.form.link_formatters[df.options]) { label = frappe.form.link_formatters[df.options](value, doc); } else { label = value; @@ -1637,21 +1637,21 @@ frappe.ui.form.Form = class FrappeForm { // return true or false if the user can make a particlar doctype // will check permission, `can_make_methods` if exists, or will decided on // basis of whether the document is submittable - if(!frappe.model.can_create(doctype)) { + if (!frappe.model.can_create(doctype)) { return false; } - if(this.custom_make_buttons && this.custom_make_buttons[doctype]) { + if (this.custom_make_buttons && this.custom_make_buttons[doctype]) { // custom buttons are translated and so are the keys const key = __(this.custom_make_buttons[doctype]); // if the button is present, then show make return !!this.custom_buttons[key]; } - if(this.can_make_methods && this.can_make_methods[doctype]) { + if (this.can_make_methods && this.can_make_methods[doctype]) { return this.can_make_methods[doctype](this); } else { - if(this.meta.is_submittable && !this.doc.docstatus==1) { + if (this.meta.is_submittable && !this.doc.docstatus==1) { return false; } else { return true; @@ -1664,9 +1664,9 @@ frappe.ui.form.Form = class FrappeForm { // will handover to `make_methods` if defined // or will create and match link fields let me = this; - if(this.make_methods && this.make_methods[doctype]) { + if (this.make_methods && this.make_methods[doctype]) { return this.make_methods[doctype](this); - } else if(this.custom_make_buttons && this.custom_make_buttons[doctype]) { + } else if (this.custom_make_buttons && this.custom_make_buttons[doctype]) { this.custom_buttons[__(this.custom_make_buttons[doctype])].trigger('click'); } else { frappe.model.with_doctype(doctype, function() { @@ -1697,10 +1697,10 @@ frappe.ui.form.Form = class FrappeForm { update_in_all_rows(table_fieldname, fieldname, value) { // update the child value in all tables where it is missing - if(!value) return; + if (!value) return; var cl = this.doc[table_fieldname] || []; for(var i = 0; i < cl.length; i++){ - if(!cl[i][fieldname]) cl[i][fieldname] = value; + if (!cl[i][fieldname]) cl[i][fieldname] = value; } refresh_field("items"); } From ae91bfa49ed26b083fd181bcdd716819d1edc188 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Wed, 2 Feb 2022 13:04:24 +0530 Subject: [PATCH 5/9] fix: sider fix --- frappe/public/js/frappe/form/form.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/frappe/public/js/frappe/form/form.js b/frappe/public/js/frappe/form/form.js index 0f88983c7e..2fd93295ce 100644 --- a/frappe/public/js/frappe/form/form.js +++ b/frappe/public/js/frappe/form/form.js @@ -282,7 +282,7 @@ frappe.ui.form.Form = class FrappeForm { setup_notify_on_rename() { $(document).on('rename', (ev, dt, old_name, new_name) => { - if (dt==this.doctype) + if (dt == this.doctype) this.rename_notify(dt, old_name, new_name); }); } @@ -606,7 +606,7 @@ frappe.ui.form.Form = class FrappeForm { } } - if (this.meta.autoname && this.meta.autoname.substr(0,6)=='field:' && !this.doc.__islocal) { + if (this.meta.autoname && this.meta.autoname.substr(0, 6) == 'field:' && !this.doc.__islocal) { var fn = this.meta.autoname.substr(6); if (this.doc[fn]) { @@ -614,7 +614,7 @@ frappe.ui.form.Form = class FrappeForm { } } - if (this.meta.autoname=="naming_series:" && !this.doc.__islocal) { + if (this.meta.autoname == "naming_series:" && !this.doc.__islocal) { this.toggle_display("naming_series", false); } } @@ -676,7 +676,7 @@ frappe.ui.form.Form = class FrappeForm { // to remove hash from URL to avoid scroll after save history.replaceState(null, null, ' '); if (!r.exc) { - if (["Save", "Update", "Amend"].indexOf(save_action)!==-1) { + if (["Save", "Update", "Amend"].indexOf(save_action) !== -1) { frappe.utils.play_sound("click"); } @@ -984,7 +984,7 @@ frappe.ui.form.Form = class FrappeForm { // trigger link fields which have default values set if (this.is_new() && this.doc.__run_link_triggers) { $.each(this.fields_dict, function(fieldname, field) { - if (field.df.fieldtype=="Link" && this.doc[fieldname]) { + if (field.df.fieldtype == "Link" && this.doc[fieldname]) { // triggers add fetch, sets value in model and runs triggers field.set_value(this.doc[fieldname], true); } @@ -1045,9 +1045,9 @@ frappe.ui.form.Form = class FrappeForm { } check_doctype_conflict(docname) { - if (this.doctype=='DocType' && docname=='DocType') { + if (this.doctype == 'DocType' && docname == 'DocType') { frappe.msgprint(__('Allowing DocType, DocType. Be careful!')); - } else if (this.doctype=='DocType') { + } else if (this.doctype == 'DocType') { if (frappe.views.formview[docname] || frappe.pages['List/'+docname]) { window.location.reload(); // frappe.msgprint(__("Cannot open {0} when its instance is open", ['DocType'])) @@ -1477,7 +1477,7 @@ frappe.ui.form.Form = class FrappeForm { } }; - if (typeof field=="string") { + if (typeof field == "string") { return _set(field, value); } else if ($.isPlainObject(field)) { let tasks = []; @@ -1651,7 +1651,7 @@ frappe.ui.form.Form = class FrappeForm { if (this.can_make_methods && this.can_make_methods[doctype]) { return this.can_make_methods[doctype](this); } else { - if (this.meta.is_submittable && !this.doc.docstatus==1) { + if (this.meta.is_submittable && !this.doc.docstatus == 1) { return false; } else { return true; @@ -1699,7 +1699,7 @@ frappe.ui.form.Form = class FrappeForm { // update the child value in all tables where it is missing if (!value) return; var cl = this.doc[table_fieldname] || []; - for(var i = 0; i < cl.length; i++){ + for (var i = 0; i < cl.length; i++) { if (!cl[i][fieldname]) cl[i][fieldname] = value; } refresh_field("items"); From d1846b6dcf19cc86821323f67aa321a3d1c9926d Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Tue, 15 Feb 2022 12:20:56 +0530 Subject: [PATCH 6/9] fix: add new row with linked field only if table is mandatory --- frappe/public/js/frappe/form/form.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/public/js/frappe/form/form.js b/frappe/public/js/frappe/form/form.js index 748ebda772..1fa5d6192d 100644 --- a/frappe/public/js/frappe/form/form.js +++ b/frappe/public/js/frappe/form/form.js @@ -1684,7 +1684,7 @@ frappe.ui.form.Form = class FrappeForm { new_doc[df.fieldname] = me.doc.name; } else if (['Link', 'Dynamic Link'].includes(df.fieldtype) && me.doc[df.fieldname]) { new_doc[df.fieldname] = me.doc[df.fieldname]; - } else if (df.fieldtype === 'Table' && df.options) { + } else if (df.fieldtype === 'Table' && df.options && df.reqd) { let row = new_doc[df.fieldname][0]; me.set_link_field(df.options, row); } From 39d65c5211a30c413920ea43c4a607248c7e36b9 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Tue, 15 Feb 2022 12:22:14 +0530 Subject: [PATCH 7/9] test: UI test to check if linked field is populated in child table --- cypress/fixtures/child_table_doctype.js | 30 +++++++++++++ cypress/fixtures/doctype_to_link.js | 45 +++++++++++++++++++ cypress/fixtures/doctype_with_child_table.js | 46 ++++++++++++++++++++ cypress/integration/dashboard_links.js | 24 ++++++++++ frappe/tests/ui_test_helpers.py | 14 ++++++ 5 files changed, 159 insertions(+) create mode 100644 cypress/fixtures/child_table_doctype.js create mode 100644 cypress/fixtures/doctype_to_link.js create mode 100644 cypress/fixtures/doctype_with_child_table.js diff --git a/cypress/fixtures/child_table_doctype.js b/cypress/fixtures/child_table_doctype.js new file mode 100644 index 0000000000..697b0b5632 --- /dev/null +++ b/cypress/fixtures/child_table_doctype.js @@ -0,0 +1,30 @@ +export default { + name: "Child Table Doctype", + actions: [], + custom: 1, + autoname: "field:title", + creation: "2022-02-09 20:15:21.242213", + doctype: "DocType", + editable_grid: 1, + engine: "InnoDB", + fields: [ + { + fieldname: "title", + fieldtype: "Data", + in_list_view: 1, + label: "Title", + unique: 1 + } + ], + links: [], + istable: 1, + modified: "2022-02-10 12:03:12.603763", + modified_by: "Administrator", + module: "Custom", + naming_rule: "By fieldname", + owner: "Administrator", + permissions: [], + sort_field: 'modified', + sort_order: 'ASC', + track_changes: 1 +} \ No newline at end of file diff --git a/cypress/fixtures/doctype_to_link.js b/cypress/fixtures/doctype_to_link.js new file mode 100644 index 0000000000..c12cf8ca55 --- /dev/null +++ b/cypress/fixtures/doctype_to_link.js @@ -0,0 +1,45 @@ +export default { + name: "Doctype to Link", + actions: [], + custom: 1, + naming_rule: "By fieldname", + autoname: "field:title", + creation: "2022-02-09 20:15:21.242213", + doctype: "DocType", + editable_grid: 1, + engine: "InnoDB", + fields: [ + { + "fieldname": "title", + "fieldtype": "Data", + "label": "Title", + "unique": 1 + } + ], + links: [ + { + "group": "Child Doctype", + "link_doctype": "Doctype With Child Table", + "link_fieldname": "title" + } + ], + modified: "2022-02-10 12:03:12.603763", + modified_by: "Administrator", + module: "Custom", + owner: "Administrator", + permissions: [ + { + create: 1, + delete: 1, + email: 1, + print: 1, + read: 1, + role: 'System Manager', + share: 1, + write: 1 + } + ], + sort_field: 'modified', + sort_order: 'ASC', + track_changes: 1 +} \ No newline at end of file diff --git a/cypress/fixtures/doctype_with_child_table.js b/cypress/fixtures/doctype_with_child_table.js new file mode 100644 index 0000000000..bc96badab2 --- /dev/null +++ b/cypress/fixtures/doctype_with_child_table.js @@ -0,0 +1,46 @@ +export default { + name: "Doctype With Child Table", + actions: [], + custom: 1, + autoname: "field:title", + creation: "2022-02-09 20:15:21.242213", + doctype: "DocType", + editable_grid: 1, + engine: "InnoDB", + fields: [ + { + fieldname: "title", + fieldtype: "Data", + label: "Title", + unique: 1 + }, + { + fieldname: "child_table", + fieldtype: "Table", + label: "Child Table", + options: "Child Table Doctype", + reqd: 1 + } + ], + links: [], + modified: "2022-02-10 12:03:12.603763", + modified_by: "Administrator", + module: "Custom", + naming_rule: "By fieldname", + owner: "Administrator", + permissions: [ + { + create: 1, + delete: 1, + email: 1, + print: 1, + read: 1, + role: 'System Manager', + share: 1, + write: 1 + } + ], + sort_field: 'modified', + sort_order: 'ASC', + track_changes: 1 +} diff --git a/cypress/integration/dashboard_links.js b/cypress/integration/dashboard_links.js index 16ffd41cf4..93d10cf1fd 100644 --- a/cypress/integration/dashboard_links.js +++ b/cypress/integration/dashboard_links.js @@ -1,7 +1,21 @@ +import doctype_with_child_table from '../fixtures/doctype_with_child_table'; +import child_table_doctype from '../fixtures/child_table_doctype'; +import doctype_to_link from '../fixtures/doctype_to_link'; +const doctype_to_link_name = doctype_to_link.name; +const child_table_doctype_name = child_table_doctype.name; + context('Dashboard links', () => { before(() => { cy.visit('/login'); cy.login(); + cy.insert_doc('DocType', child_table_doctype, true); + cy.insert_doc('DocType', doctype_with_child_table, true); + cy.insert_doc('DocType', doctype_to_link, true); + return cy.window().its('frappe').then(frappe => { + return frappe.xcall("frappe.tests.ui_test_helpers.update_child_table", { + name: child_table_doctype_name + }); + }); }); it('Adding a new contact, checking for the counter on the dashboard and deleting the created contact', () => { @@ -62,4 +76,14 @@ context('Dashboard links', () => { cy.findByText('Website Analytics'); }); }); + + it('check if child table is populated with linked field on creation from dashboard link', () => { + cy.new_form(doctype_to_link_name); + cy.fill_field("title", "Test Linking"); + cy.findByRole("button", {name: "Save"}).click(); + + cy.get('.document-link .btn-new').click(); + cy.get('.frappe-control[data-fieldname="child_table"] .rows .data-row .col[data-fieldname="doctype_to_link"]') + .should('contain.text', 'Test Linking'); + }); }); diff --git a/frappe/tests/ui_test_helpers.py b/frappe/tests/ui_test_helpers.py index b299df522c..5d3e75f02c 100644 --- a/frappe/tests/ui_test_helpers.py +++ b/frappe/tests/ui_test_helpers.py @@ -256,3 +256,17 @@ def update_webform_to_multistep(): _doc.route = "update-profile-duplicate" _doc.is_standard = False _doc.save() + +@frappe.whitelist() +def update_child_table(name): + doc = frappe.get_doc('DocType', name) + if len(doc.fields) == 1: + doc.append('fields', { + 'fieldname': 'doctype_to_link', + 'fieldtype': 'Link', + 'in_list_view': 1, + 'label': 'Doctype to Link', + 'options': 'Doctype to Link' + }) + + doc.save() \ No newline at end of file From f01de7e5387268b54310b27923ceba33f6cf2d8a Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Tue, 15 Feb 2022 12:28:29 +0530 Subject: [PATCH 8/9] fix(sider): missing semicolons --- cypress/fixtures/child_table_doctype.js | 2 +- cypress/fixtures/doctype_to_link.js | 2 +- cypress/fixtures/doctype_with_child_table.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cypress/fixtures/child_table_doctype.js b/cypress/fixtures/child_table_doctype.js index 697b0b5632..f65e5d1765 100644 --- a/cypress/fixtures/child_table_doctype.js +++ b/cypress/fixtures/child_table_doctype.js @@ -27,4 +27,4 @@ export default { sort_field: 'modified', sort_order: 'ASC', track_changes: 1 -} \ No newline at end of file +}; \ No newline at end of file diff --git a/cypress/fixtures/doctype_to_link.js b/cypress/fixtures/doctype_to_link.js index c12cf8ca55..f5335b1755 100644 --- a/cypress/fixtures/doctype_to_link.js +++ b/cypress/fixtures/doctype_to_link.js @@ -42,4 +42,4 @@ export default { sort_field: 'modified', sort_order: 'ASC', track_changes: 1 -} \ No newline at end of file +}; \ No newline at end of file diff --git a/cypress/fixtures/doctype_with_child_table.js b/cypress/fixtures/doctype_with_child_table.js index bc96badab2..bbb2127448 100644 --- a/cypress/fixtures/doctype_with_child_table.js +++ b/cypress/fixtures/doctype_with_child_table.js @@ -43,4 +43,4 @@ export default { sort_field: 'modified', sort_order: 'ASC', track_changes: 1 -} +}; From 6d4df45ab0cd094f195a860dfddc5b00bcc8f960 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Wed, 16 Feb 2022 13:58:24 +0530 Subject: [PATCH 9/9] revert: unrelated formatting changes --- frappe/public/js/frappe/form/form.js | 196 +++++++++++++-------------- 1 file changed, 98 insertions(+), 98 deletions(-) diff --git a/frappe/public/js/frappe/form/form.js b/frappe/public/js/frappe/form/form.js index 1fa5d6192d..171115b60b 100644 --- a/frappe/public/js/frappe/form/form.js +++ b/frappe/public/js/frappe/form/form.js @@ -44,7 +44,7 @@ frappe.ui.form.Form = class FrappeForm { setup_meta() { this.meta = frappe.get_doc('DocType', this.doctype); - if (this.meta.istable) { + if(this.meta.istable) { this.meta.in_dialog = 1; } @@ -248,7 +248,7 @@ frappe.ui.form.Form = class FrappeForm { // on main doc frappe.model.on(me.doctype, "*", function(fieldname, value, doc) { // set input - if (doc.name === me.docname) { + if(doc.name===me.docname) { me.dirty(); let field = me.fields_dict[fieldname]; @@ -271,7 +271,7 @@ frappe.ui.form.Form = class FrappeForm { // using $.each to preserve df via closure $.each(table_fields, function(i, df) { frappe.model.on(df.options, "*", function(fieldname, value, doc) { - if (doc.parent === me.docname && doc.parentfield === df.fieldname) { + if(doc.parent===me.docname && doc.parentfield===df.fieldname) { me.dirty(); me.fields_dict[df.fieldname].grid.set_value(fieldname, value, doc); return me.script_manager.trigger(fieldname, doc.doctype, doc.name); @@ -282,7 +282,7 @@ frappe.ui.form.Form = class FrappeForm { setup_notify_on_rename() { $(document).on('rename', (ev, dt, old_name, new_name) => { - if (dt == this.doctype) + if (dt==this.doctype) this.rename_notify(dt, old_name, new_name); }); } @@ -299,7 +299,7 @@ frappe.ui.form.Form = class FrappeForm { e.stopPropagation(); e.preventDefault(); - if (me.doc.__islocal) { + if(me.doc.__islocal) { frappe.msgprint(__("Please save before attaching.")); throw "attach error"; } @@ -322,13 +322,13 @@ frappe.ui.form.Form = class FrappeForm { refresh(docname) { var switched = docname ? true : false; - if (docname) { + if(docname) { this.switch_doc(docname); } cur_frm = this; - if (this.docname) { // document to show + if(this.docname) { // document to show this.save_disabled = false; // set the doc this.doc = frappe.get_doc(this.doctype, this.docname); @@ -353,7 +353,7 @@ frappe.ui.form.Form = class FrappeForm { } // do setup - if (!this.setup_done) { + if(!this.setup_done) { this.setup(); } @@ -361,12 +361,12 @@ frappe.ui.form.Form = class FrappeForm { this.trigger_onload(switched); // if print format is shown, refresh the format - // if (this.print_preview.wrapper.is(":visible")) { + // if(this.print_preview.wrapper.is(":visible")) { // this.print_preview.preview(); // } - if (switched) { - if (this.show_print_first && this.doc.docstatus === 1) { + if(switched) { + if(this.show_print_first && this.doc.docstatus===1) { // show print view this.print_doc(); } @@ -374,9 +374,9 @@ frappe.ui.form.Form = class FrappeForm { // set status classes this.$wrapper.removeClass('validated-form') - .toggleClass('editable-form', this.doc.docstatus === 0) - .toggleClass('submitted-form', this.doc.docstatus === 1) - .toggleClass('cancelled-form', this.doc.docstatus === 2); + .toggleClass('editable-form', this.doc.docstatus===0) + .toggleClass('submitted-form', this.doc.docstatus===1) + .toggleClass('cancelled-form', this.doc.docstatus===2); this.show_conflict_message(); } @@ -415,7 +415,7 @@ frappe.ui.form.Form = class FrappeForm { frappe.throw(`Action ${action} not found`); } } - if (action.action_type === 'Server Action') { + if (action.action_type==='Server Action') { return frappe.xcall(action.action, {'doc': this.doc}).then((doc) => { if (doc.doctype) { // document is returned by the method, @@ -430,7 +430,7 @@ frappe.ui.form.Form = class FrappeForm { alert: true }); }); - } else if (action.action_type === 'Route') { + } else if (action.action_type==='Route') { return frappe.set_route(action.action); } } @@ -449,7 +449,7 @@ frappe.ui.form.Form = class FrappeForm { } check_reload() { - if (this.doc && (!this.doc.__unsaved) && this.doc.__last_sync_on && + if(this.doc && (!this.doc.__unsaved) && this.doc.__last_sync_on && (new Date() - this.doc.__last_sync_on) > (this.refresh_if_stale_for * 1000)) { this.reload_doc(); return true; @@ -458,7 +458,7 @@ frappe.ui.form.Form = class FrappeForm { trigger_onload(switched) { this.cscript.is_onload = false; - if (!this.opendocs[this.docname]) { + if(!this.opendocs[this.docname]) { var me = this; this.cscript.is_onload = true; this.initialize_new_doc(); @@ -496,13 +496,13 @@ frappe.ui.form.Form = class FrappeForm { }); // update seen - if (this.meta.track_seen) { + if(this.meta.track_seen) { $('.list-id[data-name="'+ me.docname +'"]').addClass('seen'); } } render_form(switched) { - if (!this.meta.istable) { + if(!this.meta.istable) { this.layout.doc = this.doc; this.layout.attach_doc_and_docfields(); @@ -530,7 +530,7 @@ frappe.ui.form.Form = class FrappeForm { () => this.script_manager.trigger("refresh"), // call onload post render for callbacks to be fired () => { - if (this.cscript.is_onload) { + if(this.cscript.is_onload) { return this.script_manager.trigger("onload_post_render"); } }, @@ -547,7 +547,7 @@ frappe.ui.form.Form = class FrappeForm { this.cscript.is_onload && this.set_first_tab_as_active(); - if (!this.hidden) { + if(!this.hidden) { this.layout.show_empty_form_message(); } @@ -588,7 +588,7 @@ frappe.ui.form.Form = class FrappeForm { } cleanup_refresh() { - if (this.fields_dict['amended_from']) { + if(this.fields_dict['amended_from']) { if (this.doc.amended_from) { unhide_field('amended_from'); if (this.fields_dict['amendment_date']) unhide_field('amendment_date'); @@ -598,15 +598,15 @@ frappe.ui.form.Form = class FrappeForm { } } - if (this.fields_dict['trash_reason']) { - if (this.doc.trash_reason && this.doc.docstatus == 2) { + if(this.fields_dict['trash_reason']) { + if(this.doc.trash_reason && this.doc.docstatus == 2) { unhide_field('trash_reason'); } else { hide_field('trash_reason'); } } - if (this.meta.autoname && this.meta.autoname.substr(0, 6) == 'field:' && !this.doc.__islocal) { + if(this.meta.autoname && this.meta.autoname.substr(0,6)=='field:' && !this.doc.__islocal) { var fn = this.meta.autoname.substr(6); if (this.doc[fn]) { @@ -614,7 +614,7 @@ frappe.ui.form.Form = class FrappeForm { } } - if (this.meta.autoname == "naming_series:" && !this.doc.__islocal) { + if(this.meta.autoname=="naming_series:" && !this.doc.__islocal) { this.toggle_display("naming_series", false); } } @@ -622,12 +622,12 @@ frappe.ui.form.Form = class FrappeForm { refresh_header(switched) { // set title // main title - if (!this.meta.in_dialog || this.in_form) { + if(!this.meta.in_dialog || this.in_form) { frappe.utils.set_title(this.meta.issingle ? this.doctype : this.docname); } // show / hide buttons - if (this.toolbar) { + if(this.toolbar) { if (switched) { this.toolbar.current_status = undefined; } @@ -645,11 +645,11 @@ frappe.ui.form.Form = class FrappeForm { // SAVE save_or_update() { - if (this.save_disabled) return; + if(this.save_disabled) return; - if (this.doc.docstatus === 0) { + if(this.doc.docstatus===0) { this.save(); - } else if (this.doc.docstatus === 1 && this.doc.__unsaved) { + } else if(this.doc.docstatus===1 && this.doc.__unsaved) { this.save("Update"); } } @@ -669,14 +669,14 @@ frappe.ui.form.Form = class FrappeForm { validate_and_save(save_action, callback, btn, on_error, resolve, reject) { var me = this; - if (!save_action) save_action = "Save"; + if(!save_action) save_action = "Save"; this.validate_form_action(save_action, resolve); var after_save = function(r) { // to remove hash from URL to avoid scroll after save history.replaceState(null, null, ' '); - if (!r.exc) { - if (["Save", "Update", "Amend"].indexOf(save_action) !== -1) { + if(!r.exc) { + if (["Save", "Update", "Amend"].indexOf(save_action)!==-1) { frappe.utils.play_sound("click"); } @@ -694,7 +694,7 @@ frappe.ui.form.Form = class FrappeForm { } me.refresh(); } else { - if (on_error) { + if(on_error) { on_error(); reject(); } @@ -708,20 +708,20 @@ frappe.ui.form.Form = class FrappeForm { console.error(e) } btn && $(btn).prop("disabled", false); - if (on_error) { + if(on_error) { on_error(); reject(); } }; - if (save_action != "Update") { + if(save_action != "Update") { // validate frappe.validated = true; frappe.run_serially([ () => this.script_manager.trigger("validate"), () => this.script_manager.trigger("before_save"), () => { - if (!frappe.validated) { + if(!frappe.validated) { fail(); return; } @@ -741,12 +741,12 @@ frappe.ui.form.Form = class FrappeForm { frappe.confirm(__("Permanently Submit {0}?", [this.docname]), function() { frappe.validated = true; me.script_manager.trigger("before_submit").then(function() { - if (!frappe.validated) { + if(!frappe.validated) { return me.handle_save_fail(btn, on_error); } me.save('Submit', function(r) { - if (r.exc) { + if(r.exc) { me.handle_save_fail(btn, on_error); } else { frappe.utils.play_sound("submit"); @@ -935,7 +935,7 @@ frappe.ui.form.Form = class FrappeForm { } if (!this.perm[0][perm_to_check] && !allowed_for_workflow) { - if (resolve) { + if(resolve) { // re-enable buttons resolve(); } @@ -980,7 +980,7 @@ frappe.ui.form.Form = class FrappeForm { // trigger link fields which have default values set if (this.is_new() && this.doc.__run_link_triggers) { $.each(this.fields_dict, function(fieldname, field) { - if (field.df.fieldtype == "Link" && this.doc[fieldname]) { + if (field.df.fieldtype=="Link" && this.doc[fieldname]) { // triggers add fetch, sets value in model and runs triggers field.set_value(this.doc[fieldname], true); } @@ -991,8 +991,8 @@ frappe.ui.form.Form = class FrappeForm { } show_conflict_message() { - if (this.doc.__needs_refresh) { - if (this.doc.__unsaved) { + if(this.doc.__needs_refresh) { + if(this.doc.__unsaved) { this.dashboard.clear_headline(); this.dashboard.set_headline_alert(__("This form has been modified after you have loaded it") + '' @@ -1004,20 +1004,20 @@ frappe.ui.form.Form = class FrappeForm { } show_submit_message() { - if (this.meta.is_submittable + if(this.meta.is_submittable && this.perm[0] && this.perm[0].submit && !this.is_dirty() && !this.is_new() && !frappe.model.has_workflow(this.doctype) // show only if no workflow - && this.doc.docstatus === 0) { + && this.doc.docstatus===0) { this.dashboard.add_comment(__('Submit this document to confirm'), 'blue', true); } } show_web_link() { - if (!this.doc.__islocal && this.doc.__onload && this.doc.__onload.is_website_generator) { + if(!this.doc.__islocal && this.doc.__onload && this.doc.__onload.is_website_generator) { this.web_link && this.web_link.remove(); - if (this.doc.__onload.published) { + if(this.doc.__onload.published) { this.add_web_link("/" + this.doc.route); } } @@ -1034,16 +1034,16 @@ frappe.ui.form.Form = class FrappeForm { var dt = this.parent_doctype ? this.parent_doctype : this.doctype; this.perm = frappe.perm.get_perm(dt, this.doc); - if (!this.perm[0].read) { + if(!this.perm[0].read) { return 0; } return 1; } check_doctype_conflict(docname) { - if (this.doctype == 'DocType' && docname == 'DocType') { + if(this.doctype=='DocType' && docname=='DocType') { frappe.msgprint(__('Allowing DocType, DocType. Be careful!')); - } else if (this.doctype == 'DocType') { + } else if(this.doctype=='DocType') { if (frappe.views.formview[docname] || frappe.pages['List/'+docname]) { window.location.reload(); // frappe.msgprint(__("Cannot open {0} when its instance is open", ['DocType'])) @@ -1062,16 +1062,16 @@ frappe.ui.form.Form = class FrappeForm { // notify this form of renamed records rename_notify(dt, old, name) { // from form - if (this.meta.istable) + if(this.meta.istable) return; - if (this.docname == old) + if(this.docname == old) this.docname = name; else return; // cleanup - if (this && this.opendocs[old] && frappe.meta.docfield_copy[dt]) { + if(this && this.opendocs[old] && frappe.meta.docfield_copy[dt]) { // delete docfield copy frappe.meta.docfield_copy[dt][name] = frappe.meta.docfield_copy[dt][old]; delete frappe.meta.docfield_copy[dt][old]; @@ -1080,7 +1080,7 @@ frappe.ui.form.Form = class FrappeForm { delete this.opendocs[old]; this.opendocs[name] = true; - if (this.meta.in_dialog || !this.in_form) { + if(this.meta.in_dialog || !this.in_form) { return; } @@ -1155,7 +1155,7 @@ frappe.ui.form.Form = class FrappeForm { newdoc.idx = null; newdoc.__run_link_triggers = false; - if (onload) { + if(onload) { onload(newdoc); } frappe.set_route('Form', newdoc.doctype, newdoc.name); @@ -1164,7 +1164,7 @@ frappe.ui.form.Form = class FrappeForm { reload_doc() { this.check_doctype_conflict(this.docname); - if (!this.doc.__islocal) { + if(!this.doc.__islocal) { frappe.model.remove_from_locals(this.doctype, this.docname); return frappe.model.with_doc(this.doctype, this.docname, () => { this.refresh(); @@ -1311,9 +1311,9 @@ frappe.ui.form.Form = class FrappeForm { $.each(fields_list, function(i, fname) { var docfield = frappe.meta.docfield_map[doctype][fname]; - if (docfield) { + if(docfield) { var label = __(docfield.label || "").replace(/\([^\)]*\)/g, ""); // eslint-disable-line - if (parentfield) { + if(parentfield) { grid_field_label_map[doctype + "-" + fname] = label.trim() + " (" + __(currency) + ")"; } else { @@ -1333,8 +1333,8 @@ frappe.ui.form.Form = class FrappeForm { } field_map(fnames, fn) { - if (typeof fnames === 'string') { - if (fnames == '*') { + if(typeof fnames==='string') { + if(fnames == '*') { fnames = Object.keys(this.fields_dict); } else { fnames = [fnames]; @@ -1343,7 +1343,7 @@ frappe.ui.form.Form = class FrappeForm { for (var i=0, l=fnames.length; i { - if (!unique_keys.includes(key)) { + if(!unique_keys.includes(key)) { d[key] = values[key]; } }); @@ -1448,9 +1448,9 @@ frappe.ui.form.Form = class FrappeForm { var me = this; var _set = function(f, v) { var fieldobj = me.fields_dict[f]; - if (fieldobj) { - if (!if_missing || !frappe.model.has_value(me.doctype, me.doc.name, f)) { - if (frappe.model.table_fields.includes(fieldobj.df.fieldtype) && $.isArray(v)) { + if(fieldobj) { + if(!if_missing || !frappe.model.has_value(me.doctype, me.doc.name, f)) { + if(frappe.model.table_fields.includes(fieldobj.df.fieldtype) && $.isArray(v)) { frappe.model.clear_table(me.doc, fieldobj.df.fieldname); @@ -1473,13 +1473,13 @@ frappe.ui.form.Form = class FrappeForm { } }; - if (typeof field == "string") { + if(typeof field=="string") { return _set(field, value); - } else if ($.isPlainObject(field)) { + } else if($.isPlainObject(field)) { let tasks = []; for (let f in field) { let v = field[f]; - if (me.get_field(f)) { + if(me.get_field(f)) { tasks.push(() => _set(f, v)); } } @@ -1489,7 +1489,7 @@ frappe.ui.form.Form = class FrappeForm { call(opts, args, callback) { var me = this; - if (typeof opts === 'string') { + if(typeof opts==='string') { // called as frm.call('do_this', {with_arg: 'arg'}); opts = { method: opts, @@ -1498,19 +1498,19 @@ frappe.ui.form.Form = class FrappeForm { callback: callback }; } - if (!opts.doc) { - if (opts.method.indexOf(".") === -1) + if(!opts.doc) { + if(opts.method.indexOf(".")===-1) opts.method = frappe.model.get_server_module_name(me.doctype) + "." + opts.method; opts.original_callback = opts.callback; opts.callback = function(r) { - if ($.isPlainObject(r.message)) { - if (opts.child) { + if($.isPlainObject(r.message)) { + if(opts.child) { // update child doc opts.child = locals[opts.child.doctype][opts.child.name]; var std_field_list = ["doctype"].concat(frappe.model.std_fields_list); for (var key in r.message) { - if (std_field_list.indexOf(key) === -1) { + if (std_field_list.indexOf(key)===-1) { opts.child[key] = r.message[key]; } } @@ -1526,7 +1526,7 @@ frappe.ui.form.Form = class FrappeForm { } else { opts.original_callback = opts.callback; opts.callback = function(r) { - if (!r.exc) me.refresh_fields(); + if(!r.exc) me.refresh_fields(); opts.original_callback && opts.original_callback(r); }; @@ -1567,7 +1567,7 @@ frappe.ui.form.Form = class FrappeForm { } get_title() { - if (this.meta.title_field) { + if(this.meta.title_field) { return this.doc[this.meta.title_field]; } else { return this.doc.name; @@ -1581,11 +1581,11 @@ frappe.ui.form.Form = class FrappeForm { // handle TableMultiselect child fields let _selected = []; - if (me.fields_dict[df.fieldname].grid) { + if(me.fields_dict[df.fieldname].grid) { _selected = me.fields_dict[df.fieldname].grid.get_selected(); } - if (_selected.length) { + if(_selected.length) { selected[df.fieldname] = _selected; } }); @@ -1595,11 +1595,11 @@ frappe.ui.form.Form = class FrappeForm { set_indicator_formatter(fieldname, get_color, get_text) { // get doctype from parent var doctype; - if (frappe.meta.docfield_map[this.doctype][fieldname]) { + if(frappe.meta.docfield_map[this.doctype][fieldname]) { doctype = this.doctype; } else { frappe.meta.get_table_fields(this.doctype).every(function(df) { - if (frappe.meta.docfield_map[df.options][fieldname]) { + if(frappe.meta.docfield_map[df.options][fieldname]) { doctype = df.options; return false; } else { @@ -1610,11 +1610,11 @@ frappe.ui.form.Form = class FrappeForm { frappe.meta.docfield_map[doctype][fieldname].formatter = function(value, df, options, doc) { - if (value) { + if(value) { var label; - if (get_text) { + if(get_text) { label = get_text(doc); - } else if (frappe.form.link_formatters[df.options]) { + } else if(frappe.form.link_formatters[df.options]) { label = frappe.form.link_formatters[df.options](value, doc); } else { label = value; @@ -1633,21 +1633,21 @@ frappe.ui.form.Form = class FrappeForm { // return true or false if the user can make a particlar doctype // will check permission, `can_make_methods` if exists, or will decided on // basis of whether the document is submittable - if (!frappe.model.can_create(doctype)) { + if(!frappe.model.can_create(doctype)) { return false; } - if (this.custom_make_buttons && this.custom_make_buttons[doctype]) { + if(this.custom_make_buttons && this.custom_make_buttons[doctype]) { // custom buttons are translated and so are the keys const key = __(this.custom_make_buttons[doctype]); // if the button is present, then show make return !!this.custom_buttons[key]; } - if (this.can_make_methods && this.can_make_methods[doctype]) { + if(this.can_make_methods && this.can_make_methods[doctype]) { return this.can_make_methods[doctype](this); } else { - if (this.meta.is_submittable && !this.doc.docstatus == 1) { + if(this.meta.is_submittable && !this.doc.docstatus==1) { return false; } else { return true; @@ -1660,9 +1660,9 @@ frappe.ui.form.Form = class FrappeForm { // will handover to `make_methods` if defined // or will create and match link fields let me = this; - if (this.make_methods && this.make_methods[doctype]) { + if(this.make_methods && this.make_methods[doctype]) { return this.make_methods[doctype](this); - } else if (this.custom_make_buttons && this.custom_make_buttons[doctype]) { + } else if(this.custom_make_buttons && this.custom_make_buttons[doctype]) { this.custom_buttons[__(this.custom_make_buttons[doctype])].trigger('click'); } else { frappe.model.with_doctype(doctype, function() { @@ -1693,10 +1693,10 @@ frappe.ui.form.Form = class FrappeForm { update_in_all_rows(table_fieldname, fieldname, value) { // update the child value in all tables where it is missing - if (!value) return; + if(!value) return; var cl = this.doc[table_fieldname] || []; - for (var i = 0; i < cl.length; i++) { - if (!cl[i][fieldname]) cl[i][fieldname] = value; + for(var i = 0; i < cl.length; i++){ + if(!cl[i][fieldname]) cl[i][fieldname] = value; } refresh_field("items"); }