1 import { io } from 'socket.io-client';
2 import { TimeManager } from '../shared/time';
4 function $<T>(selector: string, root: any = document): T {
5 return root.querySelector(selector) as T;
8 function $$<T>(selector: string, root: any = document): T[] {
9 return Array.from(root.querySelectorAll(selector)) as T[];
12 export function authToken(): string {
13 const token = localStorage.getItem('authToken');
17 function setTimeGradient() {
18 const gradientName = time.gradientName();
19 const $body = $<HTMLElement>('body');
20 $body.classList.value = gradientName;
22 $body.classList.add(time.getTimePeriod());
25 if(time.get24Hour() >= 5 && time.get24Hour() < 9) {
28 else if(time.get24Hour() >= 9 && time.get24Hour() < 17) {
31 else if(time.get24Hour() >= 17 && time.get24Hour() < 20) {
38 $<HTMLElement>('#time-of-day').innerHTML = `<img src="/assets/img/icons/time-of-day/${icon}.png"> ${time.getHour()}${time.getAmPm()}`;
41 function renderChatMessage(msg: string) {
42 $<HTMLElement>('#chat-messages').innerHTML += msg;
43 $<HTMLElement>('#chat-messages').scrollTop = $<HTMLElement>('#chat-messages').scrollHeight;
46 const time = new TimeManager();
49 'x-authtoken': authToken()
53 setInterval(setTimeGradient, 60 * 1000);
55 socket.on('connect', () => {
56 console.log(`Connected: ${socket.id}`);
59 socket.on('authToken', (authToken: string) => {
60 console.log(`recv auth token ${authToken}`);
61 localStorage.setItem('authToken', authToken);
64 socket.on('status', html => $<HTMLElement>('#server-stats').innerHTML = html);
66 socket.on('chat', renderChatMessage);
67 socket.on('ready', bootstrap);
70 $$<HTMLElement>('nav a').forEach(el => {
71 el.addEventListener('click', e => {
72 const el = e.target as HTMLElement;
74 $$<HTMLElement>('a', el.closest('nav')).forEach(el => {
75 el.classList.remove('active');
78 el.classList.add('active');
80 const targetEl = $<HTMLElement>(el.getAttribute('hx-target')!.toString());
82 Array.from(targetEl.parentElement!.children).forEach(el => {
83 el.classList.remove('active');
85 targetEl.classList.add('active');
89 $<HTMLElement>('body').addEventListener('click', e => {
90 const target = e.target as HTMLElement;
92 if(target!.parentElement!.classList.contains('filter')) {
93 // ok this is afunky filter object!
94 const children = Array.from(target!.parentElement!.children);
95 children.forEach(el => el.classList.remove('active'));
96 target.classList.add('active');
98 const dataFilter = target.getAttribute('data-filter');
100 const targetPane = $<HTMLElement>(`.filter-result[data-filter="${dataFilter}"]`, target.closest('.filter-container'));
103 Array.from(targetPane!.parentElement!.children).forEach(el => {
104 if(el.getAttribute('data-filter') === dataFilter) {
105 el.classList.remove('hidden');
106 el.classList.add('active');
109 el.classList.add('hidden');
110 el.classList.remove('active');
116 if(target.getAttribute('formmethod') === 'dialog' && target.getAttribute('value') === 'cancel') {
117 target.closest('dialog')?.close();
121 const modalMutations = new MutationObserver((list, observer) => {
127 list.forEach(mutation => {
128 switch(((mutation.target) as HTMLElement).id) {
129 case 'modal-wrapper':
140 if($<HTMLElement>('#modal-wrapper').children.length) {
141 $$<HTMLDialogElement>('#modal-wrapper dialog').forEach(el => {
150 if($<HTMLElement>('#alerts').children.length) {
151 $$<HTMLElement>('#alerts .alert').forEach(el => {
152 if(!el.getAttribute('data-dismiss-at')) {
153 const dismiss = Date.now() + 3000;
154 el.setAttribute('data-dismiss-at', dismiss.toString());
155 setTimeout(() => { el.remove(); }, 3000);
163 modalMutations.observe($<HTMLElement>('#modal-wrapper'), { childList: true });
165 modalMutations.observe($<HTMLElement>('#alerts'), { childList: true });
168 function bootstrap() {
169 console.log('Server connection verified');
170 $$<HTMLElement>('nav a')[0].click();
172 document.body.addEventListener('htmx:configRequest', function(evt) {
174 evt.detail.headers['x-authtoken'] = authToken();
176 document.body.addEventListener('htmx:load', function(evt) {
177 $$<HTMLElement>('.disabled[data-block]').forEach(el => {
178 setTimeout(() => { el.removeAttribute('disabled'); }, 3000);
181 document.body.addEventListener('htmx:beforeSwap', function(e) {
182 const el = e.target as HTMLElement;
183 if(el.id === 'chat-form') {
184 $<HTMLInputElement>('#message').value = '';
187 else if(e.detail.serverResponse === 'logout') {
188 localStorage.removeItem('authToken');
189 window.location.reload();