Notes/static/js/app.mjs

175 lines
4.3 KiB
JavaScript
Raw Permalink Normal View History

2023-06-15 07:24:23 +02:00
import 'preact/devtools'
2023-06-17 09:11:14 +02:00
import { signal } from 'preact/signals'
2023-06-15 07:24:23 +02:00
import { h, Component, render, createRef } from 'preact'
import htm from 'htm'
2023-06-16 07:11:27 +02:00
import { Session } from 'session'
2023-06-17 09:11:14 +02:00
import { NodeUI } from 'node'
2023-06-15 07:24:23 +02:00
const html = htm.bind(h)
class App extends Component {
constructor() {//{{{
super()
this.websocket_uri = `ws://localhost:1371/ws`
2023-06-15 07:24:23 +02:00
this.websocket = null
this.websocket_int_ping = null
this.websocket_int_reconnect = null
this.wsConnect()
this.wsLoop()
2023-06-16 07:11:27 +02:00
this.session = new Session(this)
this.session.initialize()
2023-06-17 09:11:14 +02:00
this.login = createRef()
this.nodeUI = createRef()
2023-06-18 20:13:35 +02:00
this.nodeModified = signal(false)
2023-06-15 07:24:23 +02:00
}//}}}
render() {//{{{
2023-06-16 07:11:27 +02:00
if(!this.session.initialized) {
2023-06-17 09:11:14 +02:00
return html`<div>Validating session</div>`
2023-06-16 07:11:27 +02:00
}
if(!this.session.authenticated()) {
2023-06-17 09:11:14 +02:00
return html`<${Login} ref=${this.login} />`
2023-06-15 07:24:23 +02:00
}
2023-06-17 09:11:14 +02:00
return html`<${NodeUI} app=${this} ref=${this.nodeUI} />`
2023-06-15 07:24:23 +02:00
}//}}}
wsLoop() {//{{{
setInterval(()=>{
if(this.websocket === null) {
console.log("wsLoop connect")
this.wsConnect()
}
}, 1000)
}//}}}
wsConnect() {//{{{
2023-06-18 20:13:35 +02:00
this.websocket = new WebSocket(this.websocket_uri)
2023-06-15 07:24:23 +02:00
this.websocket.onopen = evt=>this.wsOpen(evt)
this.websocket.onmessage = evt=>this.wsMessage(evt)
this.websocket.onerror = evt=>this.wsError(evt)
this.websocket.onclose = evt=>this.wsClose(evt)
}//}}}
wsOpen() {//{{{
this.setState({ wsConnected: true })
// A ping interval to implement a rudimentary keep-alive.
}//}}}
wsClose() {//{{{
console.log("Lost connection to Notes server")
this.websocket = null
}//}}}
wsError(evt) {//{{{
console.log("websocket error", evt)
this.websocket = null;
}//}}}
wsMessage(evt) {//{{{
let msg = JSON.parse(evt.data)
// Broadcast message
if(msg.ID == '') {
this.broadcastHandler(msg)
} else {
this.msgHandler(msg)
}
}//}}}
2023-06-15 14:12:35 +02:00
responseError({comm, app}) {//{{{
if(comm !== undefined) {
comm.text().then(body=>alert(body))
return
}
2023-06-17 09:11:14 +02:00
if(app !== undefined && app.hasOwnProperty('Error')) {
alert(app.Error)
2023-06-15 14:12:35 +02:00
return
}
if(app !== undefined) {
alert(JSON.stringify(app))
}
}//}}}
async request(url, params) {//{{{
return new Promise((resolve, reject)=>{
let headers = {
'Content-Type': 'application/json',
}
2023-06-16 07:11:27 +02:00
if(this.session.UUID !== '')
headers['X-Session-Id'] = this.session.UUID
2023-06-15 14:12:35 +02:00
fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(params),
})
.then(response=>{
// A HTTP communication level error occured
if(!response.ok || response.status != 200)
return reject({comm: response})
return response.json()
})
.then(json=>{
// An application level error occured
2023-06-17 09:11:14 +02:00
if(!json.OK) {
2023-06-15 14:12:35 +02:00
return reject({app: json})
2023-06-17 09:11:14 +02:00
}
2023-06-15 14:12:35 +02:00
return resolve(json)
})
.catch(err=>reject({comm: err}))
})
}//}}}
2023-06-15 07:24:23 +02:00
broadcastHandler(msg) {//{{{
switch(msg.Op) {
case 'css_reload':
refreshCSS()
break;
}
}//}}}
}
class Login extends Component {
2023-06-17 09:11:14 +02:00
constructor() {//{{{
super()
this.authentication_failed = signal(false)
this.state = {
username: '',
password: '',
}
}//}}}
render({}, { username, password }) {//{{{
console.log('login render')
let authentication_failed = html``;
if(this.authentication_failed.value)
authentication_failed = html`<div class="auth-failed">Authentication failed</div>`;
2023-06-15 07:24:23 +02:00
return html`
<div id="login">
<h1>Notes</h1>
2023-06-17 09:11:14 +02:00
<input id="username" type="text" placeholder="Username" value=${username} oninput=${evt=>this.setState({ username: evt.target.value})} onkeydown=${evt=>{ if(evt.code == 'Enter') this.login() }} />
<input id="password" type="password" placeholder="Password" value=${password} oninput=${evt=>this.setState({ password: evt.target.value})} onkeydown=${evt=>{ if(evt.code == 'Enter') this.login() }} />
2023-06-15 14:12:35 +02:00
<button onclick=${()=>this.login()}>Login</button>
2023-06-17 09:11:14 +02:00
${authentication_failed}
2023-06-15 07:24:23 +02:00
</div>
`
2023-06-15 14:12:35 +02:00
}//}}}
componentDidMount() {//{{{
document.getElementById('username').focus()
}//}}}
login() {//{{{
let username = document.getElementById('username').value
let password = document.getElementById('password').value
2023-06-16 07:11:27 +02:00
window._app.current.session.authenticate(username, password)
2023-06-15 14:12:35 +02:00
}//}}}
2023-06-15 07:24:23 +02:00
}
// Init{{{
window._app = createRef()
window._resourceModels = []
render(html`<${App} ref=${window._app} />`, document.getElementById('app'))
//}}}
// vim: foldmethod=marker