You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
203 lines
6.8 KiB
JavaScript
203 lines
6.8 KiB
JavaScript
2 years ago
|
// This file contains definitions which help to reduce the amount
|
||
2 years ago
|
// of redundant values in the main file, especially those that could
|
||
2 years ago
|
// change in the foreseeable future.
|
||
|
|
||
|
export const dom = {
|
||
2 years ago
|
/** @return {HTMLTableElement | null} */
|
||
2 years ago
|
tbl_communities: () => document.getElementById("tbl_communities"),
|
||
2 years ago
|
tbl_communities_content_rows:
|
||
2 years ago
|
() => Array.from(dom.tbl_communities()?.rows)?.filter(row => !row.querySelector('th')),
|
||
2 years ago
|
community_row: (communityID) => document.querySelector(`.room-row[${ATTRIBUTES.ROW.IDENTIFIER}="${communityID}"]`),
|
||
2 years ago
|
row_info: (row) => {
|
||
2 years ago
|
const dateCreated = new Date(row.getAttribute(ATTRIBUTES.ROW.DATE_CREATED) * 1000);
|
||
2 years ago
|
/** @type {string[]} */
|
||
|
return {
|
||
|
language_flag: row.querySelector('.td_language').textContent.trim(),
|
||
2 years ago
|
name: row.querySelector('.td_name-inner').textContent.trim(),
|
||
2 years ago
|
description: row.querySelector('.td_description').textContent.trim(),
|
||
|
users: parseFloat(row.querySelector('.td_users').textContent.trim()),
|
||
|
preview_link: row.querySelector('.td_preview a[href]').getAttribute('href'),
|
||
|
join_link: row.querySelector('.td_join_url a[href]').getAttribute('href'),
|
||
2 years ago
|
identifier: row.getAttribute(ATTRIBUTES.ROW.IDENTIFIER),
|
||
2 years ago
|
hostname: row.getAttribute(ATTRIBUTES.ROW.HOSTNAME),
|
||
|
public_key: row.getAttribute(ATTRIBUTES.ROW.PUBLIC_KEY),
|
||
2 years ago
|
staff: row.getAttribute(ATTRIBUTES.ROW.STAFF_DATA),
|
||
2 years ago
|
/** @type {{type: string, text: string, description: string}[]} */
|
||
|
tags: JSON.parse(row.getAttribute(ATTRIBUTES.ROW.TAGS)),
|
||
2 years ago
|
icon: row.getAttribute(ATTRIBUTES.ROW.ROOM_ICON),
|
||
|
has_icon: row.getAttribute(ATTRIBUTES.ROW.ROOM_ICON).trim() != "",
|
||
2 years ago
|
icon_safety: row.getAttribute(ATTRIBUTES.ROW.ROOM_ICON_SAFETY),
|
||
|
date_created: dateCreated,
|
||
|
creation_datestring: dateCreated.toLocaleDateString(undefined, {dateStyle: "medium"})
|
||
2 years ago
|
};
|
||
|
},
|
||
2 years ago
|
meta_timestamp: () => document.querySelector('meta[name=timestamp]'),
|
||
2 years ago
|
last_checked: () => document.getElementById("last_checked_value"),
|
||
2 years ago
|
/** @return {HTMLDialogElement | null} */
|
||
|
details_modal: () => document.getElementById('details-modal'),
|
||
2 years ago
|
details_modal_tag_container: () => document.getElementById('details-modal-room-tags'),
|
||
2 years ago
|
details_modal_qr_code: () => document.getElementById('details-modal-qr-code'),
|
||
2 years ago
|
details_modal_room_icon: () => document.getElementById('details-modal-community-icon'),
|
||
2 years ago
|
join_urls: () => document.getElementsByClassName("join_url_container"),
|
||
2 years ago
|
servers_hidden: () => document.getElementById("servers_hidden"),
|
||
2 years ago
|
snackbar: () => document.getElementById("copy-snackbar"),
|
||
|
qr_code_buttons: () => document.querySelectorAll('.qr-code-button'),
|
||
2 years ago
|
/** @return {HTMLInputElement | null} */
|
||
2 years ago
|
btn_toggle_search: () => document.querySelector('#btn-toggle-search'),
|
||
2 years ago
|
/** @return {HTMLInputElement | null} */
|
||
|
search_bar: () => document.querySelector('#search-bar'),
|
||
2 years ago
|
btn_clear_search: () => document.querySelector("#btn-clear-search"),
|
||
|
search_container: () => document.querySelector("#search-container"),
|
||
2 years ago
|
sample_searches: () => document.querySelectorAll(".sample-search")
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
export const JOIN_URL_PASTE = "Copied URL to clipboard. Paste into Session app to join";
|
||
|
|
||
2 years ago
|
export const STAFF_ID_PASTE = "Copied staff ping to clipboard. Use it in the selected Community to alert a random moderator.";
|
||
|
|
||
2 years ago
|
export const IDENTIFIER_PASTE = "Copied internal room identifier. Use it to identify a room, such as when contributing language labels."
|
||
|
|
||
2 years ago
|
export const DETAILS_LINK_PASTE = "Copied link to Community details.";
|
||
|
|
||
2 years ago
|
export const communityQRCodeURL = (communityID) => `qr-codes/${communityID}.png`
|
||
|
|
||
2 years ago
|
export const COLUMN = {
|
||
2 years ago
|
LANGUAGE: 0, NAME: 1,
|
||
|
DESCRIPTION: 2, USERS: 3, PREVIEW: 4,
|
||
|
QR_CODE: 5, SERVER_ICON: 6, JOIN_URL: 7
|
||
2 years ago
|
};
|
||
|
|
||
|
// Reverse enum.
|
||
|
// Takes original key-value pairs, flips them, and casefolds the new values.
|
||
|
// Should correspond to #th_{} and .td_{} elements in communities table.
|
||
|
export const COLUMN_LITERAL = Object.fromEntries(
|
||
2 years ago
|
Object.entries(COLUMN).map(([name, id]) => [id, name.toLowerCase()])
|
||
2 years ago
|
);
|
||
|
|
||
|
export const COMPARISON = {
|
||
|
GREATER: 1, EQUAL: 0, SMALLER: -1
|
||
|
};
|
||
|
|
||
|
export const ATTRIBUTES = {
|
||
2 years ago
|
ROW: {
|
||
2 years ago
|
TAGS: 'data-tags',
|
||
2 years ago
|
IDENTIFIER: 'data-identifier',
|
||
|
PUBLIC_KEY: 'data-pubkey',
|
||
|
HOSTNAME: 'data-hostname',
|
||
2 years ago
|
STAFF_DATA: 'data-staff',
|
||
2 years ago
|
ROOM_ICON: 'data-icon',
|
||
2 years ago
|
ROOM_ICON_SAFETY: 'data-icon-safe',
|
||
|
DATE_CREATED: 'data-created'
|
||
2 years ago
|
},
|
||
2 years ago
|
SORTING: {
|
||
|
ACTIVE: 'data-sort',
|
||
|
ASCENDING: 'data-sort-asc',
|
||
|
COLUMN: 'data-sorted-by',
|
||
2 years ago
|
// COLUMN_LITERAL: 'sorted-by'
|
||
2 years ago
|
},
|
||
|
HYDRATION: {
|
||
|
CONTENT: 'data-hydrate-with'
|
||
2 years ago
|
},
|
||
|
SEARCH: {
|
||
|
TARGET_SEARCH: 'data-search'
|
||
2 years ago
|
}
|
||
2 years ago
|
};
|
||
|
|
||
2 years ago
|
export const CLASSES = {
|
||
2 years ago
|
COMPONENTS: {
|
||
|
COLLAPSED: 'collapsed',
|
||
|
},
|
||
2 years ago
|
SEARCH: {
|
||
2 years ago
|
NO_RESULTS: 'search-no-results',
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
const CODEPOINT_REGIONAL_INDICATOR_A = 0x1F1E6;
|
||
|
const CODEPOINT_LOWERCASE_A = 0x61;
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @param {string} flag
|
||
|
*/
|
||
|
export function flagToLanguageAscii(flag) {
|
||
|
const regionalIndicators = [0, 2].map(idx => flag.codePointAt(idx));
|
||
2 years ago
|
if (regionalIndicators.includes(undefined)) {
|
||
|
return "";
|
||
|
}
|
||
2 years ago
|
const ascii = regionalIndicators
|
||
|
.map(codePoint => codePoint - CODEPOINT_REGIONAL_INDICATOR_A)
|
||
|
.map(codePoint => codePoint + CODEPOINT_LOWERCASE_A)
|
||
|
.map(codePoint => String.fromCodePoint(codePoint))
|
||
|
.join("");
|
||
|
|
||
|
switch (ascii) {
|
||
|
case "gb":
|
||
|
return "en";
|
||
|
case "cn":
|
||
|
return "zh";
|
||
|
default:
|
||
|
return ascii;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
2 years ago
|
export function columnAscendingByDefault(column) {
|
||
|
return column != COLUMN.USERS;
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
export function columnIsSortable(column) {
|
||
|
return ![
|
||
|
COLUMN.QR_CODE,
|
||
|
COLUMN.PREVIEW,
|
||
|
// Join URL contents are not guaranteed to have visible text.
|
||
|
COLUMN.JOIN_URL
|
||
|
].includes(column);
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
/**
|
||
|
* @type {Record<string, (el: HTMLTableCellElement) => any>}
|
||
|
*/
|
||
|
const TRANSFORMATION = {
|
||
|
numeric: (el) => parseInt(el.innerText),
|
||
|
casefold: (el) => el.innerText.toLowerCase().trim(),
|
||
2 years ago
|
getSortKey: (el) => el.getAttribute("data-sort-by")
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
/**
|
||
|
* @type {Dictionary<number, (el: HTMLTableCellElement) => any>}
|
||
|
*/
|
||
|
export const COLUMN_TRANSFORMATION = {
|
||
|
[COLUMN.USERS]: TRANSFORMATION.numeric,
|
||
|
[COLUMN.IDENTIFIER]: TRANSFORMATION.casefold,
|
||
2 years ago
|
[COLUMN.NAME]: TRANSFORMATION.getSortKey,
|
||
2 years ago
|
[COLUMN.DESCRIPTION]: TRANSFORMATION.casefold,
|
||
2 years ago
|
[COLUMN.SERVER_ICON]: TRANSFORMATION.getSortKey
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
/**
|
||
|
* Creates an element, and adds attributes and elements to it.
|
||
|
* @param {string} tag - HTML Tag name.
|
||
|
* @param {Object|HTMLElement} args - Array of child elements, may start with props.
|
||
|
* @returns {HTMLElement}
|
||
|
*/
|
||
|
function createElement(tag, ...args) {
|
||
2 years ago
|
const element = document.createElement(tag);
|
||
|
if (args.length === 0) return element;
|
||
|
const propsCandidate = args[0];
|
||
|
if (typeof propsCandidate !== "string" && !(propsCandidate instanceof Element)) {
|
||
|
// args[0] is not child element or text node
|
||
|
// must be props object
|
||
|
Object.assign(element, propsCandidate);
|
||
|
args.shift();
|
||
|
}
|
||
|
element.append(...args);
|
||
|
return element;
|
||
2 years ago
|
}
|
||
|
|
||
|
export const element = new Proxy({}, {
|
||
2 years ago
|
get(_, key) {
|
||
|
return (...args) => createElement(key, ...args)
|
||
|
}
|
||
2 years ago
|
});
|
||
|
|