From 5d4308cfa201535a618a4bbf929d1a36f732519d Mon Sep 17 00:00:00 2001 From: kutyel Date: Tue, 26 Jul 2016 19:01:49 +0200 Subject: [PATCH] [IMP][web_widget_image_download] save image with the correct type --- web_widget_image_download/README.rst | 10 +++---- web_widget_image_download/__openerp__.py | 2 +- .../src/js/web_widget_image_download.js | 27 ++++++++++++++++--- .../src/xml/web_widget_image_download.xml | 7 +++-- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/web_widget_image_download/README.rst b/web_widget_image_download/README.rst index 8e3aff0d..f714914b 100644 --- a/web_widget_image_download/README.rst +++ b/web_widget_image_download/README.rst @@ -22,6 +22,11 @@ To use this module, you need to: :alt: Try me on Runbot :target: https://runbot.odoo-community.org/runbot/162/8.0 +Known Issues / Roadmap +====================== + +* In order to work correctly, this widget has to detect image type, the server should include this information in the `Content-Type` header. Right now, odoo is not doing so, but a fix has been `proposed `_. + Bug Tracker =========== @@ -33,11 +38,6 @@ help us smashing it by providing a detailed and welcomed feedback. Credits ======= -Images ------- - -* Odoo Community Association: `Icon `_. - Contributors ------------ diff --git a/web_widget_image_download/__openerp__.py b/web_widget_image_download/__openerp__.py index eafc71d0..ed01155b 100644 --- a/web_widget_image_download/__openerp__.py +++ b/web_widget_image_download/__openerp__.py @@ -3,7 +3,7 @@ # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). { "name": "Web Widget - Image Download", - "summary": "Allows to download the avatar for the contact", + "summary": "Allows to download any image from its widget", "version": "8.0.1.0.0", "category": "web", "website": "https://www.tecnativa.com", diff --git a/web_widget_image_download/static/src/js/web_widget_image_download.js b/web_widget_image_download/static/src/js/web_widget_image_download.js index 092a42b8..bd31db31 100644 --- a/web_widget_image_download/static/src/js/web_widget_image_download.js +++ b/web_widget_image_download/static/src/js/web_widget_image_download.js @@ -2,15 +2,36 @@ * Copyright 2016 Flavio Corpa * License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). */ -openerp.web_widget_image_download = function(instance) { +openerp.web_widget_image_download = function (instance) { 'use strict'; instance.web.form.web_widget_image_download = instance.web.form.FieldBinaryImage.include({ render_value() { this._super(); - // take image URI from preceding tag - this.$el.find('.oe_form_binary_file_download').attr('href', $('img[name="image"]').attr('src')); + const widget = this.$el.find('.oe_form_binary_file_download'); + + this.imgSrc = this.$el.find('img[name="image"]').attr('src'); + + $.ajax({ + type: 'HEAD', + url: this.imgSrc, + complete(xhr) { + // retrieve image type from server ("Content-Type" header) + widget.attr('download', xhr.getResponseHeader("Content-Type").replace('/', '.')); + } + }); + + // use jquery instead of `replace` with qweb (to avoid breaking inheritance) + if (this.has_custom_image()) { + this.$el.find('.oe_form_binary_file_clear').removeClass('col-md-offset-5'); + } + + widget.attr('href', this.imgSrc); + }, + has_custom_image() { + // check if the image of the widget is different from the default placeholder + return this.imgSrc && !this.imgSrc.includes('/placeholder.png'); } }); } diff --git a/web_widget_image_download/static/src/xml/web_widget_image_download.xml b/web_widget_image_download/static/src/xml/web_widget_image_download.xml index bec197a6..096d3256 100644 --- a/web_widget_image_download/static/src/xml/web_widget_image_download.xml +++ b/web_widget_image_download/static/src/xml/web_widget_image_download.xml @@ -5,10 +5,9 @@