diff options
author | carson <carson@leaddyno.com> | 2022-02-17 11:19:48 -0700 |
---|---|---|
committer | carson <carson@leaddyno.com> | 2022-02-17 11:19:48 -0700 |
commit | dd2e4704228cd8dd128f17725babe6a69256ef5c (patch) | |
tree | ea6c497c3a9a67555c502ff27c41018795e4adbb /www/js/demo/it.js | |
parent | b3751e1ff2617301d2c2319f4d70ee6334988531 (diff) | |
download | htmx-dd2e4704228cd8dd128f17725babe6a69256ef5c.tar.gz htmx-dd2e4704228cd8dd128f17725babe6a69256ef5c.zip |
demo site setup
Diffstat (limited to 'www/js/demo/it.js')
-rw-r--r-- | www/js/demo/it.js | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/www/js/demo/it.js b/www/js/demo/it.js new file mode 100644 index 00000000..31eb8009 --- /dev/null +++ b/www/js/demo/it.js @@ -0,0 +1,58 @@ +function addScript(url) { + var myScript = document.createElement('script'); + myScript.setAttribute('src', url); + document.head.appendChild(myScript); +} + +function interpolate(str, params) { + var returnStr = ""; + try { + var charArray = Array.from(str); + while (charArray.length > 0) { + var current = charArray.shift(); + if (current === "$" && charArray[0] === "{") { + var evalStr = "(function(env) { with(env) { return " + charArray.shift(); + while (charArray.length > 0 && charArray[0] !== "}") { + evalStr += charArray.shift() + } + charArray.shift(); + evalStr += " } })"; + // console.log("Evaling", evalStr); + returnStr += eval(evalStr)(params); + } else { + returnStr += current; + } + } + } catch (e) { + returnStr = e.message; + } + 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, parameters) { + console.log(request, response, parameters) + return interpolate(elt.innerHTML, parameters); + }, + 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(); |