2018-05-15 19:36:45 +02:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import LoaderWorker from "worker-loader?inline&fallback=false!./LoaderWorker";
|
|
|
|
import Utils from "../core/Utils";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Waiter to handle events related to the input.
|
|
|
|
*/
|
|
|
|
class InputWaiter {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* InputWaiter constructor.
|
|
|
|
*
|
|
|
|
* @param {App} app - The main view object for CyberChef.
|
|
|
|
* @param {Manager} manager - The CyberChef event manager.
|
|
|
|
*/
|
|
|
|
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-22 16:10:19 +01:00
|
|
|
this.loaderWorkers = {};
|
|
|
|
this.fileBuffers = {};
|
|
|
|
this.inputs = {};
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-03-22 16:10:19 +01:00
|
|
|
* Gets the user's input from the active input textarea.
|
2018-05-15 19:36:45 +02:00
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
get() {
|
2019-03-22 16:10:19 +01:00
|
|
|
const textArea = document.getElementById("input-text");
|
|
|
|
const value = textArea.value;
|
|
|
|
const inputNum = this.getActiveTab();
|
|
|
|
if (this.fileBuffers[inputNum] && this.fileBuffers[inputNum].fileBuffer) {
|
|
|
|
return this.fileBuffers[inputNum].fileBuffer;
|
|
|
|
}
|
|
|
|
return value;
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
2019-03-27 10:05:10 +01:00
|
|
|
/**
|
|
|
|
* Gets the inputs from all tabs
|
|
|
|
*
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
getAll() {
|
|
|
|
const inputs = [];
|
|
|
|
const tabsContainer = document.getElementById("input-tabs");
|
|
|
|
const tabs = tabsContainer.firstElementChild.children;
|
|
|
|
|
|
|
|
for (let i = 0; i < tabs.length; i++) {
|
|
|
|
const inputNum = tabs.item(i).id.replace("input-tab-", "");
|
|
|
|
if (this.fileBuffers[inputNum] && this.fileBuffers[inputNum].fileBuffer) {
|
|
|
|
inputs.push({
|
|
|
|
inputNum: inputNum,
|
|
|
|
input: this.fileBuffers[inputNum].fileBuffer
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
inputs.push({
|
|
|
|
inputNum: inputNum,
|
|
|
|
input: this.inputs[inputNum]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return inputs;
|
|
|
|
}
|
|
|
|
|
2018-05-15 19:36:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the input in the input area.
|
|
|
|
*
|
|
|
|
* @param {string|File} input
|
2018-07-27 17:18:08 +02:00
|
|
|
* @param {boolean} [silent=false] - Suppress statechange event
|
2018-05-15 19:36:45 +02:00
|
|
|
*
|
|
|
|
* @fires Manager#statechange
|
|
|
|
*/
|
2018-07-27 17:18:08 +02:00
|
|
|
set(input, silent=false) {
|
2018-05-15 19:36:45 +02:00
|
|
|
const inputText = document.getElementById("input-text");
|
2019-03-22 16:10:19 +01:00
|
|
|
const inputNum = this.getActiveTab();
|
2018-05-15 19:36:45 +02:00
|
|
|
if (input instanceof File) {
|
|
|
|
this.setFile(input);
|
|
|
|
inputText.value = "";
|
|
|
|
this.setInputInfo(input.size, null);
|
2019-03-22 16:10:19 +01:00
|
|
|
this.displayTabInfo(input);
|
2018-05-15 19:36:45 +02:00
|
|
|
} else {
|
|
|
|
inputText.value = input;
|
2019-03-22 16:10:19 +01:00
|
|
|
this.inputs[inputNum] = input;
|
|
|
|
this.closeFile(inputNum);
|
2018-07-27 17:18:08 +02:00
|
|
|
if (!silent) window.dispatchEvent(this.manager.statechange);
|
2018-05-15 19:36:45 +02:00
|
|
|
const lines = input.length < (this.app.options.ioDisplayThreshold * 1024) ?
|
|
|
|
input.count("\n") + 1 : null;
|
|
|
|
this.setInputInfo(input.length, lines);
|
2019-03-22 16:10:19 +01:00
|
|
|
this.displayTabInfo(input);
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows file details.
|
|
|
|
*
|
|
|
|
* @param {File} file
|
|
|
|
*/
|
|
|
|
setFile(file) {
|
|
|
|
// Display file overlay in input area with details
|
2019-03-22 16:10:19 +01:00
|
|
|
const inputNum = this.getActiveTab();
|
2018-05-15 19:36:45 +02:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
this.fileBuffers[inputNum] = {
|
|
|
|
fileBuffer: new ArrayBuffer(),
|
|
|
|
name: file.name,
|
|
|
|
size: file.size.toLocaleString(),
|
|
|
|
type: file.type || "unknown",
|
|
|
|
loaded: "0%"
|
|
|
|
};
|
|
|
|
|
|
|
|
this.setFileInfo(this.fileBuffers[inputNum]);
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays information about the input.
|
|
|
|
*
|
|
|
|
* @param {number} length - The length of the current input string
|
|
|
|
* @param {number} lines - The number of the lines in the current input string
|
|
|
|
*/
|
|
|
|
setInputInfo(length, lines) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById("input-info").innerHTML = msg;
|
2019-03-22 16:10:19 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays information about the input file.
|
|
|
|
*
|
|
|
|
* @param fileObj
|
|
|
|
*/
|
|
|
|
setFileInfo(fileObj) {
|
|
|
|
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");
|
|
|
|
|
|
|
|
fileOverlay.style.display = "block";
|
|
|
|
fileName.textContent = fileObj.name;
|
|
|
|
fileSize.textContent = fileObj.size + " bytes";
|
|
|
|
fileType.textContent = fileObj.type;
|
|
|
|
fileLoaded.textContent = fileObj.loaded;
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for input change events.
|
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*
|
|
|
|
* @fires Manager#statechange
|
|
|
|
*/
|
|
|
|
inputChange(e) {
|
|
|
|
// Ignore this function if the input is a File
|
2019-03-22 16:10:19 +01:00
|
|
|
const inputNum = this.getActiveTab();
|
|
|
|
if (this.fileBuffers[inputNum]) 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
|
|
|
|
const inputText = this.get();
|
2019-03-22 16:10:19 +01:00
|
|
|
this.inputs[inputNum] = inputText;
|
2018-05-15 19:36:45 +02:00
|
|
|
const lines = inputText.length < (this.app.options.ioDisplayThreshold * 1024) ?
|
|
|
|
inputText.count("\n") + 1 : null;
|
|
|
|
|
|
|
|
this.setInputInfo(inputText.length, lines);
|
2019-03-22 16:10:19 +01:00
|
|
|
this.displayTabInfo(inputText);
|
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()
|
|
|
|
});
|
|
|
|
|
|
|
|
this.loaderWorker = new LoaderWorker();
|
|
|
|
this.loaderWorker.addEventListener("message", this.handleLoaderMessage.bind(this));
|
2019-03-22 16:10:19 +01:00
|
|
|
this.loaderWorker.postMessage({"file": file, "inputNum": 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.
|
|
|
|
* Loads the dragged data into the input textarea.
|
|
|
|
*
|
|
|
|
* @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) {
|
|
|
|
for (let i = 0; i < e.dataTransfer.files.length; i++) {
|
|
|
|
const file = e.dataTransfer.files[i];
|
|
|
|
if (i !== 0) {
|
|
|
|
this.addTab();
|
|
|
|
}
|
|
|
|
this.loadFile(file, this.getActiveTab());
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// TODO : CHANGE THIS TO HANDLE MULTIPLE FILES
|
|
|
|
for (let i = 0; i < e.srcElement.files.length; i++) {
|
|
|
|
const file = e.srcElement.files[i];
|
|
|
|
if (i !== 0) {
|
|
|
|
this.addTab();
|
|
|
|
}
|
|
|
|
this.loadFile(file, this.getActiveTab());
|
|
|
|
}
|
2019-01-18 16:07:19 +01:00
|
|
|
}
|
|
|
|
|
2018-05-15 19:36:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for messages sent back by the LoaderWorker.
|
|
|
|
*
|
|
|
|
* @param {MessageEvent} e
|
|
|
|
*/
|
|
|
|
handleLoaderMessage(e) {
|
|
|
|
const r = e.data;
|
2019-03-22 16:10:19 +01:00
|
|
|
let inputNum;
|
|
|
|
const tabNum = this.getActiveTab();
|
|
|
|
if (r.hasOwnProperty("inputNum")) {
|
|
|
|
inputNum = r.inputNum;
|
|
|
|
}
|
2018-05-15 19:36:45 +02:00
|
|
|
if (r.hasOwnProperty("progress")) {
|
2019-03-22 16:10:19 +01:00
|
|
|
this.fileBuffers[inputNum].loaded = r.progress + "%";
|
|
|
|
if (tabNum === inputNum) {
|
|
|
|
const fileLoaded = document.getElementById("input-file-loaded");
|
|
|
|
fileLoaded.textContent = r.progress + "%";
|
|
|
|
}
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r.hasOwnProperty("error")) {
|
2018-06-20 01:18:59 +02:00
|
|
|
this.app.alert(r.error, 10000);
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r.hasOwnProperty("fileBuffer")) {
|
|
|
|
log.debug("Input file loaded");
|
2019-03-22 16:10:19 +01:00
|
|
|
this.fileBuffers[inputNum].fileBuffer = r.fileBuffer;
|
2018-05-15 19:36:45 +02:00
|
|
|
this.displayFilePreview();
|
|
|
|
window.dispatchEvent(this.manager.statechange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows a chunk of the file in the input behind the file overlay.
|
|
|
|
*/
|
|
|
|
displayFilePreview() {
|
2019-03-22 16:10:19 +01:00
|
|
|
const inputNum = this.getActiveTab();
|
2018-05-15 19:36:45 +02:00
|
|
|
const inputText = document.getElementById("input-text"),
|
2019-03-22 16:10:19 +01:00
|
|
|
fileSlice = this.fileBuffers[inputNum].fileBuffer.slice(0, 4096);
|
2018-05-15 19:36:45 +02:00
|
|
|
|
|
|
|
inputText.style.overflow = "hidden";
|
|
|
|
inputText.classList.add("blur");
|
|
|
|
inputText.value = Utils.printable(Utils.arrayBufferToStr(fileSlice));
|
2019-03-22 16:10:19 +01:00
|
|
|
if (this.fileBuffers[inputNum].fileBuffer.byteLength > 4096) {
|
2018-05-15 19:36:45 +02:00
|
|
|
inputText.value += "[truncated]...";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for file close events.
|
|
|
|
*/
|
2019-03-22 16:10:19 +01:00
|
|
|
closeFile(inputNum) {
|
|
|
|
if (this.loaderWorkers[inputNum]) this.loaderWorkers[inputNum].terminate();
|
|
|
|
delete this.fileBuffers[inputNum];
|
2018-05-15 19:36:45 +02:00
|
|
|
document.getElementById("input-file").style.display = "none";
|
|
|
|
const inputText = document.getElementById("input-text");
|
|
|
|
inputText.style.overflow = "auto";
|
|
|
|
inputText.classList.remove("blur");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-11 15:48:25 +01:00
|
|
|
/**
|
|
|
|
* Loads a file into the input.
|
|
|
|
*
|
|
|
|
* @param {File} file
|
2019-03-22 16:10:19 +01:00
|
|
|
* @param {string} inputNum
|
2019-02-11 15:48:25 +01:00
|
|
|
*/
|
2019-03-22 16:10:19 +01:00
|
|
|
loadFile(file, inputNum) {
|
|
|
|
if (file && inputNum) {
|
|
|
|
this.closeFile(inputNum);
|
|
|
|
this.loaderWorkers[inputNum] = new LoaderWorker();
|
|
|
|
this.loaderWorkers[inputNum].addEventListener("message", this.handleLoaderMessage.bind(this));
|
|
|
|
this.loaderWorkers[inputNum].postMessage({"file": file, "inputNum": inputNum});
|
2019-02-11 15:48:25 +01:00
|
|
|
this.set(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 19:36:45 +02:00
|
|
|
/**
|
|
|
|
* Handler for clear IO events.
|
|
|
|
* Resets the input, output and info areas.
|
|
|
|
*
|
|
|
|
* @fires Manager#statechange
|
|
|
|
*/
|
|
|
|
clearIoClick() {
|
2019-03-22 16:10:19 +01:00
|
|
|
this.closeFile(this.getActiveTab());
|
2018-05-15 19:36:45 +02:00
|
|
|
this.manager.output.closeFile();
|
|
|
|
this.manager.highlighter.removeHighlights();
|
|
|
|
document.getElementById("input-text").value = "";
|
|
|
|
document.getElementById("output-text").value = "";
|
|
|
|
document.getElementById("input-info").innerHTML = "";
|
|
|
|
document.getElementById("output-info").innerHTML = "";
|
|
|
|
document.getElementById("input-selection-info").innerHTML = "";
|
|
|
|
document.getElementById("output-selection-info").innerHTML = "";
|
|
|
|
window.dispatchEvent(this.manager.statechange);
|
|
|
|
}
|
|
|
|
|
2019-03-21 13:31:01 +01:00
|
|
|
/**
|
2019-03-22 16:10:19 +01:00
|
|
|
* Handler for clear all IO events.
|
|
|
|
* Resets the input, output and info areas.
|
|
|
|
*
|
|
|
|
* @fires Manager#statechange
|
|
|
|
*/
|
|
|
|
clearAllIoClick() {
|
|
|
|
const tabs = document.getElementById("input-tabs").getElementsByTagName("li");
|
|
|
|
for (let i = tabs.length - 1; i >= 0; i--) {
|
|
|
|
const tabItem = tabs.item(i);
|
|
|
|
this.closeFile(this.getActiveTab(tabItem.id.replace("input-tab-", "")));
|
|
|
|
this.removeTab(tabItem);
|
|
|
|
}
|
|
|
|
this.manager.output.closeFile();
|
|
|
|
// this.manager.highlighter.removeHighlights();
|
|
|
|
const inputNum = this.getActiveTab();
|
2019-03-22 16:55:35 +01:00
|
|
|
document.getElementById(`input-tab-${inputNum}`).firstElementChild.innerText = `${inputNum}: New Tab`;
|
2019-03-22 16:10:19 +01:00
|
|
|
document.getElementById("input-text").value = "";
|
|
|
|
document.getElementById("output-text").value = "";
|
|
|
|
document.getElementById("input-info").innerHTML = "";
|
|
|
|
document.getElementById("output-info").innerHTML = "";
|
|
|
|
document.getElementById("input-selection-info").innerHTML = "";
|
|
|
|
document.getElementById("output-selection-info").innerHTML = "";
|
|
|
|
window.dispatchEvent(this.manager.statechange);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to create a new tab
|
2019-03-21 13:31:01 +01:00
|
|
|
*
|
2019-03-22 16:10:19 +01:00
|
|
|
* @param {boolean} changeTab
|
2019-03-21 13:31:01 +01:00
|
|
|
*/
|
2019-03-22 16:10:19 +01:00
|
|
|
addTab(changeTab = true) {
|
2019-03-21 13:31:01 +01:00
|
|
|
const tabWrapper = document.getElementById("input-tabs");
|
|
|
|
const tabsList = tabWrapper.children[0];
|
|
|
|
const lastTabNum = tabsList.lastElementChild.id.replace("input-tab-", "");
|
|
|
|
const newTabNum = parseInt(lastTabNum, 10) + 1;
|
|
|
|
|
|
|
|
tabWrapper.style.display = "block";
|
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
document.getElementById("input-wrapper").style.height = "calc(100% - var(--tab-height) - var(--title-height))";
|
2019-03-22 16:19:03 +01:00
|
|
|
document.getElementById("input-highlighter").style.height = "calc(100% - var(--tab-height) - var(--title-height))";
|
2019-03-22 16:10:19 +01:00
|
|
|
document.getElementById("input-file").style.height = "calc(100% - var(--tab-height) - var(--title-height))";
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
this.inputs[newTabNum.toString()] = "";
|
2019-03-21 13:31:01 +01:00
|
|
|
|
|
|
|
const newTab = document.createElement("li");
|
|
|
|
newTab.id = `input-tab-${newTabNum}`;
|
|
|
|
|
|
|
|
const newTabContent = document.createElement("div");
|
|
|
|
newTabContent.classList.add("input-tab-content");
|
2019-03-22 16:55:35 +01:00
|
|
|
newTabContent.innerText = `${newTabNum}: New Tab`;
|
2019-03-21 13:31:01 +01:00
|
|
|
|
|
|
|
const newTabCloseBtn = document.createElement("button");
|
|
|
|
newTabCloseBtn.className = "btn btn-primary bmd-btn-icon btn-close-tab";
|
|
|
|
newTabCloseBtn.id = `btn-close-tab-${newTabNum}`;
|
|
|
|
|
|
|
|
const newTabCloseBtnIcon = document.createElement("i");
|
|
|
|
newTabCloseBtnIcon.classList.add("material-icons");
|
|
|
|
newTabCloseBtnIcon.innerText = "clear";
|
|
|
|
|
|
|
|
newTabCloseBtn.appendChild(newTabCloseBtnIcon);
|
|
|
|
newTab.appendChild(newTabContent);
|
|
|
|
newTab.appendChild(newTabCloseBtn);
|
|
|
|
|
|
|
|
tabsList.appendChild(newTab);
|
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
if (changeTab) {
|
|
|
|
this.changeTab(newTabContent);
|
2019-03-21 13:31:01 +01:00
|
|
|
}
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to remove a tab
|
|
|
|
*
|
|
|
|
* @param {Element} tabLiItem
|
|
|
|
*/
|
|
|
|
removeTab(tabLiItem) {
|
|
|
|
const tabList= tabLiItem.parentElement;
|
|
|
|
if (tabList.children.length > 1) {
|
|
|
|
if (tabLiItem.classList.contains("active-input-tab")) {
|
|
|
|
if (tabLiItem.previousElementSibling) {
|
|
|
|
this.changeTab(tabLiItem.previousElementSibling.firstElementChild);
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
window.dispatchEvent(this.manager.statechange);
|
|
|
|
} else if (tabLiItem.nextElementSibling) {
|
|
|
|
this.changeTab(tabLiItem.nextElementSibling.firstElementChild);
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
window.dispatchEvent(this.manager.statechange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const tabNum = tabLiItem.id.replace("input-tab-", "");
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
delete this.fileBuffers[tabNum];
|
|
|
|
delete this.inputs[tabNum];
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
tabList.removeChild(tabLiItem);
|
|
|
|
} else {
|
|
|
|
const tabNum = tabLiItem.id.replace("input-tab-", "");
|
|
|
|
delete this.fileBuffers[tabNum];
|
|
|
|
this.inputs[tabNum] = "";
|
|
|
|
document.getElementById("input-text").value = "";
|
2019-03-22 16:55:35 +01:00
|
|
|
tabLiItem.firstElementChild.innerText = `${tabNum}: New Tab`;
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
|
|
|
if (tabList.children.length === 1) {
|
|
|
|
document.getElementById("input-tabs").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-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
/**
|
|
|
|
* Handler for removing an input tab
|
|
|
|
*
|
|
|
|
* @param {event} mouseEvent
|
|
|
|
*/
|
|
|
|
removeTabClick(mouseEvent) {
|
|
|
|
if (!mouseEvent.srcElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.removeTab(mouseEvent.srcElement.parentElement.parentElement);
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
}
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
/**
|
|
|
|
* Change the active tab to tabElement
|
|
|
|
*
|
|
|
|
* @param {Element} tabElement The tab element to change to
|
|
|
|
*/
|
|
|
|
changeTab(tabElement) {
|
|
|
|
const liItem = tabElement.parentElement;
|
|
|
|
const newTabNum = liItem.id.replace("input-tab-", "");
|
|
|
|
const currentTabNum = this.getActiveTab();
|
|
|
|
const inputText = document.getElementById("input-text");
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
document.getElementById(`input-tab-${currentTabNum}`).classList.remove("active-input-tab");
|
|
|
|
liItem.classList.add("active-input-tab");
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
this.inputs[currentTabNum] = inputText.value;
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
if (this.fileBuffers[newTabNum]) {
|
|
|
|
const fileObj = this.fileBuffers[newTabNum];
|
2019-03-22 16:55:35 +01:00
|
|
|
this.setInputInfo(fileObj.size, null);
|
2019-03-22 16:10:19 +01:00
|
|
|
this.setFileInfo(fileObj);
|
|
|
|
this.displayFilePreview();
|
|
|
|
} else {
|
|
|
|
inputText.value = this.inputs[newTabNum];
|
|
|
|
inputText.style.overflow = "auto";
|
|
|
|
inputText.classList.remove("blur");
|
2019-03-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
this.inputChange(null);
|
|
|
|
document.getElementById("input-file").style.display = "none";
|
|
|
|
}
|
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-22 16:10:19 +01:00
|
|
|
this.changeTab(mouseEvent.srcElement);
|
2019-03-21 13:31:01 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-22 16:10:19 +01:00
|
|
|
* Display input information in the tab header
|
2019-03-21 13:31:01 +01:00
|
|
|
*
|
2019-03-22 16:10:19 +01:00
|
|
|
* @param {string|File} input
|
2019-03-21 13:31:01 +01:00
|
|
|
*/
|
2019-03-22 16:10:19 +01:00
|
|
|
displayTabInfo(input) {
|
2019-03-22 16:55:35 +01:00
|
|
|
const tabNum = this.getActiveTab();
|
|
|
|
const activeTab = document.getElementById(`input-tab-${tabNum}`);
|
|
|
|
const activeTabContent = activeTab.firstElementChild;
|
|
|
|
if (input instanceof File) {
|
2019-03-27 10:05:10 +01:00
|
|
|
activeTabContent.innerText = `${tabNum}: ${input.name}`;
|
2019-03-22 16:55:35 +01:00
|
|
|
} else {
|
|
|
|
if (input.length > 0) {
|
|
|
|
const inputText = input.slice(0, 100).split(/[\r\n]/)[0];
|
|
|
|
activeTabContent.innerText = `${tabNum}: ${inputText}`;
|
2019-03-22 16:10:19 +01:00
|
|
|
} else {
|
2019-03-22 16:55:35 +01:00
|
|
|
activeTabContent.innerText = `${tabNum}: New Tab`;
|
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-21 13:31:01 +01:00
|
|
|
|
2019-03-22 16:10:19 +01:00
|
|
|
/**
|
|
|
|
* Gets the number of the current active tab
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
getActiveTab() {
|
|
|
|
const activeTabs = document.getElementsByClassName("active-input-tab");
|
|
|
|
if (activeTabs.length > 0) {
|
|
|
|
const activeTab = activeTabs.item(0);
|
|
|
|
const activeTabNum = activeTab.id.replace("input-tab-", "");
|
|
|
|
return activeTabNum;
|
|
|
|
} else {
|
|
|
|
throw "Could not find an active tab";
|
2019-03-21 13:31:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 19:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default InputWaiter;
|