123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- #[cfg(feature = "web")]
- use js_sys::Function;
- #[cfg(feature = "web")]
- use sledgehammer_bindgen::bindgen;
- #[cfg(feature = "web")]
- use web_sys::Node;
- #[cfg(feature = "web")]
- pub const SLEDGEHAMMER_JS: &str = GENERATED_JS;
- #[cfg(feature = "web")]
- #[bindgen(module)]
- mod js {
- const JS_FILE: &str = "./src/common.js";
- const JS: &str = r#"
- class ListenerMap {
- constructor(root) {
- // bubbling events can listen at the root element
- this.global = {};
- // non bubbling events listen at the element the listener was created at
- this.local = {};
- this.root = null;
- this.handler = null;
- }
- create(event_name, element, bubbles) {
- if (bubbles) {
- if (this.global[event_name] === undefined) {
- this.global[event_name] = {};
- this.global[event_name].active = 1;
- this.root.addEventListener(event_name, this.handler);
- } else {
- this.global[event_name].active++;
- }
- }
- else {
- const id = element.getAttribute("data-dioxus-id");
- if (!this.local[id]) {
- this.local[id] = {};
- }
- element.addEventListener(event_name, this.handler);
- }
- }
- remove(element, event_name, bubbles) {
- if (bubbles) {
- this.global[event_name].active--;
- if (this.global[event_name].active === 0) {
- this.root.removeEventListener(event_name, this.global[event_name].callback);
- delete this.global[event_name];
- }
- }
- else {
- const id = element.getAttribute("data-dioxus-id");
- delete this.local[id][event_name];
- if (this.local[id].length === 0) {
- delete this.local[id];
- }
- element.removeEventListener(event_name, this.handler);
- }
- }
- removeAllNonBubbling(element) {
- const id = element.getAttribute("data-dioxus-id");
- delete this.local[id];
- }
- }
- function LoadChild(ptr, len) {
- // iterate through each number and get that child
- node = stack[stack.length - 1];
- ptr_end = ptr + len;
- for (; ptr < ptr_end; ptr++) {
- end = m.getUint8(ptr);
- for (node = node.firstChild; end > 0; end--) {
- node = node.nextSibling;
- }
- }
- return node;
- }
- const listeners = new ListenerMap();
- let nodes = [];
- let stack = [];
- let root;
- const templates = {};
- let node, els, end, ptr_end, k;
- export function save_template(nodes, tmpl_id) {
- templates[tmpl_id] = nodes;
- }
- export function hydrate(ids) {
- console.log("hydrating", ids);
- const hydrateNodes = document.querySelectorAll('[data-node-hydration]');
- for (let i = 0; i < hydrateNodes.length; i++) {
- const hydrateNode = hydrateNodes[i];
- const hydration = hydrateNode.getAttribute('data-node-hydration');
- const split = hydration.split(',');
- const id = ids[parseInt(split[0])];
- nodes[id] = hydrateNode;
- console.log("hydrating node", hydrateNode, id);
- if (split.length > 1) {
- hydrateNode.listening = split.length - 1;
- hydrateNode.setAttribute('data-dioxus-id', id);
- for (let j = 1; j < split.length; j++) {
- const listener = split[j];
- const split2 = listener.split(':');
- const event_name = split2[0];
- const bubbles = split2[1] === '1';
- console.log("hydrating listener", event_name, bubbles);
- listeners.create(event_name, hydrateNode, bubbles);
- }
- }
- }
- const treeWalker = document.createTreeWalker(
- document.body,
- NodeFilter.SHOW_COMMENT,
- );
- let currentNode = treeWalker.nextNode();
- while (currentNode) {
- const id = currentNode.textContent;
- const split = id.split('node-id');
- if (split.length > 1) {
- console.log("hydrating text", currentNode.nextSibling, id);
- nodes[ids[parseInt(split[1])]] = currentNode.nextSibling;
- }
- currentNode = treeWalker.nextNode();
- }
- }
- export function get_node(id) {
- return nodes[id];
- }
- export function initialize(root, handler) {
- listeners.handler = handler;
- nodes = [root];
- stack = [root];
- listeners.root = root;
- }
- function AppendChildren(id, many){
- root = nodes[id];
- els = stack.splice(stack.length-many);
- for (k = 0; k < many; k++) {
- root.appendChild(els[k]);
- }
- }
- "#;
- extern "C" {
- #[wasm_bindgen]
- pub fn save_template(nodes: Vec<Node>, tmpl_id: u16);
- #[wasm_bindgen]
- pub fn hydrate(ids: Vec<u32>);
- #[wasm_bindgen]
- pub fn get_node(id: u32) -> Node;
- #[wasm_bindgen]
- pub fn initialize(root: Node, handler: &Function);
- }
- fn mount_to_root() {
- "{AppendChildren(root, stack.length-1);}"
- }
- fn push_root(root: u32) {
- "{stack.push(nodes[$root$]);}"
- }
- fn append_children(id: u32, many: u16) {
- "{AppendChildren($id$, $many$);}"
- }
- fn pop_root() {
- "{stack.pop();}"
- }
- fn replace_with(id: u32, n: u16) {
- "{root = nodes[$id$]; els = stack.splice(stack.length-$n$); if (root.listening) { listeners.removeAllNonBubbling(root); } root.replaceWith(...els);}"
- }
- fn insert_after(id: u32, n: u16) {
- "{nodes[$id$].after(...stack.splice(stack.length-$n$));}"
- }
- fn insert_before(id: u32, n: u16) {
- "{nodes[$id$].before(...stack.splice(stack.length-$n$));}"
- }
- fn remove(id: u32) {
- "{node = nodes[$id$]; if (node !== undefined) { if (node.listening) { listeners.removeAllNonBubbling(node); } node.remove(); }}"
- }
- fn create_raw_text(text: &str) {
- "{stack.push(document.createTextNode($text$));}"
- }
- fn create_text_node(text: &str, id: u32) {
- "{node = document.createTextNode($text$); nodes[$id$] = node; stack.push(node);}"
- }
- fn create_placeholder(id: u32) {
- "{node = document.createElement('pre'); node.hidden = true; stack.push(node); nodes[$id$] = node;}"
- }
- fn new_event_listener(event_name: &str<u8, evt>, id: u32, bubbles: u8) {
- r#"node = nodes[id]; if(node.listening){node.listening += 1;}else{node.listening = 1;} node.setAttribute('data-dioxus-id', `\${id}`); listeners.create($event_name$, node, $bubbles$);"#
- }
- fn remove_event_listener(event_name: &str<u8, evt>, id: u32, bubbles: u8) {
- "{node = nodes[$id$]; node.listening -= 1; node.removeAttribute('data-dioxus-id'); listeners.remove(node, $event_name$, $bubbles$);}"
- }
- fn set_text(id: u32, text: &str) {
- "{nodes[$id$].textContent = $text$;}"
- }
- fn set_attribute(id: u32, field: &str<u8, attr>, value: &str, ns: &str<u8, ns_cache>) {
- "{node = nodes[$id$]; setAttributeInner(node, $field$, $value$, $ns$);}"
- }
- fn remove_attribute(id: u32, field: &str<u8, attr>, ns: &str<u8, ns_cache>) {
- r#"{
- node = nodes[$id$];
- if (!ns) {
- switch (field) {
- case "value":
- node.value = "";
- break;
- case "checked":
- node.checked = false;
- break;
- case "selected":
- node.selected = false;
- break;
- case "dangerous_inner_html":
- node.innerHTML = "";
- break;
- default:
- node.removeAttribute(field);
- break;
- }
- } else if (ns == "style") {
- node.style.removeProperty(name);
- } else {
- node.removeAttributeNS(ns, field);
- }
- }"#
- }
- fn assign_id(ptr: u32, len: u8, id: u32) {
- "{nodes[$id$] = LoadChild($ptr$, $len$);}"
- }
- fn hydrate_text(ptr: u32, len: u8, value: &str, id: u32) {
- r#"{
- node = LoadChild($ptr$, $len$);
- if (node.nodeType == Node.TEXT_NODE) {
- node.textContent = value;
- } else {
- let text = document.createTextNode(value);
- node.replaceWith(text);
- node = text;
- }
- nodes[$id$] = node;
- }"#
- }
- fn replace_placeholder(ptr: u32, len: u8, n: u16) {
- "{els = stack.splice(stack.length - $n$); node = LoadChild($ptr$, $len$); node.replaceWith(...els);}"
- }
- fn load_template(tmpl_id: u16, index: u16, id: u32) {
- "{node = templates[$tmpl_id$][$index$].cloneNode(true); nodes[$id$] = node; stack.push(node);}"
- }
- }
- #[cfg(feature = "binary-protocol")]
- pub mod binary_protocol {
- use sledgehammer_bindgen::bindgen;
- pub const SLEDGEHAMMER_JS: &str = GENERATED_JS;
- #[bindgen]
- mod protocol_js {
- const JS_FILE: &str = "./src/interpreter.js";
- const JS_FILE: &str = "./src/common.js";
- fn mount_to_root() {
- "{AppendChildren(root, stack.length-1);}"
- }
- fn push_root(root: u32) {
- "{stack.push(nodes[$root$]);}"
- }
- fn append_children(id: u32, many: u16) {
- "{AppendChildren($id$, $many$);}"
- }
- fn append_children_to_top(many: u16) {
- "{
- root = stack[stack.length-many-1];
- els = stack.splice(stack.length-many);
- for (k = 0; k < many; k++) {
- root.appendChild(els[k]);
- }
- }"
- }
- fn pop_root() {
- "{stack.pop();}"
- }
- fn replace_with(id: u32, n: u16) {
- "{root = nodes[$id$]; els = stack.splice(stack.length-$n$); if (root.listening) { listeners.removeAllNonBubbling(root); } root.replaceWith(...els);}"
- }
- fn insert_after(id: u32, n: u16) {
- "{nodes[$id$].after(...stack.splice(stack.length-$n$));}"
- }
- fn insert_before(id: u32, n: u16) {
- "{nodes[$id$].before(...stack.splice(stack.length-$n$));}"
- }
- fn remove(id: u32) {
- "{node = nodes[$id$]; if (node !== undefined) { if (node.listening) { listeners.removeAllNonBubbling(node); } node.remove(); }}"
- }
- fn create_raw_text(text: &str) {
- "{stack.push(document.createTextNode($text$));}"
- }
- fn create_text_node(text: &str, id: u32) {
- "{node = document.createTextNode($text$); nodes[$id$] = node; stack.push(node);}"
- }
- fn create_element(element: &'static str<u8, el>) {
- "{stack.push(document.createElement($element$))}"
- }
- fn create_element_ns(element: &'static str<u8, el>, ns: &'static str<u8, namespace>) {
- "{stack.push(document.createElementNS($ns$, $element$))}"
- }
- fn create_placeholder(id: u32) {
- "{node = document.createElement('pre'); node.hidden = true; stack.push(node); nodes[$id$] = node;}"
- }
- fn add_placeholder() {
- "{node = document.createElement('pre'); node.hidden = true; stack.push(node);}"
- }
- fn new_event_listener(event: &str<u8, evt>, id: u32, bubbles: u8) {
- r#"
- bubbles = bubbles == 1;
- node = nodes[id];
- if(node.listening){
- node.listening += 1;
- } else {
- node.listening = 1;
- }
- node.setAttribute('data-dioxus-id', `\${id}`);
- const event_name = $event$;
-
- // if this is a mounted listener, we send the event immediately
- if (event_name === "mounted") {
- window.ipc.postMessage(
- window.interpreter.serializeIpcMessage("user_event", {
- name: event_name,
- element: id,
- data: null,
- bubbles,
- })
- );
- } else {
- listeners.create(event_name, node, bubbles, (event) => {
- handler(event, event_name, bubbles, config);
- });
- }"#
- }
- fn remove_event_listener(event_name: &str<u8, evt>, id: u32, bubbles: u8) {
- "{node = nodes[$id$]; node.listening -= 1; node.removeAttribute('data-dioxus-id'); listeners.remove(node, $event_name$, $bubbles$);}"
- }
- fn set_text(id: u32, text: &str) {
- "{nodes[$id$].textContent = $text$;}"
- }
- fn set_attribute(id: u32, field: &str<u8, attr>, value: &str, ns: &str<u8, ns_cache>) {
- "{node = nodes[$id$]; setAttributeInner(node, $field$, $value$, $ns$);}"
- }
- fn set_top_attribute(field: &str<u8, attr>, value: &str, ns: &str<u8, ns_cache>) {
- "{setAttributeInner(stack[stack.length-1], $field$, $value$, $ns$);}"
- }
- fn remove_attribute(id: u32, field: &str<u8, attr>, ns: &str<u8, ns_cache>) {
- r#"{
- node = nodes[$id$];
- if (!ns) {
- switch (field) {
- case "value":
- node.value = "";
- break;
- case "checked":
- node.checked = false;
- break;
- case "selected":
- node.selected = false;
- break;
- case "dangerous_inner_html":
- node.innerHTML = "";
- break;
- default:
- node.removeAttribute(field);
- break;
- }
- } else if (ns == "style") {
- node.style.removeProperty(name);
- } else {
- node.removeAttributeNS(ns, field);
- }
- }"#
- }
- fn assign_id(array: &[u8], id: u32) {
- "{nodes[$id$] = LoadChild($array$);}"
- }
- fn hydrate_text(array: &[u8], value: &str, id: u32) {
- r#"{
- node = LoadChild($array$);
- if (node.nodeType == Node.TEXT_NODE) {
- node.textContent = value;
- } else {
- let text = document.createTextNode(value);
- node.replaceWith(text);
- node = text;
- }
- nodes[$id$] = node;
- }"#
- }
- fn replace_placeholder(array: &[u8], n: u16) {
- "{els = stack.splice(stack.length - $n$); node = LoadChild($array$); node.replaceWith(...els);}"
- }
- fn load_template(tmpl_id: u16, index: u16, id: u32) {
- "{node = templates[$tmpl_id$][$index$].cloneNode(true); nodes[$id$] = node; stack.push(node);}"
- }
- fn add_templates(tmpl_id: u16, len: u16) {
- "{templates[$tmpl_id$] = stack.splice(stack.length-$len$);}"
- }
- }
- }
|