123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- const Keyboard = {
- elements: {
- main: null,
- keysContainer: null,
- keys: []
- },
- eventHandlers: {
- oninput: null,
- onclose: null
- },
- properties: {
- value: "",
- capsLock: false
- },
- init() {
- // Create main elements
- this.elements.main = document.createElement("div");
- this.elements.keysContainer = document.createElement("div");
- // Setup main elements
- this.elements.main.classList.add("keyboard");
- this.elements.keysContainer.classList.add("keyboard__keys");
- this.elements.keysContainer.appendChild(this._createKeys());
- this.elements.keys = this.elements.keysContainer.querySelectorAll(".keyboard__key");
- // Add to DOM
- this.elements.main.appendChild(this.elements.keysContainer);
- document.body.appendChild(this.elements.main);
- // Automatically use keyboard for elements with .use-keyboard-input
- document.querySelectorAll(".use-keyboard-input").forEach(element => {
- element.addEventListener("focus", () => {
- this.open(element.value, currentValue => {
- element.value = currentValue;
- });
- });
- });
- },
- _createKeys() {
- const fragment = document.createDocumentFragment();
- const keyLayout = [
- "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "backspace",
- "q", "w", "e", "r", "t", "y", "u", "i", "o", "p",
- "caps", "a", "s", "d", "f", "g", "h", "j", "k", "l", "enter",
- "z", "x", "c", "v", "b", "n", "m", ",", ".", "?",
- "space"
- ];
- keyLayout.forEach(key => {
- const keyElement = document.createElement("button");
- const insertLineBreak = ["backspace", "p", "enter", "?"].indexOf(key) !== -1;
- // Add attributes/classes
- keyElement.setAttribute("type", "button");
- keyElement.classList.add("keyboard__key");
- switch (key) {
- case "backspace":
- keyElement.classList.add("keyboard__key--wide");
- keyElement.innerHTML = `<i class="ui long arrow alternate left icon"></i>`;
- keyElement.addEventListener("click", (event) => {
- event.preventDefault();
- event.stopImmediatePropagation()
- event.stopPropagation();
- backSpace();
- //this.properties.value = this.properties.value.substring(0, this.properties.value.length - 1);
- //this._triggerEvent("oninput");
- });
- break;
- case "caps":
- keyElement.classList.add("keyboard__key--wide", "keyboard__key--activatable");
- keyElement.innerHTML = `<i class="ui chevron up icon"></i>`;
- keyElement.addEventListener("click", () => {
- this._toggleCapsLock();
- keyElement.classList.toggle("keyboard__key--active", this.properties.capsLock);
- });
- break;
- case "enter":
- keyElement.classList.add("keyboard__key--wide");
- keyElement.innerHTML = `<i style="transform: rotate(90deg);" class="ui level down alternate icon"></i>`;
- keyElement.addEventListener("click", (event) => {
- event.preventDefault();
- event.stopImmediatePropagation()
- event.stopPropagation();
- addtext("\n");
- //this.properties.value += "\n";
- //this._triggerEvent("oninput");
- });
- break;
- case "space":
- keyElement.classList.add("keyboard__key--extra-wide");
- keyElement.innerHTML = ``;
- keyElement.addEventListener("click", (event) => {
- event.preventDefault();
- event.stopImmediatePropagation()
- event.stopPropagation();
- addtext(" ");
- //this.properties.value += " ";
- //this._triggerEvent("oninput");
- });
- break;
- case "done":
- /*
- keyElement.classList.add("keyboard__key--wide", "keyboard__key--dark");
- keyElement.innerHTML = createIconHTML("check_circle");
- keyElement.addEventListener("click", () => {
- this.close();
- this._triggerEvent("onclose");
- });
- */
- break;
- default:
- keyElement.textContent = key.toLowerCase();
- keyElement.addEventListener("click", (event) => {
- event.preventDefault();
- event.stopImmediatePropagation()
- event.stopPropagation();
- addtext(this.properties.capsLock ? key.toUpperCase() : key.toLowerCase());
- //this.properties.value += this.properties.capsLock ? key.toUpperCase() : key.toLowerCase();
- //this._triggerEvent("oninput");
- });
- break;
- }
- fragment.appendChild(keyElement);
- if (insertLineBreak) {
- fragment.appendChild(document.createElement("br"));
- }
- });
- return fragment;
- },
- _triggerEvent(handlerName) {
- if (typeof this.eventHandlers[handlerName] == "function") {
- this.eventHandlers[handlerName](this.properties.value);
- }
- },
- _toggleCapsLock() {
- this.properties.capsLock = !this.properties.capsLock;
- for (const key of this.elements.keys) {
- if (key.childElementCount === 0) {
- key.textContent = this.properties.capsLock ? key.textContent.toUpperCase() : key.textContent.toLowerCase();
- }
- }
- },
- open(initialValue, oninput, onclose) {
- this.properties.value = initialValue || "";
- this.eventHandlers.oninput = oninput;
- this.eventHandlers.onclose = onclose;
- this.elements.main.classList.remove("keyboard--hidden");
- },
- close() {
- this.properties.value = "";
- this.eventHandlers.oninput = oninput;
- this.eventHandlers.onclose = onclose;
- this.elements.main.classList.add("keyboard--hidden");
- }
- };
- window.addEventListener("DOMContentLoaded", function () {
- Keyboard.init();
- });
|