Skip to content

Commit

Permalink
show image faster, open url
Browse files Browse the repository at this point in the history
  • Loading branch information
strukturart committed Jan 13, 2024
1 parent 1a6d648 commit a4c4ccc
Showing 1 changed file with 91 additions and 16 deletions.
107 changes: 91 additions & 16 deletions application/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ let debug = false;
let filter_query;
let file_content = [];
let current_file;
let files = [];
let files = JSON.parse(localStorage.getItem("files")) || [];
console.log("start:", files);

let action = null;
let action_element = null;
let selected_image;
Expand All @@ -27,6 +29,17 @@ if (debug) {
};
}

let save_files = () => {
localStorage.setItem("files", JSON.stringify(files));
};

let url_test = (string) => {
// Regular expression for a basic URL validation
const urlRegex = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/i;

return urlRegex.test(string);
};

let set_tabindex = () => {
document
.querySelectorAll('.item:not([style*="display: none"]')
Expand Down Expand Up @@ -112,17 +125,27 @@ try {
});
} catch (e) {}

let filename_list = [];
//list files
let read_files = (callback) => {
files = [];
//files = [];
try {
var d = navigator.getDeviceStorage("sdcard");

var cursor = d.enumerate();

cursor.onsuccess = function () {
if (!this.result) {
for (let i = 0; i < files.length; i++) {
if (filename_list.indexOf(files[i].path) == -1) {
// Remove the element from the array
files.splice(i, 1);
}
}
save_files();

m.route.set("/start");

callback();
}
if (cursor.result.name !== null) {
Expand All @@ -136,12 +159,24 @@ let read_files = (callback) => {
file.name.includes("/passport/") &&
!file.name.includes("/sdcard/.")
) {
files.push({
"path": file.name,
"name": file_name,
"file": f,
"type": type[type.length - 1],
filename_list.push(file.name);
let fileExists = false;
files.forEach((e) => {
if (e.path == file.name) {
fileExists = true;
e.file = f;
}
});

if (!fileExists) {
files.push({
"path": file.name,
"name": file_name,
"file": f,
"type": type[type.length - 1],
"qr": true,
});
}
}
this.continue();
}
Expand Down Expand Up @@ -176,12 +211,23 @@ let read_files = (callback) => {
file.value.name.includes("/passport/") &&
!file.value.name.includes("/sdcard/.")
) {
files.push({
path: file.value.name,
file: f,
type: file_name.split(".").pop(),
name: file_name,
let fileExists = false;
files.forEach((e) => {
if (e.path == file.name) {
fileExists = true;
e.file = f;
}
});

if (!fileExists) {
files.push({
"path": file.name,
"name": file_name,
"file": f,
"type": type[type.length - 1],
"qr": true,
});
}
}

next(_files);
Expand Down Expand Up @@ -228,6 +274,14 @@ let load_qrcode_content = (blobUrl) => {
qrcode_content = "";
helper.bottom_bar("", "", "");
document.querySelector(".loading-spinner").style.display = "none";

files.forEach((e) => {
if (e.path == selected_image_url) {
e.qr = false;
}
});

save_files();
}
};

Expand Down Expand Up @@ -339,7 +393,7 @@ function write_file(data, filename, filetype) {
var request = sdcard.addNamed(file, filename);

request.onsuccess = function () {
files = [];
//files = [];
read_files();
startup = false;
m.route.set("/start?focus=" + filename);
Expand Down Expand Up @@ -523,12 +577,25 @@ document.addEventListener("DOMContentLoaded", function () {
m("img", {
src: selected_image,
id: "image",
oninit: () => {},
oninit: () => {
console.log(selected_image_url);
},
oncreate: () => {
qrcode_content = "";
helper.bottom_bar("", "", "");
try {
load_qrcode_content(selected_image);
files.forEach((e) => {
if (e.path == selected_image_url) {
if (e.qr) {
load_qrcode_content(selected_image);
} else {
qrcode_content = "";
helper.bottom_bar("", "", "");
document.querySelector(".loading-spinner").style.display =
"none";
}
}
});
} catch (e) {
document.querySelector(".loading-spinner").style.display = "none";
}
Expand Down Expand Up @@ -567,6 +634,10 @@ document.addEventListener("DOMContentLoaded", function () {
if (status == "after_scan") {
helper.bottom_bar("", "<img src='assets/images/save.svg'>", "");
}

if (url_test(qrcode_content)) {
helper.bottom_bar("j", "", "");
}
},
},
qrcode_content
Expand Down Expand Up @@ -632,7 +703,7 @@ document.addEventListener("DOMContentLoaded", function () {
let cb = () => {
m.route.set("/start?focus=+/sdcard/passport/" + filename);
};

files = [];
read_files(cb);
};

Expand Down Expand Up @@ -776,6 +847,10 @@ document.addEventListener("DOMContentLoaded", function () {
break;
}

if (m.route.get().includes("/show_qr_content")) {
if (url_test(qrcode_content)) window.open(qrcode_content);
}

if (m.route.get().includes("/start")) {
if (general.fileAction) {
general.blocker = true;
Expand Down

0 comments on commit a4c4ccc

Please sign in to comment.