1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-23 05:10:27 +01:00

110 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-10-03 12:17:14 +02:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
(function(){
"use strict";
2019-04-08 00:02:29 +02:00
let scope;
if ((typeof exports) !== "undefined"){
scope = exports;
}
else {
scope = require.register("./gui", {});
}
2017-10-03 12:17:14 +02:00
2019-11-28 01:26:35 +01:00
const logging = require("../lib/logging");
2019-04-08 00:02:29 +02:00
const extension = require("../lib/extension");
2017-10-03 12:17:14 +02:00
scope.createCollapser = function(){
2017-10-10 13:38:55 +02:00
const messages = {
2019-04-08 00:02:29 +02:00
more: extension.getTranslation("more"),
less: extension.getTranslation("less")
2017-10-10 13:38:55 +02:00
};
2017-10-03 12:17:14 +02:00
return function createCollapser(container){
2019-11-28 01:26:35 +01:00
const collapser = document.createElement("span");
2017-10-03 12:17:14 +02:00
collapser.className = "collapser";
2017-10-10 13:38:55 +02:00
["more", "less"].forEach(function(type){
2019-11-28 01:26:35 +01:00
const span = document.createElement("span");
2017-10-10 13:38:55 +02:00
span.className = type;
span.textContent = messages[type];
collapser.appendChild(span);
});
2017-10-03 12:17:14 +02:00
container.appendChild(collapser);
collapser.addEventListener("click", function(){
container.classList.toggle("collapsed");
});
2018-08-22 22:16:49 +02:00
container.classList.add("collapsible");
2017-10-03 12:17:14 +02:00
container.classList.add("collapsed");
};
}();
scope.createActionButtons = function createActionButtons(container, actions, data, horizontal){
2019-11-28 01:26:35 +01:00
actions.forEach(function(action){
const button = document.createElement("button");
button.className = action.name + " action";
2019-04-09 08:29:52 +02:00
button.title = extension.getTranslation(action.name);
if (action.isIcon || action.icon){
button.classList.add("isIcon");
2019-11-28 01:26:35 +01:00
const img = document.createElement("img");
button.appendChild(img);
img.src = "../icons/" + (action.icon || `pageAction-${action.name}.svg`);
}
else {
button.textContent = button.title;
}
2017-10-03 12:17:14 +02:00
button.addEventListener("click", action.callback.bind(undefined, data));
container.appendChild(button);
if (horizontal){
2017-10-03 12:17:14 +02:00
container.appendChild(document.createElement("br"));
}
});
};
scope.modalChoice = function modalChoice(messageText, choices){
2019-11-28 01:26:35 +01:00
logging.message("open modal choice");
return new Promise(function(resolve){
document.body.innerHTML = "";
document.body.className = "modal";
document.body.appendChild(document.createTextNode(messageText));
const stack = document.createElement("div");
stack.className = "stackedInputs";
choices.forEach(function(choice){
const button = document.createElement("button");
button.addEventListener("click", function(){
resolve(choice.value || choice);
2019-11-28 01:26:35 +01:00
logging.message("modal choice closed with value", choice.value || choice);
});
button.appendChild(document.createTextNode(choice.text || choice));
stack.appendChild(button);
});
document.body.append(stack);
});
};
2017-10-03 12:17:14 +02:00
scope.modalPrompt = function modalPrompt(messageText, defaultValue){
2019-11-28 01:26:35 +01:00
logging.message("open modal prompt");
return new Promise(function(resolve){
2017-10-03 12:17:14 +02:00
document.body.innerHTML = "";
document.body.className = "modal";
2017-10-03 12:17:14 +02:00
document.body.appendChild(document.createTextNode(messageText));
const stack = document.createElement("div");
stack.className = "stackedInputs";
const input = document.createElement("input");
2017-10-03 12:17:14 +02:00
input.value = defaultValue;
stack.appendChild(input);
const button = document.createElement("button");
2017-10-03 12:17:14 +02:00
button.textContent = "OK";
button.addEventListener("click", function(){
resolve(input.value);
2019-11-28 01:26:35 +01:00
logging.message("modal prompt closed with value", input.value);
2017-10-03 12:17:14 +02:00
});
stack.appendChild(button);
document.body.append(stack);
2017-10-03 12:17:14 +02:00
});
};
}());