diff --git a/CHANGELOG b/CHANGELOG index 8c5489e36f..788d0f8989 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Add drag & drop support on attachment button + Version 5.0.0 - 2018-10-01 * Bug fixes (see mercurial logs for details) * Prevent similar tab diff --git a/src/common.js b/src/common.js index 31fba74beb..65d3d57ee5 100644 --- a/src/common.js +++ b/src/common.js @@ -3448,22 +3448,23 @@ }; Sao.common.get_input_data = function(input, callback, char_) { - function read(file) { - var reader = new FileReader(); - reader.onload = function() { - var value = new Uint8Array(reader.result); - if (char_) { - value = String.fromCharCode.apply(null, value); - } - callback(value, file.name); - }; - reader.readAsArrayBuffer(file); - } for (var i = 0; i < input[0].files.length; i++) { - read(input[0].files[i]); + Sao.common.get_file_data(input[0].files[i], callback, char_); } }; + Sao.common.get_file_data = function(file, callback, char_) { + var reader = new FileReader(); + reader.onload = function() { + var value = new Uint8Array(reader.result); + if (char_) { + value = String.fromCharCode.apply(null, value); + } + callback(value, file.name); + }; + reader.readAsArrayBuffer(file); + }; + Sao.common.ellipsize = function(string, length) { if (string.length <= length) { return string; diff --git a/src/tab.js b/src/tab.js index a0fe5da602..2e0d33d6e7 100644 --- a/src/tab.js +++ b/src/tab.js @@ -631,6 +631,9 @@ }.bind(this)); }.bind(this)); }.bind(this)); + this.buttons.attach + .on('dragover', false) + .on('drop', this.attach_drop.bind(this)); return toolbar; }, compare: function(attributes) { @@ -1023,6 +1026,81 @@ }))); }); }, + attach_drop: function(evt) { + evt.preventDefault(); + evt.stopPropagation(); + evt = evt.originalEvent; + var record = this.screen.current_record; + if (!record || record.id < 0) { + return; + } + + var i, file; + var files = [], + uris = [], + texts = []; + if (evt.dataTransfer.items) { + console.log(evt.dataTransfer.items); + for (i = 0; i < evt.dataTransfer.items.length; i++) { + var item = evt.dataTransfer.items[i]; + if (item.kind == 'string') { + var list; + if (item.type == 'text/uri-list') { + list = uris; + } else if (item.type == 'text/plain') { + list = texts; + } else { + continue; + } + var prm = jQuery.Deferred(); + evt.dataTransfer.items[i].getAsString(prm.resolve); + list.push(prm); + break; + } else { + file = evt.dataTransfer.items[i].getAsFile(); + if (file) { + files.push(file); + } + } + } + } else { + for (i = 0; i < evt.dataTransfer.files.length; i++) { + file = evt.dataTransfer.files[i]; + if (file) { + files.push(file); + } + } + } + + var window_ = new Sao.Window.Attachment(record, function() { + this.update_attachment_count(true); + }.bind(this)); + files.forEach(function(file) { + Sao.common.get_file_data(file, function(data, filename) { + window_.add_data(data, filename); + }); + }); + jQuery.when.apply(jQuery, uris).then(function() { + function empty(value) { + return Boolean(value); + } + for (var i = 0; i < arguments.length; i++) { + arguments[i].split('\r\n') + .filter(empty) + .forEach(window_.add_uri, window_); + } + }); + jQuery.when.apply(jQuery, texts).then(function() { + for (var i = 0; i < arguments.length; i++) { + window_.add_text(arguments[i]); + } + }); + if (evt.dataTransfer.items) { + evt.dataTransfer.items.clear(); + } else { + evt.dataTransfer.clearData(); + } + }, update_attachment_count: function(reload) { var record = this.screen.current_record; if (record) { diff --git a/src/window.js b/src/window.js index ab89c30914..0cd47967cc 100644 --- a/src/window.js +++ b/src/window.js @@ -452,6 +452,31 @@ }); }); }, + add_uri: function(uri) { + var screen = this.screen; + this.switch_prm.then(function() { + screen.set_current_record(null); + screen.switch_view('form').then(function() { + screen.new_().then(function(record) { + record.field_set_client('link', uri); + record.field_set_client('type', 'link'); + screen.display(); + }); + }); + }); + }, + add_text: function(text) { + var screen = this.screen; + this.switch_prm.then(function() { + screen.set_current_record(null); + screen.switch_view('form').then(function() { + screen.new_().then(function(record) { + record.field_set_client('description', text); + screen.display(); + }); + }); + }); + }, }); Sao.Window.Attachment.get_attachments = function(record) { var prm;