From b722cab801964b82fd773a2de15ddf4fb0119790 Mon Sep 17 00:00:00 2001 From: Fabien Meghazi Date: Thu, 19 May 2011 14:58:45 +0200 Subject: [PATCH] [ADD] Added field binary bzr revid: fme@openerp.com-20110519125845-efw2dxjwwa6na7jq --- addons/base/static/src/js/form.js | 90 ++++++++++++++++++++++++----- addons/base/static/src/xml/base.xml | 41 ++++++++++++- 2 files changed, 117 insertions(+), 14 deletions(-) diff --git a/addons/base/static/src/js/form.js b/addons/base/static/src/js/form.js index 2558e0c1bcc..d91fef20a43 100644 --- a/addons/base/static/src/js/form.js +++ b/addons/base/static/src/js/form.js @@ -1187,24 +1187,29 @@ openerp.base.form.FieldReference = openerp.base.form.Field.extend({ } }); -openerp.base.form.FieldImage = openerp.base.form.Field.extend({ +openerp.base.form.FieldBinary = openerp.base.form.Field.extend({ init: function(view, node) { this._super(view, node); - this.template = "FieldImage"; this.iframe = this.element_id + '_iframe'; + this.binary_value = false; }, start: function() { this._super.apply(this, arguments); this.$element.find('input.oe-binary-file').change(this.on_file_change); - this.$element.find('button.oe-binary-file-clear').click(this.on_clear); + this.$element.find('.oe-binary-file-clear').click(this.on_clear); }, set_value_from_ui: function() { }, - set_image_maxwidth: function() { - this.$element.find('img.oe-binary-image').css('max-width', this.$element.width()); + human_filesize : function(size) { + var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + var i = 0; + while (size >= 1024) { + size /= 1024; + ++i; + } + return size.toFixed(2) + ' ' + units[i]; }, on_file_change: function() { - this.set_image_maxwidth(); // TODO: on modern browsers, we could directly read the file locally on client ready to be used on image cropper // http://www.html5rocks.com/tutorials/file/dndfiles/ // http://deepliquid.com/projects/Jcrop/demos.php?demo=handler @@ -1214,35 +1219,93 @@ openerp.base.form.FieldImage = openerp.base.form.Field.extend({ this.toggle_progress(); }, toggle_progress: function() { - this.$element.find('div.oe-binary-progress, div.oe-binary').toggle(); + this.$element.find('.oe-binary-progress, .oe-binary').toggle(); }, - on_file_uploaded: function(size, name, content_type, img) { + on_file_uploaded: function(size, name, content_type, file_base64) { delete(window[this.iframe]); if (size === false) { this.notification.warn("File Upload", "There was a problem while uploading your file"); // TODO: use openerp web exception handler console.log("Error while uploading file : ", name); } else { - this.value = img; - this.$element.find('img.oe-binary-image').attr('src', 'data:' + (content_type || 'image/png') + ';base64,' + img); + this.on_file_uploaded_and_valid.apply(this, arguments); this.on_ui_change(); } this.toggle_progress(); }, + on_file_uploaded_and_valid: function(size, name, content_type, file_base64) { + }, on_clear: function() { if (this.value !== false) { this.value = false; - this.$element.find('img.oe-binary-image').attr('src', '/base/static/src/img/placeholder.png'); + this.binary_value = false; this.on_ui_change(); } return false; + } +}); + +openerp.base.form.FieldBinaryFile = openerp.base.form.FieldBinary.extend({ + init: function(view, node) { + this._super(view, node); + this.template = "FieldBinaryFile"; + }, + set_value: function(value) { + this._super.apply(this, arguments); + var show_value = (value != null && value !== false) ? value : ''; + this.$element.find('input').eq(0).val(show_value); + }, + on_file_uploaded_and_valid: function(size, name, content_type, file_base64) { + this.value = file_base64; + this.binary_value = true; + var show_value = this.human_filesize(size); + this.$element.find('input').eq(0).val(show_value); + this.set_filename(name); + }, + set_filename: function(value) { + var filename = this.node.attrs.filename; + if (this.view.fields[filename]) { + this.view.fields[filename].set_value(value); + } + }, + on_clear: function() { + this._super.apply(this, arguments); + this.$element.find('input').eq(0).val(''); + this.set_filename(''); + } +}); + +openerp.base.form.FieldBinaryImage = openerp.base.form.FieldBinary.extend({ + init: function(view, node) { + this._super(view, node); + this.template = "FieldBinaryImage"; + }, + start: function() { + this._super.apply(this, arguments); + this.$image = this.$element.find('img.oe-binary-image'); + }, + set_image_maxwidth: function() { + this.$image.css('max-width', this.$element.width()); + }, + on_file_change: function() { + this.set_image_maxwidth(); + this._super.apply(this, arguments); + }, + on_file_uploaded_and_valid: function(size, name, content_type, file_base64) { + this.value = file_base64; + this.binary_value = true; + this.$image.attr('src', 'data:' + (content_type || 'image/png') + ';base64,' + file_base64); + }, + on_clear: function() { + this._super.apply(this, arguments); + this.$image.attr('src', '/base/static/src/img/placeholder.png'); }, set_value: function(value) { this._super.apply(this, arguments); this.set_image_maxwidth(); var url = '/base/formview/image?session_id=' + this.session.session_id + '&model=' + this.view.dataset.model +'&id=' + (this.view.datarecord.id || '') + '&field=' + this.name + '&t=' + (new Date().getTime()) - this.$element.find('img.oe-binary-image').attr('src', url); + this.$image.attr('src', url); } }); @@ -1273,7 +1336,8 @@ openerp.base.form.widgets = new openerp.base.Registry({ 'integer': 'openerp.base.form.FieldFloat', 'progressbar': 'openerp.base.form.FieldProgressBar', 'float_time': 'openerp.base.form.FieldFloatTime', - 'image': 'openerp.base.form.FieldImage' + 'image': 'openerp.base.form.FieldBinaryImage', + 'binary': 'openerp.base.form.FieldBinaryFile' }); }; diff --git a/addons/base/static/src/xml/base.xml b/addons/base/static/src/xml/base.xml index f6fec76f32e..24f3c551797 100644 --- a/addons/base/static/src/xml/base.xml +++ b/addons/base/static/src/xml/base.xml @@ -386,7 +386,7 @@ - +
@@ -428,6 +428,45 @@
+ + + + + + + +
+ + + + + + +
+