diff --git a/frappe/model/base_document.py b/frappe/model/base_document.py index e1097a647c..39a82ad7a2 100644 --- a/frappe/model/base_document.py +++ b/frappe/model/base_document.py @@ -364,6 +364,29 @@ class BaseDocument(object): return format_value(self.get(fieldname), self.meta.get_field(fieldname), doc=doc or self, currency=currency) + def get_print_template(self, fieldname, parent_doc=None): + """Returns print template for given fieldname if specified in controller + or parent controller. + + Templates must be specified as: + + class MyDocType(Document): + def __setup__(self): + self.print_templates = { + "[fieldname]": "templates/includes/template_name.html", + "[table fieldname].[fieldname]": "templates/includes/template_name.html" + } + + :param fieldname: Field for which template is queried. + :param parent_doc: Parent Document, if child doc.""" + src = self + if parent_doc: + src = parent_doc + fieldname = self.parentfield + "." + fieldname + if hasattr(src, "print_templates"): + return src.print_templates.get(fieldname) + + def _filter(data, filters, limit=None): """pass filters as: {"key": "val", "key": ["!=", "val"], diff --git a/frappe/model/meta.py b/frappe/model/meta.py index f020e94510..5f5a13b550 100644 --- a/frappe/model/meta.py +++ b/frappe/model/meta.py @@ -213,7 +213,7 @@ class Meta(Document): def is_print_hide(self, fieldname): df = self.get_field(fieldname) - return df.get("__print_hide") or df.print_hide + return df and (df.get("__print_hide") or df.print_hide) doctype_table_fields = [ frappe._dict({"fieldname": "fields", "options": "DocField"}), diff --git a/frappe/print/page/print_format_builder/print_format_builder.css b/frappe/print/page/print_format_builder/print_format_builder.css index 204acaac2b..98e3b69131 100644 --- a/frappe/print/page/print_format_builder/print_format_builder.css +++ b/frappe/print/page/print_format_builder/print_format_builder.css @@ -3,13 +3,16 @@ margin: 0px; margin-bottom: 15px; } -.print-format-builder-add-section { +.print-format-builder-add-section, .print-format-builder-header { border: 1px dashed #d1d8dd; - text-align: center; padding: 15px; cursor: pointer; } +.print-format-builder-header { + margin-bottom: 15px; +} + .print-format-builder-column { padding: 15px; margin: 0px -15px 0 -16px; diff --git a/frappe/print/page/print_format_builder/print_format_builder.js b/frappe/print/page/print_format_builder/print_format_builder.js index 23265f6d65..7fdca1afe5 100644 --- a/frappe/print/page/print_format_builder/print_format_builder.js +++ b/frappe/print/page/print_format_builder/print_format_builder.js @@ -166,12 +166,19 @@ frappe.PrintFormatBuilder = Class.extend({ .appendTo(this.page.main); this.setup_sortable(); this.setup_add_section(); + this.setup_edit_heading(); }, prepare_data: function() { this.data = JSON.parse(this.print_format.format_data || "[]"); if(!this.data.length) { // new layout this.data = this.meta.fields; + } else { + // extract print_heading_template if found + if(this.data[0].fieldname==="print_heading_template") { + this.print_heading_template = this.data[0].options; + this.data = this.data.splice(1); + } } this.layout_data = []; var section = null, column = null, me = this; @@ -242,7 +249,7 @@ frappe.PrintFormatBuilder = Class.extend({ !_f.print_hide && f.label) { // column names set as fieldname|width - f.visible_columns.push({fieldname: _f.fieldname, width: _f.width}); + f.visible_columns.push({fieldname: _f.fieldname, print_width: (_f.width || "")}); } }); } @@ -412,6 +419,14 @@ frappe.PrintFormatBuilder = Class.extend({ me.setup_sortable_for_column($section.find(".print-format-builder-column").get(0)); }); }, + setup_edit_heading: function() { + var me = this; + this.page.main.find(".edit-heading").on("click", function() { + var $heading = $(this).parents(".print-format-builder-header:first") + .find(".print-format-builder-print-heading"); + var d = me.get_edit_html_dialog(__("Edit Heading"), __("Heading"), $heading); + }) + }, setup_column_selector: function() { var me = this; this.page.main.on("click", ".select-columns", function() { @@ -424,7 +439,7 @@ frappe.PrintFormatBuilder = Class.extend({ $.each(columns, function(i, v) { var parts = v.split("|"); - widths[parts[0]] = parts[1]; + widths[parts[0]] = parts[1] || ""; }); var d = new frappe.ui.Dialog({ @@ -473,7 +488,7 @@ frappe.PrintFormatBuilder = Class.extend({ }); }, get_visible_columns_string: function(f) { - return $.map(f.visible_columns, function(v) { return v.fieldname + "|" + v.width }).join(",") + return $.map(f.visible_columns, function(v) { return v.fieldname + "|" + (v.print_width || "") }).join(",") }, get_no_content: function() { return '
'+__("Edit to add content")+'
' @@ -481,38 +496,48 @@ frappe.PrintFormatBuilder = Class.extend({ setup_edit_custom_html: function() { var me = this; this.page.main.on("click", ".edit-html", function() { - var parent = $(this).parents(".print-format-builder-field:first"), - $content = parent.find(".html-content"); - - var d = new frappe.ui.Dialog({ - title: __("Edit Custom HTML"), + me.get_edit_html_dialog(__("Edit Custom HTML"), __("Custom HTML"), + $(this).parents(".print-format-builder-field:first").find(".html-content")); + }); + }, + get_edit_html_dialog: function(title, label, $content) { + var d = new frappe.ui.Dialog({ + title: title, fields: [ { fieldname: "content", fieldtype: "Text Editor", - label: "Custom HTML" + label: label } ] }); - var content = $content.html(); - if(content.indexOf("data-no-content")!==-1) content = ""; + // set existing content in input + content = $content.html(); + if(content.indexOf("data-no-content")!==-1) content = ""; + d.set_input("content", content); - d.set_input("content", content); - - d.set_primary_action(__("Update"), function() { - $content.html(d.get_value("content")); - d.hide(); - }); - - d.show(); - - return false; + d.set_primary_action(__("Update"), function() { + $content.html(d.get_value("content")); + d.hide(); }); + + d.show(); + + return d; }, save_print_format: function() { var data = [], me = this; + + // add print heading as the first field + // this will be removed and set as a doc property + // before rendering + data.push({"fieldname": "print_heading_template", + fieldtype:"HTML", + options: this.page.main.find(".print-format-builder-print-heading").html()}); + + // add pages this.page.main.find(".print-format-builder-section").each(function() { data.push({"fieldtype": "Section Break"}); $(this).find(".print-format-builder-column").each(function() { @@ -530,7 +555,7 @@ frappe.PrintFormatBuilder = Class.extend({ $.each(columns, function(i, c) { var parts = c.split("|"); df.visible_columns.push({fieldname:parts[0], - width:parts[1]}); + print_width:parts[1]}); }); } if(fieldtype==="Custom HTML") { diff --git a/frappe/print/page/print_format_builder/print_format_builder_column_selector.html b/frappe/print/page/print_format_builder/print_format_builder_column_selector.html index 6cdb523dc2..f6b645aac9 100644 --- a/frappe/print/page/print_format_builder/print_format_builder_column_selector.html +++ b/frappe/print/page/print_format_builder/print_format_builder_column_selector.html @@ -21,7 +21,7 @@
diff --git a/frappe/print/page/print_format_builder/print_format_builder_layout.html b/frappe/print/page/print_format_builder/print_format_builder_layout.html index e1d1c6e2f5..713bf1643a 100644 --- a/frappe/print/page/print_format_builder/print_format_builder_layout.html +++ b/frappe/print/page/print_format_builder/print_format_builder_layout.html @@ -2,13 +2,23 @@
{%= __("Drag elements from the sidebar to add. Drag them back to trash.") %}

+ -