mirror of https://github.com/OCA/web.git
Make _handle_drop_items extensible to prevent multiple rpc calls
parent
d12de11581
commit
3970390da0
|
@ -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) {
|
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;
|
||||||
|
@ -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(
|
_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 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) {
|
_handle_drop_items: function(drop_items, e) {
|
||||||
return this._handle_file_drop_attach(
|
var self = this;
|
||||||
drop_file, e, this.dataset.model, this.datarecord.id
|
_.each(drop_items, function(item, e) {
|
||||||
|
return self._handle_file_drop_attach(
|
||||||
|
item, e, self.dataset.model, self.datarecord.id
|
||||||
);
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue