[ADD] Added field binary

bzr revid: fme@openerp.com-20110519125845-efw2dxjwwa6na7jq
This commit is contained in:
Fabien Meghazi 2011-05-19 14:58:45 +02:00
parent 9a21bc6f80
commit b722cab801
2 changed files with 117 additions and 14 deletions

View File

@ -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'
});
};

View File

@ -386,7 +386,7 @@
<span></span>
</div>
</t>
<t t-name="FieldImage">
<t t-name="FieldBinaryImage">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="center">
@ -428,6 +428,45 @@
</tr>
</table>
</t>
<t t-name="FieldBinaryFile">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td width="100%">
<input type="text"
t-att-name="widget.name"
t-att-id="widget.element_id + '_field'"
t-att-class="'field_' + widget.type" style="width: 100%"
/>
</td>
<td class="oe-binary" nowrap="true">
<!-- TODO: input@type=file inside a button won't work in firefox. Use links instead -->
<button class="button oe-binary-file-set" type="button" title="Select file">
<img src="/base/static/src/img/icons/STOCK_DIRECTORY.png"/>
<span>Select</span>
<form class="oe-binary-form" t-att-target="widget.iframe"
method="post" enctype="multipart/form-data" action="/base/formview/upload">
<input type="hidden" name="session_id" value=""/>
<input type="hidden" name="callback" t-att-value="widget.iframe"/>
<input type="file" class="oe-binary-file" name="ufile"/>
</form>
</button>
<button class="button oe-binary-file-save" type="button" title="Save As">
<img src="/base/static/src/img/icons/gtk-save.png"/>
<span>Save As</span>
</button>
<button class="button oe-binary-file-clear" type="button" title="Clear">
<img src="/base/static/src/img/icons/STOCK_MISSING_IMAGE.png"/>
<span>Clear</span>
</button>
</td>
<td class="oe-binary-progress" style="display: none" nowrap="true">
<img src="/base/static/src/img/throbber.gif" width="16" height="16"/>
<b>Uploading ...</b>
<iframe t-att-id="widget.iframe" t-att-name="widget.iframe" style="display: none"> </iframe>
</td>
</tr>
</table>
</t>
<t t-name="WidgetButton">
<button type="button"
t-att-id="widget.element_id + '_button'"