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");
|
|
|
|
};
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
2017-12-23 23:49:05 +01:00
|
|
|
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");
|
2017-10-14 12:24:53 +02:00
|
|
|
button.className = action.name + " action";
|
2019-04-09 08:29:52 +02:00
|
|
|
button.title = extension.getTranslation(action.name);
|
2017-10-14 12:24:53 +02:00
|
|
|
if (action.isIcon || action.icon){
|
|
|
|
button.classList.add("isIcon");
|
2019-11-28 01:26:35 +01:00
|
|
|
const img = document.createElement("img");
|
2017-10-14 12:24:53 +02:00
|
|
|
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);
|
2018-08-28 08:29:01 +02:00
|
|
|
if (horizontal){
|
2017-10-03 12:17:14 +02:00
|
|
|
container.appendChild(document.createElement("br"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-09-11 23:54:59 +02:00
|
|
|
scope.modalChoice = function modalChoice(messageText, choices){
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("open modal choice");
|
|
|
|
return new Promise(function(resolve){
|
2018-09-11 23:54:59 +02:00
|
|
|
document.body.innerHTML = "";
|
|
|
|
document.body.className = "modal";
|
|
|
|
document.body.appendChild(document.createTextNode(messageText));
|
2019-05-03 23:17:35 +02:00
|
|
|
const stack = document.createElement("div");
|
|
|
|
stack.className = "stackedInputs";
|
2018-09-11 23:54:59 +02:00
|
|
|
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);
|
2018-09-11 23:54:59 +02:00
|
|
|
});
|
|
|
|
button.appendChild(document.createTextNode(choice.text || choice));
|
2019-05-03 23:17:35 +02:00
|
|
|
stack.appendChild(button);
|
2018-09-11 23:54:59 +02:00
|
|
|
});
|
2019-05-03 23:17:35 +02:00
|
|
|
document.body.append(stack);
|
2018-09-11 23:54:59 +02:00
|
|
|
});
|
|
|
|
};
|
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 = "";
|
2018-09-11 23:54:59 +02:00
|
|
|
document.body.className = "modal";
|
2017-10-03 12:17:14 +02:00
|
|
|
document.body.appendChild(document.createTextNode(messageText));
|
2019-05-03 23:17:35 +02:00
|
|
|
const stack = document.createElement("div");
|
|
|
|
stack.className = "stackedInputs";
|
|
|
|
const input = document.createElement("input");
|
2017-10-03 12:17:14 +02:00
|
|
|
input.value = defaultValue;
|
2019-05-03 23:17:35 +02:00
|
|
|
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
|
|
|
});
|
2019-05-03 23:17:35 +02:00
|
|
|
stack.appendChild(button);
|
|
|
|
document.body.append(stack);
|
2017-10-03 12:17:14 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}());
|