Files
exocerebrum/controller.js

41 lines
1.2 KiB
JavaScript

const h = (name, props = {}, ...children) => {
const element = document.createElement(name);
for (const [key, value] of Object.entries(props))
key.startsWith('on')
? element.addEventListener(key.substring(2), value)
: element.setAttribute(key, value);
for (const child of children)
element.appendChild(
typeof(child) === 'string'
? document.createTextNode(child)
: child);
return element;
};
const qs = s => document.querySelector(s);
const qsa = s => Array.from(document.querySelectorAll(s))
const on_command = command => {
switch(command.type) {
case 'alert': alert(command.message); break;
case 'load_pdf': pdfjsLib.getDocument(command.url).promise.then(pdf => PDFViewerApplication.load(pdf)); break;
}
};
let call_python;
new QWebChannel(qt.webChannelTransport, channel => {
call_python = (argument, callback) =>
channel.objects.bridge.call_python(JSON.stringify(argument),
result => callback && callback(JSON.parse(result)));
channel.objects.bridge.call_javascript.connect(
result => on_command(JSON.parse(result)));
call_python({'type': 'ready'});
});