2020-09-15 01:33:53 +00:00
|
|
|
const editor = {};
|
2018-02-24 08:46:55 +00:00
|
|
|
|
2018-07-23 02:48:44 +00:00
|
|
|
editor.PARAGRAPH_TYPES = new Set(["P", "PRE"]);
|
|
|
|
|
|
|
|
editor.Editor =
|
2021-01-02 03:46:06 +00:00
|
|
|
function Editor(element_argss, on_open, on_save, on_cancel)
|
2018-02-24 08:46:55 +00:00
|
|
|
{
|
|
|
|
/*
|
2021-01-02 03:46:06 +00:00
|
|
|
This class wraps around display elements like spans, headers, and
|
|
|
|
paragraphs, and creates edit elements like inputs and textareas to edit
|
|
|
|
them with.
|
|
|
|
|
|
|
|
element_argss should be a list of dicts. Each dict is required to have "id"
|
|
|
|
which is unique amongst its peers, and "element" which is the display
|
|
|
|
element. Additionally, you may add the following properties to change the
|
|
|
|
element's behavior:
|
|
|
|
|
|
|
|
"autofocus": true
|
|
|
|
When the user opens the editor, this element will get .focus().
|
|
|
|
Only one element should have this.
|
|
|
|
|
|
|
|
"empty_text": string
|
|
|
|
If the display element contains this text, then the edit element will
|
|
|
|
be set to "" when opened.
|
2018-03-10 21:23:37 +00:00
|
|
|
If the edit element contains "", then the display element will
|
|
|
|
contain this text when saved.
|
2021-01-02 03:46:06 +00:00
|
|
|
|
|
|
|
"hide_when_empty": true
|
|
|
|
If the element does not have any text, it will get the "hidden" css
|
|
|
|
class after saving / closing.
|
|
|
|
|
|
|
|
"placeholder": string
|
|
|
|
The placeholder attribute of the edit element.
|
|
|
|
|
|
|
|
The editor object will contain a dict called elements that maps IDs to the
|
|
|
|
display element, edit elements, and your other options.
|
|
|
|
|
|
|
|
Your on_open, on_save and on_cancel hooks will be called with the editor
|
|
|
|
object as the only argument.
|
2018-02-24 08:46:55 +00:00
|
|
|
|
|
|
|
When your callbacks are used, the default `open`, `save`, `cancel`
|
|
|
|
methods are not called automatically. You should call them from within
|
2018-03-10 21:23:37 +00:00
|
|
|
your function. That's because you may wish to do some of your own
|
|
|
|
normalization before the default handler, and some of your own cleanup
|
|
|
|
after it. So it is up to you when to call the default.
|
2018-02-24 08:46:55 +00:00
|
|
|
*/
|
|
|
|
this.cancel = function()
|
|
|
|
{
|
|
|
|
this.close();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.close = function()
|
|
|
|
{
|
2021-01-02 03:46:06 +00:00
|
|
|
for (const element of Object.values(this.elements))
|
2018-02-24 08:46:55 +00:00
|
|
|
{
|
2021-01-02 03:46:06 +00:00
|
|
|
element.edit.classList.add("hidden");
|
|
|
|
if (! (element.display.innerText === "" && element.hide_when_empty))
|
|
|
|
{
|
|
|
|
element.display.classList.remove("hidden");
|
|
|
|
}
|
2018-02-24 08:46:55 +00:00
|
|
|
}
|
2019-01-13 22:35:54 +00:00
|
|
|
this.hide_spinner();
|
|
|
|
this.hide_error();
|
|
|
|
this.open_button.classList.remove("hidden");
|
2018-02-24 08:46:55 +00:00
|
|
|
this.save_button.classList.add("hidden");
|
|
|
|
this.cancel_button.classList.add("hidden");
|
|
|
|
};
|
|
|
|
|
2019-01-13 22:35:54 +00:00
|
|
|
this.hide_error = function()
|
|
|
|
{
|
|
|
|
this.error_message.classList.add("hidden");
|
|
|
|
};
|
|
|
|
|
2018-02-24 08:46:55 +00:00
|
|
|
this.hide_spinner = function()
|
|
|
|
{
|
2019-04-27 22:42:02 +00:00
|
|
|
this.spinner.hide();
|
2018-02-24 08:46:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.open = function()
|
|
|
|
{
|
2021-01-02 03:46:06 +00:00
|
|
|
for (const element of Object.values(this.elements))
|
2018-02-24 08:46:55 +00:00
|
|
|
{
|
2021-01-02 03:46:06 +00:00
|
|
|
element.display.classList.add("hidden");
|
|
|
|
element.edit.classList.remove("hidden");
|
2019-01-13 22:35:54 +00:00
|
|
|
|
2021-01-02 03:46:06 +00:00
|
|
|
if (element.autofocus)
|
|
|
|
{
|
|
|
|
element.edit.focus();
|
|
|
|
}
|
2018-02-24 08:46:55 +00:00
|
|
|
|
2021-01-02 03:46:06 +00:00
|
|
|
if (element.empty_text !== undefined && element.display.innerText == element.empty_text)
|
2018-02-24 08:46:55 +00:00
|
|
|
{
|
2021-01-02 03:46:06 +00:00
|
|
|
element.edit.value = "";
|
2018-02-24 08:46:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 03:46:06 +00:00
|
|
|
element.edit.value = element.display.innerText;
|
2018-02-24 08:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-13 22:35:54 +00:00
|
|
|
this.open_button.classList.add("hidden");
|
2018-02-24 08:46:55 +00:00
|
|
|
this.save_button.classList.remove("hidden");
|
|
|
|
this.cancel_button.classList.remove("hidden");
|
|
|
|
};
|
|
|
|
|
|
|
|
this.save = function()
|
|
|
|
{
|
2021-01-02 03:46:06 +00:00
|
|
|
for (const element of Object.values(this.elements))
|
2018-02-24 08:46:55 +00:00
|
|
|
{
|
2021-01-02 03:46:06 +00:00
|
|
|
if (element.empty_text !== undefined && element.edit.value == "")
|
2018-02-24 08:46:55 +00:00
|
|
|
{
|
2021-01-02 03:46:06 +00:00
|
|
|
element.display.innerText = element.empty_text;
|
2018-02-24 08:46:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 03:46:06 +00:00
|
|
|
element.display.innerText = element.edit.value;
|
2018-02-24 08:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.close();
|
|
|
|
};
|
|
|
|
|
2019-01-13 22:35:54 +00:00
|
|
|
this.show_error = function(message)
|
|
|
|
{
|
|
|
|
this.hide_spinner();
|
|
|
|
this.error_message.innerText = message;
|
|
|
|
this.error_message.classList.remove("hidden");
|
|
|
|
};
|
|
|
|
|
2019-04-27 22:42:02 +00:00
|
|
|
this.show_spinner = function(delay)
|
2018-02-24 08:46:55 +00:00
|
|
|
{
|
2019-01-13 22:35:54 +00:00
|
|
|
this.hide_error();
|
2019-04-27 22:42:02 +00:00
|
|
|
this.spinner.show(delay);
|
2018-02-24 08:46:55 +00:00
|
|
|
};
|
|
|
|
|
2021-01-02 03:46:06 +00:00
|
|
|
this.elements = {};
|
2018-02-24 08:46:55 +00:00
|
|
|
|
2021-01-02 03:46:06 +00:00
|
|
|
// End-user can put anything they want in here.
|
2018-02-24 08:46:55 +00:00
|
|
|
this.misc_data = {};
|
|
|
|
|
2021-01-02 03:46:06 +00:00
|
|
|
// Keep track of last edit element so we can put the toolbox after it.
|
|
|
|
let last_element;
|
|
|
|
|
|
|
|
for (const element_args of element_argss)
|
2018-02-24 08:46:55 +00:00
|
|
|
{
|
2021-01-02 03:46:06 +00:00
|
|
|
const element = {};
|
|
|
|
element.id = element_args.id;
|
|
|
|
this.elements[element.id] = element;
|
|
|
|
|
|
|
|
element.display = element_args.element;
|
|
|
|
element.empty_text = element_args.empty_text;
|
|
|
|
element.hide_when_empty = element_args.hide_when_empty;
|
|
|
|
element.autofocus = element_args.autofocus;
|
|
|
|
|
|
|
|
if (editor.PARAGRAPH_TYPES.has(element.display.tagName))
|
2018-02-24 08:46:55 +00:00
|
|
|
{
|
2021-01-02 03:46:06 +00:00
|
|
|
element.edit = document.createElement("textarea");
|
|
|
|
element.edit.rows = 6;
|
2018-02-24 08:46:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 03:46:06 +00:00
|
|
|
element.edit = document.createElement("input");
|
|
|
|
element.edit.type = "text";
|
2018-02-24 08:46:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 03:46:06 +00:00
|
|
|
element.edit.classList.add("editor_input");
|
|
|
|
element.edit.classList.add("hidden");
|
2018-02-24 08:46:55 +00:00
|
|
|
|
2021-01-02 03:46:06 +00:00
|
|
|
if (element_args.placeholder !== undefined)
|
2018-02-24 08:46:55 +00:00
|
|
|
{
|
2021-01-02 03:46:06 +00:00
|
|
|
element.edit.placeholder = element_args.placeholder;
|
2018-02-24 08:46:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 03:46:06 +00:00
|
|
|
element.display.parentElement.insertBefore(element.edit, element.display.nextSibling);
|
|
|
|
last_element = element.edit;
|
2018-02-24 08:46:55 +00:00
|
|
|
}
|
|
|
|
|
2018-03-10 21:23:37 +00:00
|
|
|
this.binder = function(func, fallback)
|
2018-02-24 08:46:55 +00:00
|
|
|
{
|
2018-03-10 21:23:37 +00:00
|
|
|
/*
|
2021-01-02 03:46:06 +00:00
|
|
|
Given a function that takes an Editor as its first argument,
|
|
|
|
return a new function which requires no arguments and calls the
|
|
|
|
function with this editor.
|
2018-03-10 21:23:37 +00:00
|
|
|
|
|
|
|
This is done so that the new function can be used in an event handler.
|
|
|
|
*/
|
2018-02-24 08:46:55 +00:00
|
|
|
if (func == undefined)
|
|
|
|
{
|
2018-03-23 17:25:03 +00:00
|
|
|
return fallback.bind(this);
|
2018-02-24 08:46:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 03:46:06 +00:00
|
|
|
const bindable = () => func(this);
|
|
|
|
return bindable.bind(this);
|
2018-02-24 08:46:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 03:46:06 +00:00
|
|
|
// In order to prevent page jumping on load, you can add an element with
|
|
|
|
// class editor_toolbox_placeholder to the page and size it so it matches
|
|
|
|
// the buttons that are going to get placed there.
|
2020-09-15 01:33:53 +00:00
|
|
|
const placeholders = document.getElementsByClassName("editor_toolbox_placeholder");
|
2020-09-03 22:33:37 +00:00
|
|
|
for (const placeholder of placeholders)
|
2018-11-18 00:50:41 +00:00
|
|
|
{
|
2020-09-03 22:33:37 +00:00
|
|
|
placeholder.parentElement.removeChild(placeholder);
|
2018-11-18 00:50:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 01:33:53 +00:00
|
|
|
const toolbox = document.createElement("div");
|
2018-02-24 08:46:55 +00:00
|
|
|
toolbox.classList.add("editor_toolbox");
|
|
|
|
last_element.parentElement.insertBefore(toolbox, last_element.nextSibling);
|
|
|
|
|
|
|
|
this.open_button = document.createElement("button");
|
|
|
|
this.open_button.innerText = "Edit";
|
|
|
|
this.open_button.classList.add("editor_button");
|
|
|
|
this.open_button.classList.add("editor_open_button");
|
|
|
|
this.open_button.classList.add("green_button");
|
2018-03-10 21:28:13 +00:00
|
|
|
this.open_button.onclick = this.binder(on_open, this.open);
|
2018-02-24 08:46:55 +00:00
|
|
|
toolbox.appendChild(this.open_button);
|
|
|
|
|
|
|
|
this.save_button = document.createElement("button");
|
|
|
|
this.save_button.innerText = "Save";
|
|
|
|
this.save_button.classList.add("editor_button");
|
|
|
|
this.save_button.classList.add("editor_save_button");
|
|
|
|
this.save_button.classList.add("green_button");
|
|
|
|
this.save_button.classList.add("hidden");
|
2018-03-10 21:23:37 +00:00
|
|
|
this.save_button.onclick = this.binder(on_save, this.save);
|
2018-02-24 08:46:55 +00:00
|
|
|
toolbox.appendChild(this.save_button);
|
2018-08-17 02:46:33 +00:00
|
|
|
toolbox.appendChild(document.createTextNode(" "));
|
2018-02-24 08:46:55 +00:00
|
|
|
|
|
|
|
this.cancel_button = document.createElement("button");
|
|
|
|
this.cancel_button.innerText = "Cancel";
|
|
|
|
this.cancel_button.classList.add("editor_button");
|
|
|
|
this.cancel_button.classList.add("editor_cancel_button");
|
2018-07-29 03:18:10 +00:00
|
|
|
this.cancel_button.classList.add("gray_button");
|
2018-02-24 08:46:55 +00:00
|
|
|
this.cancel_button.classList.add("hidden");
|
2018-03-10 21:23:37 +00:00
|
|
|
this.cancel_button.onclick = this.binder(on_cancel, this.cancel);
|
2018-02-24 08:46:55 +00:00
|
|
|
toolbox.appendChild(this.cancel_button);
|
|
|
|
|
2019-01-13 22:35:54 +00:00
|
|
|
this.error_message = document.createElement("span");
|
|
|
|
this.error_message.classList.add("editor_error");
|
|
|
|
this.error_message.classList.add("hidden");
|
|
|
|
toolbox.appendChild(this.error_message);
|
|
|
|
|
2019-04-27 22:42:02 +00:00
|
|
|
spinner_element = document.createElement("span");
|
|
|
|
spinner_element.innerText = "Submitting...";
|
|
|
|
spinner_element.classList.add("editor_spinner");
|
|
|
|
spinner_element.classList.add("hidden");
|
|
|
|
this.spinner = new spinner.Spinner(spinner_element);
|
|
|
|
toolbox.appendChild(spinner_element);
|
2018-02-24 08:46:55 +00:00
|
|
|
|
2021-01-02 03:46:06 +00:00
|
|
|
for (const element of Object.values(this.elements))
|
2018-02-24 08:46:55 +00:00
|
|
|
{
|
2021-01-02 03:46:06 +00:00
|
|
|
const ctrl_enter = element.edit.tagName == "TEXTAREA";
|
|
|
|
common.bind_box_to_button(element.edit, this.save_button, ctrl_enter);
|
2018-02-24 08:46:55 +00:00
|
|
|
}
|
|
|
|
}
|