2018-04-18 01:35:32 +00:00
|
|
|
var INPUT_TYPES = new Set(["INPUT", "TEXTAREA"]);
|
|
|
|
|
2016-12-21 05:53:59 +00:00
|
|
|
function create_message_bubble(message_area, message_positivity, message_text, lifespan)
|
2016-09-18 08:33:46 +00:00
|
|
|
{
|
|
|
|
if (lifespan === undefined)
|
|
|
|
{
|
|
|
|
lifespan = 8000;
|
|
|
|
}
|
|
|
|
var message = document.createElement("div");
|
2017-03-04 06:30:57 +00:00
|
|
|
message.className = "message_bubble " + message_positivity;
|
2016-09-18 08:33:46 +00:00
|
|
|
var span = document.createElement("span");
|
|
|
|
span.innerHTML = message_text;
|
|
|
|
message.appendChild(span);
|
|
|
|
message_area.appendChild(message);
|
|
|
|
setTimeout(function(){message_area.removeChild(message);}, lifespan);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2016-12-21 09:11:50 +00:00
|
|
|
var text = request.responseText;
|
2018-02-18 02:47:17 +00:00
|
|
|
var response = {
|
|
|
|
"data": JSON.parse(text),
|
|
|
|
"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;
|
|
|
|
}
|
|
|
|
function get(url, callback)
|
|
|
|
{
|
|
|
|
request = _request("GET", url, callback);
|
|
|
|
request.send();
|
|
|
|
}
|
|
|
|
function post(url, data, callback)
|
|
|
|
{
|
|
|
|
request = _request("POST", url, callback);
|
2016-09-18 08:33:46 +00:00
|
|
|
request.send(data);
|
|
|
|
}
|
|
|
|
|
2017-12-16 11:47:54 +00:00
|
|
|
function delete_all_children(element)
|
|
|
|
{
|
|
|
|
while (element.firstChild)
|
|
|
|
{
|
|
|
|
element.removeChild(element.firstChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
function normalize_tagname(tagname)
|
|
|
|
{
|
|
|
|
tagname = tagname.trim();
|
|
|
|
tagname = tagname.toLocaleLowerCase();
|
|
|
|
tagname = tagname.split(".");
|
|
|
|
tagname = tagname[tagname.length-1];
|
|
|
|
tagname = tagname.split("+")[0];
|
|
|
|
tagname = tagname.replace(new RegExp(" ", 'g'), "_");
|
|
|
|
tagname = tagname.replace(new RegExp("-", 'g'), "_");
|
|
|
|
return tagname;
|
|
|
|
}
|