2022-09-18 00:00:06 -06:00
|
|
|
// code related to showing and updating progressbar shown as the image is being made
|
2022-11-02 00:47:53 -06:00
|
|
|
|
2023-04-30 13:12:24 -06:00
|
|
|
function rememberGallerySelection() {
|
2022-09-23 11:46:02 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-30 13:12:24 -06:00
|
|
|
function getGallerySelectedIndex() {
|
2022-11-29 18:11:01 -07:00
|
|
|
|
2022-10-14 12:05:14 -06:00
|
|
|
}
|
|
|
|
|
2023-01-15 08:50:56 -07:00
|
|
|
function request(url, data, handler, errorHandler) {
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("POST", url, true);
|
|
|
|
xhr.setRequestHeader("Content-Type", "application/json");
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
if (xhr.status === 200) {
|
2023-01-19 05:58:32 -07:00
|
|
|
try {
|
|
|
|
var js = JSON.parse(xhr.responseText);
|
|
|
|
handler(js);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
errorHandler();
|
|
|
|
}
|
2023-01-15 08:50:56 -07:00
|
|
|
} else {
|
|
|
|
errorHandler();
|
|
|
|
}
|
2022-10-04 21:56:30 -06:00
|
|
|
}
|
2023-01-15 08:50:56 -07:00
|
|
|
};
|
|
|
|
var js = JSON.stringify(data);
|
|
|
|
xhr.send(js);
|
|
|
|
}
|
|
|
|
|
|
|
|
function pad2(x) {
|
|
|
|
return x < 10 ? '0' + x : x;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatTime(secs) {
|
|
|
|
if (secs > 3600) {
|
|
|
|
return pad2(Math.floor(secs / 60 / 60)) + ":" + pad2(Math.floor(secs / 60) % 60) + ":" + pad2(Math.floor(secs) % 60);
|
|
|
|
} else if (secs > 60) {
|
|
|
|
return pad2(Math.floor(secs / 60)) + ":" + pad2(Math.floor(secs) % 60);
|
|
|
|
} else {
|
|
|
|
return Math.floor(secs) + "s";
|
2022-09-20 12:05:25 -06:00
|
|
|
}
|
2022-09-18 00:00:06 -06:00
|
|
|
}
|
2022-09-18 02:14:42 -06:00
|
|
|
|
2024-02-14 04:26:57 -07:00
|
|
|
|
|
|
|
var originalAppTitle = undefined;
|
|
|
|
|
|
|
|
onUiLoaded(function() {
|
|
|
|
originalAppTitle = document.title;
|
|
|
|
});
|
|
|
|
|
2023-01-17 20:13:45 -07:00
|
|
|
function setTitle(progress) {
|
2024-02-14 04:26:57 -07:00
|
|
|
var title = originalAppTitle;
|
2023-01-17 20:13:45 -07:00
|
|
|
|
|
|
|
if (opts.show_progress_in_title && progress) {
|
|
|
|
title = '[' + progress.trim() + '] ' + title;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (document.title != title) {
|
2023-05-18 00:59:10 -06:00
|
|
|
document.title = title;
|
2023-01-17 20:13:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-15 08:50:56 -07:00
|
|
|
function randomId() {
|
|
|
|
return "task(" + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7) + Math.random().toString(36).slice(2, 7) + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
// starts sending progress requests to "/internal/progress" uri, creating progressbar above progressbarContainer element and
|
|
|
|
// preview inside gallery element. Cleans up all created stuff when the task is over and calls atEnd.
|
|
|
|
// calls onProgress every time there is a progress update
|
2023-04-29 13:16:54 -06:00
|
|
|
function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgress, inactivityTimeout = 40) {
|
2023-01-15 08:50:56 -07:00
|
|
|
var dateStart = new Date();
|
|
|
|
var wasEverActive = false;
|
|
|
|
var parentProgressbar = progressbarContainer.parentNode;
|
2024-06-11 17:50:00 -06:00
|
|
|
var wakeLock = null;
|
|
|
|
|
|
|
|
var requestWakeLock = async function() {
|
2024-06-11 19:14:32 -06:00
|
|
|
if (!opts.prevent_screen_sleep_during_generation || wakeLock) return;
|
2024-06-11 17:50:00 -06:00
|
|
|
try {
|
|
|
|
wakeLock = await navigator.wakeLock.request('screen');
|
|
|
|
} catch (err) {
|
2024-06-11 23:33:11 -06:00
|
|
|
console.error('Wake Lock is not supported.');
|
2024-06-11 17:50:00 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var releaseWakeLock = async function() {
|
|
|
|
if (!opts.prevent_screen_sleep_during_generation || !wakeLock) return;
|
|
|
|
try {
|
|
|
|
await wakeLock.release();
|
|
|
|
wakeLock = null;
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Wake Lock release failed', err);
|
|
|
|
}
|
|
|
|
};
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-01-15 08:50:56 -07:00
|
|
|
var divProgress = document.createElement('div');
|
|
|
|
divProgress.className = 'progressDiv';
|
2023-02-25 10:56:03 -07:00
|
|
|
divProgress.style.display = opts.show_progressbar ? "block" : "none";
|
2023-01-15 08:50:56 -07:00
|
|
|
var divInner = document.createElement('div');
|
|
|
|
divInner.className = 'progress';
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-01-15 08:50:56 -07:00
|
|
|
divProgress.appendChild(divInner);
|
|
|
|
parentProgressbar.insertBefore(divProgress, progressbarContainer);
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-08-22 02:02:14 -06:00
|
|
|
var livePreview = null;
|
2023-01-15 08:50:56 -07:00
|
|
|
|
|
|
|
var removeProgressBar = function() {
|
2024-06-11 17:50:00 -06:00
|
|
|
releaseWakeLock();
|
2023-08-22 04:57:05 -06:00
|
|
|
if (!divProgress) return;
|
2023-08-22 02:02:14 -06:00
|
|
|
|
2023-01-17 20:13:45 -07:00
|
|
|
setTitle("");
|
2023-01-15 08:50:56 -07:00
|
|
|
parentProgressbar.removeChild(divProgress);
|
2023-08-22 04:54:45 -06:00
|
|
|
if (gallery && livePreview) gallery.removeChild(livePreview);
|
2023-01-15 08:50:56 -07:00
|
|
|
atEnd();
|
2023-08-22 02:02:14 -06:00
|
|
|
|
|
|
|
divProgress = null;
|
2023-01-15 08:50:56 -07:00
|
|
|
};
|
|
|
|
|
2023-08-20 04:00:59 -06:00
|
|
|
var funProgress = function(id_task) {
|
2024-06-11 17:50:00 -06:00
|
|
|
requestWakeLock();
|
2023-08-20 04:00:59 -06:00
|
|
|
request("./internal/progress", {id_task: id_task, live_preview: false}, function(res) {
|
2023-01-15 08:50:56 -07:00
|
|
|
if (res.completed) {
|
|
|
|
removeProgressBar();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-30 13:08:52 -06:00
|
|
|
let progressText = "";
|
2023-01-15 08:50:56 -07:00
|
|
|
|
|
|
|
divInner.style.width = ((res.progress || 0) * 100.0) + '%';
|
2023-01-18 03:20:47 -07:00
|
|
|
divInner.style.background = res.progress ? "" : "transparent";
|
2023-01-15 08:50:56 -07:00
|
|
|
|
|
|
|
if (res.progress > 0) {
|
|
|
|
progressText = ((res.progress || 0) * 100.0).toFixed(0) + '%';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res.eta) {
|
|
|
|
progressText += " ETA: " + formatTime(res.eta);
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:13:45 -07:00
|
|
|
setTitle(progressText);
|
2023-01-18 03:20:47 -07:00
|
|
|
|
|
|
|
if (res.textinfo && res.textinfo.indexOf("\n") == -1) {
|
|
|
|
progressText = res.textinfo + " " + progressText;
|
|
|
|
}
|
|
|
|
|
2023-01-15 08:50:56 -07:00
|
|
|
divInner.textContent = progressText;
|
|
|
|
|
|
|
|
var elapsedFromStart = (new Date() - dateStart) / 1000;
|
|
|
|
|
|
|
|
if (res.active) wasEverActive = true;
|
|
|
|
|
|
|
|
if (!res.active && wasEverActive) {
|
|
|
|
removeProgressBar();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-29 13:16:54 -06:00
|
|
|
if (elapsedFromStart > inactivityTimeout && !res.queued && !res.active) {
|
2023-01-15 08:50:56 -07:00
|
|
|
removeProgressBar();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-20 04:00:59 -06:00
|
|
|
if (onProgress) {
|
|
|
|
onProgress(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
funProgress(id_task, res.id_live_preview);
|
|
|
|
}, opts.live_preview_refresh_period || 500);
|
|
|
|
}, function() {
|
|
|
|
removeProgressBar();
|
|
|
|
});
|
2023-08-20 04:38:35 -06:00
|
|
|
};
|
2023-01-15 08:50:56 -07:00
|
|
|
|
2023-08-20 04:00:59 -06:00
|
|
|
var funLivePreview = function(id_task, id_live_preview) {
|
|
|
|
request("./internal/progress", {id_task: id_task, id_live_preview: id_live_preview}, function(res) {
|
2023-08-22 12:05:25 -06:00
|
|
|
if (!divProgress) {
|
|
|
|
return;
|
|
|
|
}
|
2023-08-22 02:02:14 -06:00
|
|
|
|
2023-08-22 12:05:25 -06:00
|
|
|
if (res.live_preview && gallery) {
|
2023-01-15 08:50:56 -07:00
|
|
|
var img = new Image();
|
|
|
|
img.onload = function() {
|
2023-08-22 04:57:05 -06:00
|
|
|
if (!livePreview) {
|
2023-08-22 02:02:14 -06:00
|
|
|
livePreview = document.createElement('div');
|
|
|
|
livePreview.className = 'livePreview';
|
|
|
|
gallery.insertBefore(livePreview, gallery.firstElementChild);
|
|
|
|
}
|
|
|
|
|
2023-01-15 08:50:56 -07:00
|
|
|
livePreview.appendChild(img);
|
|
|
|
if (livePreview.childElementCount > 2) {
|
|
|
|
livePreview.removeChild(livePreview.firstElementChild);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
img.src = res.live_preview;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2023-08-20 04:00:59 -06:00
|
|
|
funLivePreview(id_task, res.id_live_preview);
|
2023-01-15 10:27:39 -07:00
|
|
|
}, opts.live_preview_refresh_period || 500);
|
2023-01-15 08:50:56 -07:00
|
|
|
}, function() {
|
|
|
|
removeProgressBar();
|
|
|
|
});
|
|
|
|
};
|
2022-09-18 02:14:42 -06:00
|
|
|
|
2023-08-20 04:00:59 -06:00
|
|
|
funProgress(id_task, 0);
|
2023-08-22 02:02:14 -06:00
|
|
|
|
2023-08-22 04:57:05 -06:00
|
|
|
if (gallery) {
|
2023-08-22 02:02:14 -06:00
|
|
|
funLivePreview(id_task, 0);
|
|
|
|
}
|
|
|
|
|
2022-09-18 02:14:42 -06:00
|
|
|
}
|