Skip to content

Instantly share code, notes, and snippets.

@qti3e
Created July 9, 2018 05:30
Show Gist options
  • Save qti3e/f356a3f772532fc78eb80c86056c2bc0 to your computer and use it in GitHub Desktop.
Save qti3e/f356a3f772532fc78eb80c86056c2bc0 to your computer and use it in GitHub Desktop.

Revisions

  1. qti3e created this gist Jul 9, 2018.
    67 changes: 67 additions & 0 deletions index.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    async function bottom(props, resolve, parentDom) {
    const btn = document.createElement("bottom");
    parentDom.appendChild(btn);
    btn.onclick = (e) => {
    props.onClick(e, resolve);
    }
    return btn;
    }

    function html(elementName) {
    return async (props, resolve, parentDom) => {
    const el = document.createElement(elementName);
    // TODO
    return el;
    }
    }

    function wei(c, attributes, ...childs) {
    if (typeof c === "string") c = html(c);
    if (!attributes) attributes = {};
    attributes.childs = childs;
    const data = {};
    return (parentDom, resolve?) => {
    let promise;
    if (!resolve) {
    promise = new Promise(r => resolve = r);
    }
    // For example it can be used to validate a form before submit.
    if (attributes && attributes.resolve) {
    let parentResolve = resolve;
    resolve = attributes.resolve.bind(null, () => parentResolve);
    }

    c(attributes, d => {
    Object.assign(data, d);
    if (d.done) resolve();
    }, parentDom).then(domElement => {
    if (attributes && attributes.ref) {
    attributes.ref(domElement);
    }
    if (domElement && childs) {
    for (const w of childs) {
    if (typeof w === "function") {
    w(domElement, resolve);
    } else {
    domElement.appendChild(w);
    }
    }
    }
    });

    return promise;
    }
    }

    async function _main() {
    const c = wei("form", {}, [
    wei("p", {}, ["Text"]),
    wei(bottom, {
    onClick(e, r) {
    r();
    }
    })
    ]);
    const data = await c(document.body);
    console.log(data);
    }