Skip to content

Instantly share code, notes, and snippets.

@MaxMorais
Created December 9, 2014 21:28
Show Gist options
  • Save MaxMorais/b8bab14867b4c0a7268b to your computer and use it in GitHub Desktop.
Save MaxMorais/b8bab14867b4c0a7268b to your computer and use it in GitHub Desktop.
Refresh Event Fix Patch
From eeb8334a9238e04d6b411c5759f5a8cb929effa4 Mon Sep 17 00:00:00 2001
From: MaxMorais <[email protected]>
Date: Tue, 9 Dec 2014 16:14:20 -0500
Subject: [PATCH] This commit fix the refresh event for TableControl with more
than 100 fields, and allow the support for nested tables
---
frappe/public/js/frappe/form/control.js | 78 +++++++++++++++++++++++++--------
frappe/public/js/frappe/form/grid.js | 8 ++--
frappe/public/js/frappe/form/layout.js | 11 ++---
3 files changed, 70 insertions(+), 27 deletions(-)
diff --git a/frappe/public/js/frappe/form/control.js b/frappe/public/js/frappe/form/control.js
index b2c76fd..cf763dc 100644
--- a/frappe/public/js/frappe/form/control.js
+++ b/frappe/public/js/frappe/form/control.js
@@ -8,7 +8,7 @@ frappe.ui.form.make_control = function (opts) {
} else {
console.log("Invalid Control Name: " + opts.df.fieldtype);
}
-}
+};
// old style
function make_field(docfield, doctype, parent, frm, in_grid, hide_label) { // Factory
@@ -17,7 +17,8 @@ function make_field(docfield, doctype, parent, frm, in_grid, hide_label) { // Fa
doctype: doctype,
parent: parent,
only_input: hide_label,
- frm: frm
+ frm: frm,
+ in_grid: in_grid
});
}
@@ -51,10 +52,16 @@ frappe.ui.form.Control = Class.extend({
locals[this.doctype][this.docname], this.perm || this.frm.perm, explain);
},
refresh: function() {
+ var event;
+ if (!this.in_grid) {
+ event = "refresh";
+ } else {
+ event = format("refresh-grid-{0}-{1}", [this.in_grid.id, this.in_grid.level]);
+ }
this.disp_status = this.get_status();
this.$wrapper
&& this.$wrapper.toggle(this.disp_status!="None")
- && this.$wrapper.trigger("refresh");
+ && this.$wrapper.trigger(event);
},
get_doc: function() {
return this.doctype && this.docname
@@ -90,9 +97,14 @@ frappe.ui.form.Control = Class.extend({
frappe.ui.form.ControlHTML = frappe.ui.form.Control.extend({
make: function() {
this._super();
- var me = this;
+ var me = this, event;
+ if (!this.in_grid) {
+ event = "refresh";
+ } else {
+ event = format("refresh-grid-{0}-{1}", [this.in_grid.id, this.in_grid.level]);
+ }
this.disp_area = this.wrapper;
- this.$wrapper.on("refresh", function() {
+ this.$wrapper.on(event, function() {
if(me.df.options)
me.$wrapper.html(me.df.options);
})
@@ -105,13 +117,18 @@ frappe.ui.form.ControlHTML = frappe.ui.form.Control.extend({
frappe.ui.form.ControlImage = frappe.ui.form.Control.extend({
make: function() {
this._super();
- var me = this;
+ var me = this, event;
+ if (!this.in_grid) {
+ event = "refresh";
+ } else {
+ event = format("refresh-grid-{0}-{1}", [this.in_grid.id, this.in_grid.level]);
+ }
this.$wrapper = $("<div class='row'><div class='col-xs-4'></div></div>")
.appendTo(this.parent)
.css({"max-width": "600px", "margin": "0px"});
this.$body = $("<div class='col-xs-8'>").appendTo(this.$wrapper)
.css({"margin-bottom": "10px", "max-width": "100%"})
- this.$wrapper.on("refresh", function() {
+ this.$wrapper.on(event, function() {
me.$body.empty();
if(me.df.options && me.frm.doc[me.df.options]) {
me.$img = $("<img src='"+me.frm.doc[me.df.options]+"' style='max-width: 100%;'>")
@@ -178,8 +195,13 @@ frappe.ui.form.ControlInput = frappe.ui.form.Control.extend({
// display (show/hide/read-only),
// mandatory style on refresh
setup_update_on_refresh: function() {
- var me = this;
- this.$wrapper.on("refresh", function() {
+ var me = this, event;
+ if (!this.in_grid) {
+ event = "refresh";
+ } else {
+ event = format("refresh-grid-{0}-{1}", [this.in_grid.id, this.in_grid.level]);
+ }
+ this.$wrapper.on(event, function() {
if(me.disp_status != "None") {
// refresh value
if(me.doctype && me.docname) {
@@ -215,7 +237,6 @@ frappe.ui.form.ControlInput = frappe.ui.form.Control.extend({
.html(frappe.format(this.value, this.df, {no_icon:true},
this.doc || (this.frm && this.frm.doc)));
},
-
bind_change_event: function() {
var me = this;
this.$input && this.$input.on("change", this.change || function(e) {
@@ -688,8 +709,13 @@ frappe.ui.form.ControlAttachImage = frappe.ui.form.ControlAttach.extend({
<div class="missing-image"><i class="icon-camera"></i></div></div>')
.prependTo(this.input_area)).toggle(false);
- var me = this;
- this.$wrapper.on("refresh", function() {
+ var me = this, event;
+ if (!this.in_grid) {
+ event = "refresh";
+ } else {
+ event = format("refresh-grid-{0}-{1}", [this.in_grid.id, this.in_grid.level]);
+ }
+ this.$wrapper.on(event, function() {
if(me.value) {
$(me.input_area).find(".missing-image").toggle(false);
me.img.attr("src", me.dataurl ? me.dataurl : me.value).toggle(true);
@@ -732,7 +758,12 @@ frappe.ui.form.ControlSelect = frappe.ui.form.ControlData.extend({
}
},
setup_attachment: function() {
- var me = this;
+ var me = this, event;
+ if (!this.in_grid) {
+ event = "refresh";
+ } else {
+ event = format("refresh-grid-{0}-{1}", [this.in_grid.id, this.in_grid.level]);
+ }
$(this.input).css({"width": "85%", "display": "inline-block"});
this.$attach = $("<button class='btn btn-default' title='"+ __("Add attachment") + "'\
style='padding-left: 6px; padding-right: 6px; margin-right: 6px;'>\
@@ -748,7 +779,7 @@ frappe.ui.form.ControlSelect = frappe.ui.form.ControlData.extend({
}
})
- this.$wrapper.on("refresh", function() {
+ this.$wrapper.on(event, function() {
me.$attach.toggle(!me.frm.doc.__islocal);
});
},
@@ -1056,10 +1087,10 @@ frappe.ui.form.ControlTable = frappe.ui.form.Control.extend({
// add title if prev field is not column / section heading or html
var prev_fieldtype = "";
if(this.df.idx > 1) {
- var prev_fieldtype = cur_frm.meta.fields[this.df.idx - 2].fieldtype;
+ var prev_fieldtype = (!this.in_grid) && cur_frm.meta.fields[this.df.idx - 2].fieldtype;
}
- if(frappe.model.layout_fields.indexOf(prev_fieldtype)===-1) {
+ if(prev_fieldtype==="Column Break" || frappe.model.layout_fields.indexOf(prev_fieldtype)===-1) {
$("<label>" + __(this.df.label) + "<label>").appendTo(this.wrapper);
}
@@ -1067,7 +1098,11 @@ frappe.ui.form.ControlTable = frappe.ui.form.Control.extend({
frm: this.frm,
df: this.df,
perm: this.perm || this.frm.perm,
- parent: this.wrapper
+ parent: this.wrapper,
+ in_grid: {
+ 'level': (this.in_grid && this.in_grid.level) ? this.in_grid.level + 1 : 1,
+ 'id': (this.in_grid && this.in_grid.id) ? this.in_grid.id || this.frm.grids[this.frm.grids.length] : 1
+ }
})
if(this.frm)
this.frm.grids[this.frm.grids.length] = this;
@@ -1078,8 +1113,13 @@ frappe.ui.form.ControlTable = frappe.ui.form.Control.extend({
.appendTo(this.wrapper);
}
- var me = this;
- this.$wrapper.on("refresh", function() {
+ var me = this, event;
+ if (!this.in_grid) {
+ event = "refresh";
+ } else {
+ event = format("refresh-grid-{0}-{1}", [this.in_grid.id, this.in_grid.level]);
+ }
+ this.$wrapper.on(event, function() {
me.grid.refresh();
return false;
});
diff --git a/frappe/public/js/frappe/form/grid.js b/frappe/public/js/frappe/form/grid.js
index 8744517..cd9bb89 100644
--- a/frappe/public/js/frappe/form/grid.js
+++ b/frappe/public/js/frappe/form/grid.js
@@ -85,9 +85,10 @@ frappe.ui.form.Grid = Class.extend({
docfields: this.docfields,
doc: d,
frm: this.frm,
- grid: this
+ grid: this,
+ in_grid: this.in_grid
});
- this.grid_rows.push(grid_row)
+ this.grid_rows.push(grid_row);
this.grid_rows_by_docname[d.name] = grid_row;
}
@@ -463,12 +464,13 @@ frappe.ui.form.GridRow = Class.extend({
var me = this;
this.make_form();
this.form_area.empty();
-
+
this.layout = new frappe.ui.form.Layout({
fields: this.docfields,
body: this.form_area,
no_submit_on_enter: true,
frm: this.frm,
+ in_grid: this.in_grid
});
this.layout.make();
diff --git a/frappe/public/js/frappe/form/layout.js b/frappe/public/js/frappe/form/layout.js
index 6b70abc..e108dcd 100644
--- a/frappe/public/js/frappe/form/layout.js
+++ b/frappe/public/js/frappe/form/layout.js
@@ -17,7 +17,7 @@ frappe.ui.form.Layout = Class.extend({
this.fields_dict = {};
this.labelled_section_count = 0;
this.ignore_types = frappe.model.layout_fields;
-
+
$.extend(this, opts);
},
make: function() {
@@ -118,7 +118,7 @@ frappe.ui.form.Layout = Class.extend({
make_field: function(df, colspan) {
!this.section && this.make_section();
!this.column && this.make_column();
- var fieldobj = make_field(df, this.doctype, this.column.get(0), this.frm);
+ var fieldobj = make_field(df, this.doctype, this.column.get(0), this.frm, this.in_grid);
fieldobj.layout = this;
this.fields_list.push(fieldobj);
this.fields_dict[df.fieldname] = fieldobj;
@@ -159,7 +159,7 @@ frappe.ui.form.Layout = Class.extend({
},
make_section: function(df) {
- if(!this.page) {
+ if(!this.page) {
this.page = $('<div class="form-page"></div>').appendTo(this.wrapper);
}
@@ -173,8 +173,9 @@ frappe.ui.form.Layout = Class.extend({
if(df.label) {
this.labelled_section_count++;
var head = $('<h4 class="col-md-12">'
- + (df.options ? (' <i class="icon-fixed-width text-muted '+df.options+'"></i> ') : "")
- + '<span class="section-count-label">' + __(this.labelled_section_count) + "</span>. "
+ + ((df.options && df.options.indexOf("icon")===0) ? (' <i class="icon-fixed-width text-muted '+df.options+'"></i> ') : "")
+ + '<span class="section-count-label">' + __(this.labelled_section_count) + "</span>"
+ + ". " + ((df.options && df.options.indexOf("<")===0) ? df.options + " " : "")
+ __(df.label)
+ "</h4>")
.css({"margin":"15px 0px"})
--
1.9.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment