Skip to content

Commit

Permalink
[IMP] web,web_editor: make image upload work without iframe
Browse files Browse the repository at this point in the history
This makes the code easier to understand and to maintain.

It was previously coded like this because FormData was not supported in older
browsers, but we don't support them anymore now so we can use it.

Part of task 1930726
  • Loading branch information
seb-odoo committed Feb 19, 2019
1 parent 867df17 commit 11a9f77
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 29 deletions.
4 changes: 2 additions & 2 deletions addons/web/static/src/js/core/ajax.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ function get_file(options) {
return true; return true;
} }


function post (controller_url, data) { function post (controller_url, data, form) {


var progressHandler = function (deferred) { var progressHandler = function (deferred) {
return function (state) { return function (state) {
Expand All @@ -324,7 +324,7 @@ function post (controller_url, data) {
}; };


var Def = $.Deferred(); var Def = $.Deferred();
var postData = new FormData(); var postData = new FormData(form);


$.each(data, function(i,val) { $.each(data, function(i,val) {
postData.append(i, val); postData.append(i, val);
Expand Down
10 changes: 6 additions & 4 deletions addons/web_editor/controllers/main.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -235,10 +235,12 @@ def attach(self, upload=None, url=None, disable_optimization=None, filters=None,
logger.exception("Failed to upload image to attachment") logger.exception("Failed to upload image to attachment")
message = str(e) message = str(e)


return """<script type='text/javascript'> return request.make_response(json.dumps({
window.attachments = %s; 'attachments': uploads,
window.error = %s; 'error': message
</script>""" % (json.dumps(uploads), json.dumps(message)) }), headers=[
('Content-Type', 'application/json'),
])


#------------------------------------------------------ #------------------------------------------------------
# remove attachment (images or link) # remove attachment (images or link)
Expand Down
24 changes: 7 additions & 17 deletions addons/web_editor/static/src/js/wysiwyg/widgets/media.js
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,7 @@
odoo.define('wysiwyg.widgets.media', function (require) { odoo.define('wysiwyg.widgets.media', function (require) {
'use strict'; 'use strict';


var ajax = require('web.ajax');
var core = require('web.core'); var core = require('web.core');
var Dialog = require('web.Dialog'); var Dialog = require('web.Dialog');
var fonts = require('wysiwyg.fonts'); var fonts = require('wysiwyg.fonts');
Expand Down Expand Up @@ -462,21 +463,13 @@ var ImageWidget = MediaWidget.extend({
* @private * @private
*/ */
_uploadFile: function () { _uploadFile: function () {
return this._mutex.exec(this._uploadImageIframe.bind(this)); return this._mutex.exec(this._uploadImageFile.bind(this));
}, },
_uploadImageIframe: function () { _uploadImageFile: function () {
var self = this; var self = this;
var def = $.Deferred(); var def = ajax.post('/web_editor/attachment/add', {}, this.$el[0]).then(function (res) {
/** var attachments = res.attachments;
* @todo file upload cannot be handled with _rpc smoothly. This uses the var error = res.error;
* form posting in iframe trick to handle the upload.
*/
var $iframe = this.$('iframe');
$iframe.on('load', function () {
var iWindow = $iframe[0].contentWindow;

var attachments = iWindow.attachments || [];
var error = iWindow.error;


self.$('.well > span').remove(); self.$('.well > span').remove();
self.$('.well > div').show(); self.$('.well > div').show();
Expand All @@ -488,16 +481,14 @@ var ImageWidget = MediaWidget.extend({
_processFile(null, error || !attachments.length); _processFile(null, error || !attachments.length);
} }
self.images = attachments; self.images = attachments;
for (var i = 0 ; i < attachments.length ; i++) { for (var i = 0; i < attachments.length; i++) {
_processFile(attachments[i], error); _processFile(attachments[i], error);
} }


if (self.options.onUpload) { if (self.options.onUpload) {
self.options.onUpload(attachments); self.options.onUpload(attachments);
} }


def.resolve();

function _processFile(attachment, error) { function _processFile(attachment, error) {
var $button = self.$('.o_upload_image_button'); var $button = self.$('.o_upload_image_button');
if (!error) { if (!error) {
Expand All @@ -514,7 +505,6 @@ var ImageWidget = MediaWidget.extend({
} }
} }
}); });
this.$el.submit();


this.$('.o_file_input').val(''); this.$('.o_file_input').val('');


Expand Down
7 changes: 1 addition & 6 deletions addons/web_editor/static/src/xml/wysiwyg.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@


<!-- Image choosing part of the Media Dialog --> <!-- Image choosing part of the Media Dialog -->
<t t-name="wysiwyg.widgets.image"> <t t-name="wysiwyg.widgets.image">
<t t-set="iframeName" t-value="_.uniqueId('fileframe-')"/> <form>
<form method="POST"
action="/web_editor/attachment/add"
enctype="multipart/form-data"
t-att-target="iframeName">
<input type="hidden" name="csrf_token" t-att-value="csrf_token"/> <input type="hidden" name="csrf_token" t-att-value="csrf_token"/>
<input type="hidden" name="filters" t-att-value="widget.firstFilters.join('_')"/> <input type="hidden" name="filters" t-att-value="widget.firstFilters.join('_')"/>
<input t-if="widget.options.res_id" type="hidden" name="res_id" t-att-value="widget.options.res_id"/> <input t-if="widget.options.res_id" type="hidden" name="res_id" t-att-value="widget.options.res_id"/>
Expand All @@ -120,7 +116,6 @@
<span><i>All attachments have been loaded</i></span> <span><i>All attachments have been loaded</i></span>
</div> </div>
</div> </div>
<iframe class="o_file_upload_iframe d-none" t-att-name="iframeName"/>
</form> </form>
</t> </t>


Expand Down

0 comments on commit 11a9f77

Please sign in to comment.