summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js
diff options
context:
space:
mode:
authorYannick Tivisse <yti@odoo.com>2017-01-09 16:34:36 +0100
committerYannick Tivisse <yti@odoo.com>2017-01-09 16:37:16 +0100
commit43fcbe331812efb17afd28a414e607923ffd59ce (patch)
tree3fe4a87d816f92f916bc2d7d412c1aa6c1f6536b /addons/web/static/src/js
parent2bfee397b54155b4784ffd42b2d5c69f3518724f (diff)
[IMP] web: allow to upload multiple files in `many2many_binary` widget
Diffstat (limited to 'addons/web/static/src/js')
-rw-r--r--addons/web/static/src/js/views/form_relational_widgets.js90
-rw-r--r--addons/web/static/src/js/widgets/sidebar.js5
2 files changed, 40 insertions, 55 deletions
diff --git a/addons/web/static/src/js/views/form_relational_widgets.js b/addons/web/static/src/js/views/form_relational_widgets.js
index a15b588a297..b95b708aa05 100644
--- a/addons/web/static/src/js/views/form_relational_widgets.js
+++ b/addons/web/static/src/js/views/form_relational_widgets.js
@@ -1534,43 +1534,27 @@ var FieldMany2ManyBinaryMultiFiles = AbstractManyField.extend(common.Reinitializ
'click .o_attach': function(e) {
this.$('.o_form_input_file').click();
},
- 'change .o_form_input_file': function(e) {
- e.stopPropagation();
-
- var $target = $(e.target);
- var value = $target.val();
-
- if(value !== '') {
- if(this.data[0] && this.data[0].upload) { // don't upload more of one file in same time
- return false;
- }
-
- var filename = value.replace(/.*[\\\/]/, '');
- for(var id in this.get('value')) {
- // if the files exits, delete the file before upload (if it's a new file)
- if(this.data[id] && (this.data[id].filename || this.data[id].name) == filename && !this.data[id].no_unlink) {
- this.ds_file.unlink([id]);
- }
- }
-
- if(this.node.attrs.blockui > 0) { // block UI or not
- framework.blockUI();
- }
+ 'change .o_form_input_file': function(event) {
+ event.stopPropagation();
+ var files = event.target.files,
+ attachments = this.get('value');
- // TODO : unactivate send on wizard and form
-
- // submit file
- this.$('form.o_form_binary_form').submit();
- this.$(".oe_fileupload").hide();
- // add file on data result
- this.data[0] = {
- id: 0,
- name: filename,
- filename: filename,
- url: '',
- upload: true,
- };
+ if(this.node.attrs.blockui){
+ framework.blockUI();
}
+ _.each(files, function(file){
+ var attachment = _.findWhere(_.values(this.data), {filename: file.name});
+ if(attachment && !attachment.no_unlink){
+ this.ds_file.unlink([attachment.id]);
+ attachments = _.without(attachments, attachment.id);
+ this.data = _.omit(this.data, attachment.id);
+ }
+ this.files_uploading.push(file);
+ }.bind(this));
+ this.set({value: attachments});
+ this.$('form.o_form_binary_form').submit();
+ this.$(".oe_fileupload").hide();
+ this.render_value();
},
'click .oe_delete': function(e) {
e.preventDefault();
@@ -1581,6 +1565,7 @@ var FieldMany2ManyBinaryMultiFiles = AbstractManyField.extend(common.Reinitializ
var files = _.without(this.get('value'), file_id);
if(!this.data[file_id].no_unlink) {
this.ds_file.unlink([file_id]);
+ this.data = _.omit(this.data, file_id);
}
this.set({'value': files});
}
@@ -1593,6 +1578,7 @@ var FieldMany2ManyBinaryMultiFiles = AbstractManyField.extend(common.Reinitializ
throw _.str.sprintf(_t("The type of the field '%s' must be a many2many field with a relation to 'ir.attachment' model."), this.field.string);
}
this.data = {};
+ this.files_uploading = [];
this.set_value([]);
this.ds_file = new data.DataSetSearch(this, 'ir.attachment');
this.fileupload_id = _.uniqueId('oe_fileupload_temp');
@@ -1628,10 +1614,6 @@ var FieldMany2ManyBinaryMultiFiles = AbstractManyField.extend(common.Reinitializ
this.read_name_values().then(function (ids) {
self.$('.oe_placeholder_files, .oe_attachments')
.replaceWith($(QWeb.render('FieldBinaryFileUploader.files', {'widget': self, 'values': ids})));
-
- // reinit input type file
- var $input = self.$('.o_form_input_file');
- $input.after($input.clone(true)).remove();
self.$(".oe_fileupload").show();
// display image thumbnail
@@ -1644,23 +1626,25 @@ var FieldMany2ManyBinaryMultiFiles = AbstractManyField.extend(common.Reinitializ
});
},
on_file_loaded: function(e, result) {
- if(this.node.attrs.blockui > 0) { // unblock UI
+ var attachments = this.get('value'),
+ files = Array.prototype.slice.call(arguments, 1);
+ this.files_uploading = []; // files has been uploaded clear uploading
+
+ if(this.node.attrs.blockui) { // unblock UI
framework.unblockUI();
}
-
- if(result.error || !result.id) {
- this.do_warn(_t('Uploading Error'), result.error);
- delete this.data[0];
- } else {
- if(this.data[0] && this.data[0].filename === result.filename && this.data[0].upload) {
- delete this.data[0];
- }
- result.url = this.get_file_url(result);
- this.data[result.id] = result;
- var values = _.clone(this.get('value'));
- values.push(result.id);
- this.set({value: values});
+ var upload_error = _.filter(files, function(attachment) {return attachment.error;});
+ if (upload_error.length) {
+ this.do_warn(_t('Uploading Error'), upload_error[0].error);
}
+ _.each(files, function(file){
+ if(!file.error){
+ attachments.push(file.id);
+ file.url = this.get_file_url(file);
+ this.data[file.id] = file;
+ }
+ }.bind(this));
+ this.set({value: _.clone(attachments)});
this.render_value();
},
});
diff --git a/addons/web/static/src/js/widgets/sidebar.js b/addons/web/static/src/js/widgets/sidebar.js
index 1e382c3d985..32513048087 100644
--- a/addons/web/static/src/js/widgets/sidebar.js
+++ b/addons/web/static/src/js/widgets/sidebar.js
@@ -170,8 +170,9 @@ var Sidebar = Widget.extend({
do_attachement_update: function(dataset, model_id, args) {
this.dataset = dataset;
this.model_id = model_id;
- if (args && args[0].error) {
- this.do_warn(_t('Uploading Error'), args[0].error);
+ var upload_error = _.filter(args, function(attachment) {return attachment.error;});
+ if (upload_error.length) {
+ this.do_warn(_t('Uploading Error'), upload_error[0].error);
}
if (!model_id) {
this.on_attachments_loaded([]);