diff --git a/src/main/webapp/resources/js/base/base.js b/src/main/webapp/resources/js/base/base.js index 8c995542..107370f3 100644 --- a/src/main/webapp/resources/js/base/base.js +++ b/src/main/webapp/resources/js/base/base.js @@ -761,63 +761,3 @@ function inputsInRange(fromSource, toSource) { function ignore() { console.log.apply(console, arguments); } - -function fileInput(conf) { - conf = conf || {}; - var name = conf.name || "upload", - tag = "" - .replace(/{name}/, name) - .replace(/{multiple}/, conf.multiple ? " multiple" : "") - .replace(/{accept}/, conf.accept ? " accept='" + conf.accept + "'" : ""), - obj = { - name:name, - input:null, - files:[], - - select:function(onSelect) { - if (!obj.input) { - var input = obj.input = $(tag); - $("body").append(input); - input.change(function(){ - var files = $(this).get(0).files, - length = files.length, - selected = []; - for (var i = 0; i < length; ++i) { - var file = files[i]; - file.id = name + (obj.files.length); - selected.push(file); - obj.files.push(file); - } - onSelect(selected); - }) - } - obj.input.click(); - }, - - getFiles:function() { - return obj.files; - }, - - remove:function(id){ - if (!id) return; - - var files = obj.files; - for (var i = 0; i < files.length; ++i) { - var file = files[i]; - if (id == file.id) { - files.splice(i, 1); - break; - } - } - }, - - clear:function(){ - if (obj.input) - $(obj.input).remove(); - obj.input = null; - obj.files.forEach(function(file){delete file;}); - obj.files = []; - } - }; - return obj; -} diff --git a/src/main/webapp/resources/js/base/upload-support.js b/src/main/webapp/resources/js/base/upload-support.js new file mode 100644 index 00000000..0340eb87 --- /dev/null +++ b/src/main/webapp/resources/js/base/upload-support.js @@ -0,0 +1,23 @@ +function uploadSupport(selector) { + let input = $(selector); + if (!input) + throw "Element not found: " + selector; + + let fileset = new Dataset({ + keymapper: info => info.id + }); + + input.change(function() { + var files = $(this).get(0).files, + length = files.length, + array = [] + for (var i = 0; i < length; ++i) { + var file = files[i]; + file.id = "file-" + new Date().getTime() + "-" + i; + file.url = (window.URL || window.webkitURL).createObjectURL(file); + array.push(file); + } + fileset.setData(array); + }); + return fileset; +} \ No newline at end of file