From edcf216d33609fd7a204c70bad0dcd82dfbf57c0 Mon Sep 17 00:00:00 2001 From: Fabien Meghazi Date: Thu, 14 Jun 2012 17:00:10 +0200 Subject: [PATCH] [FIX] Saving a binary field not saved yet crashes lp bug: https://launchpad.net/bugs/1010475 fixed bzr revid: fme@openerp.com-20120614150010-ik6f3rqk8li314aq --- addons/web/static/src/js/view_form.js | 46 ++++++++++++++++++--------- addons/web/static/src/xml/base.xml | 10 +++--- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/addons/web/static/src/js/view_form.js b/addons/web/static/src/js/view_form.js index 3aa0bcd7b37..c2d3b56e5a2 100644 --- a/addons/web/static/src/js/view_form.js +++ b/addons/web/static/src/js/view_form.js @@ -3986,7 +3986,7 @@ instance.web.form.FieldBinary = instance.web.form.AbstractField.extend(_.extend( }, initialize_content: function() { this.$element.find('input.oe-binary-file').change(this.on_file_change); - this.$element.find('button.oe-binary-file-save').click(this.on_save_as); + this.$element.find('button.oe_binary_file_save').click(this.on_save_as); this.$element.find('.oe-binary-file-clear').click(this.on_clear); }, human_filesize : function(size) { @@ -4016,6 +4016,7 @@ instance.web.form.FieldBinary = instance.web.form.AbstractField.extend(_.extend( // TODO: use openerp web crashmanager console.warn("Error while uploading file : ", name); } else { + this.filename = name; this.on_file_uploaded_and_valid.apply(this, arguments); } this.$element.find('.oe-binary-progress').hide(); @@ -4023,20 +4024,33 @@ instance.web.form.FieldBinary = instance.web.form.AbstractField.extend(_.extend( }, on_file_uploaded_and_valid: function(size, name, content_type, file_base64) { }, - on_save_as: function() { - $.blockUI(); - this.session.get_file({ - url: '/web/binary/saveas_ajax', - data: {data: JSON.stringify({ - model: this.view.dataset.model, - id: (this.view.datarecord.id || ''), - field: this.name, - filename_field: (this.node.attrs.filename || ''), - context: this.view.dataset.get_context() - })}, - complete: $.unblockUI, - error: instance.webclient.crashmanager.on_rpc_error - }); + on_save_as: function(ev) { + var value = this.get('value'); + if (!value) { + this.do_warn(_t("Save As..."), _t("The field is empty, there's nothing to save !")); + ev.stopPropagation(); + } else if (this._dirty_flag) { + var link = this.$('.oe_binary_file_save_data')[0]; + link.download = this.filename || "download.bin"; // Works on only on Google Chrome + //link.target = '_blank'; + link.href = "data:application/octet-stream;base64," + value; + } else { + $.blockUI(); + this.session.get_file({ + url: '/web/binary/saveas_ajax', + data: {data: JSON.stringify({ + model: this.view.dataset.model, + id: (this.view.datarecord.id || ''), + field: this.name, + filename_field: (this.node.attrs.filename || ''), + context: this.view.dataset.get_context() + })}, + complete: $.unblockUI, + error: instance.webclient.crashmanager.on_rpc_error + }); + ev.stopPropagation(); + return false; + } }, on_clear: function() { if (this.get('value') !== false) { @@ -4088,6 +4102,7 @@ instance.web.form.FieldBinaryFile = instance.web.form.FieldBinary.extend({ var show_value = name + " (" + this.human_filesize(size) + ")"; this.$element.find('input').eq(0).val(show_value); this.set_filename(name); + this.trigger('changed_value'); }, set_filename: function(value_) { var filename = this.node.attrs.filename; @@ -4130,6 +4145,7 @@ instance.web.form.FieldBinaryImage = instance.web.form.FieldBinary.extend({ this.set({'value': file_base64}); this.binary_value = true; this.render_value(); + this.trigger('changed_value'); }, on_clear: function() { this._super.apply(this, arguments); diff --git a/addons/web/static/src/xml/base.xml b/addons/web/static/src/xml/base.xml index b90addb0a2c..ca4ce011bb3 100644 --- a/addons/web/static/src/xml/base.xml +++ b/addons/web/static/src/xml/base.xml @@ -1074,10 +1074,12 @@ - + + +