summaryrefslogtreecommitdiff
path: root/addons/web/static
diff options
context:
space:
mode:
authorAlexandre Kühn <alex.e.kuhn@gmail.com>2018-01-08 22:08:21 +0100
committerAlexandre Kühn <alexkuhn@users.noreply.github.com>2018-01-12 16:12:49 +0100
commit7813a8e714f268983b0be88827ae1c8ad887e6c7 (patch)
tree928daa6f9d6f84420d8dee7afdabf99c41d232b7 /addons/web/static
parentc3e3770f06db6b427118a3a2c5629d3aa9efaaa8 (diff)
[FIX] web: binaryfile field in list views
Revert partially 255e8ca3d92e3a77d7c2a145677623518bc2f978 Removed widget 'download_link', as it was a sub-version of widget 'binary': it did the exact same thing (representation of binary fields by means of a download link) in a much better-looking way (download icon + filename as text of the download link, instead of just a download link with text "Download"). Also, the values of binary fields might be already a human-readable binsize (e.g. 2.52 MBs), instead of its plain textual string representation in base64: The contextual parameter 'bin_size', when set to true, does not download the data of binary fields. As a consequence, the server provides the sizes of the binary, instead of their value.
Diffstat (limited to 'addons/web/static')
-rw-r--r--addons/web/static/src/js/core/utils.js19
-rw-r--r--addons/web/static/src/js/fields/basic_fields.js20
-rw-r--r--addons/web/static/src/js/fields/field_registry.js1
-rw-r--r--addons/web/static/src/js/fields/field_utils.js10
-rw-r--r--addons/web/static/src/xml/base.xml3
-rw-r--r--addons/web/static/tests/fields/basic_fields_tests.js34
6 files changed, 24 insertions, 63 deletions
diff --git a/addons/web/static/src/js/core/utils.js b/addons/web/static/src/js/core/utils.js
index acf8ca4d68d..b9f5a7c9689 100644
--- a/addons/web/static/src/js/core/utils.js
+++ b/addons/web/static/src/js/core/utils.js
@@ -24,6 +24,25 @@ var utils = {
}
},
/**
+ * Check if the value is a bin_size or not.
+ * If not, compute an approximate size out of the base64 encoded string.
+ *
+ * @param {string} value original format
+ * @return {string} bin_size (human-readable)
+ */
+ binaryToBinsize: function (value) {
+ if (!value) {
+ return this.human_size(0);
+ }
+ if (!this.is_bin_size(value)) {
+ // Computing approximate size out of base64 encoded string
+ // http://en.wikipedia.org/wiki/Base64#MIME
+ return this.human_size(value.length / 1.37);
+ }
+ // already bin_size
+ return value;
+ },
+ /**
* Confines a value inside an interval
*
* @param {number} [val] the value to confine
diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js
index 38fc0cad439..09406b43711 100644
--- a/addons/web/static/src/js/fields/basic_fields.js
+++ b/addons/web/static/src/js/fields/basic_fields.js
@@ -1298,25 +1298,6 @@ var FieldBinaryFile = AbstractFieldBinary.extend({
},
});
-var FieldBinaryDownloadLink = AbstractFieldBinary.extend({
- template: 'FieldBinaryDownloadLink',
- events: _.extend({}, AbstractFieldBinary.prototype.events, {
- 'click': function (ev) {
- ev.stopPropagation();
- },
- }),
- supportedFieldTypes: ['binary'],
- init: function () {
- this._super.apply(this, arguments);
- this.filename = _t("Binary File");
- if (this.nodeOptions.filename && this.recordData[this.nodeOptions.filename]) {
- this.filename = this.recordData[this.nodeOptions.filename];
- }
- this.url = "data:application/octet-stream;base64," + this.value;
- this.text = _t("Download");
- },
-});
-
var PriorityWidget = AbstractField.extend({
// the current implementation of this widget makes it
// only usable for fields of type selection
@@ -2547,7 +2528,6 @@ return {
TranslatableFieldMixin: TranslatableFieldMixin,
DebouncedField: DebouncedField,
FieldEmail: FieldEmail,
- FieldBinaryDownloadLink: FieldBinaryDownloadLink,
FieldBinaryFile: FieldBinaryFile,
FieldBinaryImage: FieldBinaryImage,
FieldBoolean: FieldBoolean,
diff --git a/addons/web/static/src/js/fields/field_registry.js b/addons/web/static/src/js/fields/field_registry.js
index 85b4fc1187f..6692663d5f9 100644
--- a/addons/web/static/src/js/fields/field_registry.js
+++ b/addons/web/static/src/js/fields/field_registry.js
@@ -33,7 +33,6 @@ registry
.add('email', basic_fields.FieldEmail)
.add('phone', basic_fields.FieldPhone)
.add('url', basic_fields.UrlWidget)
- .add('download_link', basic_fields.FieldBinaryDownloadLink)
.add('image', basic_fields.FieldBinaryImage)
.add('binary', basic_fields.FieldBinaryFile)
.add('monetary', basic_fields.FieldMonetary)
diff --git a/addons/web/static/src/js/fields/field_utils.js b/addons/web/static/src/js/fields/field_utils.js
index 812a7f380f7..5ac180b1de1 100644
--- a/addons/web/static/src/js/fields/field_utils.js
+++ b/addons/web/static/src/js/fields/field_utils.js
@@ -28,17 +28,17 @@ var _t = core._t;
//------------------------------------------------------------------------------
/**
+ * Convert binary to bin_size
*
- * @param {string} value base64 representation of the binary
+ * @param {string} value base64 representation of the binary (might be already a bin_size!)
* @param {Object} [field]
* a description of the field (note: this parameter is ignored)
* @param {Object} [options] additional options (note: this parameter is ignored)
+ *
+ * @returns {string} bin_size (which is human-readable)
*/
function formatBinary(value, field, options) {
- if (!value) {
- return '';
- }
- return utils.human_size(value.length / 1.37); // base64 estimated size: http://en.wikipedia.org/wiki/Base64#MIME
+ return utils.binaryToBinsize(value);
}
/**
diff --git a/addons/web/static/src/xml/base.xml b/addons/web/static/src/xml/base.xml
index 0371bd7ec84..c0386a0a87f 100644
--- a/addons/web/static/src/xml/base.xml
+++ b/addons/web/static/src/xml/base.xml
@@ -935,9 +935,6 @@
</t>
</div>
</t>
-<t t-name="FieldBinaryDownloadLink">
- <a t-att-download="widget.filename" t-att-href="widget.url"><t t-esc="widget.text"/></a>
-</t>
<t t-name="HiddenInputFile">
<div t-attf-class="o_hidden_input_file #{fileupload_class or ''}" t-att-style="fileupload_style">
<form class="o_form_binary_form" t-att-target="fileupload_id"
diff --git a/addons/web/static/tests/fields/basic_fields_tests.js b/addons/web/static/tests/fields/basic_fields_tests.js
index a2f59a68e2a..9ea423250e4 100644
--- a/addons/web/static/tests/fields/basic_fields_tests.js
+++ b/addons/web/static/tests/fields/basic_fields_tests.js
@@ -1481,40 +1481,6 @@ QUnit.module('basic_fields', {
session.get_file = oldGetFile;
});
- QUnit.test('download_link widget in list view', function (assert) {
- assert.expect(5);
-
- this.data.partner.fields.filename = { string: "Filename", type: "char" };
- this.data.partner.records[0].filename = "document.txt";
-
- var form = createView({
- View: ListView,
- model: 'partner',
- data: this.data,
- arch: '<tree string="Partners">' +
- '<field name="document" widget="download_link" options="{\'filename\': \'filename\'}"/>' +
- '<field name="filename"/>' +
- '</tree>',
- res_id: 1,
- });
-
- assert.ok(form.$('.o_data_cell').first().hasClass("o_download_link_cell"),
- "this should be a download link cell");
- assert.ok(form.$('.o_data_cell').first().find('a').eq(0),
- "there should be a link in the cell");
- assert.strictEqual(form.$('.o_data_cell').first().find('a').eq(0).attr('download'),
- "document.txt",
- "it should download a file named 'document.txt'");
- assert.strictEqual(form.$('.o_data_cell').first().find('a').eq(0).attr('href').trim(),
- "data:application/octet-stream;base64,coucou==",
- "it should have the appropriate link of a binary");
- assert.strictEqual(form.$('.o_data_cell').first().find('a').eq(0).text(),
- "Download",
- "the text of the link should be 'Download'");
-
- form.destroy();
- });
-
QUnit.test('text field rendering in list view', function (assert) {
assert.expect(1);