summaryrefslogtreecommitdiffstatshomepage
path: root/www/js/demo-helper.js
blob: b2a3af00fd4fb683f9cad5ae3ed6cca31db1644b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function addScript(url) {
    var myScript = document.createElement('script');
    myScript.setAttribute('src', url);
    document.head.appendChild(myScript);
}

function interpolate(str) {
    var returnStr = "";
    var charArray = Array.from(str);
    while (charArray.length > 0) {
        var current = charArray.shift();
        if (current === "$" && charArray[0] === "{") {
            var evalStr = "(function() { return "
            charArray.shift();
            while (charArray.length > 0 && charArray[0] !== "}") {
                evalStr += charArray.shift()
            }
            charArray.shift();
            evalStr += " })()";
            // console.log("Evaling", evalStr);
            returnStr += eval(evalStr);
        } else {
            returnStr += current;
        }
    }
    return returnStr;
}

function initMockRequests() {
    if(typeof MockRequests === "undefined" ||
        typeof htmx === "undefined" ||
        typeof _hyperscript === "undefined") {
        // console.log("Not defined yet");
        setTimeout(initMockRequests, 20);
    } else {
        // console.log("defining");
        htmx.findAll("template").forEach(function(elt){
            if(elt.getAttribute("url")){
                MockRequests.setDynamicMockUrlResponse(elt.getAttribute("url"),
                    {dynamicResponseModFn:
                            function(request, response) {
                                return interpolate(elt.innerHTML);
                            },
                    usePathnameForAllQueries: true});
            }
        });
    }
}

addScript("https://unpkg.com/htmx.org@1.6.1/dist/htmx.js");
addScript("https://unpkg.com/hyperscript.org@0.9.4/dist/_hyperscript_w9y.min.js");
addScript("https://unpkg.com/mock-requests@1.3.2/index.js");
initMockRequests();