2018-07-23 02:12:08 +00:00
|
|
|
var common = {};
|
2018-04-18 01:35:32 +00:00
|
|
|
|
2018-07-23 02:12:08 +00:00
|
|
|
common.INPUT_TYPES = new Set(["INPUT", "TEXTAREA"]);
|
|
|
|
|
|
|
|
common._request =
|
2018-02-18 00:08:38 +00:00
|
|
|
function _request(method, url, callback)
|
2016-09-18 08:33:46 +00:00
|
|
|
{
|
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.onreadystatechange = function()
|
|
|
|
{
|
|
|
|
if (request.readyState == 4)
|
|
|
|
{
|
|
|
|
if (callback != null)
|
|
|
|
{
|
2018-02-18 02:47:17 +00:00
|
|
|
var response = {
|
2018-09-23 21:57:25 +00:00
|
|
|
"data": JSON.parse(request.responseText),
|
2018-02-18 02:47:17 +00:00
|
|
|
"meta": {}
|
|
|
|
};
|
|
|
|
response["meta"]["request_url"] = url;
|
|
|
|
response["meta"]["status"] = request.status;
|
2017-02-26 08:33:26 +00:00
|
|
|
callback(response);
|
2016-09-18 08:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var asynchronous = true;
|
2018-02-18 00:08:38 +00:00
|
|
|
request.open(method, url, asynchronous);
|
|
|
|
return request;
|
|
|
|
}
|
2018-07-23 02:12:08 +00:00
|
|
|
|
|
|
|
common.get =
|
2018-02-18 00:08:38 +00:00
|
|
|
function get(url, callback)
|
|
|
|
{
|
2018-07-23 02:12:08 +00:00
|
|
|
request = common._request("GET", url, callback);
|
2018-02-18 00:08:38 +00:00
|
|
|
request.send();
|
|
|
|
}
|
2018-07-23 02:12:08 +00:00
|
|
|
|
|
|
|
common.post =
|
2018-02-18 00:08:38 +00:00
|
|
|
function post(url, data, callback)
|
|
|
|
{
|
2018-07-23 02:12:08 +00:00
|
|
|
request = common._request("POST", url, callback);
|
2016-09-18 08:33:46 +00:00
|
|
|
request.send(data);
|
|
|
|
}
|
|
|
|
|
2018-07-23 02:12:08 +00:00
|
|
|
common.bind_box_to_button =
|
2018-02-24 08:24:38 +00:00
|
|
|
function bind_box_to_button(box, button, ctrl_enter)
|
|
|
|
{
|
|
|
|
// Thanks Yaroslav Yakovlev
|
|
|
|
// http://stackoverflow.com/a/9343095
|
|
|
|
var bound_box_hook = function(event)
|
|
|
|
{
|
|
|
|
if (event.key !== "Enter")
|
|
|
|
{return;}
|
|
|
|
|
|
|
|
ctrl_success = !ctrl_enter || (event.ctrlKey)
|
|
|
|
|
|
|
|
if (! ctrl_success)
|
|
|
|
{return;}
|
|
|
|
|
|
|
|
button.click();
|
|
|
|
}
|
|
|
|
box.addEventListener("keyup", bound_box_hook);
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:25:53 +00:00
|
|
|
common.create_message_bubble =
|
|
|
|
function create_message_bubble(message_area, message_positivity, message_text, lifespan)
|
|
|
|
{
|
|
|
|
if (lifespan === undefined)
|
|
|
|
{
|
|
|
|
lifespan = 8000;
|
|
|
|
}
|
|
|
|
var message = document.createElement("div");
|
|
|
|
message.className = "message_bubble " + message_positivity;
|
|
|
|
var span = document.createElement("span");
|
|
|
|
span.innerHTML = message_text;
|
|
|
|
message.appendChild(span);
|
|
|
|
message_area.appendChild(message);
|
|
|
|
setTimeout(function(){message_area.removeChild(message);}, lifespan);
|
|
|
|
}
|
|
|
|
|
|
|
|
common.delete_all_children =
|
|
|
|
function delete_all_children(element)
|
|
|
|
{
|
|
|
|
while (element.firstChild)
|
|
|
|
{
|
|
|
|
element.removeChild(element.firstChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 02:12:08 +00:00
|
|
|
common.entry_with_history_hook =
|
2018-02-24 08:24:38 +00:00
|
|
|
function entry_with_history_hook(event)
|
2016-10-10 03:50:13 +00:00
|
|
|
{
|
2018-02-24 08:24:38 +00:00
|
|
|
//console.log(event);
|
|
|
|
var box = event.target;
|
|
|
|
|
2016-10-10 03:50:13 +00:00
|
|
|
if (box.entry_history === undefined)
|
2018-02-24 08:24:38 +00:00
|
|
|
{box.entry_history = [];}
|
|
|
|
|
2016-10-10 03:50:13 +00:00
|
|
|
if (box.entry_history_pos === undefined)
|
2018-02-24 08:24:38 +00:00
|
|
|
{box.entry_history_pos = -1;}
|
|
|
|
|
|
|
|
if (event.key === "Enter")
|
2016-10-10 03:50:13 +00:00
|
|
|
{
|
|
|
|
box.entry_history.push(box.value);
|
|
|
|
}
|
2018-02-24 08:24:38 +00:00
|
|
|
else if (event.key === "ArrowUp")
|
2016-10-10 03:50:13 +00:00
|
|
|
{
|
|
|
|
if (box.entry_history.length == 0)
|
2018-02-24 08:24:38 +00:00
|
|
|
{return}
|
|
|
|
|
2016-10-10 03:50:13 +00:00
|
|
|
if (box.entry_history_pos == -1)
|
2018-02-24 08:24:38 +00:00
|
|
|
{box.entry_history_pos = box.entry_history.length - 1;}
|
2016-10-10 03:50:13 +00:00
|
|
|
else if (box.entry_history_pos > 0)
|
2018-02-24 08:24:38 +00:00
|
|
|
{box.entry_history_pos -= 1;}
|
|
|
|
|
2016-10-10 03:50:13 +00:00
|
|
|
box.value = box.entry_history[box.entry_history_pos];
|
2017-02-25 06:07:59 +00:00
|
|
|
setTimeout(function(){box.selectionStart = box.value.length;}, 0);
|
2016-10-10 03:50:13 +00:00
|
|
|
}
|
2018-02-24 08:24:38 +00:00
|
|
|
else if (event.key === "Escape")
|
2016-10-10 03:50:13 +00:00
|
|
|
{
|
|
|
|
box.value = "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
box.entry_history_pos = -1;
|
|
|
|
}
|
2016-12-21 09:11:50 +00:00
|
|
|
}
|
2018-02-18 03:12:34 +00:00
|
|
|
|
2020-01-16 04:29:56 +00:00
|
|
|
common.entry_with_tagname_replacements =
|
|
|
|
function entry_with_tagname_replacements(event)
|
|
|
|
{
|
|
|
|
var cursor_position = event.target.selectionStart;
|
|
|
|
event.target.value = common.tagname_replacements(event.target.value);
|
|
|
|
event.target.selectionStart = cursor_position;
|
|
|
|
event.target.selectionEnd = cursor_position;
|
|
|
|
}
|
|
|
|
|
2018-07-23 02:12:08 +00:00
|
|
|
common.html_to_element =
|
2018-02-18 03:12:34 +00:00
|
|
|
function html_to_element(html)
|
|
|
|
{
|
|
|
|
var template = document.createElement("template");
|
|
|
|
template.innerHTML = html;
|
|
|
|
return template.content.firstChild;
|
|
|
|
}
|
2018-07-23 01:17:39 +00:00
|
|
|
|
2018-07-29 03:21:20 +00:00
|
|
|
common.init_button_with_confirm =
|
|
|
|
function init_button_with_confirm()
|
|
|
|
{
|
|
|
|
/*
|
2019-06-15 09:15:31 +00:00
|
|
|
To create a button that requires a second confirmation step, assign it the
|
|
|
|
class "button_with_confirm".
|
|
|
|
|
|
|
|
Required:
|
|
|
|
data-onclick: String that would normally be the button's onclick.
|
2018-07-29 03:21:20 +00:00
|
|
|
|
|
|
|
Optional:
|
2019-06-15 09:15:31 +00:00
|
|
|
data-prompt: Text that appears next to the confirm button. Default is
|
|
|
|
"Are you sure?".
|
2018-07-29 03:21:20 +00:00
|
|
|
data-prompt-class
|
|
|
|
|
2019-06-15 09:15:31 +00:00
|
|
|
data-confirm: Text inside the confirm button. Default is to inherit the
|
|
|
|
original button's text.
|
2018-07-29 03:21:20 +00:00
|
|
|
data-confirm-class
|
|
|
|
|
2019-06-15 09:15:31 +00:00
|
|
|
data-cancel: Text inside the cancel button. Default is "Cancel".
|
2018-07-29 03:21:20 +00:00
|
|
|
data-cancel-class
|
2019-06-15 23:02:41 +00:00
|
|
|
|
|
|
|
data-holder-class: CSS class for the new span that holds the menu.
|
2018-07-29 03:21:20 +00:00
|
|
|
*/
|
2019-06-15 20:49:33 +00:00
|
|
|
var buttons = Array.from(document.getElementsByClassName("button_with_confirm"));
|
2019-06-15 23:02:41 +00:00
|
|
|
buttons.forEach(function(button)
|
2018-07-29 03:21:20 +00:00
|
|
|
{
|
2019-06-15 20:16:38 +00:00
|
|
|
button.classList.remove("button_with_confirm");
|
|
|
|
|
2018-07-29 03:21:20 +00:00
|
|
|
var holder = document.createElement("span");
|
|
|
|
holder.classList.add("confirm_holder");
|
2018-09-23 22:03:50 +00:00
|
|
|
holder.classList.add(button.dataset.holderClass || "confirm_holder");
|
2018-07-29 03:21:20 +00:00
|
|
|
button.parentElement.insertBefore(holder, button);
|
|
|
|
button.parentElement.removeChild(button);
|
|
|
|
|
|
|
|
var holder_stage1 = document.createElement("span");
|
|
|
|
holder_stage1.classList.add("confirm_holder_stage1");
|
|
|
|
holder_stage1.appendChild(button);
|
|
|
|
holder.appendChild(holder_stage1);
|
|
|
|
|
|
|
|
var holder_stage2 = document.createElement("span");
|
|
|
|
holder_stage2.classList.add("confirm_holder_stage2");
|
|
|
|
holder_stage2.classList.add("hidden");
|
|
|
|
holder.appendChild(holder_stage2);
|
|
|
|
|
2019-04-27 22:30:33 +00:00
|
|
|
var prompt;
|
|
|
|
var input_source;
|
|
|
|
if (button.dataset.isInput)
|
|
|
|
{
|
|
|
|
prompt = document.createElement("input");
|
|
|
|
prompt.placeholder = button.dataset.prompt || "";
|
|
|
|
input_source = prompt;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prompt = document.createElement("span");
|
|
|
|
prompt.innerText = (button.dataset.prompt || "Are you sure?") + " ";
|
|
|
|
input_source = undefined;
|
|
|
|
}
|
|
|
|
prompt.className = button.dataset.promptClass || "";
|
|
|
|
holder_stage2.appendChild(prompt)
|
2018-07-29 03:21:20 +00:00
|
|
|
delete button.dataset.prompt;
|
|
|
|
delete button.dataset.promptClass;
|
|
|
|
|
|
|
|
var button_confirm = document.createElement("button");
|
2018-09-23 21:57:25 +00:00
|
|
|
button_confirm.innerText = (button.dataset.confirm || button.innerText).trim();
|
2018-07-29 03:21:20 +00:00
|
|
|
button_confirm.className = button.dataset.confirmClass || "";
|
2019-04-27 22:30:33 +00:00
|
|
|
button_confirm.input_source = input_source;
|
2018-07-29 03:21:20 +00:00
|
|
|
holder_stage2.appendChild(button_confirm);
|
2018-08-17 02:46:33 +00:00
|
|
|
holder_stage2.appendChild(document.createTextNode(" "));
|
2019-04-27 22:30:33 +00:00
|
|
|
if (button.dataset.isInput)
|
|
|
|
{
|
|
|
|
common.bind_box_to_button(prompt, button_confirm);
|
|
|
|
}
|
2018-07-29 03:21:20 +00:00
|
|
|
delete button.dataset.confirm;
|
|
|
|
delete button.dataset.confirmClass;
|
2019-04-27 22:30:33 +00:00
|
|
|
delete button.dataset.isInput;
|
2018-07-29 03:21:20 +00:00
|
|
|
|
|
|
|
var button_cancel = document.createElement("button");
|
|
|
|
button_cancel.innerText = button.dataset.cancel || "Cancel";
|
|
|
|
button_cancel.className = button.dataset.cancelClass || "";
|
|
|
|
holder_stage2.appendChild(button_cancel);
|
|
|
|
delete button.dataset.cancel;
|
|
|
|
delete button.dataset.cancelClass;
|
|
|
|
|
|
|
|
// If this is stupid, let me know.
|
|
|
|
var confirm_onclick = button.dataset.onclick + `
|
|
|
|
;
|
|
|
|
var holder = event.target.parentElement.parentElement;
|
|
|
|
holder.getElementsByClassName("confirm_holder_stage1")[0].classList.remove("hidden");
|
|
|
|
holder.getElementsByClassName("confirm_holder_stage2")[0].classList.add("hidden");
|
|
|
|
`
|
2019-04-27 22:28:22 +00:00
|
|
|
button_confirm.onclick = Function(confirm_onclick);
|
2018-07-29 03:21:20 +00:00
|
|
|
button.removeAttribute("onclick");
|
|
|
|
button.onclick = function(event)
|
|
|
|
{
|
|
|
|
var holder = event.target.parentElement.parentElement;
|
|
|
|
holder.getElementsByClassName("confirm_holder_stage1")[0].classList.add("hidden");
|
|
|
|
holder.getElementsByClassName("confirm_holder_stage2")[0].classList.remove("hidden");
|
2019-06-15 20:04:33 +00:00
|
|
|
var input = holder.getElementsByTagName("input")[0];
|
2019-04-27 22:30:33 +00:00
|
|
|
if (input)
|
|
|
|
{
|
2019-06-15 20:04:33 +00:00
|
|
|
input.focus();
|
2019-04-27 22:30:33 +00:00
|
|
|
}
|
2018-07-29 03:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
button_cancel.onclick = function(event)
|
|
|
|
{
|
|
|
|
var holder = event.target.parentElement.parentElement;
|
|
|
|
holder.getElementsByClassName("confirm_holder_stage1")[0].classList.remove("hidden");
|
|
|
|
holder.getElementsByClassName("confirm_holder_stage2")[0].classList.add("hidden");
|
|
|
|
}
|
|
|
|
delete button.dataset.onclick;
|
2019-06-15 23:02:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var spinner_button_count = 0;
|
|
|
|
common.init_button_with_spinner =
|
|
|
|
function init_button_with_spinner()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
To create a button that has a spinner, and cannot be clicked again while
|
|
|
|
the action is running, assign it the class "button_with_spinner".
|
|
|
|
|
|
|
|
Required:
|
|
|
|
data-onclick: The string that would normally be the button's onclick.
|
|
|
|
|
|
|
|
Optional:
|
|
|
|
data-spinner-id: If you want to use your own element as the spinner,
|
|
|
|
give its ID here. Otherwise a new one will be created.
|
|
|
|
|
|
|
|
data-holder-class: CSS class for the new span that holds the menu.
|
|
|
|
*/
|
|
|
|
var buttons = Array.from(document.getElementsByClassName("button_with_spinner"));
|
|
|
|
buttons.forEach(function(button)
|
|
|
|
{
|
|
|
|
button.classList.remove("button_with_spinner");
|
|
|
|
button.innerHTML = button.innerHTML.trim();
|
|
|
|
|
|
|
|
var holder = document.createElement("span");
|
|
|
|
holder.classList.add("spinner_holder");
|
|
|
|
holder.classList.add(button.dataset.holderClass || "spinner_holder");
|
|
|
|
button.parentElement.insertBefore(holder, button);
|
|
|
|
button.parentElement.removeChild(button);
|
|
|
|
holder.appendChild(button);
|
|
|
|
|
|
|
|
var spinner_element;
|
|
|
|
if (button.dataset.spinnerId)
|
|
|
|
{
|
|
|
|
spinner_element = document.getElementById(button.dataset.spinnerId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
spinner_element = document.createElement("span");
|
|
|
|
spinner_element.innerText = "Working...";
|
|
|
|
spinner_element.classList.add("hidden");
|
|
|
|
holder.appendChild(spinner_element);
|
|
|
|
}
|
|
|
|
|
|
|
|
var spin = new spinner.Spinner(spinner_element);
|
|
|
|
var spin_delay = parseFloat(button.dataset.spinnerDelay) || 0;
|
|
|
|
|
|
|
|
var wrapped_onclick = Function(button.dataset.onclick);
|
|
|
|
button.onclick = function()
|
|
|
|
{
|
|
|
|
spin.show(spin_delay);
|
|
|
|
button.disabled = true;
|
|
|
|
wrapped_onclick();
|
|
|
|
}
|
|
|
|
delete button.dataset.onclick;
|
|
|
|
|
|
|
|
var closer_id = "spinner_closer_" + spinner_button_count;
|
|
|
|
spinner_button_count += 1;
|
|
|
|
window[closer_id] = function spinner_closer()
|
|
|
|
{
|
|
|
|
spin.hide();
|
|
|
|
button.disabled = false;
|
|
|
|
}
|
|
|
|
button.dataset.spinnerCloser = closer_id;
|
|
|
|
});
|
2018-07-29 03:21:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 08:25:53 +00:00
|
|
|
common.normalize_tagname =
|
|
|
|
function normalize_tagname(tagname)
|
|
|
|
{
|
|
|
|
tagname = tagname.trim();
|
|
|
|
tagname = tagname.toLocaleLowerCase();
|
|
|
|
tagname = tagname.split(".");
|
|
|
|
tagname = tagname[tagname.length-1];
|
|
|
|
tagname = tagname.split("+")[0];
|
2020-01-16 04:29:56 +00:00
|
|
|
tagname = common.tagname_replacements(tagname);
|
|
|
|
return tagname;
|
|
|
|
}
|
|
|
|
|
|
|
|
common.tagname_replacements =
|
|
|
|
function tagname_replacements(tagname)
|
|
|
|
{
|
2018-07-29 08:25:53 +00:00
|
|
|
tagname = tagname.replace(new RegExp(" ", 'g'), "_");
|
|
|
|
tagname = tagname.replace(new RegExp("-", 'g'), "_");
|
|
|
|
return tagname;
|
|
|
|
}
|
|
|
|
|
|
|
|
common.refresh =
|
|
|
|
function refresh()
|
|
|
|
{
|
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
|
2018-07-29 03:21:20 +00:00
|
|
|
common.on_pageload =
|
|
|
|
function on_pageload()
|
|
|
|
{
|
|
|
|
common.init_button_with_confirm();
|
2019-06-15 23:02:41 +00:00
|
|
|
common.init_button_with_spinner();
|
2018-07-29 03:21:20 +00:00
|
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", common.on_pageload);
|