// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors // MIT License. See license.txt window.get_server_fields = function(method, arg, table_field, doc, dt, dn, allow_edit, call_back) { console.warn("This function 'get_server_fields' has been deprecated and will be removed soon."); frappe.dom.freeze(); if($.isPlainObject(arg)) arg = JSON.stringify(arg); return $c('runserverobj', {'method': method, 'docs': JSON.stringify(doc), 'arg': arg }, function(r, rt) { frappe.dom.unfreeze(); if (r.message) { var d = locals[dt][dn]; var field_dict = r.message; for(var key in field_dict) { d[key] = field_dict[key]; if (table_field) refresh_field(key, d.name, table_field); else refresh_field(key); } } if(call_back){ doc = locals[doc.doctype][doc.name]; call_back(doc, dt, dn); } }); } window.set_multiple = function (dt, dn, dict, table_field) { var d = locals[dt][dn]; for(var key in dict) { d[key] = dict[key]; if (table_field) refresh_field(key, d.name, table_field); else refresh_field(key); } } window.refresh_many = function (flist, dn, table_field) { for(var i in flist) { if (table_field) refresh_field(flist[i], dn, table_field); else refresh_field(flist[i]); } } window.set_field_tip = function(n,txt) { var df = frappe.meta.get_docfield(cur_frm.doctype, n, cur_frm.docname); if(df)df.description = txt; if(cur_frm && cur_frm.fields_dict) { if(cur_frm.fields_dict[n]) cur_frm.fields_dict[n].comment_area.innerHTML = replace_newlines(txt); else console.log('[set_field_tip] Unable to set field tip: ' + n); } } refresh_field = function(n, docname, table_field) { // multiple if(typeof n==typeof []) refresh_many(n, docname, table_field); if (n && typeof n==='string' && table_field){ var grid = cur_frm.fields_dict[table_field].grid, field = frappe.utils.filter_dict(grid.docfields, {fieldname: n}); if (field && field.length){ field = field[0]; var meta = frappe.meta.get_docfield(field.parent, field.fieldname, docname); $.extend(field, meta); if (docname){ cur_frm.fields_dict[table_field].grid.grid_rows_by_docname[docname].refresh_field(n); } else { cur_frm.fields_dict[table_field].grid.refresh(); } } } else if(cur_frm) { cur_frm.refresh_field(n) } } window.set_field_options = function(n, txt) { cur_frm.set_df_property(n, 'options', txt) } window.set_field_permlevel = function(n, level) { cur_frm.set_df_property(n, 'permlevel', level) } toggle_field = function(n, hidden) { var df = frappe.meta.get_docfield(cur_frm.doctype, n, cur_frm.docname); if(df) { df.hidden = hidden; refresh_field(n); } else { console.log((hidden ? "hide_field" : "unhide_field") + " cannot find field " + n); } } hide_field = function(n) { if(cur_frm) { if(n.substr) toggle_field(n, 1); else { for(var i in n) toggle_field(n[i], 1) } } } unhide_field = function(n) { if(cur_frm) { if(n.substr) toggle_field(n, 0); else { for(var i in n) toggle_field(n[i], 0) } } } get_field_obj = function(fn) { return cur_frm.fields_dict[fn]; } _f.Frm.prototype.get_doc = function() { return locals[this.doctype][this.docname]; } _f.Frm.prototype.set_currency_labels = function(fields_list, currency, parentfield) { // To set the currency in the label // For example Total Cost(INR), Total Cost(USD) var me = this; var doctype = parentfield ? this.fields_dict[parentfield].grid.doctype : this.doc.doctype; var field_label_map = {} var grid_field_label_map = {} $.each(fields_list, function(i, fname) { var docfield = frappe.meta.docfield_map[doctype][fname]; if(docfield) { var label = __(docfield.label || "").replace(/\([^\)]*\)/g, ""); if(parentfield) { grid_field_label_map[doctype + "-" + fname] = label.trim() + " (" + __(currency) + ")"; } else { field_label_map[fname] = label.trim() + " (" + currency + ")"; } } }); $.each(field_label_map, function(fname, label) { me.fields_dict[fname].set_label(label); }); $.each(grid_field_label_map, function(fname, label) { fname = fname.split("-"); var df = frappe.meta.get_docfield(fname[0], fname[1], me.doc.name); if(df) df.label = label; }); } _f.Frm.prototype.field_map = function(fnames, fn) { if(typeof fnames==='string') { if(fnames == '*') { fnames = Object.keys(this.fields_dict); } else { fnames = [fnames]; } } for (var i=0, l=fnames.length; i { if(!unique_keys.includes(key)) { d[key] = values[key]; } }); $.extend(doc, d); } return doc; } _f.Frm.prototype.set_value = function(field, value, if_missing) { 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(fieldobj.df.fieldtype==="Table" && $.isArray(v)) { frappe.model.clear_table(me.doc, fieldobj.df.fieldname); for (var i=0, j=v.length; i < j; i++) { var d = v[i]; var child = frappe.model.add_child(me.doc, fieldobj.df.options, fieldobj.df.fieldname, i+1); $.extend(child, d); } me.refresh_field(f); } else { frappe.model.set_value(me.doctype, me.doc.name, f, v); } } } else { frappe.msgprint(__("Field {0} not found.",[f])); throw "frm.set_value"; } } if(typeof field=="string") { _set(field, value) } else if($.isPlainObject(field)) { for (var f in field) { var v = field[f]; if(me.get_field(f)) { _set(f, v); } } } } _f.Frm.prototype.call = function(opts, args, callback) { var me = this; if(typeof opts==='string') { // called as frm.call('do_this', {with_arg: 'arg'}); opts = { method: opts, doc: this.doc, args: args, callback: callback }; } 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) { // 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) { opts.child[key] = r.message[key]; } } me.fields_dict[opts.child.parentfield].refresh(); } else { // update parent doc me.set_value(r.message); } } opts.original_callback && opts.original_callback(r); } } else { opts.original_callback = opts.callback; opts.callback = function(r) { if(!r.exc) me.refresh_fields(); opts.original_callback && opts.original_callback(r); } } return frappe.call(opts); } _f.Frm.prototype.get_field = function(field) { return cur_frm.fields_dict[field]; }; _f.Frm.prototype.set_read_only = function() { var perm = []; var docperms = frappe.perm.get_perm(cur_frm.doc.doctype); for (var i=0, l=docperms.length; i%(label)s', { color: get_color(doc), doctype: df.options, name: value, label: get_text ? get_text(doc) : value }); } else { return ''; } }; } _f.Frm.prototype.can_create = function(doctype) { // 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)) { return false; } if(this.custom_make_buttons && this.custom_make_buttons[doctype]) { // if the button is present, then show make return !!this.custom_buttons[this.custom_make_buttons[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) { return false; } else { return true; } } } _f.Frm.prototype.make_new = function(doctype) { // make new doctype from the current form // will handover to `make_methods` if defined // or will create and match link fields var 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); // 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; } }); frappe.set_route('Form', doctype, new_doc.name); }); } } _f.Frm.prototype.update_in_all_rows = function(table_fieldname, fieldname, value) { // 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++){ if(!cl[i][fieldname]) cl[i][fieldname] = value; } refresh_field("items"); }