From: Sven Schöling Date: Fri, 7 May 2021 13:21:25 +0000 (+0200) Subject: ImageUpload: client side Scripte X-Git-Tag: kivitendo-mebil_0.1-0~9^2~80 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=9129b021dfac0fa3df3c446b23deb71299171f03;p=kivitendo-erp.git ImageUpload: client side Scripte --- diff --git a/js/kivi.FileDB.js b/js/kivi.FileDB.js index 3edb45b8b..b2109bda8 100644 --- a/js/kivi.FileDB.js +++ b/js/kivi.FileDB.js @@ -15,6 +15,7 @@ namespace("kivi.FileDB", function(ns) { ns.create_image_store(event.target.result); }; request.onerror = ns.onerror; + request.aftersuccess = []; request.onsuccess = () => { db = request.result; @@ -32,6 +33,8 @@ namespace("kivi.FileDB", function(ns) { }; } } + + request.aftersuccess.forEach(f => f()); }; ns.create_image_store = function (db) { @@ -84,4 +87,13 @@ namespace("kivi.FileDB", function(ns) { console.error("Error creating/accessing IndexedDB database"); console.error(event.errorState); }; + + ns.with_db = function(success) { + if (db && db_version == db.version) { + success(); + } else { + // assume the page load db init isn't done yet and push it onto the success + request.aftersuccess.push(success); + } + }; }); diff --git a/js/kivi.ImageUpload.js b/js/kivi.ImageUpload.js index d71129924..ca5d3886b 100644 --- a/js/kivi.ImageUpload.js +++ b/js/kivi.ImageUpload.js @@ -1,6 +1,8 @@ namespace("kivi.ImageUpload", function(ns) { "use strict"; + const MAXSIZE = 5*1024*1024; // 5MB size limit + ns.add_files = function(target) { let files = []; for (var i = 0; i < target.files.length; i++) { @@ -44,7 +46,16 @@ namespace("kivi.ImageUpload", function(ns) { }); }; - ns.upload_selected_files = function(id,type,filetype,maxsize) { + ns.upload_files = function() { + let id = $('#object_id').val(); + let type = $('#object_type').val(); + + ns.upload_selected_files(id, type, MAXSIZE); + }; + + ns.upload_selected_files = function(id, type, maxsize) { + $("#upload_modal").modal("open"); + kivi.FileDB.retrieve_all((myfiles) => { let filesize = 0; myfiles.forEach(file => { @@ -55,23 +66,22 @@ namespace("kivi.ImageUpload", function(ns) { } let data = new FormData(); - data.append(file); + data.append("uploadfiles[]", file); data.append("action", "File/ajax_files_uploaded"); data.append("json", "1"); data.append("object_type", type); data.append("object_id", id); - data.append("file_type", filetype); + data.append("file_type", "attachment"); $("#upload_result").html(kivi.t8("start upload")); - $.ajax({ - url: "controller.pl", - data: data, - success: ns.attSuccess, - progress: ns.attProgress, - error: ns.attFailes, - abort: ns.attCanceled - }); + let xhr = new XMLHttpRequest; + xhr.open('POST', 'controller.pl', true); + xhr.success = ns.attSuccess; + xhr.progress = ns.attProgress; + xhr.error = ns.attFailed; + xhr.abort = ns.attCanceled; + xhr.send(data); }); }); }; @@ -98,6 +108,34 @@ namespace("kivi.ImageUpload", function(ns) { $("#upload_result").html(kivi.t8("Files have been uploaded successfully.")); }; + ns.resolve_object = function(event) { + let obj_type = $('#object_type').val(); + let number = event.target.value; + + $.ajax({ + url: "controller.pl", + data: { + action: "ImageUpload/resolve_object_by_number", + object_type: obj_type, + object_number: number + }, + type: "json", + success: (json) => { + if (json.error) { + $("#object_description").html(""); + $("#object_id").val(""); + } else { + $("#object_description").html(json.description); + $("#object_id").val(json.id); + } + }, + error: () => { + $("#object_description").html(""); + $("#object_id").val(""); + } + }); + }; + ns.init = function() { ns.reload_images(); };