Skip to content

Commit

Permalink
Upgrade example project dependencies (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz authored Aug 5, 2024
1 parent 7cb088a commit 75e2493
Show file tree
Hide file tree
Showing 5 changed files with 5,124 additions and 3,334 deletions.
39 changes: 21 additions & 18 deletions example/download_htmx.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
#!/usr/bin/env python
"""
Download the given htmx version and the extensions we're using.
Download the htmx version and the extensions we're using.
This is only intended for maintaining the example app.
"""
from __future__ import annotations

import argparse
import subprocess


def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument("version", help="e.g. 1.0.1")
args = parser.parse_args(argv)
version: str = args.version

download_file(version, "htmx.js")
download_file(version, "ext/debug.js")
download_file(version, "ext/event-header.js")
def main() -> int:
# Per: https://htmx.org/docs/#installing
# Note using @ to pin version because htmx hasn’t made v2 the “latest” at
# time of writing.
download_file(
"https://unpkg.com/htmx.org@2.0.1/dist/htmx.js",
"htmx.js",
)
# Per: https://github.com/bigskysoftware/htmx-extensions/tree/main/src/debug
download_file(
"https://unpkg.com/htmx-ext-debug/debug.js",
"ext/debug.js",
)
# Per: https://github.com/bigskysoftware/htmx-extensions/tree/main/src/event-header
download_file(
"https://unpkg.com/htmx-ext-event-header/event-header.js",
"ext/event-header.js",
)

print("✅")
return 0


def download_file(version: str, name: str) -> None:
def download_file(url: str, name: str) -> None:
print(f"{name}...")
proc = subprocess.run(
[
"curl",
"--fail",
"--location",
f"https://unpkg.com/htmx.org@{version}/dist/{name}",
url,
"-o",
f"example/static/{name}",
],
)
if proc.returncode != 0:
raise SystemExit(1)
# Fix lack of trailing newline in minified files as otherwise pre-commit
# has to fix it.
if name.endswith(".min.js"):
with open(f"example/static/{name}", "a") as fp:
fp.write("\n")


if __name__ == "__main__":
Expand Down
18 changes: 9 additions & 9 deletions example/example/static/ext/debug.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
htmx.defineExtension('debug', {
onEvent: function (name, evt) {
if (console.debug) {
console.debug(name, evt);
} else if (console) {
console.log("DEBUG:", name, evt);
} else {
throw "NO CONSOLE SUPPORTED"
}
onEvent: function(name, evt) {
if (console.debug) {
console.debug(name, evt)
} else if (console) {
console.log('DEBUG:', name, evt)
} else {
throw new Error('NO CONSOLE SUPPORTED')
}
});
}
})
68 changes: 34 additions & 34 deletions example/example/static/ext/event-header.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
(function(){
function stringifyEvent(event) {
var obj = {};
for (var key in event) {
obj[key] = event[key];
}
return JSON.stringify(obj, function(key, value){
if(value instanceof Node){
var nodeRep = value.tagName;
if (nodeRep) {
nodeRep = nodeRep.toLowerCase();
if(value.id){
nodeRep += "#" + value.id;
}
if(value.classList && value.classList.length){
nodeRep += "." + value.classList.toString().replace(" ", ".")
}
return nodeRep;
} else {
return "Node"
}
}
if (value instanceof Window) return 'Window';
return value;
});
(function() {
function stringifyEvent(event) {
var obj = {}
for (var key in event) {
obj[key] = event[key]
}
return JSON.stringify(obj, function(key, value) {
if (value instanceof Node) {
var nodeRep = value.tagName
if (nodeRep) {
nodeRep = nodeRep.toLowerCase()
if (value.id) {
nodeRep += '#' + value.id
}
if (value.classList && value.classList.length) {
nodeRep += '.' + value.classList.toString().replace(' ', '.')
}
return nodeRep
} else {
return 'Node'
}
}
if (value instanceof Window) return 'Window'
return value
})
}

htmx.defineExtension('event-header', {
onEvent: function (name, evt) {
if (name === "htmx:configRequest") {
if (evt.detail.triggeringEvent) {
evt.detail.headers['Triggering-Event'] = stringifyEvent(evt.detail.triggeringEvent);
}
}
htmx.defineExtension('event-header', {
onEvent: function(name, evt) {
if (name === 'htmx:configRequest') {
if (evt.detail.triggeringEvent) {
evt.detail.headers['Triggering-Event'] = stringifyEvent(evt.detail.triggeringEvent)
}
});
})();
}
}
})
})()
Loading

0 comments on commit 75e2493

Please sign in to comment.