ftd 0.2.0

ftd: FifthTry Document Format
Documentation
<!DOCTYPE html>
<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
    <script type="ftd" id="ftd-source">
        ___ftd_json___
    </script>
</head>
<body>
<script type="module">
    import init, { create } from '/ftd_rt.js';

    async function run() {
        await init();
        window.ftd_handles = {};
        window.ftd_handles["main"] = create("main", JSON.parse(document.getElementById("ftd-source").innerText));
        window.ftd_handles["main"].render();
        on_frame();
        on_second();
    }

    run();
</script>
<div id="counter"></div>
<div id="main">
    <div>
        <script type="json">
        {
            "data": {
                "fifthtry/ft#mobile": {
                    value: "true",
                    dependencies: {
                        "one.asdlkj.b": "visible if true",
                        "one.asdlkj.a": "visible if false",
                    }
                }
            },

            "events": {
                "one.asdlkj.b": {
                    "onclick": ["toggle fifthtry/ft#mobile"]
                }
            },

            "external": {
                "foo:0-external": [
                    {
                        conditions: ["main:0,0"],
                        set_at: "main:0,0"
                    },
                    {
                        conditions: ["main:0,0"],
                        set_at: "main:0,0"
                    }
                ]
            }
        }

        // -- component foo:
        // open: main
        //
        // --- desktop:
        // if: not mobile
        // id: main
        //
        // --- mobile:
        // if: mobile
        // id: main
        //
        // --- ftd.text: footer
        //
        // -- foo:
        // id: foo
        //
        // -- ftd.text: hello
    </script>

        <div id="foo:0">
            <div id="main:0,0" style="display: block">
                desktop
                <div id="foo:0-external">hello</div>
            </div>
            <div id="main:0,1" style="display: none">
                mobile
            </div>
            <div id="0,2" onclick="function(){toggle('mobile')"}>footer</div>
        </div>
    </div>
</div>

<script>  // ftd-rt.js
    window.ftd_rt = (function() {
        function read_json_from_node(node) {
            // find the first strip tag
            // parse the body of script as json and return the data
        }
        let exports = {};

        let instances = {}

        function event_handler(id, actions) {
            // example actions: ["toggle fifthtry/ft#mobile"]
            // TODO: execute the actions (toggle the variable)
            // TODO: update dom based on dependencies
        }

        exports.init = function(id) {
            // find the id in page
            let node = document.getElementById(id);

            instances[id] = {
                "data": read_json_from_node(node);
                "node": node,
            }

            // go through all the events, and attach the event handler
        }

        return exports;
    })();
</script>


<button onclick="toggle()">Click me</button>
<script>
    let current = true;
    function toggle() {
        if (!window.ftd_handle) {
            console.log("oops");
            return;
        }

        current = !current;
        window.ftd_handle.set_bool("fifthtry/ft-core#mobile", current);
    }
    let counter = 0;

    function on_frame() {
        counter += 1;
        toggle();
        window.requestAnimationFrame(on_frame);
    }

    function on_second() {
        document.getElementById("counter").innerText = counter;
        counter = 0;
        window.setTimeout(on_second, 1000);
    }

</script>
</body>
</html>