mirror of https://github.com/OCA/web.git
Make _handle_drop_items extensible to prevent multiple rpc calls
parent
c3467d9787
commit
2cd4fd5fdf
|
@ -81,11 +81,8 @@ Contributors
|
||||||
~~~~~~~~~~~~
|
~~~~~~~~~~~~
|
||||||
|
|
||||||
* Holger Brunn <hbrunn@therp.nl>
|
* Holger Brunn <hbrunn@therp.nl>
|
||||||
<<<<<<< HEAD
|
|
||||||
* Akim Juillerat <akim.juillerat@camptocamp.com>
|
* Akim Juillerat <akim.juillerat@camptocamp.com>
|
||||||
=======
|
|
||||||
* Pablo Fuentes <pablo@studio73.es>
|
* Pablo Fuentes <pablo@studio73.es>
|
||||||
>>>>>>> 25dd8787... [IMP] web_drop_target: Upload multiple files
|
|
||||||
|
|
||||||
Maintainers
|
Maintainers
|
||||||
~~~~~~~~~~~
|
~~~~~~~~~~~
|
||||||
|
|
|
@ -26,21 +26,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) {
|
if(!drop_items) {
|
||||||
var reader = new FileReader();
|
return;
|
||||||
reader.onloadend = self.proxy(
|
}
|
||||||
_.partial(self._handle_file_drop, drop_item.getAsFile())
|
|
||||||
);
|
|
||||||
reader.readAsArrayBuffer(drop_item.getAsFile());
|
|
||||||
});
|
|
||||||
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;
|
||||||
|
@ -68,29 +64,29 @@ 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(
|
_create_attachment: function(file, reader, e, res_model, res_id, extra_data) {
|
||||||
drop_file, 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 this._rpc({
|
return this._rpc({
|
||||||
model: 'ir.attachment',
|
model: 'ir.attachment',
|
||||||
method: 'create',
|
method: 'create',
|
||||||
args: [{
|
args: [
|
||||||
'name': drop_file.name,
|
_.extend({
|
||||||
'datas': base64js.fromByteArray(
|
name: file.name,
|
||||||
new Uint8Array(e.target.result)
|
datas: base64js.fromByteArray(
|
||||||
|
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 || {})
|
||||||
|
],
|
||||||
})
|
})
|
||||||
.then(function() {
|
.then(function() {
|
||||||
// try to find a sidebar and update it if we found one
|
// try to find a sidebar and update it if we found one
|
||||||
|
@ -107,6 +103,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'),
|
||||||
|
@ -141,13 +149,18 @@ 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) {
|
_handle_drop_items: function(drop_items, e) {
|
||||||
return this._handle_file_drop_attach(
|
var self = this;
|
||||||
drop_file, e, this.renderer.state.model, this.renderer.state.res_id
|
_.each(drop_items, function(item, e) {
|
||||||
);
|
return self._handle_file_drop_attach(
|
||||||
|
item, e, self.renderer.state.model,
|
||||||
|
self.renderer.state.res_id
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'DropTargetMixin': DropTargetMixin,
|
'DropTargetMixin': DropTargetMixin,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue