diff options
author | carson <carson@leaddyno.com> | 2022-02-13 20:49:09 -0700 |
---|---|---|
committer | carson <carson@leaddyno.com> | 2022-02-13 20:49:09 -0700 |
commit | d1a0e4a1ee52b20e3fb4d1f8a91695dab304c852 (patch) | |
tree | e2f6207185d3bb5fc9c936b5122edd001b41313f /www/js/demo-helper.js | |
parent | a9d67900ed5c983604739c3678e0af4110a962a0 (diff) | |
download | htmx-d1a0e4a1ee52b20e3fb4d1f8a91695dab304c852.tar.gz htmx-d1a0e4a1ee52b20e3fb4d1f8a91695dab304c852.zip |
move to js folder
Diffstat (limited to 'www/js/demo-helper.js')
-rw-r--r-- | www/js/demo-helper.js | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/www/js/demo-helper.js b/www/js/demo-helper.js new file mode 100644 index 00000000..b7cf4b5f --- /dev/null +++ b/www/js/demo-helper.js @@ -0,0 +1,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(); |