2018-05-15 19:36:45 +02:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
2019-03-29 14:29:24 +01:00
|
|
|
* @author j433866 [j433866@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2019
|
2018-05-15 19:36:45 +02:00
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import LoaderWorker from "worker-loader?inline&fallback=false!./LoaderWorker";
|
|
|
|
import Utils from "../core/Utils";
|
2019-03-19 15:38:46 +01:00
|
|
|
import { toBase64 } from "../core/lib/Base64";
|
|
|
|
import { isImage } from "../core/lib/FileType";
|
2018-05-15 19:36:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Waiter to handle events related to the input.
|
|
|
|
*/
|
|
|
|
class InputWaiter {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* InputWaiter constructor.
|
|
|
|
*
|
2019-03-29 14:29:24 +01:00
|
|
|
* @param {App} app - The main view object for CyberChef
|
|
|
|
* @param {Manager} manager- The CyberChef event manager.
|
2018-05-15 19:36:45 +02:00
|
|
|
*/
|
|
|
|
constructor(app, manager) {
|
|
|
|
this.app = app;
|
|
|
|
this.manager = manager;
|
|
|
|
|
|
|
|
// Define keys that don't change the input so we don't have to autobake when they are pressed
|
|
|
|
this.badKeys = [
|
|
|
|
16, //Shift
|
|
|
|
17, //Ctrl
|
|
|
|
18, //Alt
|
|
|
|
19, //Pause
|
|
|
|
20, //Caps
|
|
|
|
27, //Esc
|
|
|
|
33, 34, 35, 36, //PgUp, PgDn, End, Home
|
|
|
|
37, 38, 39, 40, //Directional
|
|
|
|
44, //PrntScrn
|
|
|
|
91, 92, //Win
|
|
|
|
93, //Context
|
|
|
|
112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, //F1-12
|
|
|
|
144, //Num
|
|
|
|
145, //Scroll
|
|
|
|
];
|
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
this.loaderWorkers = [];
|
|
|
|
this.maxWorkers = navigator.hardwareConcurrency || 4;
|
|
|
|
this.inputs = [];
|
|
|
|
this.pendingFiles = [];
|
|
|
|
this.maxTabs = 4; // Calculate this
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
/**
|
|
|
|
* Terminates any existing loader workers and sets up a new worker
|
|
|
|
*/
|
|
|
|
setupLoaderWorker() {
|
|
|
|
for (let i = 0; i < this.loaderWorkers.length; i++) {
|
|
|
|
const worker = this.loaderWorkers.pop();
|
|
|
|
worker.terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addLoaderWorker();
|
|
|
|
}
|
2018-05-15 19:36:45 +02:00
|
|
|
|
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Adds a new loaderWorker
|
2018-05-15 19:36:45 +02:00
|
|
|
*
|
2019-03-29 14:29:24 +01:00
|
|
|
* @returns {number} The index of the created worker
|
2018-05-15 19:36:45 +02:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
addLoaderWorker() {
|
|
|
|
for (let i = 0; i < this.loaderWorkers.length; i++) {
|
|
|
|
if (!this.loaderWorkers[i].active) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.loaderWorkers.length === this.maxWorkers) {
|
|
|
|
return -1;
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
log.debug("Adding new LoaderWorker.");
|
|
|
|
const newWorker = new LoaderWorker();
|
|
|
|
newWorker.addEventListener("message", this.handleLoaderMessage.bind(this));
|
|
|
|
const newWorkerObj = {
|
|
|
|
worker: newWorker,
|
|
|
|
active: false,
|
|
|
|
inputNum: 0
|
|
|
|
};
|
|
|
|
this.loaderWorkers.push(newWorkerObj);
|
|
|
|
return this.loaderWorkers.indexOf(newWorkerObj);
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
2019-03-27 10:05:10 +01:00
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Removes a loaderworker using inputNum
|
2019-03-27 10:05:10 +01:00
|
|
|
*
|
2019-03-29 14:29:24 +01:00
|
|
|
* @param {Object} workerObj
|
2019-03-27 10:05:10 +01:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
removeLoaderWorker(workerObj) {
|
|
|
|
const idx = this.loaderWorkers.indexOf(workerObj);
|
|
|
|
if (idx === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.loaderWorkers[idx].worker.terminate();
|
|
|
|
this.loaderWorkers.splice(idx, 1);
|
|
|
|
if (this.loaderWorkers.length === 0) {
|
|
|
|
// There should always be 1 loaderworker loaded
|
|
|
|
this.addLoaderWorker();
|
|
|
|
}
|
|
|
|
}
|
2019-03-27 10:05:10 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
/**
|
|
|
|
* Finds and returns the object for the loaderWorker of a given inputNum
|
|
|
|
*
|
|
|
|
* @param {number} inputNum
|
|
|
|
*/
|
|
|
|
getLoaderWorker(inputNum) {
|
|
|
|
for (let i = 0; i < this.loaderWorkers.length; i++) {
|
|
|
|
if (this.loaderWorkers[i].inputNum === inputNum) {
|
|
|
|
return this.loaderWorkers[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a file into the input
|
|
|
|
*
|
|
|
|
* @param {File} file
|
|
|
|
* @param {number} inputNum
|
|
|
|
*/
|
|
|
|
loadFile(file, inputNum) {
|
|
|
|
if (file && inputNum) {
|
|
|
|
this.closeFile(this.getLoaderWorker(inputNum));
|
|
|
|
let loaded = false;
|
|
|
|
|
|
|
|
const workerId = this.addLoaderWorker();
|
|
|
|
if (workerId !== -1) {
|
|
|
|
this.loaderWorkers[workerId].active = true;
|
|
|
|
this.loaderWorkers[workerId].inputNum = inputNum;
|
|
|
|
this.loaderWorkers[workerId].worker.postMessage({
|
|
|
|
file: file,
|
|
|
|
inputNum: inputNum
|
2019-03-27 10:05:10 +01:00
|
|
|
});
|
2019-03-29 14:29:24 +01:00
|
|
|
loaded = true;
|
2019-03-27 10:05:10 +01:00
|
|
|
} else {
|
2019-03-29 14:29:24 +01:00
|
|
|
this.pendingFiles.push({
|
|
|
|
file: file,
|
|
|
|
inputNum: inputNum
|
2019-03-27 10:05:10 +01:00
|
|
|
});
|
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
if (this.getInput(inputNum) !== null) {
|
|
|
|
this.removeInput(inputNum);
|
|
|
|
}
|
|
|
|
this.inputs.push({
|
|
|
|
inputNum: inputNum,
|
|
|
|
data: {
|
|
|
|
fileBuffer: new ArrayBuffer(),
|
|
|
|
name: file.name,
|
|
|
|
size: file.size.toLocaleString(),
|
|
|
|
type: file.type || "unknown"
|
|
|
|
},
|
|
|
|
status: (loaded) ? "loading" : "pending",
|
|
|
|
progress: 0
|
2019-03-27 14:48:54 +01:00
|
|
|
});
|
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
}
|
2019-03-27 14:48:54 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
/**
|
|
|
|
* Closes a file and removes it from inputs
|
|
|
|
*
|
|
|
|
* @param {number} inputNum
|
|
|
|
*/
|
|
|
|
closeFile(inputNum) {
|
|
|
|
this.removeLoaderWorker(this.getLoaderWorker(inputNum));
|
|
|
|
this.removeInput(inputNum);
|
|
|
|
|
|
|
|
if (inputNum === this.getActiveTab()) {
|
|
|
|
const fileOverlay = document.getElementById("input-file"),
|
|
|
|
fileName = document.getElementById("input-file-name"),
|
|
|
|
fileSize = document.getElementById("input-file-size"),
|
|
|
|
fileType = document.getElementById("input-file-type"),
|
|
|
|
fileLoaded = document.getElementById("input-file-loaded");
|
2019-03-29 14:35:30 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
fileOverlay.style.display = "none";
|
|
|
|
fileName.textContent = "";
|
|
|
|
fileSize.textContent = "";
|
|
|
|
fileType.textContent = "";
|
|
|
|
fileLoaded.textContent = "";
|
|
|
|
|
2019-03-29 14:35:30 +01:00
|
|
|
const inputText = document.getElementById("input-text"),
|
|
|
|
fileThumb = document.getElementById("input-file-thumbnail");
|
2019-03-29 14:29:24 +01:00
|
|
|
inputText.style.overflow = "auto";
|
|
|
|
inputText.classList.remove("blur");
|
2019-03-29 14:35:30 +01:00
|
|
|
fileThumb.src = require("./static/images/file-128x128.png");
|
2019-03-29 14:29:24 +01:00
|
|
|
}
|
2019-03-27 10:05:10 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
/**
|
|
|
|
* Remove an input from the input list
|
|
|
|
* @param {number} inputNum
|
|
|
|
*/
|
|
|
|
removeInput(inputNum) {
|
|
|
|
for (let i = 0; i < this.inputs.length; i++) {
|
|
|
|
if (this.inputs[i].inputNum === inputNum) {
|
|
|
|
this.inputs.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if (this.inputs.length === 0) {
|
|
|
|
// this.inputs.push({
|
|
|
|
// inputNum: inputNum,
|
|
|
|
// data: "",
|
|
|
|
// status: "loaded",
|
|
|
|
// progress: 100
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
}
|
2018-05-15 19:36:45 +02:00
|
|
|
|
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Updates the progress value of an input
|
2018-05-15 19:36:45 +02:00
|
|
|
*
|
2019-03-29 14:29:24 +01:00
|
|
|
* @param {number} inputNum
|
|
|
|
* @param {number} progress
|
2018-05-15 19:36:45 +02:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
updateInputProgress(inputNum, progress) {
|
|
|
|
for (let i = 0; i < this.inputs.length; i++) {
|
|
|
|
if (this.inputs[i].inputNum === inputNum) {
|
|
|
|
// Don't let progress go over 100
|
|
|
|
this.inputs[i].progress = (progress <= 100) ? progress : 100;
|
|
|
|
}
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
/**
|
|
|
|
* Updates the stored value of an input
|
|
|
|
*
|
|
|
|
* @param {number} inputNum
|
|
|
|
* @param {ArrayBuffer | String} value
|
|
|
|
*/
|
|
|
|
updateInputValue(inputNum, value) {
|
|
|
|
|
|
|
|
for (let i = 0; i < this.inputs.length; i++) {
|
|
|
|
if (this.inputs[i].inputNum === inputNum) {
|
|
|
|
if (typeof value === "string") {
|
|
|
|
this.inputs[i].data = value;
|
|
|
|
} else {
|
|
|
|
this.inputs[i].data.fileBuffer = value;
|
|
|
|
|
|
|
|
if (inputNum === this.getActiveTab()) {
|
|
|
|
this.displayFilePreview();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.inputs[i].progress = 100;
|
|
|
|
this.inputs[i].status = "loaded";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we get to here, an input for inputNum could not be found
|
|
|
|
|
|
|
|
if (typeof value === "string") {
|
|
|
|
this.inputs.push({
|
|
|
|
inputNum: inputNum,
|
|
|
|
data: value,
|
|
|
|
status: "loaded",
|
|
|
|
progress: 100
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-05-15 19:36:45 +02:00
|
|
|
|
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Handler for messages sent back by LoaderWorkers
|
2018-05-15 19:36:45 +02:00
|
|
|
*
|
2019-03-29 14:29:24 +01:00
|
|
|
* @param {MessageEvent} else
|
2018-05-15 19:36:45 +02:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
handleLoaderMessage(e) {
|
|
|
|
const r = e.data;
|
|
|
|
let inputNum = 0;
|
2018-05-15 19:36:45 +02:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
if (r.hasOwnProperty("inputNum")) {
|
|
|
|
inputNum = r.inputNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r.hasOwnProperty("progress")) {
|
|
|
|
this.updateInputProgress(inputNum, r.progress);
|
|
|
|
this.setFile(inputNum);
|
|
|
|
// UI here
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r.hasOwnProperty("error")) {
|
|
|
|
this.app.alert(r.error, 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r.hasOwnProperty("fileBuffer")) {
|
|
|
|
log.debug(`Input file ${inputNum} loaded.`);
|
|
|
|
this.updateInputValue(inputNum, r.fileBuffer);
|
|
|
|
|
|
|
|
this.setLoadingInfo();
|
|
|
|
|
|
|
|
const currentWorker = this.getLoaderWorker(inputNum);
|
|
|
|
|
|
|
|
if (this.pendingFiles.length > 0) {
|
|
|
|
log.debug("Loading file completed. Loading next file.");
|
|
|
|
const nextFile = this.pendingFiles.pop();
|
|
|
|
currentWorker.inputNum = nextFile.inputNum;
|
|
|
|
currentWorker.worker.postMessage({
|
|
|
|
file: nextFile.file,
|
|
|
|
inputNum: nextFile.inputNum
|
|
|
|
});
|
2019-03-22 16:10:19 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
} else {
|
|
|
|
// LoaderWorker no longer needed
|
|
|
|
log.debug("Loading file completed. Closing LoaderWorker.");
|
|
|
|
const progress = this.getLoadProgress();
|
|
|
|
if (progress.total === progress.loaded) {
|
|
|
|
window.dispatchEvent(this.manager.statechange);
|
|
|
|
}
|
|
|
|
this.removeLoaderWorker(currentWorker);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
/**
|
|
|
|
* Gets the input for the specified input number
|
|
|
|
*
|
|
|
|
* @param {number} inputNum
|
|
|
|
*/
|
|
|
|
getInput(inputNum) {
|
|
|
|
const index = this.getInputIndex(inputNum);
|
|
|
|
if (index === -1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (this.inputs[index].inputNum === inputNum) {
|
|
|
|
if (typeof this.inputs[index].data === "string") {
|
|
|
|
return this.inputs[index].data;
|
|
|
|
} else {
|
|
|
|
return this.inputs[index].data.fileBuffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2018-05-15 19:36:45 +02:00
|
|
|
|
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Gets the index of the input in the inputs list
|
2018-05-15 19:36:45 +02:00
|
|
|
*
|
2019-03-29 14:29:24 +01:00
|
|
|
* @param {number} inputNum
|
2018-05-15 19:36:45 +02:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
getInputIndex(inputNum) {
|
|
|
|
for (let i = 0; i < this.inputs.length; i++) {
|
|
|
|
if (this.inputs[i].inputNum === inputNum) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2018-05-15 19:36:45 +02:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
/**
|
|
|
|
* Gets the input for the active tab
|
|
|
|
*/
|
|
|
|
getActive() {
|
|
|
|
const textArea = document.getElementById("input-text");
|
|
|
|
const value = (textArea.value !== undefined) ? textArea.value : "";
|
|
|
|
const inputNum = this.getActiveTab();
|
2018-05-15 19:36:45 +02:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
if (this.getInput(inputNum) === null || typeof this.getInput(inputNum) === "string") {
|
|
|
|
this.updateInputValue(inputNum, value);
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
return this.getInput(inputNum);
|
2019-03-22 16:10:19 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Gets the input for all tabs
|
2019-03-22 16:10:19 +01:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
getAll() {
|
|
|
|
// Need to make sure here that the active input is actually saved in this inputs
|
|
|
|
this.getActive();
|
|
|
|
const inputs = [];
|
2019-03-22 16:10:19 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
for (let i = 0; i < this.inputs.length; i++) {
|
|
|
|
if (this.inputs[i].status === "loaded") {
|
|
|
|
inputs.push({
|
|
|
|
inputNum: this.inputs[i].inputNum,
|
|
|
|
input: this.getInput(this.inputs[i].inputNum) || ""
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (inputs.length === 0) {
|
|
|
|
inputs.push({
|
|
|
|
inputNum: 1,
|
|
|
|
input: ""
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return inputs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the progress of the loaderWorkers
|
|
|
|
*/
|
|
|
|
getLoadProgress() {
|
|
|
|
const totalInputs = this.inputs.length;
|
|
|
|
const pendingInputs = this.pendingFiles.length;
|
|
|
|
let loadingInputs = 0;
|
|
|
|
for (let i = 0; i < this.loaderWorkers.length; i++) {
|
|
|
|
if (this.loaderWorkers[i].active) {
|
|
|
|
loadingInputs += 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
total: totalInputs,
|
|
|
|
pending: pendingInputs,
|
|
|
|
loading: loadingInputs,
|
|
|
|
loaded: (totalInputs - pendingInputs - loadingInputs)
|
|
|
|
};
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Handler for input change events
|
2018-05-15 19:36:45 +02:00
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*
|
|
|
|
* @fires Manager#statechange
|
|
|
|
*/
|
|
|
|
inputChange(e) {
|
2019-03-29 14:29:24 +01:00
|
|
|
// Ignore this function if the input is a file
|
|
|
|
const input = this.getActive();
|
|
|
|
if (typeof input !== "string") return;
|
2018-05-15 19:36:45 +02:00
|
|
|
|
|
|
|
// Remove highlighting from input and output panes as the offsets might be different now
|
2019-03-22 16:10:19 +01:00
|
|
|
// this.manager.highlighter.removeHighlights();
|
2018-05-15 19:36:45 +02:00
|
|
|
|
|
|
|
// Reset recipe progress as any previous processing will be redundant now
|
|
|
|
this.app.progress = 0;
|
|
|
|
|
|
|
|
// Update the input metadata info
|
2019-03-29 14:29:24 +01:00
|
|
|
const lines = input.length < (this.app.options.ioDisplayThreshold * 1024) ?
|
|
|
|
input.count("\n") + 1 : null;
|
2018-05-15 19:36:45 +02:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
this.setInputInfo(input.length, lines);
|
|
|
|
this.displayTabInfo(this.getActiveTab());
|
2018-05-15 19:36:45 +02:00
|
|
|
|
|
|
|
if (e && this.badKeys.indexOf(e.keyCode) < 0) {
|
|
|
|
// Fire the statechange event as the input has been modified
|
|
|
|
window.dispatchEvent(this.manager.statechange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for input paste events.
|
|
|
|
* Checks that the size of the input is below the display limit, otherwise treats it as a file/blob.
|
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*/
|
|
|
|
inputPaste(e) {
|
|
|
|
const pastedData = e.clipboardData.getData("Text");
|
|
|
|
|
|
|
|
if (pastedData.length < (this.app.options.ioDisplayThreshold * 1024)) {
|
|
|
|
this.inputChange(e);
|
|
|
|
} else {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
const file = new File([pastedData], "PastedData", {
|
|
|
|
type: "text/plain",
|
|
|
|
lastModified: Date.now()
|
|
|
|
});
|
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
this.loadFile(file, this.getActiveTab());
|
2018-05-15 19:36:45 +02:00
|
|
|
this.set(file);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for input dragover events.
|
|
|
|
* Gives the user a visual cue to show that items can be dropped here.
|
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*/
|
|
|
|
inputDragover(e) {
|
|
|
|
// This will be set if we're dragging an operation
|
|
|
|
if (e.dataTransfer.effectAllowed === "move")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
e.target.closest("#input-text,#input-file").classList.add("dropping-file");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for input dragleave events.
|
|
|
|
* Removes the visual cue.
|
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*/
|
|
|
|
inputDragleave(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2019-03-22 16:10:19 +01:00
|
|
|
e.target.closest("#input-text,#input-file").classList.remove("dropping-file");
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for input drop events.
|
2019-03-29 14:29:24 +01:00
|
|
|
* Loads the dragged data into the input textarea
|
2018-05-15 19:36:45 +02:00
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*/
|
|
|
|
inputDrop(e) {
|
|
|
|
// This will be set if we're dragging an operation
|
|
|
|
if (e.dataTransfer.effectAllowed === "move")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const text = e.dataTransfer.getData("Text");
|
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
e.target.closest("#input-text,#input-file").classList.remove("dropping-file");
|
2018-05-15 19:36:45 +02:00
|
|
|
|
|
|
|
if (text) {
|
2019-03-22 16:10:19 +01:00
|
|
|
this.closeFile(this.getActiveTab());
|
2018-05-15 19:36:45 +02:00
|
|
|
this.set(text);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
if (e.dataTransfer.files && e.dataTransfer.files.length > 0) {
|
2019-03-29 14:29:24 +01:00
|
|
|
this.loadUIFiles(e.dataTransfer.files);
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 16:07:19 +01:00
|
|
|
/**
|
|
|
|
* Handler for open input button events
|
|
|
|
* Loads the opened data into the input textarea
|
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*/
|
|
|
|
inputOpen(e) {
|
|
|
|
e.preventDefault();
|
2019-03-22 16:10:19 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
if (e.srcElement.files.length > 0) {
|
|
|
|
this.loadUIFiles(e.srcElement.files);
|
|
|
|
e.srcElement.value = "";
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
2019-01-18 16:07:19 +01:00
|
|
|
}
|
|
|
|
|
2018-05-15 19:36:45 +02:00
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Load files from the UI into the input, creating tabs if needed
|
2018-05-15 19:36:45 +02:00
|
|
|
*
|
2019-03-29 14:29:24 +01:00
|
|
|
* @param files
|
2018-05-15 19:36:45 +02:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
loadUIFiles(files) {
|
2019-03-22 16:10:19 +01:00
|
|
|
let inputNum;
|
2019-04-01 17:15:09 +02:00
|
|
|
if (files.length > 20) {
|
|
|
|
this.manager.controls.setAutoBake(false);
|
|
|
|
this.app.alert("Auto-Bake is disabled by default when inputting more than 20 files.", 5000);
|
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
inputNum = this.getActiveTab();
|
|
|
|
if (i > 0) {
|
|
|
|
inputNum = this.addTab(false);
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
this.loadFile(files[i], inputNum);
|
2018-05-15 19:36:45 +02:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
if (inputNum === this.getActiveTab()) {
|
|
|
|
this.setFile(inputNum);
|
|
|
|
}
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
this.changeTab(inputNum);
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Sets the input in the input area
|
|
|
|
*
|
|
|
|
* @param {string|File} input
|
|
|
|
* @param {boolean} [silent=false] - Suppress statechange event
|
|
|
|
*
|
|
|
|
* @fires Manager#statechange
|
|
|
|
*
|
2018-05-15 19:36:45 +02:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
set(input, silent=false) {
|
|
|
|
const inputText = document.getElementById("input-text");
|
2019-03-22 16:10:19 +01:00
|
|
|
const inputNum = this.getActiveTab();
|
2019-03-29 14:29:24 +01:00
|
|
|
if (input instanceof File) {
|
|
|
|
this.setFile(inputNum);
|
|
|
|
inputText.value = "";
|
|
|
|
this.setInputInfo(input.size, null);
|
|
|
|
this.displayTabInfo(inputNum);
|
|
|
|
} else {
|
|
|
|
inputText.value = input;
|
|
|
|
this.updateInputValue(inputNum, input);
|
|
|
|
this.closeFile(inputNum);
|
2018-05-15 19:36:45 +02:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
if (!silent) window.dispatchEvent(this.manager.statechange);
|
|
|
|
|
|
|
|
const lines = input.length < (this.app.options.ioDisplayThreshold * 1024) ?
|
|
|
|
input.count("\n") + 1 : null;
|
|
|
|
this.setInputInfo(input.length, lines);
|
|
|
|
this.displayTabInfo(inputNum);
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Shows file details
|
|
|
|
*
|
|
|
|
* @param {number} inputNum
|
2018-05-15 19:36:45 +02:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
setFile(inputNum) {
|
|
|
|
if (inputNum === this.getActiveTab()) {
|
|
|
|
for (let i = 0; i < this.inputs.length; i++) {
|
|
|
|
if (this.inputs[i].inputNum === inputNum && typeof this.inputs[i].data !== "string") {
|
|
|
|
const fileOverlay = document.getElementById("input-file"),
|
|
|
|
fileName = document.getElementById("input-file-name"),
|
|
|
|
fileSize = document.getElementById("input-file-size"),
|
|
|
|
fileType = document.getElementById("input-file-type"),
|
|
|
|
fileLoaded = document.getElementById("input-file-loaded"),
|
|
|
|
fileObj = this.inputs[i];
|
|
|
|
fileOverlay.style.display = "block";
|
|
|
|
fileName.textContent = fileObj.data.name;
|
|
|
|
fileSize.textContent = fileObj.data.size + " bytes";
|
|
|
|
fileType.textContent = fileObj.data.type;
|
|
|
|
fileLoaded.textContent = fileObj.progress + "%";
|
|
|
|
|
|
|
|
this.setInputInfo(fileObj.data.size, null);
|
|
|
|
this.displayFilePreview();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.displayTabInfo(inputNum);
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
2019-02-11 15:48:25 +01:00
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Displays information about the input.
|
2019-02-11 15:48:25 +01:00
|
|
|
*
|
2019-03-29 14:29:24 +01:00
|
|
|
* @param {number} length - The length of the current input string
|
|
|
|
* @param {number} lines - The number of the lines in the current input string
|
2019-02-11 15:48:25 +01:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
setInputInfo(length, lines) {
|
|
|
|
// This should also update the tab?
|
|
|
|
let width = length.toString().length;
|
|
|
|
width = width < 2 ? 2 : width;
|
|
|
|
|
|
|
|
const lengthStr = length.toString().padStart(width, " ").replace(/ /g, " ");
|
|
|
|
let msg = "length: " + lengthStr;
|
|
|
|
|
|
|
|
if (typeof lines === "number") {
|
|
|
|
const linesStr = lines.toString().padStart(width, " ").replace(/ /g, " ");
|
|
|
|
msg += "<br>lines: " + linesStr;
|
2019-02-11 15:48:25 +01:00
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
|
|
|
|
document.getElementById("input-info").innerHTML = msg;
|
|
|
|
|
2019-02-11 15:48:25 +01:00
|
|
|
}
|
|
|
|
|
2018-05-15 19:36:45 +02:00
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Display progress information for file loading in header
|
2018-05-15 19:36:45 +02:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
setLoadingInfo() {
|
|
|
|
const progress = this.getLoadProgress();
|
|
|
|
let width = progress.total.toString().length;
|
|
|
|
width = width < 2 ? 2 : width;
|
|
|
|
|
|
|
|
const totalStr = progress.total.toString().padStart(width, " ").replace(/ /g, " ");
|
|
|
|
let msg = "Total: " + totalStr;
|
|
|
|
|
|
|
|
const loadedStr = progress.loaded.toString().padStart(width, " ").replace(/ /g, " ");
|
|
|
|
msg += "<br>Loaded: " + loadedStr;
|
|
|
|
|
|
|
|
if (progress.pending > 0) {
|
|
|
|
const pendingStr = progress.pending.toString().padStart(width, " ").replace(/ /g, " ");
|
|
|
|
msg += "<br>Pending: " + pendingStr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById("input-files-info").innerHTML = msg;
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
2019-03-21 13:31:01 +01:00
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Display input information in the tab header
|
2019-03-22 16:10:19 +01:00
|
|
|
*
|
2019-03-29 14:29:24 +01:00
|
|
|
* @param {number} inputNum
|
2019-03-22 16:10:19 +01:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
displayTabInfo(inputNum) {
|
|
|
|
const tabItem = this.getTabItem(inputNum);
|
|
|
|
const input = this.inputs[this.getInputIndex(inputNum)];
|
|
|
|
if (!tabItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tabContent = tabItem.firstElementChild;
|
|
|
|
if (typeof input.data !== "string") {
|
|
|
|
tabContent.innerText = `${inputNum}: ${input.data.name}`;
|
|
|
|
} else {
|
|
|
|
if (input.data.length > 0) {
|
|
|
|
const inputText = input.data.slice(0, 100).split(/[\r\n]/)[0];
|
|
|
|
tabContent.innerText = `${inputNum}: ${inputText}`;
|
|
|
|
} else {
|
|
|
|
tabContent.innerText = `${inputNum}: New Tab`;
|
|
|
|
}
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Shows a chunk of the file in the input behind the file overlay.
|
2019-03-21 13:31:01 +01:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
displayFilePreview() {
|
|
|
|
const inputNum = this.getActiveTab(),
|
2019-04-01 09:53:52 +02:00
|
|
|
input = this.getInput(inputNum),
|
2019-03-29 14:29:24 +01:00
|
|
|
inputText = document.getElementById("input-text"),
|
2019-04-01 09:53:52 +02:00
|
|
|
fileSlice = input.slice(0, 4096),
|
2019-03-19 15:38:46 +01:00
|
|
|
fileThumb = document.getElementById("input-file-thumbnail"),
|
2019-04-01 09:53:52 +02:00
|
|
|
arrBuffer = new Uint8Array(input),
|
2019-03-19 15:38:46 +01:00
|
|
|
type = isImage(arrBuffer);
|
2019-04-01 09:53:52 +02:00
|
|
|
if (type && type !== "image/tiff" && this.app.options.imagePreview && input.byteLength < 1024000) {
|
2019-03-19 15:38:46 +01:00
|
|
|
// Don't show TIFFs as not much supports them
|
|
|
|
fileThumb.src = `data:${type};base64,${toBase64(arrBuffer)}`;
|
|
|
|
} else {
|
|
|
|
fileThumb.src = require("./static/images/file-128x128.png");
|
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
inputText.style.overflow = "hidden";
|
|
|
|
inputText.classList.add("blur");
|
|
|
|
inputText.value = Utils.printable(Utils.arrayBufferToStr(fileSlice));
|
|
|
|
if (this.getInput(inputNum).byteLength > 4096) {
|
|
|
|
inputText.value += "[truncated]...";
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
/**
|
|
|
|
* Create a tab element for the input tab bar
|
|
|
|
*
|
|
|
|
* @param {number} inputNum
|
|
|
|
* @returns {Element}
|
|
|
|
*/
|
|
|
|
createTabElement(inputNum) {
|
2019-03-21 13:31:01 +01:00
|
|
|
const newTab = document.createElement("li");
|
2019-03-29 14:29:24 +01:00
|
|
|
newTab.setAttribute("inputNum", inputNum.toString());
|
2019-03-21 13:31:01 +01:00
|
|
|
|
|
|
|
const newTabContent = document.createElement("div");
|
|
|
|
newTabContent.classList.add("input-tab-content");
|
2019-03-29 14:29:24 +01:00
|
|
|
newTabContent.innerText = `${inputNum.toString()}: New Tab`;
|
|
|
|
|
|
|
|
const newTabButton = document.createElement("button");
|
|
|
|
newTabButton.type = "button";
|
|
|
|
newTabButton.className = "btn btn-primary bmd-btn-icon btn-close-tab";
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
const newTabButtonIcon = document.createElement("i");
|
|
|
|
newTabButtonIcon.classList.add("material-icons");
|
|
|
|
newTabButtonIcon.innerText = "clear";
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
newTabButton.appendChild(newTabButtonIcon);
|
2019-03-21 13:31:01 +01:00
|
|
|
|
|
|
|
newTab.appendChild(newTabContent);
|
2019-03-29 14:29:24 +01:00
|
|
|
newTab.appendChild(newTabButton);
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
return newTab;
|
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
/**
|
|
|
|
* Adds a new input to inputs.
|
|
|
|
* Will create a new tab if there's less than maxtabs visible.
|
|
|
|
*
|
|
|
|
* @param {boolean} [changeTab=true]
|
|
|
|
*/
|
|
|
|
addTab(changeTab = true) {
|
|
|
|
let inputNum;
|
|
|
|
if (this.inputs.length === 0) {
|
|
|
|
inputNum = 1;
|
|
|
|
} else {
|
|
|
|
inputNum = this.getLargestInputNum() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.inputs.push({
|
|
|
|
inputNum: inputNum,
|
|
|
|
data: "",
|
|
|
|
status: "loaded",
|
|
|
|
progress: 100
|
|
|
|
});
|
|
|
|
|
|
|
|
const tabsWrapper = document.getElementById("input-tabs");
|
2019-04-01 17:15:09 +02:00
|
|
|
const numTabs = tabsWrapper.children.length;
|
2019-03-29 14:29:24 +01:00
|
|
|
|
|
|
|
if (numTabs < this.maxTabs) {
|
|
|
|
// Create a tab element
|
|
|
|
const newTab = this.createTabElement(inputNum);
|
|
|
|
|
2019-04-01 17:15:09 +02:00
|
|
|
tabsWrapper.appendChild(newTab);
|
2019-03-29 14:29:24 +01:00
|
|
|
|
|
|
|
if (numTabs > 0) {
|
2019-04-01 17:15:09 +02:00
|
|
|
tabsWrapper.parentElement.style.display = "block";
|
|
|
|
|
|
|
|
const tabButtons = document.getElementsByClassName("tab-buttons");
|
|
|
|
for (let i = 0; i < tabButtons.length; i++) {
|
|
|
|
tabButtons.item(i).style.display = "inline-block";
|
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
|
|
|
|
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))";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-03-27 14:48:54 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
if (changeTab) {
|
2019-03-29 14:29:24 +01:00
|
|
|
this.changeTab(inputNum);
|
2019-03-21 13:31:01 +01:00
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
|
|
|
|
return inputNum;
|
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Removes a tab and it's corresponding input
|
2019-03-22 16:10:19 +01:00
|
|
|
*
|
2019-03-29 14:29:24 +01:00
|
|
|
* @param {number} inputNum
|
2019-03-22 16:10:19 +01:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
removeTab(inputNum) {
|
|
|
|
const inputIdx = this.getInputIndex(inputNum);
|
|
|
|
let activeTab = this.getActiveTab();
|
|
|
|
if (inputIdx === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tabElement = this.getTabItem(inputNum);
|
|
|
|
|
|
|
|
this.removeInput(inputNum);
|
|
|
|
|
|
|
|
if (tabElement !== null) {
|
|
|
|
if (inputNum === activeTab) {
|
|
|
|
activeTab = this.getPreviousInputNum(activeTab);
|
|
|
|
if (activeTab === this.getActiveTab()) {
|
|
|
|
activeTab = this.getNextInputNum(activeTab);
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
this.refreshTabs(activeTab);
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
/**
|
|
|
|
* Redraw the entire tab bar to remove any outdated tabs
|
|
|
|
* @param {number} activeTab
|
|
|
|
*/
|
|
|
|
refreshTabs(activeTab) {
|
2019-04-01 17:15:09 +02:00
|
|
|
const tabsList = document.getElementById("input-tabs");
|
2019-03-29 14:29:24 +01:00
|
|
|
let newInputs = this.getNearbyNums(activeTab, "right");
|
|
|
|
if (newInputs.length < this.maxTabs) {
|
|
|
|
newInputs = this.getNearbyNums(activeTab, "left");
|
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
for (let i = tabsList.children.length - 1; i >= 0; i--) {
|
|
|
|
tabsList.children.item(i).remove();
|
|
|
|
}
|
2019-03-27 14:48:54 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
for (let i = 0; i < newInputs.length; i++) {
|
|
|
|
tabsList.appendChild(this.createTabElement(newInputs[i]));
|
2019-03-29 15:51:11 +01:00
|
|
|
this.displayTabInfo(newInputs[i]);
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
|
|
|
|
if (newInputs.length > 1) {
|
2019-04-01 17:15:09 +02:00
|
|
|
tabsList.parentElement.style.display = "block";
|
|
|
|
|
|
|
|
const tabButtons = document.getElementsByClassName("tab-buttons");
|
|
|
|
for (let i = 0; i < tabButtons.length; i++) {
|
|
|
|
tabButtons.item(i).style.display = "inline-block";
|
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
|
|
|
|
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 {
|
2019-04-01 17:15:09 +02:00
|
|
|
tabsList.parentElement.style.display = "none";
|
|
|
|
|
|
|
|
const tabButtons = document.getElementsByClassName("tab-buttons");
|
|
|
|
for (let i = 0; i < tabButtons.length; i++) {
|
|
|
|
tabButtons.item(i).style.display = "none";
|
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
document.getElementById("input-wrapper").style.height = "calc(100% - var(--title-height))";
|
2019-03-22 16:19:03 +01:00
|
|
|
document.getElementById("input-highlighter").style.height = "calc(100% - var(--title-height))";
|
2019-03-22 16:10:19 +01:00
|
|
|
document.getElementById("input-file").style.height = "calc(100% - var(--title-height))";
|
2019-03-29 14:29:24 +01:00
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
if (newInputs.length === 0) {
|
|
|
|
activeTab = this.addTab();
|
2019-03-29 15:51:11 +01:00
|
|
|
this.displayTabInfo(activeTab);
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
|
|
|
|
this.changeTab(activeTab);
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Handler for remove tab button click
|
2019-03-22 16:10:19 +01:00
|
|
|
* @param {event} mouseEvent
|
|
|
|
*/
|
|
|
|
removeTabClick(mouseEvent) {
|
|
|
|
if (!mouseEvent.srcElement) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
const tabNum = mouseEvent.srcElement.parentElement.parentElement.getAttribute("inputNum");
|
|
|
|
if (tabNum) {
|
|
|
|
this.removeTab(parseInt(tabNum, 10));
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
/**
|
|
|
|
* Generates a list of the nearby inputNums
|
|
|
|
*
|
|
|
|
* @param {number} inputNum
|
|
|
|
* @param {string} direction
|
|
|
|
*/
|
|
|
|
getNearbyNums(inputNum, direction) {
|
|
|
|
const inputs = [];
|
|
|
|
if (direction === "left") {
|
|
|
|
let reachedEnd = false;
|
|
|
|
for (let i = 0; i < this.maxTabs; i++) {
|
|
|
|
let newNum;
|
|
|
|
if (i === 0) {
|
|
|
|
newNum = inputNum;
|
|
|
|
} else {
|
|
|
|
newNum = this.getNextInputNum(inputs[i-1]);
|
|
|
|
}
|
|
|
|
if (newNum === inputs[i-1]) {
|
|
|
|
reachedEnd = true;
|
|
|
|
inputs.sort(function(a, b) {
|
|
|
|
return b - a;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (reachedEnd) {
|
|
|
|
newNum = this.getPreviousInputNum(inputs[i-1]);
|
|
|
|
}
|
|
|
|
if (newNum >= 0) {
|
|
|
|
inputs.push(newNum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let reachedEnd = false;
|
|
|
|
for (let i = 0; i < this.maxTabs; i++) {
|
|
|
|
let newNum;
|
|
|
|
if (i === 0) {
|
|
|
|
newNum = inputNum;
|
|
|
|
} else {
|
|
|
|
if (!reachedEnd) {
|
|
|
|
newNum = this.getPreviousInputNum(inputs[i-1]);
|
|
|
|
}
|
|
|
|
if (newNum === inputs[i-1]) {
|
|
|
|
reachedEnd = true;
|
|
|
|
inputs.sort(function(a, b) {
|
|
|
|
return b - a;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (reachedEnd) {
|
|
|
|
newNum = this.getNextInputNum(inputs[i-1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (newNum >= 0) {
|
|
|
|
inputs.push(newNum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inputs.sort(function(a, b) {
|
|
|
|
return a - b;
|
|
|
|
});
|
|
|
|
return inputs;
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Changes the active tab
|
2019-03-22 16:10:19 +01:00
|
|
|
*
|
2019-03-29 14:29:24 +01:00
|
|
|
* @param {number} inputNum
|
2019-03-22 16:10:19 +01:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
changeTab(inputNum) {
|
|
|
|
const inputIdx = this.getInputIndex(inputNum);
|
|
|
|
let currentIdx = -1;
|
|
|
|
try {
|
|
|
|
currentIdx = this.getActiveTab();
|
|
|
|
} catch (err) {}
|
|
|
|
if (inputIdx === -1) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
const tabsWrapper = document.getElementById("input-tabs");
|
2019-04-01 17:15:09 +02:00
|
|
|
const tabs = tabsWrapper.children;
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
let found = false;
|
|
|
|
for (let i = 0; i < tabs.length; i++) {
|
|
|
|
if (tabs.item(i).getAttribute("inputNum") === inputNum.toString()) {
|
|
|
|
tabs.item(i).classList.add("active-input-tab");
|
|
|
|
found = true;
|
|
|
|
} else {
|
|
|
|
tabs.item(i).classList.remove("active-input-tab");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
// Shift the tabs here
|
|
|
|
let direction = "right";
|
|
|
|
if (currentIdx > inputIdx) {
|
|
|
|
direction = "left";
|
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
const newInputs = this.getNearbyNums(inputNum, direction);
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
for (let i = 0; i < newInputs.length; i++) {
|
|
|
|
tabs.item(i).setAttribute("inputNum", newInputs[i].toString());
|
|
|
|
this.displayTabInfo(newInputs[i]);
|
|
|
|
if (newInputs[i] === inputNum) {
|
|
|
|
tabs.item(i).classList.add("active-input-tab");
|
|
|
|
}
|
|
|
|
}
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
const input = this.getInput(inputNum);
|
|
|
|
if (typeof input === "string") {
|
|
|
|
this.set(this.getInput(inputNum));
|
|
|
|
} else {
|
|
|
|
this.setFile(inputNum);
|
2019-03-27 14:48:54 +01:00
|
|
|
}
|
|
|
|
|
2019-03-21 13:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-22 16:10:19 +01:00
|
|
|
* Handler for changing tabs event
|
2019-03-21 13:31:01 +01:00
|
|
|
*
|
|
|
|
* @param {event} mouseEvent
|
|
|
|
*/
|
2019-03-22 16:10:19 +01:00
|
|
|
changeTabClick(mouseEvent) {
|
|
|
|
if (!mouseEvent.srcElement) {
|
2019-03-21 13:31:01 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
const tabNum = mouseEvent.srcElement.parentElement.getAttribute("inputNum");
|
|
|
|
if (tabNum) {
|
|
|
|
this.changeTab(parseInt(tabNum, 10));
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
/**
|
|
|
|
* Handler for changing to the left tab
|
|
|
|
*/
|
|
|
|
changeTabLeft() {
|
|
|
|
const currentTab = this.getActiveTab();
|
|
|
|
const currentInput = this.getInputIndex(currentTab);
|
|
|
|
if (currentInput > 0) {
|
|
|
|
this.changeTab(this.getPreviousInputNum(currentTab));
|
|
|
|
} else {
|
|
|
|
this.changeTab(this.inputs[0].inputNum);
|
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-29 14:29:24 +01:00
|
|
|
* Handler for changing to the right tab
|
|
|
|
*/
|
|
|
|
changeTabRight() {
|
|
|
|
const currentTab = this.getActiveTab();
|
|
|
|
this.changeTab(this.getNextInputNum(currentTab));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for go to tab button clicked
|
|
|
|
*/
|
|
|
|
goToTab() {
|
|
|
|
const tabNum = parseInt(window.prompt("Enter tab number:", this.getActiveTab().toString()), 10);
|
|
|
|
if (this.getInputIndex(tabNum)) {
|
|
|
|
this.changeTab(tabNum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the largest inputNum
|
2019-03-21 13:31:01 +01:00
|
|
|
*
|
2019-03-29 14:29:24 +01:00
|
|
|
* @returns {number}
|
2019-03-21 13:31:01 +01:00
|
|
|
*/
|
2019-03-29 14:29:24 +01:00
|
|
|
getLargestInputNum() {
|
|
|
|
let largest = 0;
|
|
|
|
for (let i = 0; i < this.inputs.length; i++) {
|
|
|
|
if (this.inputs[i].inputNum > largest) {
|
|
|
|
largest = this.inputs[i].inputNum;
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
return largest;
|
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
/**
|
|
|
|
* Gets the previous inputNum
|
|
|
|
*
|
|
|
|
* @param {number} inputNum - The current input number
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
getPreviousInputNum(inputNum) {
|
|
|
|
let num = -1;
|
|
|
|
for (let i = 0; i < this.inputs.length; i++) {
|
|
|
|
if (this.inputs[i].inputNum < inputNum) {
|
|
|
|
if (this.inputs[i].inputNum > num) {
|
|
|
|
num = this.inputs[i].inputNum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the next inputNum
|
|
|
|
*
|
|
|
|
* @param {number} inputNum - The current input number
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
getNextInputNum(inputNum) {
|
|
|
|
let num = this.getLargestInputNum();
|
|
|
|
for (let i = 0; i < this.inputs.length; i++) {
|
|
|
|
if (this.inputs[i].inputNum > inputNum) {
|
|
|
|
if (this.inputs[i].inputNum < num) {
|
|
|
|
num = this.inputs[i].inputNum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return num;
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
/**
|
|
|
|
* Gets the number of the current active tab
|
|
|
|
*
|
2019-03-29 14:29:24 +01:00
|
|
|
* @returns {number}
|
2019-03-22 16:10:19 +01:00
|
|
|
*/
|
|
|
|
getActiveTab() {
|
|
|
|
const activeTabs = document.getElementsByClassName("active-input-tab");
|
|
|
|
if (activeTabs.length > 0) {
|
|
|
|
const activeTab = activeTabs.item(0);
|
2019-03-29 14:29:24 +01:00
|
|
|
const tabNum = activeTab.getAttribute("inputNum");
|
|
|
|
return parseInt(tabNum, 10);
|
2019-03-22 16:10:19 +01:00
|
|
|
} else {
|
2019-03-29 14:29:24 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the li element for the tab of an input number
|
|
|
|
*
|
|
|
|
* @param {number} inputNum
|
|
|
|
* @returns {Element}
|
|
|
|
*/
|
|
|
|
getTabItem(inputNum) {
|
2019-04-01 17:15:09 +02:00
|
|
|
const tabs = document.getElementById("input-tabs").children;
|
2019-03-29 14:29:24 +01:00
|
|
|
for (let i = 0; i < tabs.length; i++) {
|
|
|
|
if (parseInt(tabs.item(i).getAttribute("inputNum"), 10) === inputNum) {
|
|
|
|
return tabs.item(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for clear all IO events.
|
|
|
|
* Resets the input, output and info areas
|
|
|
|
*/
|
|
|
|
clearAllIoClick() {
|
|
|
|
for (let i = this.inputs.length - 1; i >= 0; i--) {
|
|
|
|
this.removeInput(this.inputs[i].inputNum);
|
2019-03-21 13:31:01 +01:00
|
|
|
}
|
2019-03-29 14:29:24 +01:00
|
|
|
this.refreshTabs();
|
2019-03-21 13:31:01 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:29:24 +01:00
|
|
|
/**
|
|
|
|
* Handler for clear IO click event.
|
|
|
|
* Resets the input for the current tab
|
|
|
|
*/
|
|
|
|
clearIoClick() {
|
|
|
|
const inputNum = this.getActiveTab();
|
|
|
|
this.removeInput(inputNum);
|
|
|
|
|
|
|
|
this.inputs.push({
|
|
|
|
inputNum: inputNum,
|
|
|
|
data: "",
|
|
|
|
status: "loaded",
|
|
|
|
progress: 0
|
|
|
|
});
|
|
|
|
|
|
|
|
this.set("");
|
|
|
|
}
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default InputWaiter;
|