Skip to content

Commit

Permalink
I learned the value of 'format on save'
Browse files Browse the repository at this point in the history
thanks Zig
  • Loading branch information
nektro committed Apr 17, 2022
1 parent 1a226ea commit 9476e57
Show file tree
Hide file tree
Showing 25 changed files with 171 additions and 94 deletions.
14 changes: 9 additions & 5 deletions www/chat/js/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const caches = [
];

//
function fetchE(endpoint, method="get", data={}) {
function fetchE(endpoint, method = "get", data = {}) {
const body = new FormData();
for (const k in data) {
if (!Object.prototype.hasOwnProperty.call(data, k)) continue;
Expand All @@ -37,7 +37,7 @@ function fetchE(endpoint, method="get", data={}) {
}
body.set(k, data[k]);
}
const opts = method === "get" ? {} : {method, body};
const opts = method === "get" ? {} : { method, body };
return fetch(`./../api${endpoint}`, opts).then((x) => {
if (x.headers.getSafe("content-type").includes("application/json")) return x.json();
return x.text();
Expand All @@ -49,23 +49,27 @@ function fetchE(endpoint, method="get", data={}) {
return x.message;
});
}

function fetchI(endpoint, cl, ...a) {
return fetchE(endpoint).then((x) => {
if (cl === undefined) return x;
return new cl(x, ...a);
});
}

function fetchL(endpoint, cl, ...a) {
return fetchE(endpoint).then((x) => {
return x.map((y) => {
return new cl(y, ...a);
});
});
}

function fetchIC(endpoint, cl, cch, key) {
if (caches[cch].has(key)) return caches[cch].get(key);
return fetchI(endpoint, cl);
}

function resource_factory(name, cl, cch) {
return {
me: () => {
Expand All @@ -77,7 +81,7 @@ function resource_factory(name, cl, cch) {
get: (uid) => {
return fetchIC(`/${name}/${uid}`, cl, cch, uid);
},
update: (uid,k,v) => {
update: (uid, k, v) => {
return fetchE(`/${name}/${uid}`, "put", { p_name: k, p_value: v, });
},
delete: (uid) => {
Expand Down Expand Up @@ -122,7 +126,7 @@ export const M = {
online: () => {
return fetchE("/users/online");
},
update: (uid,k,v) => {
update: (uid, k, v) => {
return fetchE(`/users/${uid}`, "put", { p_name: k, p_value: v, });
},
},
Expand All @@ -138,7 +142,7 @@ export const M = {
return fetchL(`/channels/${ch_uid}/messages?after=${uid}`, Message, ch_uid);
},
delete: (uids) => {
return fetchE(`/channels/${ch_uid}/messages`, "delete", { "ids":uids });
return fetchE(`/channels/${ch_uid}/messages`, "delete", { "ids": uids });
},
remove: (uid) => {
return C.messages.get(ch_uid).delete(uid);
Expand Down
6 changes: 3 additions & 3 deletions www/chat/js/api/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Role {

//
new Role({
uuid:"o",
name:"Owner",
color:"",
uuid: "o",
name: "Owner",
color: "",
});
6 changes: 5 additions & 1 deletion www/chat/js/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ export class User {
this.is_null = false;
cache.set(this.uuid, this);
}

/** @returns {Promise<api.Role[]>} */
async getRoles() {
return Promise.all(this.roles.map((v) => api.M.roles.get(v))).then((l) => {
return l.sort((a,b) => a.position - b.position);
return l.sort((a, b) => a.position - b.position);
});
}

/** @returns {string} */
getName() {
if (this.nickname.length > 0) {
return this.nickname;
}
return this.name;
}

/** @returns {Promise<string>} */
async getHightestDistinguishedRoleUID() {
const o = await api.M.users.get(this.uuid);
Expand All @@ -38,6 +41,7 @@ export class User {
const d = l.length > 0 ? l[0].uuid : "";
return d;
}

/** @returns {Promise<string>} */
async getHightestColoredRoleUID() {
const o = await api.M.users.get(this.uuid);
Expand Down
11 changes: 7 additions & 4 deletions www/chat/js/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@

//
if (!("removeAllChildren" in Element.prototype)) {
Element.prototype.removeAllChildren = function() {
Element.prototype.removeAllChildren = function () {
while (this.children.length > 0) {
this.children[0].remove();
}
};
}

if (!("indexOfMe" in Element.prototype)) {
Element.prototype.indexOfMe = function() {
Element.prototype.indexOfMe = function () {
return Array.from(this.parentElement.children).indexOf(this);
};
}

if (!("path" in Element.prototype)) {
Element.prototype.path = function() {
Element.prototype.path = function () {
const p = [];
let r = this;
while (r !== null) {
Expand All @@ -25,8 +27,9 @@ if (!("path" in Element.prototype)) {
return p;
};
}

if (!("getSafe" in Headers.prototype)) {
Headers.prototype.getSafe = function(name) {
Headers.prototype.getSafe = function (name) {
if (!this.has(name)) return "";
return this.get(name);
};
Expand Down
13 changes: 8 additions & 5 deletions www/chat/js/script.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ $(document).on("click", (e) => {
const s = document.querySelectorAll("dialog[open]");
s.forEach((v) => v.removeAttribute("open"));
});

window.addEventListener("blur", () => {
ui.volatile.windowActive = false;
});

window.addEventListener("focus", () => {
ui.volatile.windowActive = true;
});

document.getElementById("shrink_uonline").addEventListener("click", () => {
output.classList.toggle("extended-right");
el_uonline.toggleAttribute("hidden");
Expand All @@ -30,7 +33,7 @@ document.getElementById("shrink_uonline").addEventListener("click", () => {
});

//
(async function() {
(async function () {
//
moment.defaultFormat = "ddd MMM DD Y HH:mm:ss zZZ";

Expand Down Expand Up @@ -77,7 +80,7 @@ document.getElementById("shrink_uonline").addEventListener("click", () => {

//
await api.M.roles.me().then((x) => {
const rls = x.sort((a,b) => a.position > b.position);
const rls = x.sort((a, b) => a.position > b.position);
//
for (const item of rls) {
ui.M.role.add(item);
Expand All @@ -92,7 +95,7 @@ document.getElementById("shrink_uonline").addEventListener("click", () => {
await output.setActiveChannel(x[0].uuid);

el_1.querySelector("button").addEventListener("click", async () => {
const {value: name} = await Swal({
const { value: name } = await Swal({
title: "Enter the new channel's name",
input: "text",
showCancelButton: true,
Expand Down Expand Up @@ -155,7 +158,7 @@ document.getElementById("shrink_uonline").addEventListener("click", () => {
type: "ping",
}));
}
}, 30*1000);
}, 30 * 1000);

//
input.addEventListener("keydown", (e) => {
Expand Down Expand Up @@ -201,7 +204,7 @@ document.getElementById("shrink_uonline").addEventListener("click", () => {
}));
});

usr_list.appendChild(create_element("li", [["data-uuid",usr_uuid]], [dcTN(usr_name)]));
usr_list.appendChild(create_element("li", [["data-uuid", usr_uuid]], [dcTN(usr_name)]));
});
}
else {
Expand Down
4 changes: 4 additions & 0 deletions www/chat/js/ui.channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,26 @@ export class Channel {
constructor(uid) {
this.el = el_1.querySelector(`[data-uuid="${uid}"]`);
}

/**
* @returns {Number}
*/
get unread() {
return parseInt(this.el.dataset.unread, 10);
}

/**
* @param {Number} x
*/
set unread(x) {
this.el.dataset.unread = x.toString();
this.el.querySelector(".unred").textContent = x.toString();
}

get p_name() {
return this.el.children[0].textContent;
}

set p_name(n) {
this.el.children[0].textContent = n;
}
Expand Down
4 changes: 2 additions & 2 deletions www/chat/js/ui.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export const M = {
},
channel: {
add: async (o) => {
el_1.firstElementChild.appendChild(create_element("li", [["data-uuid",o.uuid],["data-unread","0"]], [
el_1.firstElementChild.appendChild(create_element("li", [["data-uuid", o.uuid], ["data-unread", "0"]], [
create_element("div", [], [dcTN(o.name)]),
// create_element("div", [["class","ments"]], [dcTN("0")]),
create_element("div", [["class","unred"]], [dcTN("0")]),
create_element("div", [["class", "unred"]], [dcTN("0")]),
]));
getSettingsSelection("server", "channels").addItem(o);
await api.M.channels.with(o.uuid).messages.latest();
Expand Down
2 changes: 1 addition & 1 deletion www/chat/js/ui.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const msg_processors = [
["/shrug", "¯\\_(ツ)_/¯"],
["/tableflip", "(╯°□°)╯︵ ┻━┻"],
["/unflip", "┬─┬ ノ( ゜-゜ノ)"],
["/shobon","(´・ω・`)"],
["/shobon", "(´・ω・`)"],
];

export function getSettingsSelection(f, s) {
Expand Down
2 changes: 1 addition & 1 deletion www/chat/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export function safe_html_replace(ele, regex, matcher) {
ele.insertBefore(itn, item);
}
item.remove();
i += fixed.length-1;
i += fixed.length - 1;
}
}

Expand Down
4 changes: 2 additions & 2 deletions www/chat/js/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const M = {
},
update: (d) => {
const o = new api.User(d.user);
if (["add_role","remove_role"].includes(d.key)) {
if (["add_role", "remove_role"].includes(d.key)) {
document.querySelectorAll(`dialog.popup.user ol [data-role="${d.value}"]`).forEach((v) => {
v.classList.toggle("active");
});
Expand Down Expand Up @@ -61,7 +61,7 @@ export const M = {
if (["color"].includes(d.key)) {
const x = document.getElementById("link-role-color");
const y = x.href.split("=");
x.href = y[0]+"="+(parseInt(y[1],10)+1).toString();
x.href = y[0] + "=" + (parseInt(y[1], 10) + 1).toString();
}
if (d.key === "distinguish") {
if (d.value === "0") {
Expand Down
1 change: 1 addition & 0 deletions www/chat/js/x/w-setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class WSetting extends HTMLElement {
constructor() {
super();
}

defaultEndpoint() {
return `./../api/${this.parentElement.parentElement.parentElement.getAttribute("data-s-section")}/%s`;
}
Expand Down
12 changes: 8 additions & 4 deletions www/chat/js/x/x-2s-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ customElements.define("x-2s-toggle", class extends WSetting {
constructor() {
super();
}

connectedCallback() {
const ln = this.getAttribute("local-name");
const n = this.getAttribute("name");
const d = this.getAttribute("label")||"";
const d = this.getAttribute("label") || "";
this.appendChild(create_element("div", null, [dcTN(d)]));
this.appendChild(create_element("label", null, [
create_element("input", [["type","checkbox"]]),
create_element("input", [["type", "checkbox"]]),
create_element("span")
]));
if (ln !== null) {
Expand All @@ -28,21 +29,24 @@ customElements.define("x-2s-toggle", class extends WSetting {
}
this.children[1].children[0].addEventListener("change", () => {
const de = this.defaultEndpoint();
const e = this.getAttribute("endpoint")||de;
const f = this.getAttribute("fill")||"";
const e = this.getAttribute("endpoint") || de;
const f = this.getAttribute("fill") || "";
const e2 = e.replace("%s", f);
const fd = new FormData();
fd.append("p_name", n);
fd.append("p_value", this._value);
return fetch(e2, { method: "put", body: fd, });
});
}

get _value() {
return this.children[1].children[0].checked ? "1" : "0";
}

static get observedAttributes() {
return ["value"];
}

attributeChangedCallback(name, oV, nV) {
if (name === "value") {
const b = nV === "true" || nV === "1";
Expand Down

0 comments on commit 9476e57

Please sign in to comment.