2020-06-06 00:03:37 +02:00
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
const api = require("../api.js");
|
|
|
|
|
const pools = require("../pools.js");
|
|
|
|
|
const misc = require("../util/misc.js");
|
|
|
|
|
const uri = require("../util/uri.js");
|
|
|
|
|
const Pool = require("../models/pool.js");
|
|
|
|
|
const settings = require("../models/settings.js");
|
|
|
|
|
const events = require("../events.js");
|
|
|
|
|
const views = require("../util/views.js");
|
|
|
|
|
const PoolAutoCompleteControl = require("./pool_auto_complete_control.js");
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
|
|
|
|
const KEY_SPACE = 32;
|
|
|
|
|
const KEY_RETURN = 13;
|
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
|
const SOURCE_INIT = "init";
|
|
|
|
|
const SOURCE_IMPLICATION = "implication";
|
|
|
|
|
const SOURCE_USER_INPUT = "user-input";
|
|
|
|
|
const SOURCE_CLIPBOARD = "clipboard";
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
|
const template = views.getTemplate("pool-input");
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
|
|
|
|
function _fadeOutListItemNodeStatus(listItemNode) {
|
|
|
|
|
if (listItemNode.classList.length) {
|
|
|
|
|
if (listItemNode.fadeTimeout) {
|
|
|
|
|
window.clearTimeout(listItemNode.fadeTimeout);
|
|
|
|
|
}
|
|
|
|
|
listItemNode.fadeTimeout = window.setTimeout(() => {
|
|
|
|
|
while (listItemNode.classList.length) {
|
2020-06-06 00:03:37 +02:00
|
|
|
|
listItemNode.classList.remove(listItemNode.classList.item(0));
|
2020-05-04 11:20:23 +02:00
|
|
|
|
}
|
|
|
|
|
listItemNode.fadeTimeout = null;
|
|
|
|
|
}, 2500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class PoolInputControl extends events.EventTarget {
|
|
|
|
|
constructor(hostNode, poolList) {
|
|
|
|
|
super();
|
|
|
|
|
this.pools = poolList;
|
|
|
|
|
this._hostNode = hostNode;
|
|
|
|
|
this._poolToListItemNode = new Map();
|
|
|
|
|
|
|
|
|
|
// dom
|
|
|
|
|
const editAreaNode = template();
|
|
|
|
|
this._editAreaNode = editAreaNode;
|
2020-06-06 00:03:37 +02:00
|
|
|
|
this._poolInputNode = editAreaNode.querySelector("input");
|
|
|
|
|
this._poolListNode = editAreaNode.querySelector("ul.compact-pools");
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
|
|
|
|
this._autoCompleteControl = new PoolAutoCompleteControl(
|
2020-06-06 00:03:37 +02:00
|
|
|
|
this._poolInputNode,
|
|
|
|
|
{
|
2020-05-04 11:20:23 +02:00
|
|
|
|
getTextToFind: () => {
|
|
|
|
|
return this._poolInputNode.value;
|
|
|
|
|
},
|
2020-06-06 00:03:37 +02:00
|
|
|
|
confirm: (pool) => {
|
|
|
|
|
this._poolInputNode.value = "";
|
2020-05-04 11:20:23 +02:00
|
|
|
|
this.addPool(pool, SOURCE_USER_INPUT);
|
|
|
|
|
},
|
2020-06-06 00:03:37 +02:00
|
|
|
|
delete: (pool) => {
|
|
|
|
|
this._poolInputNode.value = "";
|
2020-05-04 11:20:23 +02:00
|
|
|
|
this.deletePool(pool);
|
|
|
|
|
},
|
2020-06-06 00:03:37 +02:00
|
|
|
|
verticalShift: -2,
|
|
|
|
|
}
|
|
|
|
|
);
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
|
|
|
|
// show
|
2020-06-06 00:03:37 +02:00
|
|
|
|
this._hostNode.style.display = "none";
|
2020-05-04 11:20:23 +02:00
|
|
|
|
this._hostNode.parentNode.insertBefore(
|
2020-06-06 00:03:37 +02:00
|
|
|
|
this._editAreaNode,
|
|
|
|
|
hostNode.nextSibling
|
|
|
|
|
);
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
|
|
|
|
// add existing pools
|
|
|
|
|
for (let pool of [...this.pools]) {
|
|
|
|
|
const listItemNode = this._createListItemNode(pool);
|
|
|
|
|
this._poolListNode.appendChild(listItemNode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addPool(pool, source) {
|
2020-06-05 03:01:28 +02:00
|
|
|
|
if (source !== SOURCE_INIT && this.pools.hasPoolId(pool.id)) {
|
2020-05-04 11:20:23 +02:00
|
|
|
|
return Promise.resolve();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
|
this.pools.add(pool, false);
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
2020-06-03 02:43:18 +02:00
|
|
|
|
const listItemNode = this._createListItemNode(pool);
|
|
|
|
|
if (!pool.category) {
|
2020-06-06 00:03:37 +02:00
|
|
|
|
listItemNode.classList.add("new");
|
2020-06-03 02:43:18 +02:00
|
|
|
|
}
|
|
|
|
|
this._poolListNode.prependChild(listItemNode);
|
|
|
|
|
_fadeOutListItemNodeStatus(listItemNode);
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
|
this.dispatchEvent(
|
|
|
|
|
new CustomEvent("add", {
|
|
|
|
|
detail: { pool: pool, source: source },
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
this.dispatchEvent(new CustomEvent("change"));
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
2020-06-03 02:43:18 +02:00
|
|
|
|
return Promise.resolve();
|
2020-05-04 11:20:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 02:43:18 +02:00
|
|
|
|
deletePool(pool) {
|
|
|
|
|
if (!this.pools.hasPoolId(pool.id)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.pools.removeById(pool.id);
|
|
|
|
|
this._hideAutoComplete();
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
2020-06-03 02:43:18 +02:00
|
|
|
|
this._deleteListItemNode(pool);
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
|
this.dispatchEvent(
|
|
|
|
|
new CustomEvent("remove", {
|
|
|
|
|
detail: { pool: pool },
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
this.dispatchEvent(new CustomEvent("change"));
|
2020-06-03 02:43:18 +02:00
|
|
|
|
}
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
2020-06-03 02:43:18 +02:00
|
|
|
|
_createListItemNode(pool) {
|
2020-06-06 00:03:37 +02:00
|
|
|
|
const className = pool.category
|
|
|
|
|
? misc.makeCssName(pool.category, "pool")
|
|
|
|
|
: null;
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
|
const poolLinkNode = document.createElement("a");
|
2020-06-03 02:43:18 +02:00
|
|
|
|
if (className) {
|
|
|
|
|
poolLinkNode.classList.add(className);
|
|
|
|
|
}
|
|
|
|
|
poolLinkNode.setAttribute(
|
2020-06-06 00:03:37 +02:00
|
|
|
|
"href",
|
|
|
|
|
uri.formatClientLink("pool", pool.names[0])
|
|
|
|
|
);
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
|
const poolIconNode = document.createElement("i");
|
|
|
|
|
poolIconNode.classList.add("fa");
|
|
|
|
|
poolIconNode.classList.add("fa-pool");
|
2020-06-03 02:43:18 +02:00
|
|
|
|
poolLinkNode.appendChild(poolIconNode);
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
|
const searchLinkNode = document.createElement("a");
|
2020-06-03 02:43:18 +02:00
|
|
|
|
if (className) {
|
|
|
|
|
searchLinkNode.classList.add(className);
|
|
|
|
|
}
|
|
|
|
|
searchLinkNode.setAttribute(
|
2020-06-06 00:03:37 +02:00
|
|
|
|
"href",
|
|
|
|
|
uri.formatClientLink("posts", { query: "pool:" + pool.id })
|
|
|
|
|
);
|
|
|
|
|
searchLinkNode.textContent = pool.names[0] + " ";
|
|
|
|
|
|
|
|
|
|
const usagesNode = document.createElement("span");
|
|
|
|
|
usagesNode.classList.add("pool-usages");
|
|
|
|
|
usagesNode.setAttribute("data-pseudo-content", pool.postCount);
|
|
|
|
|
|
|
|
|
|
const removalLinkNode = document.createElement("a");
|
|
|
|
|
removalLinkNode.classList.add("remove-pool");
|
|
|
|
|
removalLinkNode.setAttribute("href", "");
|
|
|
|
|
removalLinkNode.setAttribute("data-pseudo-content", "×");
|
|
|
|
|
removalLinkNode.addEventListener("click", (e) => {
|
2020-06-03 02:43:18 +02:00
|
|
|
|
e.preventDefault();
|
|
|
|
|
this.deletePool(pool);
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-06 00:03:37 +02:00
|
|
|
|
const listItemNode = document.createElement("li");
|
2020-06-03 02:43:18 +02:00
|
|
|
|
listItemNode.appendChild(removalLinkNode);
|
|
|
|
|
listItemNode.appendChild(poolLinkNode);
|
|
|
|
|
listItemNode.appendChild(searchLinkNode);
|
|
|
|
|
listItemNode.appendChild(usagesNode);
|
|
|
|
|
for (let name of pool.names) {
|
|
|
|
|
this._poolToListItemNode.set(name, listItemNode);
|
|
|
|
|
}
|
|
|
|
|
return listItemNode;
|
2020-05-04 11:20:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 02:43:18 +02:00
|
|
|
|
_deleteListItemNode(pool) {
|
|
|
|
|
const listItemNode = this._getListItemNode(pool);
|
|
|
|
|
if (listItemNode) {
|
|
|
|
|
listItemNode.parentNode.removeChild(listItemNode);
|
|
|
|
|
}
|
|
|
|
|
for (let name of pool.names) {
|
|
|
|
|
this._poolToListItemNode.delete(name);
|
|
|
|
|
}
|
2020-05-04 11:20:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 02:43:18 +02:00
|
|
|
|
_getListItemNode(pool) {
|
|
|
|
|
return this._poolToListItemNode.get(pool.names[0]);
|
|
|
|
|
}
|
2020-05-04 11:20:23 +02:00
|
|
|
|
|
2020-06-03 02:43:18 +02:00
|
|
|
|
_hideAutoComplete() {
|
|
|
|
|
this._autoCompleteControl.hide();
|
|
|
|
|
}
|
2020-05-04 11:20:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = PoolInputControl;
|