Skip to content

Commit f4e9be4

Browse files
committed
Manually pulled in changes from espruino/EspruinoAppLoaderCore#80
1 parent c467340 commit f4e9be4

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

core/utils.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,9 @@ while (d!==undefined) {console.log(btoa(d));d=f.read(${CHUNKSIZE});}
701701
* @param {'text' | 'arraybuffer'} options.type (default 'text') Callback with either 'text' or 'arraybuffer'
702702
* @param {string | undefined} options.mimeType Optional comma-separated list of accepted mime types for files or extensions (eg. ".js,application/javascript")
703703
* @param {(contents: ArrayBuffer | string, mimeType: string, fileName: string) => void} callback
704+
* @param {(files: Array<{contents: (ArrayBuffer|string), mimeType: string, fileName: string}>) => void | undefined} options.onComplete
705+
* Optional callback returns all files as an array of {contents, mimeType, fileName}. Called with `undefined` if the dialog is cancelled.
706+
* @param {(contents: ArrayBuffer | string, mimeType: string, fileName: string) => void} callback Called for each file. Called with `undefined` if the dialog is cancelled.
704707
*/
705708
function fileOpenDialog(options, callback) {
706709
function readerLoaded(e,files,i,options,fileLoader) {
@@ -712,17 +715,30 @@ while (d!==undefined) {console.log(btoa(d));d=f.read(${CHUNKSIZE});}
712715
result = "";
713716
for (var j=0;j<a.length;j++)
714717
result += String.fromCharCode(a[j]);
715-
} else
718+
} else {
716719
result = e.target.result;
717-
fileLoader.callback(result, files[i].type, files[i].name);
720+
}
721+
722+
fileLoader._filesResult.push({ contents: result, mimeType: files[i].type, fileName: files[i].name });
723+
724+
if (fileLoader.callback) {
725+
fileLoader.callback(result, files[i].type, files[i].name);
726+
}
718727

719728

720729
// If there's a file left to load
721730
if (i < files.length - 1 && options.multi) {
722731
// Load the next file
723732
setupReader(files, i+1,options,fileLoader);
724733
} else {
734+
// All files processed
735+
if (fileLoader.onComplete) {
736+
var results = fileLoader._filesResult || [];
737+
fileLoader.onComplete(results);
738+
}
739+
fileLoader._filesResult = undefined;
725740
fileLoader.callback = undefined;
741+
fileLoader.onComplete = undefined;
726742
}
727743
}
728744

@@ -753,15 +769,24 @@ while (d!==undefined) {console.log(btoa(d));d=f.read(${CHUNKSIZE});}
753769
e.target.value = ''; // handle repeated upload of the same file
754770
});
755771
fileLoader.addEventListener('change', function(e) {
756-
if (!fileLoader.callback) return;
772+
if (!fileLoader.callback && !fileLoader.onComplete) return;
757773

758774
var files = e.target.files;
759775
setupReader(files,0,options,fileLoader);
760776

761777
}, false);
778+
fileLoader.addEventListener('cancel', function(e) {
779+
if (fileLoader.callback) fileLoader.callback();
780+
if (fileLoader.onComplete) fileLoader.onComplete();
781+
fileLoader._filesResult = undefined;
782+
fileLoader.callback = undefined;
783+
fileLoader.onComplete = undefined;
784+
}, false);
762785
document.body.appendChild(fileLoader);
763786
}
787+
fileLoader._filesResult = [];
764788
fileLoader.callback = callback;
789+
fileLoader.onComplete = options.onComplete;
765790
fileLoader.click();
766791
}
767792

0 commit comments

Comments
 (0)