2021-07-05 22:37:15 +00:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
|
|
|
|
<head>
|
2021-07-12 06:23:46 +00:00
|
|
|
<script>
|
|
|
|
class Interpreter {
|
|
|
|
constructor(root) {
|
2021-07-24 06:52:05 +00:00
|
|
|
this.root = root;
|
2021-07-12 06:23:46 +00:00
|
|
|
this.stack = [root];
|
2021-07-24 06:52:05 +00:00
|
|
|
this.listeners = {
|
|
|
|
"onclick": {}
|
|
|
|
};
|
2021-07-12 06:23:46 +00:00
|
|
|
this.lastNodeWasText = false;
|
2021-07-24 06:52:05 +00:00
|
|
|
this.nodes = [root, root, root, root];
|
2021-07-12 06:23:46 +00:00
|
|
|
}
|
2021-07-05 22:37:15 +00:00
|
|
|
|
2021-07-12 06:23:46 +00:00
|
|
|
top() {
|
|
|
|
return this.stack[this.stack.length - 1];
|
|
|
|
}
|
2021-07-05 22:37:15 +00:00
|
|
|
|
2021-07-12 06:23:46 +00:00
|
|
|
pop() {
|
|
|
|
return this.stack.pop();
|
|
|
|
}
|
2021-07-05 22:37:15 +00:00
|
|
|
|
2021-07-24 06:52:05 +00:00
|
|
|
PushRoot(edit) {
|
|
|
|
const id = edit.id;
|
|
|
|
const node = this.nodes[id];
|
|
|
|
console.log("pushing root ", node, "with id", id);
|
|
|
|
this.stack.push(node);
|
2021-07-12 06:23:46 +00:00
|
|
|
}
|
2021-07-24 06:52:05 +00:00
|
|
|
|
|
|
|
AppendChildren(edit) {
|
|
|
|
let root = this.stack[this.stack.length - (edit.many + 1)];
|
2021-07-12 06:23:46 +00:00
|
|
|
for (let i = 0; i < edit.many; i++) {
|
|
|
|
console.log("popping ", i, edit.many);
|
2021-07-24 06:52:05 +00:00
|
|
|
let node = this.pop();
|
2021-07-12 06:23:46 +00:00
|
|
|
root.appendChild(node);
|
|
|
|
}
|
|
|
|
}
|
2021-07-08 16:01:31 +00:00
|
|
|
|
2021-07-24 06:52:05 +00:00
|
|
|
ReplaceWith(edit) {
|
|
|
|
|
|
|
|
let root = this.stack[this.stack.length - (edit.many + 1)];
|
2021-07-12 06:23:46 +00:00
|
|
|
let els = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < edit.many; i++) {
|
2021-07-24 06:52:05 +00:00
|
|
|
els.push(this.pop());
|
2021-07-12 06:23:46 +00:00
|
|
|
}
|
2021-07-05 22:37:15 +00:00
|
|
|
|
2021-07-12 06:23:46 +00:00
|
|
|
root.replaceWith(...els);
|
2021-07-05 22:37:15 +00:00
|
|
|
}
|
2021-07-24 06:52:05 +00:00
|
|
|
|
|
|
|
Remove(edit) {
|
|
|
|
const node = this.stack.pop();
|
2021-07-12 06:23:46 +00:00
|
|
|
node.remove();
|
2021-07-05 22:37:15 +00:00
|
|
|
}
|
2021-07-24 06:52:05 +00:00
|
|
|
|
|
|
|
RemoveAllChildren(edit) {}
|
|
|
|
|
|
|
|
CreateTextNode(edit) {
|
|
|
|
const node = document.createTextNode(edit.text);
|
|
|
|
this.nodes[edit.id] = node;
|
|
|
|
this.stack.push(node);
|
2021-07-12 06:23:46 +00:00
|
|
|
}
|
2021-07-24 06:52:05 +00:00
|
|
|
|
|
|
|
CreateElement(edit) {
|
2021-07-12 06:23:46 +00:00
|
|
|
const tagName = edit.tag;
|
2021-07-24 06:52:05 +00:00
|
|
|
const el = document.createElement(tagName);
|
|
|
|
this.nodes[edit.id] = el;
|
2021-07-12 06:23:46 +00:00
|
|
|
console.log(`creating element: `, edit);
|
2021-07-24 06:52:05 +00:00
|
|
|
this.stack.push(el);
|
2021-07-12 06:23:46 +00:00
|
|
|
}
|
2021-07-24 06:52:05 +00:00
|
|
|
|
|
|
|
CreateElementNs(edit) {
|
2021-07-21 21:05:48 +00:00
|
|
|
const tagName = edit.tag;
|
|
|
|
console.log(`creating namespaced element: `, edit);
|
2021-07-24 06:52:05 +00:00
|
|
|
this.stack.push(document.createElementNS(edit.ns, edit.tag));
|
2021-07-12 06:23:46 +00:00
|
|
|
}
|
2021-07-24 06:52:05 +00:00
|
|
|
|
|
|
|
CreatePlaceholder(edit) {
|
|
|
|
const a = `this.stack.push(document.createElement(" pre"))`;
|
|
|
|
this.stack.push(document.createComment("vroot"));
|
2021-07-12 06:23:46 +00:00
|
|
|
}
|
2021-07-24 06:52:05 +00:00
|
|
|
|
|
|
|
NewEventListener(edit) {
|
|
|
|
const element_id = edit.element_id;
|
|
|
|
const event_name = edit.event_name;
|
|
|
|
const mounted_node_id = edit.mounted_node_id;
|
|
|
|
const scope = edit.scope;
|
|
|
|
|
|
|
|
const element = this.top();
|
|
|
|
element.setAttribute(`dioxus-event-${event_name}`, `${scope}.${mounted_node_id}`);
|
|
|
|
|
|
|
|
console.log("listener map is", this.listeners);
|
|
|
|
if (this.listeners[event_name] === undefined) {
|
|
|
|
console.log("adding listener!");
|
|
|
|
this.listeners[event_name] = "bla";
|
|
|
|
this.root.addEventListener(event_name, (event) => {
|
|
|
|
const target = event.target;
|
|
|
|
const type = event.type;
|
|
|
|
const val = target.getAttribute(`dioxus-event-${event_name}`);
|
|
|
|
const fields = val.split(".");
|
|
|
|
const scope_id = parseInt(fields[0]);
|
|
|
|
const real_id = parseInt(fields[1]);
|
|
|
|
|
|
|
|
console.log(`parsed event with scope_id ${scope_id} and real_id ${real_id}`);
|
|
|
|
|
|
|
|
rpc.call('user_event', {
|
|
|
|
event: event_name,
|
|
|
|
scope: scope_id,
|
|
|
|
mounted_dom_id: real_id,
|
|
|
|
}).then((reply) => {
|
|
|
|
console.log(reply);
|
|
|
|
this.stack.push(this.root);
|
|
|
|
|
|
|
|
for (let x = 0; x < reply.length; x++) {
|
|
|
|
let edit = reply[x];
|
|
|
|
console.log(edit);
|
|
|
|
|
|
|
|
let f = this[edit.type];
|
|
|
|
f.call(this, edit);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("initiated");
|
|
|
|
}).catch((err) => {
|
|
|
|
console.log("failed to initiate", err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-07-12 06:23:46 +00:00
|
|
|
}
|
2021-07-24 06:52:05 +00:00
|
|
|
|
|
|
|
RemoveEventListener(edit) {}
|
|
|
|
|
|
|
|
SetText(edit) {
|
|
|
|
this.top().textContent = edit.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetAttribute(edit) {
|
2021-07-12 06:23:46 +00:00
|
|
|
const name = edit.field;
|
|
|
|
const value = edit.value;
|
2021-07-21 21:05:48 +00:00
|
|
|
const ns = edit.ns;
|
2021-07-24 06:52:05 +00:00
|
|
|
const node = this.top(this.stack);
|
2021-07-21 21:05:48 +00:00
|
|
|
if (ns == "style") {
|
|
|
|
node.style[name] = value;
|
|
|
|
} else if (ns !== undefined) {
|
|
|
|
node.setAttributeNS(ns, name, value);
|
|
|
|
} else {
|
|
|
|
node.setAttribute(name, value);
|
|
|
|
}
|
2021-07-24 06:52:05 +00:00
|
|
|
if (name === "value") {
|
2021-07-12 06:23:46 +00:00
|
|
|
node.value = value;
|
|
|
|
}
|
2021-07-24 06:52:05 +00:00
|
|
|
if (name === "checked") {
|
2021-07-12 06:23:46 +00:00
|
|
|
node.checked = true;
|
|
|
|
}
|
2021-07-24 06:52:05 +00:00
|
|
|
if (name === "selected") {
|
2021-07-12 06:23:46 +00:00
|
|
|
node.selected = true;
|
|
|
|
}
|
|
|
|
}
|
2021-07-24 06:52:05 +00:00
|
|
|
RemoveAttribute(edit) {
|
2021-07-12 06:23:46 +00:00
|
|
|
const name = edit.field;
|
2021-07-24 06:52:05 +00:00
|
|
|
const node = this.top(this.stack);
|
2021-07-12 06:23:46 +00:00
|
|
|
node.removeAttribute(name);
|
|
|
|
|
2021-07-24 06:52:05 +00:00
|
|
|
if (name === "value") {
|
2021-07-12 06:23:46 +00:00
|
|
|
node.value = null;
|
|
|
|
}
|
2021-07-24 06:52:05 +00:00
|
|
|
if (name === "checked") {
|
2021-07-12 06:23:46 +00:00
|
|
|
node.checked = false;
|
|
|
|
}
|
2021-07-24 06:52:05 +00:00
|
|
|
if (name === "selected") {
|
2021-07-12 06:23:46 +00:00
|
|
|
node.selected = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-24 06:52:05 +00:00
|
|
|
|
|
|
|
|
2021-07-12 06:23:46 +00:00
|
|
|
|
|
|
|
async function initialize() {
|
|
|
|
const reply = await rpc.call('initiate');
|
2021-07-15 16:18:11 +00:00
|
|
|
const interpreter = new Interpreter(window.document.getElementById("_dioxusroot"));
|
2021-07-12 06:23:46 +00:00
|
|
|
console.log(reply);
|
|
|
|
|
|
|
|
for (let x = 0; x < reply.length; x++) {
|
|
|
|
let edit = reply[x];
|
|
|
|
console.log(edit);
|
2021-07-24 06:52:05 +00:00
|
|
|
|
|
|
|
let f = interpreter[edit.type];
|
|
|
|
f.call(interpreter, edit);
|
2021-07-05 22:37:15 +00:00
|
|
|
}
|
2021-07-12 06:23:46 +00:00
|
|
|
|
|
|
|
console.log("stack completed: ", interpreter.stack);
|
2021-07-05 22:37:15 +00:00
|
|
|
}
|
2021-07-12 06:23:46 +00:00
|
|
|
console.log("initializing...");
|
|
|
|
initialize();
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
2021-07-12 13:40:13 +00:00
|
|
|
<div id="_dioxusroot">
|
2021-07-12 06:23:46 +00:00
|
|
|
</div>
|
|
|
|
</body>
|
2021-07-05 22:37:15 +00:00
|
|
|
|
|
|
|
</html>
|