2024-08-15 00:57:30 +03:00
|
|
|
// class Menu {
|
|
|
|
// #application;
|
|
|
|
// #root;
|
|
|
|
// #items;
|
|
|
|
//
|
|
|
|
// constructor(application) {
|
|
|
|
// this.#application = application;
|
|
|
|
// this.#root = document.querySelector('#menu');
|
|
|
|
// this.#items = this.#root.querySelectorAll('input[data-page]');
|
|
|
|
// this.#items.forEach(item => {
|
|
|
|
// item.addEventListener('click', () => this.#application.open(item.dataset.page));
|
|
|
|
// });
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// setActive(pageId) {
|
|
|
|
// this.#items.forEach(item => item.classList.remove('active'));
|
|
|
|
// this.#items.forEach(item => item.dataset.page === pageId ? item.classList.add('active') : null);
|
|
|
|
// };
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// class Application {
|
|
|
|
// static DEFAULT_PAGE = 'dashboard';
|
|
|
|
//
|
|
|
|
// #inputContainer;
|
|
|
|
// #outputContainer;
|
|
|
|
// #menu;
|
|
|
|
// #pageId;
|
|
|
|
//
|
|
|
|
// constructor() {
|
|
|
|
// this.#inputContainer = document.querySelector('#input');
|
|
|
|
// this.#outputContainer = document.querySelector('#output');
|
|
|
|
// this.#menu = new Menu(this);
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// open(pageId) {
|
|
|
|
// this.#pageId = pageId;
|
|
|
|
// document.querySelectorAll('#input [data-page]').forEach(page => page.classList.remove('active'));
|
|
|
|
// document.querySelector(`#input [data-page="${pageId}"]`).classList.add('active');
|
|
|
|
// document.querySelectorAll('#output [data-page]').forEach(page => page.classList.remove('active'));
|
|
|
|
// this.#menu.setActive(pageId);
|
|
|
|
// };
|
|
|
|
// }
|
|
|
|
|
|
|
|
// const mutableStateOf = {
|
|
|
|
// _value: null,
|
|
|
|
// constructor(value) {
|
|
|
|
// this._value = value
|
|
|
|
// },
|
|
|
|
// get value() {
|
|
|
|
// return this._value
|
|
|
|
// },
|
|
|
|
// set value(value) {
|
|
|
|
// this._value = value
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2024-08-15 15:32:28 +03:00
|
|
|
const syncUrl = new URL('https://cit.csasq.ru/ws/sync')
|
2024-08-15 00:57:30 +03:00
|
|
|
|
2024-08-15 15:32:28 +03:00
|
|
|
const SyncDataType = {
|
|
|
|
TEXT: 0,
|
|
|
|
BOOLEAN: 1,
|
|
|
|
}
|
2024-08-15 00:57:30 +03:00
|
|
|
|
2024-08-15 15:32:28 +03:00
|
|
|
const enableSync = (url) => {
|
|
|
|
let ws = new WebSocket(url)
|
|
|
|
|
|
|
|
document
|
|
|
|
.querySelectorAll('md-outlined-text-field[data-sync]')
|
|
|
|
.forEach(element => element.addEventListener('input', () => {
|
|
|
|
const data = JSON.stringify({
|
|
|
|
id: element.id,
|
|
|
|
type: SyncDataType.TEXT,
|
|
|
|
value: element.value,
|
|
|
|
})
|
|
|
|
ws.send(data)
|
|
|
|
}))
|
|
|
|
|
|
|
|
document
|
|
|
|
.querySelectorAll('md-switch[data-sync]')
|
|
|
|
.forEach(element => element.addEventListener('change', () => {
|
|
|
|
const data = JSON.stringify({
|
|
|
|
id: element.id,
|
|
|
|
type: SyncDataType.BOOLEAN,
|
|
|
|
value: !element.hasAttribute('selected'),
|
|
|
|
})
|
|
|
|
ws.send(data)
|
|
|
|
}))
|
2024-08-15 00:57:30 +03:00
|
|
|
|
|
|
|
ws.onmessage = (event) => {
|
|
|
|
const data = JSON.parse(event.data)
|
2024-08-15 15:32:28 +03:00
|
|
|
const elements = document.querySelectorAll(`#${data.id}`)
|
2024-08-15 00:57:30 +03:00
|
|
|
|
2024-08-15 15:32:28 +03:00
|
|
|
switch (data.type) {
|
|
|
|
case SyncDataType.TEXT:
|
|
|
|
elements.forEach(element => element.value = data.value)
|
|
|
|
break
|
2024-08-15 00:57:30 +03:00
|
|
|
|
2024-08-15 15:32:28 +03:00
|
|
|
case SyncDataType.BOOLEAN:
|
|
|
|
elements.forEach(element => element.toggleAttribute('selected', data.value))
|
|
|
|
break
|
|
|
|
}
|
2024-08-15 00:57:30 +03:00
|
|
|
}
|
|
|
|
|
2024-08-15 15:32:28 +03:00
|
|
|
ws.onclose = () => () => ws = new WebSocket(url)
|
2024-08-15 00:57:30 +03:00
|
|
|
|
2024-08-15 15:32:28 +03:00
|
|
|
ws.onerror = () => {
|
|
|
|
console.log('ws_error')
|
2024-08-15 00:57:30 +03:00
|
|
|
}
|
2024-08-14 01:09:41 +03:00
|
|
|
}
|
2024-08-15 00:57:30 +03:00
|
|
|
|
2024-08-15 15:32:28 +03:00
|
|
|
document.addEventListener('DOMContentLoaded', () => enableSync(syncUrl))
|