Browse Source

Make _handle_drop_items extensible to prevent multiple rpc calls

pull/971/head
Akim Juillerat 7 years ago
committed by Pablo Fuentes
parent
commit
3970390da0
  1. 47
      web_drop_target/static/src/js/web_drop_target.js

47
web_drop_target/static/src/js/web_drop_target.js

@ -28,21 +28,17 @@ odoo.define('web_drop_target', function(require) {
}, },
_on_drop: function(e) { _on_drop: function(e) {
var self = this;
var drop_items = this._get_drop_items(e); var drop_items = this._get_drop_items(e);
_.each(drop_items, function(drop_item) {
var reader = new FileReader();
reader.onloadend = self.proxy(
_.partial(self._handle_file_drop, drop_item.getAsFile())
);
reader.readAsArrayBuffer(drop_item.getAsFile());
});
if(!drop_items) {
return;
}
this._remove_overlay(); this._remove_overlay();
e.preventDefault(); e.preventDefault();
this._handle_drop_items(drop_items, e)
}, },
_on_dragenter: function(e) { _on_dragenter: function(e) {
if(this._get_drop_items(e).length) {
if(this._get_drop_items(e)) {
e.preventDefault(); e.preventDefault();
this._add_overlay(); this._add_overlay();
return false; return false;
@ -70,26 +66,24 @@ odoo.define('web_drop_target', function(require) {
}, },
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
_handle_file_drop: function(drop_file, e) {
_handle_drop_items: function(drop_items, e) {
// do something here, for example call the helper function below // do something here, for example call the helper function below
// e is the on_load_end handler for the FileReader above, // e is the on_load_end handler for the FileReader above,
// so e.target.result contains an ArrayBuffer of the data // so e.target.result contains an ArrayBuffer of the data
}, },
_handle_file_drop_attach: function(
drop_file, e, res_model, res_id, extra_data
) {
_create_attachment: function(file, reader, e, res_model, res_id, extra_data) {
// helper to upload an attachment and update the sidebar // helper to upload an attachment and update the sidebar
var self = this; var self = this;
return new Model('ir.attachment').call( return new Model('ir.attachment').call(
'create', 'create',
[ [
_.extend({ _.extend({
name: drop_file.name,
name: file.name,
datas: base64js.fromByteArray( datas: base64js.fromByteArray(
new Uint8Array(e.target.result)
new Uint8Array(reader.result)
), ),
datas_fname: drop_file.name,
datas_fname: file.name,
res_model: res_model, res_model: res_model,
res_id: res_id, res_id: res_id,
}, extra_data || {}) }, extra_data || {})
@ -110,6 +104,18 @@ odoo.define('web_drop_target', function(require) {
}); });
}, },
_handle_file_drop_attach: function(
item, e, res_model, res_id, extra_data
) {
var self = this;
var file = item.getAsFile();
var reader = new FileReader();
reader.onloadend = self.proxy(
_.partial(self._create_attachment, file, reader, e, res_model, res_id, extra_data)
);
reader.readAsArrayBuffer(file);
},
_add_overlay: function() { _add_overlay: function() {
if(!this._drop_overlay){ if(!this._drop_overlay){
var o_content = jQuery('.o_content'), var o_content = jQuery('.o_content'),
@ -144,10 +150,13 @@ odoo.define('web_drop_target', function(require) {
} }
return this._super.apply(this, arguments); return this._super.apply(this, arguments);
}, },
_handle_file_drop: function(drop_file, e) {
return this._handle_file_drop_attach(
drop_file, e, this.dataset.model, this.datarecord.id
_handle_drop_items: function(drop_items, e) {
var self = this;
_.each(drop_items, function(item, e) {
return self._handle_file_drop_attach(
item, e, self.dataset.model, self.datarecord.id
); );
});
} }
})); }));

Loading…
Cancel
Save