From 43747accd56996e692b1226c7fef43b223b436ec Mon Sep 17 00:00:00 2001 From: j433866 Date: Wed, 8 May 2019 14:47:05 +0100 Subject: [PATCH] Change to add input tabs as inputs are added, instead of at the end. Change outputWaiter to use outputExists instead of getOutput --- src/web/InputWaiter.mjs | 62 +++++++++++++++++++++--------------- src/web/InputWorker.mjs | 24 ++++++-------- src/web/OutputWaiter.mjs | 68 +++++++++++++++++----------------------- 3 files changed, 74 insertions(+), 80 deletions(-) diff --git a/src/web/InputWaiter.mjs b/src/web/InputWaiter.mjs index cd263485..e17192c6 100644 --- a/src/web/InputWaiter.mjs +++ b/src/web/InputWaiter.mjs @@ -828,15 +828,6 @@ class InputWaiter { return newTab; } - // addTab - // UI bit can be done here - // Adding an input should be sent to the inputWorker - - // removeTab - // UI here - // remove input sent to the inputWorker - - // refreshTabs /** * Redraw the tab bar with an updated list of tabs * @@ -856,22 +847,18 @@ class InputWaiter { tabsList.appendChild(this.createTabElement(nums[i], active)); } - if (nums.length > 1) { + if (nums.length > 0) { tabsList.parentElement.style.display = "block"; document.getElementById("input-wrapper").style.height = "calc(100% - var(--tab-height) - var(--title-height))"; document.getElementById("input-highlighter").style.height = "calc(100% - var(--tab-height) - var(--title-height))"; document.getElementById("input-file").style.height = "calc(100% - var(--tab-height) - var(--title-height))"; - - document.getElementById("save-all-to-file").style.display = "inline-block"; } else { tabsList.parentElement.style.display = "none"; document.getElementById("input-wrapper").style.height = "calc(100% - var(--title-height))"; document.getElementById("input-highlighter").style.height = "calc(100% - var(--title-height))"; document.getElementById("input-file").style.height = "calc(100% - var(--title-height))"; - - document.getElementById("save-all-to-file").style.display = "none"; } this.changeTab(activeTab, this.app.options.syncTabs); @@ -1092,24 +1079,49 @@ class InputWaiter { * @param {number} inputNum */ inputAdded(changeTab, inputNum) { - if (changeTab) { - this.changeTab(inputNum, this.app.options.syncTabs); - } + this.addTab(inputNum, changeTab); this.manager.output.addOutput(inputNum, changeTab); } /** - * Handler for when the inputWorker adds multiple inputs + * Adds a new input tab. * - * @param {array} inputNums + * @param {number} inputNum + * @param {boolean} [changeTab=true] */ - addInputs(inputNums) { - const activeTab = this.getActiveTab(); - for (let i = 0; i < inputNums.length; i++) { - if (inputNums[i] === activeTab) continue; - this.manager.output.addOutput(inputNums[i], false); + addTab(inputNum, changeTab = true) { + const tabsWrapper = document.getElementById("input-tabs"); + const numTabs = tabsWrapper.children.length; + + if (!this.getTabItem(inputNum) && numTabs < this.maxTabs) { + const newTab = this.createTabElement(inputNum, false); + + tabsWrapper.appendChild(newTab); + + if (numTabs > 0) { + tabsWrapper.parentElement.style.display = "block"; + + document.getElementById("input-wrapper").style.height = "calc(100% - var(--tab-height) - var(--title-height))"; + document.getElementById("input-highlighter").style.height = "calc(100% - var(--tab-height) - var(--title-height))"; + document.getElementById("input-file").style.height = "calc(100% - var(--tab-height) - var(--title-height))"; + } else { + tabsWrapper.parentElement.style.display = "none"; + + document.getElementById("input-wrapper").style.height = "calc(100% - var(--title-height))"; + document.getElementById("input-highlighter").style.height = "calc(100% - var(--title-height))"; + document.getElementById("input-file").style.height = "calc(100% - var(--title-height))"; + } } - // this.changeTab(inputNums[inputNums.length - 1], this.app.options.syncTabs); + + if (changeTab) { + this.changeTab(inputNum, true); + } + } + + /** + * Handler for when the inputWorker adds multiple inputs + */ + addInputs() { this.inputWorker.postMessage({ action: "refreshTabs", data: { diff --git a/src/web/InputWorker.mjs b/src/web/InputWorker.mjs index e1481c08..fe93b3be 100644 --- a/src/web/InputWorker.mjs +++ b/src/web/InputWorker.mjs @@ -563,13 +563,7 @@ self.loadFiles = function(filesData) { self.activateLoaderWorker(); } - // self.refreshTabs(lastInputNum, "right"); self.getLoadProgress(); - - self.postMessage({ - action: "addInputs", - data: inputNums - }); }; /** @@ -611,15 +605,15 @@ self.addInput = function(changeTab=false, type, fileData={name: "unknown", size: } self.inputs[inputNum] = newInputObj; - if (changeTab) { - self.postMessage({ - action: "inputAdded", - data: { - changeTab: changeTab, - inputNum: inputNum - } - }); - } + + self.postMessage({ + action: "inputAdded", + data: { + changeTab: changeTab, + inputNum: inputNum + } + + }); return inputNum; }; diff --git a/src/web/OutputWaiter.mjs b/src/web/OutputWaiter.mjs index 19398fbe..236ef18c 100755 --- a/src/web/OutputWaiter.mjs +++ b/src/web/OutputWaiter.mjs @@ -76,6 +76,20 @@ class OutputWaiter { } } + /** + * Checks if an output exists in the output dictionary + * + * @param {number} inputNum + * @returns {boolean} + */ + outputExists(inputNum) { + if (this.outputs[inputNum] === undefined || + this.outputs[inputNum] === null) { + return false; + } + return true; + } + /** * Gets the output string or FileBuffer for the active input * @@ -94,11 +108,9 @@ class OutputWaiter { * @param {boolean} [changeTab=true] */ addOutput(inputNum, changeTab = true) { - const output = this.getOutput(inputNum); - if (output !== -1) { - // Remove the output if it already exists - delete this.outputs[inputNum]; - } + // Remove the output (will only get removed if it already exists) + this.removeOutput(inputNum); + const newOutput = { data: null, inputNum: inputNum, @@ -122,7 +134,7 @@ class OutputWaiter { * @param {number} inputNum */ updateOutputValue(data, inputNum) { - if (this.getOutput(inputNum) === -1) { + if (!this.outputExists(inputNum)) { this.addOutput(inputNum); } @@ -140,14 +152,14 @@ class OutputWaiter { * @param {number} inputNum */ updateOutputMessage(statusMessage, inputNum) { - if (this.getOutput(inputNum) === -1) return; + if (!this.outputExists(inputNum)) return; this.outputs[inputNum].statusMessage = statusMessage; this.set(inputNum); } /** * Updates the error value for the output in the output array. - * If this is the active output tab, calls app.handleError. + * If this is the active output tab, calls app.handleError. * Otherwise, the error will be handled when the output is switched to * * @param {Error} error @@ -155,7 +167,7 @@ class OutputWaiter { * @param {number} [progress=0] */ updateOutputError(error, inputNum, progress=0) { - if (this.getOutput(inputNum) === -1) return; + if (!this.outputExists(inputNum)) return; this.outputs[inputNum].error = error; this.outputs[inputNum].progress = progress; @@ -169,7 +181,7 @@ class OutputWaiter { * @param {number} inputNum */ updateOutputStatus(status, inputNum) { - if (this.getOutput(inputNum) === -1) return; + if (!this.outputExists(inputNum)) return; this.outputs[inputNum].status = status; if (status !== "error") { @@ -186,7 +198,7 @@ class OutputWaiter { * @param {number} inputNum */ updateOutputBakeId(bakeId, inputNum) { - if (this.getOutput(inputNum) === -1) return; + if (!this.outputExists(inputNum)) return; this.outputs[inputNum].bakeId = bakeId; } @@ -197,7 +209,7 @@ class OutputWaiter { * @param {number} inputNum */ updateOutputProgress(progress, inputNum) { - if (this.getOutput(inputNum) === -1) return; + if (!this.outputExists(inputNum)) return; this.outputs[inputNum].progress = progress; } @@ -207,7 +219,7 @@ class OutputWaiter { * @param {number} inputNum */ removeOutput(inputNum) { - if (this.getOutput(inputNum) === -1) return; + if (!this.outputExists(inputNum)) return; delete (this.outputs[inputNum]); } @@ -437,7 +449,6 @@ class OutputWaiter { outputElement.disabled = false; outputLoader.style.opacity = 0; outputLoader.style.visibility = "hidden"; - // this.setStatusMsg(""); } } @@ -579,29 +590,6 @@ class OutputWaiter { this.terminateZipWorker(); } - - /** - * Handler for download all files events. - */ - async downloadAllFilesOld() { - const fileName = window.prompt("Please enter a filename: ", "download.zip"); - const fileExt = window.prompt("Please enter a file extension for the files: ", ".txt"); - const zip = new Zlib.Zip(); - const inputNums = Object.keys(this.outputs); - for (let i = 0; i < inputNums.length; i++) { - const name = Utils.strToByteArray(inputNums[i] + fileExt); - let out = this.getOutput(inputNums[i]); - if (typeof out === "string") { - out = Utils.strToUtf8ByteArray(out); - } - out = new Uint8Array(out); - // options.filename = Utils.strToByteArray(this.outputs[i].inputNum + ".dat"); - zip.addFile(out, {filename: name}); - } - const file = new File([zip.compress()], fileName); - FileSaver.saveAs(file, fileName, false); - } - /** * Adds a new output tab. * @@ -653,8 +641,8 @@ class OutputWaiter { * @param {boolean} [changeInput = false] */ changeTab(inputNum, changeInput = false) { + if (!this.outputExists(inputNum)) return; const currentNum = this.getActiveTab(); - if (this.getOutput(inputNum) === -1) return; this.hideMagicButton(); @@ -732,7 +720,7 @@ class OutputWaiter { */ goToTab() { const tabNum = parseInt(window.prompt("Enter tab number:", this.getActiveTab().toString()), 10); - if (this.getOutput(tabNum) !== undefined) { + if (this.outputExists(tabNum)) { this.changeTab(tabNum, this.app.options.syncTabs); } } @@ -855,8 +843,8 @@ class OutputWaiter { * @param {number} inputNum */ removeTab(inputNum) { + if (!this.outputExists(inputNum)) return; let activeTab = this.getActiveTab(); - if (this.getOutput(inputNum) === -1) return; const tabElement = this.getTabItem(inputNum);