diff --git a/public/js/legacy/widgets/form/clientscriptAPI.js b/public/js/legacy/widgets/form/clientscriptAPI.js index 0048bdac59..058263d75d 100644 --- a/public/js/legacy/widgets/form/clientscriptAPI.js +++ b/public/js/legacy/widgets/form/clientscriptAPI.js @@ -99,21 +99,11 @@ set_field_tip = function(n,txt) { refresh_field = function(n, docname, table_field) { // multiple - if(typeof n==typeof []) refresh_many(n, docname, table_field); + if(typeof n==typeof []) + refresh_many(n, docname, table_field); if(table_field) { // for table - if(_f.frm_dialog && _f.frm_dialog.display) { - _f.frm_dialog.cur_frm.refresh_field(n); - } else { - var g = _f.cur_grid_cell; - if(g) var hc = g.grid.head_row.cells[g.cellIndex]; - - if(g && hc && hc.fieldname==n && g.row.docname==docname) { - hc.template.refresh(); // if active - } else if (cur_frm.fields_dict[table_field]) { - cur_frm.fields_dict[table_field].grid.refresh(); - } - } + cur_frm.fields_dict[table_field].grid.grid_rows_by_docname[docname].refresh_field(n); } else if(cur_frm) { cur_frm.refresh_field(n) } diff --git a/public/js/legacy/widgets/form/form.js b/public/js/legacy/widgets/form/form.js index 459b2e55ee..34a94ea693 100644 --- a/public/js/legacy/widgets/form/form.js +++ b/public/js/legacy/widgets/form/form.js @@ -126,6 +126,7 @@ _f.Frm.prototype.setup = function() { this.script_manager = new wn.ui.form.ScriptManager({ frm: this }) + this.watch_model_updates(); this.setup_header(); @@ -134,9 +135,35 @@ _f.Frm.prototype.setup = function() { parent: this.layout_main }) + this.setup_done = true; } +_f.Frm.prototype.watch_model_updates = function() { + // watch model updates + var me = this; + + // on main doc + wn.model.on(me.doctype, "*", function(fieldname, value, doc) { + // set input + if(doc.name===me.docname) { + me.fields_dict[fieldname] + && me.fields_dict[fieldname].set_input(value); + me.script_manager.trigger(fieldname, doc.doctype, doc.name); + } + }) + + // on table fields + $.each(wn.model.get("DocField", {fieldtype:"Table", parent: me.doctype}), function(i, df) { + wn.model.on(df.options, "*", function(fieldname, value, doc) { + if(doc.parent===me.docname) { + me.fields_dict[df.fieldname].grid.set_value(fieldname, value, doc); + me.script_manager.trigger(fieldname, doc.doctype, doc.name); + } + }) + }) + +} _f.Frm.prototype.setup_print_layout = function() { var me = this; diff --git a/public/js/wn/form/control.js b/public/js/wn/form/control.js index e6c3be21e2..cc5e72f7ce 100644 --- a/public/js/wn/form/control.js +++ b/public/js/wn/form/control.js @@ -47,8 +47,11 @@ wn.ui.form.Control = Class.extend({ this.validate ? this.validate(value, set) : set(value); }, set_model_value: function(value) { - wn.model.set_value(this.doctype, this.docname, this.df.fieldname, value); - this.frm && this.frm.dirty(); + if(this.last_value!==value) { + wn.model.set_value(this.doctype, this.docname, this.df.fieldname, value); + this.frm && this.frm.dirty(); + this.last_value = value; + } }, }); @@ -207,6 +210,7 @@ wn.ui.form.ControlData = wn.ui.form.ControlInput.extend({ }, set_input: function(val) { this.$input.val(this.format_for_input(val)); + this.last_value = val; }, get_value: function() { return this.$input.val(); @@ -381,6 +385,7 @@ wn.ui.form.ControlCheck = wn.ui.form.ControlData.extend({ }, set_input: function(value) { this.input.checked = value ? 1 : 0; + this.last_value = value; } }); @@ -427,8 +432,10 @@ wn.ui.form.ControlSelect = wn.ui.form.ControlData.extend({ // model value is not an option, // set the default option (displayed) var model_value = wn.model.get_value(this.doctype, this.docname, this.df.fieldname); - if(this.$input.val() != model_value) { + if(this.$input.val() != (model_value || "")) { this.set_model_value(this.$input.val()); + } else { + this.last_value = value; } } }, @@ -503,7 +510,15 @@ wn.ui.form.ControlLink = wn.ui.form.ControlData.extend({ this.$input = this.$input_area.find('input'); this.input = this.$input.get(0); this.has_input = true; - this.bind_change_event(); + //this.bind_change_event(); + var me = this; + this.$input.on("blur", function() { + if(me.doctype && me.docname && !me.autocomplete_open) { + var value = me.get_value(); + if(value!==me.last_value) { + me.parse_validate_and_set_in_model(value); + } + }}); this.setup_buttons(); this.setup_autocomplete(); }, @@ -572,6 +587,12 @@ wn.ui.form.ControlLink = wn.ui.form.ControlData.extend({ }, }); }, + open: function(event, ui) { + me.autocomplete_open = true; + }, + close: function(event, ui) { + me.autocomplete_open = false; + }, select: function(event, ui) { me.set_model_value(ui.item.value); } @@ -621,7 +642,6 @@ wn.ui.form.ControlLink = wn.ui.form.ControlData.extend({ }, set_fetch_values: function() { var fl = this.frm.fetch_dict[this.df.fieldname].fields; - var changed_fields = []; for(var i=0; i< fl.length; i++) { wn.model.set_value(this.doctype, this.docname. fl[i], fetch_values[i]); } @@ -647,6 +667,7 @@ wn.ui.form.ControlCode = wn.ui.form.ControlInput.extend({ }, set_input: function(value) { this.editor.set_input(value); + this.last_value = value; } }); diff --git a/public/js/wn/form/grid.js b/public/js/wn/form/grid.js index aaad95b86e..9453ab4838 100644 --- a/public/js/wn/form/grid.js +++ b/public/js/wn/form/grid.js @@ -39,38 +39,61 @@ wn.ui.form.Grid = Class.extend({ refresh: function() { !this.wrapper && this.make(); var me = this, - $rows = $(me.parent).find(".rows"); - - var open_row = $(".grid-row-open").data("grid_row"); - - this.wrapper.find(".grid-row").remove(); - this.make_head(); - - $.each(this.get_data() || [], function(ri, d) { - var grid_row = new wn.ui.form.GridRow({ - parent: $rows, - parent_df: me.df, - docfields: me.docfields, - doc: d, - frm: me.frm, - grid: me - }); - - // open if last open - if(open_row && d.name===open_row.doc.name) { - open_row.toggle_view(true); - } - }); - + $rows = $(me.parent).find(".rows"), + data = this.get_data(); + this.display_status = wn.perm.get_field_display_status(this.df, this.frm.doc, this.perm); - this.wrapper.find(".grid-add-row").toggle(this.display_status=="Write" - && !this.static_rows); - if(this.display_status=="Write" && !this.static_rows) { - this.make_sortable($rows); + if(this.data_rows_are_same(data)) { + // soft refresh + $.each(this.grid_rows, function(i, g) { + g.refresh(); + }); + } else { + // redraw + this.wrapper.find(".grid-row").remove(); + this.make_head(); + this.grid_rows = []; + this.grid_rows_by_docname = {}; + $.each(data || [], function(ri, d) { + var grid_row = new wn.ui.form.GridRow({ + parent: $rows, + parent_df: me.df, + docfields: me.docfields, + doc: d, + frm: me.frm, + grid: me + }); + me.grid_rows.push(grid_row) + me.grid_rows_by_docname[d.name] = grid_row; + }); + + this.wrapper.find(".grid-add-row").toggle(this.display_status=="Write" + && !this.static_rows); + if(this.display_status=="Write" && !this.static_rows) { + this.make_sortable($rows); + } + + this.last_display_status = this.display_status; + this.last_docname = this.frm.docname; + } + }, + refresh_row: function(docname) { + this.grid_rows_by_docname[docname] && + this.grid_rows_by_docname[docname].refresh(); + }, + data_rows_are_same: function(data) { + if(this.grid_rows) { + var same = data.length==this.grid_rows.length + && this.display_status==this.last_display_status + && this.frm.docname==this.last_docname + && !$.map(this.grid_rows, function(g, i) { + return g.doc.name==data[i].name ? null : true; + }).length; + + return same; } - }, make_sortable: function($rows) { var me =this; @@ -102,6 +125,9 @@ wn.ui.form.Grid = Class.extend({ this.fieldinfo[fieldname] = {} return this.fieldinfo[fieldname]; }, + set_value: function(fieldname, value, doc) { + this.grid_rows_by_docname[doc.name].set_value(fieldname, value); + } }); wn.ui.form.GridRow = Class.extend({ @@ -118,7 +144,7 @@ wn.ui.form.GridRow = Class.extend({