CyberChef/src/web/OptionsWaiter.js

146 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-11-28 11:42:58 +01:00
/**
* Waiter to handle events related to the CyberChef options.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*
* @constructor
* @param {App} app - The main view object for CyberChef.
2016-11-28 11:42:58 +01:00
*/
2017-04-13 19:08:50 +02:00
const OptionsWaiter = function(app) {
2016-11-28 11:42:58 +01:00
this.app = app;
};
/**
* Loads options and sets values of switches and inputs to match them.
*
* @param {Object} options
*/
OptionsWaiter.prototype.load = function(options) {
$(".option-item input:checkbox").bootstrapSwitch({
size: "small",
animate: false,
});
2017-02-09 16:09:33 +01:00
2017-04-13 19:08:50 +02:00
for (const option in options) {
2016-11-28 11:42:58 +01:00
this.app.options[option] = options[option];
}
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
// Set options to match object
2017-04-13 19:08:50 +02:00
const cboxes = document.querySelectorAll("#options-body input[type=checkbox]");
2017-04-13 19:31:26 +02:00
let i;
for (i = 0; i < cboxes.length; i++) {
2016-11-28 11:42:58 +01:00
$(cboxes[i]).bootstrapSwitch("state", this.app.options[cboxes[i].getAttribute("option")]);
}
2017-04-13 19:08:50 +02:00
const nboxes = document.querySelectorAll("#options-body input[type=number]");
2016-11-28 11:42:58 +01:00
for (i = 0; i < nboxes.length; i++) {
nboxes[i].value = this.app.options[nboxes[i].getAttribute("option")];
nboxes[i].dispatchEvent(new CustomEvent("change", {bubbles: true}));
}
2017-02-09 16:09:33 +01:00
2017-04-13 19:08:50 +02:00
const selects = document.querySelectorAll("#options-body select");
2016-11-28 11:42:58 +01:00
for (i = 0; i < selects.length; i++) {
selects[i].value = this.app.options[selects[i].getAttribute("option")];
selects[i].dispatchEvent(new CustomEvent("change", {bubbles: true}));
}
};
/**
* Handler for options click events.
* Dispays the options pane.
*/
OptionsWaiter.prototype.optionsClick = function() {
2016-11-28 11:42:58 +01:00
$("#options-modal").modal();
};
/**
* Handler for reset options click events.
* Resets options back to their default values.
*/
OptionsWaiter.prototype.resetOptionsClick = function() {
2016-11-28 11:42:58 +01:00
this.load(this.app.doptions);
};
/**
* Handler for switch change events.
* Modifies the option state and saves it to local storage.
*
* @param {event} e
* @param {boolean} state
*/
OptionsWaiter.prototype.switchChange = function(e, state) {
2017-04-13 19:08:50 +02:00
let el = e.target,
2016-11-28 11:42:58 +01:00
option = el.getAttribute("option");
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
this.app.options[option] = state;
localStorage.setItem("options", JSON.stringify(this.app.options));
};
/**
* Handler for number change events.
* Modifies the option value and saves it to local storage.
*
* @param {event} e
*/
OptionsWaiter.prototype.numberChange = function(e) {
2017-04-13 19:08:50 +02:00
let el = e.target,
2016-11-28 11:42:58 +01:00
option = el.getAttribute("option");
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
this.app.options[option] = parseInt(el.value, 10);
localStorage.setItem("options", JSON.stringify(this.app.options));
};
/**
* Handler for select change events.
* Modifies the option value and saves it to local storage.
*
* @param {event} e
*/
OptionsWaiter.prototype.selectChange = function(e) {
2017-04-13 19:08:50 +02:00
let el = e.target,
2016-11-28 11:42:58 +01:00
option = el.getAttribute("option");
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
this.app.options[option] = el.value;
localStorage.setItem("options", JSON.stringify(this.app.options));
};
/**
* Sets or unsets word wrap on the input and output depending on the wordWrap option value.
2016-11-28 11:42:58 +01:00
*/
OptionsWaiter.prototype.setWordWrap = function() {
2016-11-28 11:42:58 +01:00
document.getElementById("input-text").classList.remove("word-wrap");
document.getElementById("output-text").classList.remove("word-wrap");
document.getElementById("output-html").classList.remove("word-wrap");
document.getElementById("input-highlighter").classList.remove("word-wrap");
document.getElementById("output-highlighter").classList.remove("word-wrap");
2017-02-09 16:09:33 +01:00
if (!this.app.options.wordWrap) {
2016-11-28 11:42:58 +01:00
document.getElementById("input-text").classList.add("word-wrap");
document.getElementById("output-text").classList.add("word-wrap");
document.getElementById("output-html").classList.add("word-wrap");
document.getElementById("input-highlighter").classList.add("word-wrap");
document.getElementById("output-highlighter").classList.add("word-wrap");
}
};
2017-04-25 01:21:38 +02:00
/**
* Changes the theme by setting the class of the <html> element.
*/
OptionsWaiter.prototype.themeChange = function (e) {
var themeClass = e.target.value;
document.querySelector(":root").className = themeClass;
};
export default OptionsWaiter;