Theme improvements

This commit is contained in:
roberto 2025-05-14 18:03:13 +02:00
parent 2e2ebe83fe
commit fdf12ffeaa
45 changed files with 6327 additions and 54 deletions

View file

@ -4,13 +4,13 @@
{{ partial "head.html" . }}
<body lang="{{ .Site.Language.Lang }}">
<body hx-boost="true" lang="{{ .Site.Language.Lang }}">
{{ partial "nav.html" . }}
<header class="blog">
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="col m9">
<h2><a href="{{ "blog/spot" | relLangURL }}">IBIS Blog</a></h2>
{{ partial "widgets/categoriesibis.html" . }}
</div>
@ -21,14 +21,14 @@
<div id="content">
<div class="container">
<div class="row">
<div class="col-md-9">
<section class="col-sm-12">
<div class="col l9 m9">
<section class="col s12">
<div class="list-group">
{{ $paginator := .Paginate ((where .Pages "Type" "blogibis").ByParam "lastmod").Reverse}}
{{ range $paginator.Pages }}
<div class="row blog-article-item-list">
<h4><a href="{{ .Permalink }}">{{.Title}}</a></h4>
<div class="col-lg-4 col-md-4 col-sm-12">
<div class="col l4 col m4 col s12">
{{ if isset .Params "images"}}
{{ if (fileExists (printf "assets/%s" (index .Params.images 0))) -}}
@ -40,7 +40,7 @@
{{ end }}
</div>
<div class="col-lg-8 col-md-8 col-sm-12">
<div class="col l8 col m8 col s12">
<p class="blog-article-introduction">
{{ .Summary | plainify}}
@ -65,6 +65,7 @@
<!-- /#content -->
{{ partial "footer.html" . }}
{{ partial "scripts.html" }}
</body>
</html>

View file

@ -3,13 +3,13 @@
{{ partial "head.html" . }}
<body lang="{{ .Site.Language.Lang }}">
<body hx-boost="true" lang="{{ .Site.Language.Lang }}">
{{ partial "nav.html" . }}
<header class="blog-single">
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="col m9">
<h2><a href="{{ "blog/ibis" | relLangURL }}">IBIS Blog</a></h2>
{{ partial "widgets/categoriesibis.html" . }}
<h3 class="title">{{ .Title }}</h3>
@ -21,14 +21,13 @@
<div id="content">
<div class="container">
<div class="row">
<div class="col-md-9">
<section class="col-sm-12">
<div class="col m9">
<section class="col s12">
<article>
{{ if isset .Params "images"}}
{{ if (fileExists (printf "assets/%s" (index .Params.images 0))) -}}
{{ $mainimage := resources.Get (index .Params.images 0) }}
<p class="text-center"><img class="img-fluid" src="{{ $mainimage.RelPermalink }}"
width="500">
<p style="text-algin: center"><img class="img-fluid" src="{{ $mainimage.RelPermalink }}" width="500">
</p>
{{ end }}
{{ end }}
@ -52,6 +51,7 @@
<!-- /#content -->
{{ partial "footer.html" . }}
{{ partial "scripts.html" }}
</body>
</html>

View file

@ -3,7 +3,7 @@
{{ partial "head.html" . }}
<body lang="{{ .Site.Language.Lang }}">
<body hx-boost="true" lang="{{ .Site.Language.Lang }}">
{{ partial "nav.html" . }}
<header class="blog">
@ -64,6 +64,7 @@
<!-- /#content -->
{{ partial "footer.html" . }}
{{ partial "scripts.html" }}
</body>
</html>

View file

@ -3,7 +3,7 @@
{{ partial "head.html" . }}
<body lang="{{ .Site.Language.Lang }}">
<body hx-boost="true" lang="{{ .Site.Language.Lang }}">
{{ partial "nav.html" . }}
<header class="blog-single">
@ -53,6 +53,7 @@
<!-- /#content -->
{{ partial "footer.html" . }}
{{ partial "scripts.html" }}
</body>
</html>

View file

@ -3,7 +3,7 @@
{{ partial "head.html" . }}
<body lang="{{ .Site.Language.Lang }}">
<body hx-boost="true" lang="{{ .Site.Language.Lang }}">
{{ partial "nav.html" . }}
<header class="service">
@ -67,6 +67,7 @@
<!-- /#content -->
{{ partial "footer.html" . }}
{{ partial "scripts.html" }}
</body>
</html>

View file

@ -3,7 +3,7 @@
{{ partial "head.html" . }}
<body lang="{{ .Site.Language.Lang }}">
<body hx-boost="true" lang="{{ .Site.Language.Lang }}">
{{ partial "nav.html" . }}
<header class="blog">
@ -68,6 +68,7 @@
<!-- /#content -->
{{ partial "footer.html" . }}
{{ partial "scripts.html" }}
</body>
</html>

View file

@ -1,4 +1,4 @@
<section class="col-sm-3 text-center blogsidebar" style="border-left: 1px solid #ddd">
<section class="col s3 text-center blogsidebar" style="border-left: 1px solid #ddd">
{{ if isset .Site.Taxonomies "tagsibis" }}
{{ if not (eq (len .Site.Taxonomies.tagsibis) 0) }}
<div class="tags">
@ -29,7 +29,7 @@
<h4 class="text-center">{{ i18n "latest_articles" }}</h4>
{{ range . }}
<div class="col-12 article" style="text-align: center">
<div class="col l12 article" style="text-align: center">
{{ partial "widgets/article.html" . }}
</div>

View file

@ -3,7 +3,7 @@
{{ with $projects }}
<h4 class="text-center">{{ i18n "related_projects" }}</h4>
{{ range . }}
<div class="col-12 article" style="text-align: center">
<div class="col l12 article" style="text-align: center">
{{ partial "widgets/article.html" . }}
</div>
{{ end }}

View file

@ -3,14 +3,14 @@
{{ partial "head.html" . }}
<body lang="{{ .Site.Language.Lang }}">
<body hx-boost="true" lang="{{ .Site.Language.Lang }}">
{{ partial "nav.html" . }}
<div id="page-content">
<header class="service">
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="col m9">
<h2>{{ .Title }}</h2>
<p>{{ .Params.shortDescription }}</p>
</div>
@ -25,12 +25,11 @@
<div class="container">
<div class="row">
<div class="col-md-9">
<section class="col-sm-12">
<div class="col m9 l9">
<section class="col s12">
{{ if (fileExists (printf "assets/%s" (index .Params.images 0))) -}}
{{ $mainimage := resources.Get (index .Params.images 0) }}
<p class="text-center"><img class="img-fluid" src="{{ $mainimage.RelPermalink }}"
width="500">
<p style="text-align: center"><img class="img-fluid" src="{{ $mainimage.RelPermalink }}" width="500">
</p>
{{ end }}
<div style="text-align: justify;">
@ -39,7 +38,7 @@
</section>
</div>
<div class="col-md-3 servicesidebar">
<div class="col m3 l3 servicesidebar">
{{ partial "widgets/servicesidebar.html" . }}
</div>
@ -55,6 +54,6 @@
</div>
{{ partial "footer.html" . }}
{{ partial "scripts.html" }}
</body>
</html>

View file

@ -3,7 +3,7 @@
{{ partial "head.html" . }}
<body lang="{{ .Site.Language.Lang }}">
<body hx-boost="true" lang="{{ .Site.Language.Lang }}">
{{ partial "nav.html" . }}
<header class="blog">
@ -68,6 +68,7 @@
<!-- /#content -->
{{ partial "footer.html" . }}
{{ partial "scripts.html" }}
</body>
</html>

View file

@ -3,7 +3,7 @@
{{ partial "head.html" . }}
<body lang="{{ .Site.Language.Lang }}">
<body hx-boost="true" lang="{{ .Site.Language.Lang }}">
{{ partial "nav.html" . }}
<header class="blog">
@ -68,6 +68,7 @@
<!-- /#content -->
{{ partial "footer.html" . }}
{{ partial "scripts.html" }}
</body>
</html>

View file

@ -51,6 +51,12 @@
--svg: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23000' d='M10.616 16.077H7.077q-1.692 0-2.884-1.192T3 12t1.193-2.885t2.884-1.193h3.539v1H7.077q-1.27 0-2.173.904Q4 10.731 4 12t.904 2.173t2.173.904h3.539zM8.5 12.5v-1h7v1zm4.885 3.577v-1h3.538q1.27 0 2.173-.904Q20 13.269 20 12t-.904-2.173t-2.173-.904h-3.538v-1h3.538q1.692 0 2.885 1.192T21 12t-1.193 2.885t-2.884 1.193z'/%3E%3C/svg%3E");
}
.icon-menu {
--svg: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23000' d='M4 17.27v-1h16v1zm0-4.77v-1h16v1zm0-4.77v-1h16v1z'/%3E%3C/svg%3E");
-webkit-mask-image: var(--svg);
mask-image: var(--svg);
}
#home-introduction {
text-align: justify;
}

View file

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="{{ .Site.Language.Lang }}">
{{ partial "head.html" . }}
<body>
<body hx-boost="true" lang="{{ .Site.Language.Lang }}">
{{ partial "nav.html" . }}
{{ partial "home-banner.html" . }}
@ -20,6 +20,7 @@
{{ partial "home-bottom.html" . }}
</div>
{{ partial "footer.html" . }}
{{ partial "scripts.html" }}
</body>
</html>

View file

@ -5,7 +5,7 @@
</a>
<div id="topmenu">
<input type="checkbox" id="responsive-button"><label for="responsive-button"></label>
<button id="responsive-button" onclick="htmx.toggleClass(htmx.find('#topmenu'), 'responsive')"><i class="icon icon-menu"></i></button>
<ul>
{{ $current := . }}
{{ range .Site.Menus.main }}

View file

@ -0,0 +1,2 @@
<script src="/js/htmx/htmx.min.js"></script>
<script src="/js/main.js"></script>

View file

@ -115,7 +115,7 @@ a {
/* End reset */
body {
padding-top: 60px;
padding-top: 100px;
font-family: "Arial";
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
@ -331,36 +331,32 @@ body {
display: none;
margin: 0;
padding: 0;
height: 45px;
width: 100%;
opacity: 0;
width: 32px;
height: 32px;
cursor: pointer
}
#responsive-button + label {cursor: pointer;}
}
@media screen and (max-width: 992px) {
body {
padding-top: 60px;
};
#topmenu {
position:relative;
#responsive-button {
display: block;
}
label:before {
font-size: 1.6em;
color: #FFFFFF;
content: "\2261";
margin-left: 20px;
}
ul {
background:var(--dark-color);
position:absolute;
z-index:3;
height:auto;
display:none;
top: 0;
border-radius: 6px;
top: 45px;
right: -10px;
flex-direction: column;
border: 1px solid #fff;
border-bottom-left-radius: 10px;
padding-left:0;
}
@ -386,13 +382,14 @@ body {
color: #fff;
}
li {display:block;float:left;width:auto;}
input, label {position:absolute;right:0;display:block}
input {z-index:4}
input:checked + label {color:#FFFFFF}
input:checked + label:before {content:"\00d7"}
input:checked ~ ul, input:checked ~ ul>li>ul.submenu {display: flex;visibility: visible; opacity: 1}
input:checked ~ ul>li>ul.submenu>li {visibility: visible; display:block;}
:is(ul li:hover > ul, li:focus-within > ul, ul li ul:hover, ul li ul:focus) {
display: flex;
}
}
#topmenu.responsive ul {display: flex;visibility: visible; opacity: 1; margin-top:0}
#topmenu.responsive ul>li>ul.submenu>li {visibility: visible; display:block;}
}
#home-introduction {
@ -692,9 +689,7 @@ footer {
}
}
/* Max large */
@media (max-width: 992px) {
@media (max-width: 1140px) {
#page-content .container, footer .container {
padding-left: 20px;
padding-right: 20px;

View file

@ -0,0 +1,7 @@
htmx.defineExtension('ajax-header', {
onEvent: function (name, evt) {
if (name === "htmx:configRequest") {
evt.detail.headers['X-Requested-With'] = 'XMLHttpRequest';
}
}
});

View file

@ -0,0 +1,16 @@
htmx.defineExtension('alpine-morph', {
isInlineSwap: function (swapStyle) {
return swapStyle === 'morph';
},
handleSwap: function (swapStyle, target, fragment) {
if (swapStyle === 'morph') {
if (fragment.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
Alpine.morph(target, fragment.firstElementChild);
return [target];
} else {
Alpine.morph(target, fragment.outerHTML);
return [target];
}
}
}
});

View file

@ -0,0 +1,92 @@
(function () {
function splitOnWhitespace(trigger) {
return trigger.split(/\s+/);
}
function parseClassOperation(trimmedValue) {
var split = splitOnWhitespace(trimmedValue);
if (split.length > 1) {
var operation = split[0];
var classDef = split[1].trim();
var cssClass;
var delay;
if (classDef.indexOf(":") > 0) {
var splitCssClass = classDef.split(':');
cssClass = splitCssClass[0];
delay = htmx.parseInterval(splitCssClass[1]);
} else {
cssClass = classDef;
delay = 100;
}
return {
operation: operation,
cssClass: cssClass,
delay: delay
}
} else {
return null;
}
}
function performOperation(elt, classOperation, classList, currentRunTime) {
setTimeout(function () {
elt.classList[classOperation.operation].call(elt.classList, classOperation.cssClass);
}, currentRunTime)
}
function toggleOperation(elt, classOperation, classList, currentRunTime) {
setTimeout(function () {
setInterval(function () {
elt.classList[classOperation.operation].call(elt.classList, classOperation.cssClass);
}, classOperation.delay);
}, currentRunTime)
}
function processClassList(elt, classList) {
var runs = classList.split("&");
for (var i = 0; i < runs.length; i++) {
var run = runs[i];
var currentRunTime = 0;
var classOperations = run.split(",");
for (var j = 0; j < classOperations.length; j++) {
var value = classOperations[j];
var trimmedValue = value.trim();
var classOperation = parseClassOperation(trimmedValue);
if (classOperation) {
if (classOperation.operation === "toggle") {
toggleOperation(elt, classOperation, classList, currentRunTime);
currentRunTime = currentRunTime + classOperation.delay;
} else {
currentRunTime = currentRunTime + classOperation.delay;
performOperation(elt, classOperation, classList, currentRunTime);
}
}
}
}
}
function maybeProcessClasses(elt) {
if (elt.getAttribute) {
var classList = elt.getAttribute("classes") || elt.getAttribute("data-classes");
if (classList) {
processClassList(elt, classList);
}
}
}
htmx.defineExtension('class-tools', {
onEvent: function (name, evt) {
if (name === "htmx:afterProcessNode") {
var elt = evt.detail.elt;
maybeProcessClasses(elt);
if (elt.querySelectorAll) {
var children = elt.querySelectorAll("[classes], [data-classes]");
for (var i = 0; i < children.length; i++) {
maybeProcessClasses(children[i]);
}
}
}
}
});
})();

View file

@ -0,0 +1,96 @@
htmx.defineExtension('client-side-templates', {
transformResponse : function(text, xhr, elt) {
var mustacheTemplate = htmx.closest(elt, "[mustache-template]");
if (mustacheTemplate) {
var data = JSON.parse(text);
var templateId = mustacheTemplate.getAttribute('mustache-template');
var template = htmx.find("#" + templateId);
if (template) {
return Mustache.render(template.innerHTML, data);
} else {
throw "Unknown mustache template: " + templateId;
}
}
var mustacheArrayTemplate = htmx.closest(elt, "[mustache-array-template]");
if (mustacheArrayTemplate) {
var data = JSON.parse(text);
var templateId = mustacheArrayTemplate.getAttribute('mustache-array-template');
var template = htmx.find("#" + templateId);
if (template) {
return Mustache.render(template.innerHTML, {"data": data });
} else {
throw "Unknown mustache template: " + templateId;
}
}
var handlebarsTemplate = htmx.closest(elt, "[handlebars-template]");
if (handlebarsTemplate) {
var data = JSON.parse(text);
var templateId = handlebarsTemplate.getAttribute('handlebars-template');
var templateElement = htmx.find('#' + templateId).innerHTML;
var renderTemplate = Handlebars.compile(templateElement);
if (renderTemplate) {
return renderTemplate(data);
} else {
throw "Unknown handlebars template: " + templateId;
}
}
var handlebarsArrayTemplate = htmx.closest(elt, "[handlebars-array-template]");
if (handlebarsArrayTemplate) {
var data = JSON.parse(text);
var templateId = handlebarsArrayTemplate.getAttribute('handlebars-array-template');
var templateElement = htmx.find('#' + templateId).innerHTML;
var renderTemplate = Handlebars.compile(templateElement);
if (renderTemplate) {
return renderTemplate(data);
} else {
throw "Unknown handlebars template: " + templateId;
}
}
var nunjucksTemplate = htmx.closest(elt, "[nunjucks-template]");
if (nunjucksTemplate) {
var data = JSON.parse(text);
var templateName = nunjucksTemplate.getAttribute('nunjucks-template');
var template = htmx.find('#' + templateName);
if (template) {
return nunjucks.renderString(template.innerHTML, data);
} else {
return nunjucks.render(templateName, data);
}
}
var xsltTemplate = htmx.closest(elt, "[xslt-template]");
if (xsltTemplate) {
var templateId = xsltTemplate.getAttribute('xslt-template');
var template = htmx.find("#" + templateId);
if (template) {
var content = template.innerHTML ? new DOMParser().parseFromString(template.innerHTML, 'application/xml')
: template.contentDocument;
var processor = new XSLTProcessor();
processor.importStylesheet(content);
var data = new DOMParser().parseFromString(text, "application/xml");
var frag = processor.transformToFragment(data, document);
return new XMLSerializer().serializeToString(frag);
} else {
throw "Unknown XSLT template: " + templateId;
}
}
var nunjucksArrayTemplate = htmx.closest(elt, "[nunjucks-array-template]");
if (nunjucksArrayTemplate) {
var data = JSON.parse(text);
var templateName = nunjucksArrayTemplate.getAttribute('nunjucks-array-template');
var template = htmx.find('#' + templateName);
if (template) {
return nunjucks.renderString(template.innerHTML, {"data": data});
} else {
return nunjucks.render(templateName, {"data": data});
}
}
return text;
}
});

View file

@ -0,0 +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"
}
}
});

View file

@ -0,0 +1,18 @@
"use strict";
// Disable Submit Button
htmx.defineExtension('disable-element', {
onEvent: function (name, evt) {
let elt = evt.detail.elt;
let target = elt.getAttribute("hx-disable-element");
let targetElements = (target == "self") ? [ elt ] : document.querySelectorAll(target);
for (var i = 0; i < targetElements.length; i++) {
if (name === "htmx:beforeRequest" && targetElements[i]) {
targetElements[i].disabled = true;
} else if (name == "htmx:afterRequest" && targetElements[i]) {
targetElements[i].disabled = false;
}
}
}
});

View file

@ -0,0 +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;
});
}
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);
}
}
}
});
})();

View file

@ -0,0 +1,141 @@
//==========================================================
// head-support.js
//
// An extension to htmx 1.0 to add head tag merging.
//==========================================================
(function(){
var api = null;
function log() {
//console.log(arguments);
}
function mergeHead(newContent, defaultMergeStrategy) {
if (newContent && newContent.indexOf('<head') > -1) {
const htmlDoc = document.createElement("html");
// remove svgs to avoid conflicts
var contentWithSvgsRemoved = newContent.replace(/<svg(\s[^>]*>|>)([\s\S]*?)<\/svg>/gim, '');
// extract head tag
var headTag = contentWithSvgsRemoved.match(/(<head(\s[^>]*>|>)([\s\S]*?)<\/head>)/im);
// if the head tag exists...
if (headTag) {
var added = []
var removed = []
var preserved = []
var nodesToAppend = []
htmlDoc.innerHTML = headTag;
var newHeadTag = htmlDoc.querySelector("head");
var currentHead = document.head;
if (newHeadTag == null) {
return;
} else {
// put all new head elements into a Map, by their outerHTML
var srcToNewHeadNodes = new Map();
for (const newHeadChild of newHeadTag.children) {
srcToNewHeadNodes.set(newHeadChild.outerHTML, newHeadChild);
}
}
// determine merge strategy
var mergeStrategy = api.getAttributeValue(newHeadTag, "hx-head") || defaultMergeStrategy;
// get the current head
for (const currentHeadElt of currentHead.children) {
// If the current head element is in the map
var inNewContent = srcToNewHeadNodes.has(currentHeadElt.outerHTML);
var isReAppended = currentHeadElt.getAttribute("hx-head") === "re-eval";
var isPreserved = api.getAttributeValue(currentHeadElt, "hx-preserve") === "true";
if (inNewContent || isPreserved) {
if (isReAppended) {
// remove the current version and let the new version replace it and re-execute
removed.push(currentHeadElt);
} else {
// this element already exists and should not be re-appended, so remove it from
// the new content map, preserving it in the DOM
srcToNewHeadNodes.delete(currentHeadElt.outerHTML);
preserved.push(currentHeadElt);
}
} else {
if (mergeStrategy === "append") {
// we are appending and this existing element is not new content
// so if and only if it is marked for re-append do we do anything
if (isReAppended) {
removed.push(currentHeadElt);
nodesToAppend.push(currentHeadElt);
}
} else {
// if this is a merge, we remove this content since it is not in the new head
if (api.triggerEvent(document.body, "htmx:removingHeadElement", {headElement: currentHeadElt}) !== false) {
removed.push(currentHeadElt);
}
}
}
}
// Push the tremaining new head elements in the Map into the
// nodes to append to the head tag
nodesToAppend.push(...srcToNewHeadNodes.values());
log("to append: ", nodesToAppend);
for (const newNode of nodesToAppend) {
log("adding: ", newNode);
var newElt = document.createRange().createContextualFragment(newNode.outerHTML);
log(newElt);
if (api.triggerEvent(document.body, "htmx:addingHeadElement", {headElement: newElt}) !== false) {
currentHead.appendChild(newElt);
added.push(newElt);
}
}
// remove all removed elements, after we have appended the new elements to avoid
// additional network requests for things like style sheets
for (const removedElement of removed) {
if (api.triggerEvent(document.body, "htmx:removingHeadElement", {headElement: removedElement}) !== false) {
currentHead.removeChild(removedElement);
}
}
api.triggerEvent(document.body, "htmx:afterHeadMerge", {added: added, kept: preserved, removed: removed});
}
}
}
htmx.defineExtension("head-support", {
init: function(apiRef) {
// store a reference to the internal API.
api = apiRef;
htmx.on('htmx:afterSwap', function(evt){
var serverResponse = evt.detail.xhr.response;
if (api.triggerEvent(document.body, "htmx:beforeHeadMerge", evt.detail)) {
mergeHead(serverResponse, evt.detail.boosted ? "merge" : "append");
}
})
htmx.on('htmx:historyRestore', function(evt){
if (api.triggerEvent(document.body, "htmx:beforeHeadMerge", evt.detail)) {
if (evt.detail.cacheMiss) {
mergeHead(evt.detail.serverResponse, "merge");
} else {
mergeHead(evt.detail.item.head, "merge");
}
}
})
htmx.on('htmx:historyItemCreated', function(evt){
var historyItem = evt.detail.item;
historyItem.head = document.head.outerHTML;
})
}
});
})()

View file

@ -0,0 +1,45 @@
const slTypes = 'sl-checkbox, sl-color-picker, sl-input, sl-radio-group, sl-range, sl-rating, sl-select, sl-switch, sl-textarea'
/* Lightly modified version of the same function in htmx.js */
function shouldInclude(elt) {
// sl-rating doesn't have a name attribute exposed through the Shoelace API (for elt.name) so this check needs to come before the name==="" check
if (elt.tagName === 'SL-RATING' && elt.getAttribute('name')) {
return true
}
if (elt.name === "" || elt.name == null || elt.disabled || elt.closest("fieldset[disabled]")) {
return false
}
if (elt.tagName === 'SL-CHECKBOX' || elt.tagName === 'SL-SWITCH') {
return elt.checked
}
if (elt.tagName === "SL-RADIO-GROUP") {
return elt.value.length > 0
}
return true;
}
htmx.defineExtension('shoelace', {
onEvent : function(name, evt) {
if ((name === "htmx:configRequest") && (evt.detail.elt.tagName === 'FORM')) {
evt.detail.elt.querySelectorAll(slTypes).forEach((elt) => {
if (shouldInclude(elt)) {
if (elt.tagName === 'SL-CHECKBOX' || elt.tagName === 'SL-SWITCH') {
// Shoelace normally does this bit internally when the formdata event fires, but htmx doesn't fire the formdata event, so we do it here instead. See https://github.com/shoelace-style/shoelace/issues/1891
evt.detail.parameters[elt.name] = elt.value || 'on'
} else if (elt.tagName == 'SL-RATING') {
evt.detail.parameters[elt.getAttribute('name')] = elt.value
} else {
evt.detail.parameters[elt.name] = elt.value
}
}
})
}
}
})

View file

@ -0,0 +1,24 @@
(function(){
function mergeObjects(obj1, obj2) {
for (var key in obj2) {
if (obj2.hasOwnProperty(key)) {
obj1[key] = obj2[key];
}
}
return obj1;
}
htmx.defineExtension('include-vals', {
onEvent: function (name, evt) {
if (name === "htmx:configRequest") {
var includeValsElt = htmx.closest(evt.detail.elt, "[include-vals],[data-include-vals]");
if (includeValsElt) {
var includeVals = includeValsElt.getAttribute("include-vals") || includeValsElt.getAttribute("data-include-vals");
var valuesToInclude = eval("({" + includeVals + "})");
mergeObjects(evt.detail.parameters, valuesToInclude);
}
}
}
});
})();

View file

@ -0,0 +1,12 @@
htmx.defineExtension('json-enc', {
onEvent: function (name, evt) {
if (name === "htmx:configRequest") {
evt.detail.headers['Content-Type'] = "application/json";
}
},
encodeParameters : function(xhr, parameters, elt) {
xhr.overrideMimeType('text/json');
return (JSON.stringify(parameters));
}
});

View file

@ -0,0 +1,183 @@
;(function () {
let loadingStatesUndoQueue = []
function loadingStateContainer(target) {
return htmx.closest(target, '[data-loading-states]') || document.body
}
function mayProcessUndoCallback(target, callback) {
if (document.body.contains(target)) {
callback()
}
}
function mayProcessLoadingStateByPath(elt, requestPath) {
const pathElt = htmx.closest(elt, '[data-loading-path]')
if (!pathElt) {
return true
}
return pathElt.getAttribute('data-loading-path') === requestPath
}
function queueLoadingState(sourceElt, targetElt, doCallback, undoCallback) {
const delayElt = htmx.closest(sourceElt, '[data-loading-delay]')
if (delayElt) {
const delayInMilliseconds =
delayElt.getAttribute('data-loading-delay') || 200
const timeout = setTimeout(function () {
doCallback()
loadingStatesUndoQueue.push(function () {
mayProcessUndoCallback(targetElt, undoCallback)
})
}, delayInMilliseconds)
loadingStatesUndoQueue.push(function () {
mayProcessUndoCallback(targetElt, function () { clearTimeout(timeout) })
})
} else {
doCallback()
loadingStatesUndoQueue.push(function () {
mayProcessUndoCallback(targetElt, undoCallback)
})
}
}
function getLoadingStateElts(loadingScope, type, path) {
return Array.from(htmx.findAll(loadingScope, "[" + type + "]")).filter(
function (elt) { return mayProcessLoadingStateByPath(elt, path) }
)
}
function getLoadingTarget(elt) {
if (elt.getAttribute('data-loading-target')) {
return Array.from(
htmx.findAll(elt.getAttribute('data-loading-target'))
)
}
return [elt]
}
htmx.defineExtension('loading-states', {
onEvent: function (name, evt) {
if (name === 'htmx:beforeRequest') {
const container = loadingStateContainer(evt.target)
const loadingStateTypes = [
'data-loading',
'data-loading-class',
'data-loading-class-remove',
'data-loading-disable',
'data-loading-aria-busy',
]
let loadingStateEltsByType = {}
loadingStateTypes.forEach(function (type) {
loadingStateEltsByType[type] = getLoadingStateElts(
container,
type,
evt.detail.pathInfo.requestPath
)
})
loadingStateEltsByType['data-loading'].forEach(function (sourceElt) {
getLoadingTarget(sourceElt).forEach(function (targetElt) {
queueLoadingState(
sourceElt,
targetElt,
function () {
targetElt.style.display =
sourceElt.getAttribute('data-loading') ||
'inline-block' },
function () { targetElt.style.display = 'none' }
)
})
})
loadingStateEltsByType['data-loading-class'].forEach(
function (sourceElt) {
const classNames = sourceElt
.getAttribute('data-loading-class')
.split(' ')
getLoadingTarget(sourceElt).forEach(function (targetElt) {
queueLoadingState(
sourceElt,
targetElt,
function () {
classNames.forEach(function (className) {
targetElt.classList.add(className)
})
},
function() {
classNames.forEach(function (className) {
targetElt.classList.remove(className)
})
}
)
})
}
)
loadingStateEltsByType['data-loading-class-remove'].forEach(
function (sourceElt) {
const classNames = sourceElt
.getAttribute('data-loading-class-remove')
.split(' ')
getLoadingTarget(sourceElt).forEach(function (targetElt) {
queueLoadingState(
sourceElt,
targetElt,
function () {
classNames.forEach(function (className) {
targetElt.classList.remove(className)
})
},
function() {
classNames.forEach(function (className) {
targetElt.classList.add(className)
})
}
)
})
}
)
loadingStateEltsByType['data-loading-disable'].forEach(
function (sourceElt) {
getLoadingTarget(sourceElt).forEach(function (targetElt) {
queueLoadingState(
sourceElt,
targetElt,
function() { targetElt.disabled = true },
function() { targetElt.disabled = false }
)
})
}
)
loadingStateEltsByType['data-loading-aria-busy'].forEach(
function (sourceElt) {
getLoadingTarget(sourceElt).forEach(function (targetElt) {
queueLoadingState(
sourceElt,
targetElt,
function () { targetElt.setAttribute("aria-busy", "true") },
function () { targetElt.removeAttribute("aria-busy") }
)
})
}
)
}
if (name === 'htmx:beforeOnLoad') {
while (loadingStatesUndoQueue.length > 0) {
loadingStatesUndoQueue.shift()()
}
}
},
})
})()

View file

@ -0,0 +1,11 @@
htmx.defineExtension('method-override', {
onEvent: function (name, evt) {
if (name === "htmx:configRequest") {
var method = evt.detail.verb;
if (method !== "get" || method !== "post") {
evt.detail.headers['X-HTTP-Method-Override'] = method.toUpperCase();
evt.detail.verb = "post";
}
}
}
});

View file

@ -0,0 +1,17 @@
htmx.defineExtension('morphdom-swap', {
isInlineSwap: function(swapStyle) {
return swapStyle === 'morphdom';
},
handleSwap: function (swapStyle, target, fragment) {
if (swapStyle === 'morphdom') {
if (fragment.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
// IE11 doesn't support DocumentFragment.firstElementChild
morphdom(target, fragment.firstElementChild || fragment.firstChild);
return [target];
} else {
morphdom(target, fragment.outerHTML);
return [target];
}
}
}
});

View file

@ -0,0 +1,45 @@
(function () {
/** @type {import("../htmx").HtmxInternalApi} */
var api;
htmx.defineExtension('multi-swap', {
init: function (apiRef) {
api = apiRef;
},
isInlineSwap: function (swapStyle) {
return swapStyle.indexOf('multi:') === 0;
},
handleSwap: function (swapStyle, target, fragment, settleInfo) {
if (swapStyle.indexOf('multi:') === 0) {
var selectorToSwapStyle = {};
var elements = swapStyle.replace(/^multi\s*:\s*/, '').split(/\s*,\s*/);
elements.map(function (element) {
var split = element.split(/\s*:\s*/);
var elementSelector = split[0];
var elementSwapStyle = typeof (split[1]) !== "undefined" ? split[1] : "innerHTML";
if (elementSelector.charAt(0) !== '#') {
console.error("HTMX multi-swap: unsupported selector '" + elementSelector + "'. Only ID selectors starting with '#' are supported.");
return;
}
selectorToSwapStyle[elementSelector] = elementSwapStyle;
});
for (var selector in selectorToSwapStyle) {
var swapStyle = selectorToSwapStyle[selector];
var elementToSwap = fragment.querySelector(selector);
if (elementToSwap) {
api.oobSwap(swapStyle, elementToSwap, settleInfo);
} else {
console.warn("HTMX multi-swap: selector '" + selector + "' not found in source content.");
}
}
return true;
}
}
});
})();

View file

@ -0,0 +1,60 @@
(function(undefined){
'use strict';
// Save a reference to the global object (window in the browser)
var _root = this;
function dependsOn(pathSpec, url) {
if (pathSpec === "ignore") {
return false;
}
var dependencyPath = pathSpec.split("/");
var urlPath = url.split("/");
for (var i = 0; i < urlPath.length; i++) {
var dependencyElement = dependencyPath.shift();
var pathElement = urlPath[i];
if (dependencyElement !== pathElement && dependencyElement !== "*") {
return false;
}
if (dependencyPath.length === 0 || (dependencyPath.length === 1 && dependencyPath[0] === "")) {
return true;
}
}
return false;
}
function refreshPath(path) {
var eltsWithDeps = htmx.findAll("[path-deps]");
for (var i = 0; i < eltsWithDeps.length; i++) {
var elt = eltsWithDeps[i];
if (dependsOn(elt.getAttribute('path-deps'), path)) {
htmx.trigger(elt, "path-deps");
}
}
}
htmx.defineExtension('path-deps', {
onEvent: function (name, evt) {
if (name === "htmx:beforeOnLoad") {
var config = evt.detail.requestConfig;
// mutating call
if (config.verb !== "get" && evt.target.getAttribute('path-deps') !== 'ignore') {
refreshPath(config.path);
}
}
}
});
/**
* ********************
* Expose functionality
* ********************
*/
_root.PathDeps = {
refresh: function(path) {
refreshPath(path);
}
};
}).call(this);

View file

@ -0,0 +1,147 @@
// This adds the "preload" extension to htmx. By default, this will
// preload the targets of any tags with `href` or `hx-get` attributes
// if they also have a `preload` attribute as well. See documentation
// for more details
htmx.defineExtension("preload", {
onEvent: function(name, event) {
// Only take actions on "htmx:afterProcessNode"
if (name !== "htmx:afterProcessNode") {
return;
}
// SOME HELPER FUNCTIONS WE'LL NEED ALONG THE WAY
// attr gets the closest non-empty value from the attribute.
var attr = function(node, property) {
if (node == undefined) {return undefined;}
return node.getAttribute(property) || node.getAttribute("data-" + property) || attr(node.parentElement, property)
}
// load handles the actual HTTP fetch, and uses htmx.ajax in cases where we're
// preloading an htmx resource (this sends the same HTTP headers as a regular htmx request)
var load = function(node) {
// Called after a successful AJAX request, to mark the
// content as loaded (and prevent additional AJAX calls.)
var done = function(html) {
if (!node.preloadAlways) {
node.preloadState = "DONE"
}
if (attr(node, "preload-images") == "true") {
document.createElement("div").innerHTML = html // create and populate a node to load linked resources, too.
}
}
return function() {
// If this value has already been loaded, then do not try again.
if (node.preloadState !== "READY") {
return;
}
// Special handling for HX-GET - use built-in htmx.ajax function
// so that headers match other htmx requests, then set
// node.preloadState = TRUE so that requests are not duplicated
// in the future
var hxGet = node.getAttribute("hx-get") || node.getAttribute("data-hx-get")
if (hxGet) {
htmx.ajax("GET", hxGet, {
source: node,
handler:function(elt, info) {
done(info.xhr.responseText);
}
});
return;
}
// Otherwise, perform a standard xhr request, then set
// node.preloadState = TRUE so that requests are not duplicated
// in the future.
if (node.getAttribute("href")) {
var r = new XMLHttpRequest();
r.open("GET", node.getAttribute("href"));
r.onload = function() {done(r.responseText);};
r.send();
return;
}
}
}
// This function processes a specific node and sets up event handlers.
// We'll search for nodes and use it below.
var init = function(node) {
// If this node DOES NOT include a "GET" transaction, then there's nothing to do here.
if (node.getAttribute("href") + node.getAttribute("hx-get") + node.getAttribute("data-hx-get") == "") {
return;
}
// Guarantee that we only initialize each node once.
if (node.preloadState !== undefined) {
return;
}
// Get event name from config.
var on = attr(node, "preload") || "mousedown"
const always = on.indexOf("always") !== -1
if (always) {
on = on.replace('always', '').trim()
}
// FALL THROUGH to here means we need to add an EventListener
// Apply the listener to the node
node.addEventListener(on, function(evt) {
if (node.preloadState === "PAUSE") { // Only add one event listener
node.preloadState = "READY"; // Required for the `load` function to trigger
// Special handling for "mouseover" events. Wait 100ms before triggering load.
if (on === "mouseover") {
window.setTimeout(load(node), 100);
} else {
load(node)() // all other events trigger immediately.
}
}
})
// Special handling for certain built-in event handlers
switch (on) {
case "mouseover":
// Mirror `touchstart` events (fires immediately)
node.addEventListener("touchstart", load(node));
// WHhen the mouse leaves, immediately disable the preload
node.addEventListener("mouseout", function(evt) {
if ((evt.target === node) && (node.preloadState === "READY")) {
node.preloadState = "PAUSE";
}
})
break;
case "mousedown":
// Mirror `touchstart` events (fires immediately)
node.addEventListener("touchstart", load(node));
break;
}
// Mark the node as ready to run.
node.preloadState = "PAUSE";
node.preloadAlways = always;
htmx.trigger(node, "preload:init") // This event can be used to load content immediately.
}
// Search for all child nodes that have a "preload" attribute
event.target.querySelectorAll("[preload]").forEach(function(node) {
// Initialize the node with the "preload" attribute
init(node)
// Initialize all child elements that are anchors or have `hx-get` (use with care)
node.querySelectorAll("a,[hx-get],[data-hx-get]").forEach(init)
})
}
})

View file

@ -0,0 +1,10 @@
htmx.defineExtension('rails-method', {
onEvent: function (name, evt) {
if (name === "configRequest.htmx") {
var methodOverride = evt.detail.headers['X-HTTP-Method-Override'];
if (methodOverride) {
evt.detail.parameters['_method'] = methodOverride;
}
}
}
});

View file

@ -0,0 +1,27 @@
(function(){
function maybeRemoveMe(elt) {
var timing = elt.getAttribute("remove-me") || elt.getAttribute("data-remove-me");
if (timing) {
setTimeout(function () {
elt.parentElement.removeChild(elt);
}, htmx.parseInterval(timing));
}
}
htmx.defineExtension('remove-me', {
onEvent: function (name, evt) {
if (name === "htmx:afterProcessNode") {
var elt = evt.detail.elt;
if (elt.getAttribute) {
maybeRemoveMe(elt);
if (elt.querySelectorAll) {
var children = elt.querySelectorAll("[remove-me], [data-remove-me]");
for (var i = 0; i < children.length; i++) {
maybeRemoveMe(children[i]);
}
}
}
}
}
});
})();

View file

@ -0,0 +1,130 @@
(function(){
/** @type {import("../htmx").HtmxInternalApi} */
var api;
var attrPrefix = 'hx-target-';
// IE11 doesn't support string.startsWith
function startsWith(str, prefix) {
return str.substring(0, prefix.length) === prefix
}
/**
* @param {HTMLElement} elt
* @param {number} respCode
* @returns {HTMLElement | null}
*/
function getRespCodeTarget(elt, respCodeNumber) {
if (!elt || !respCodeNumber) return null;
var respCode = respCodeNumber.toString();
// '*' is the original syntax, as the obvious character for a wildcard.
// The 'x' alternative was added for maximum compatibility with HTML
// templating engines, due to ambiguity around which characters are
// supported in HTML attributes.
//
// Start with the most specific possible attribute and generalize from
// there.
var attrPossibilities = [
respCode,
respCode.substr(0, 2) + '*',
respCode.substr(0, 2) + 'x',
respCode.substr(0, 1) + '*',
respCode.substr(0, 1) + 'x',
respCode.substr(0, 1) + '**',
respCode.substr(0, 1) + 'xx',
'*',
'x',
'***',
'xxx',
];
if (startsWith(respCode, '4') || startsWith(respCode, '5')) {
attrPossibilities.push('error');
}
for (var i = 0; i < attrPossibilities.length; i++) {
var attr = attrPrefix + attrPossibilities[i];
var attrValue = api.getClosestAttributeValue(elt, attr);
if (attrValue) {
if (attrValue === "this") {
return api.findThisElement(elt, attr);
} else {
return api.querySelectorExt(elt, attrValue);
}
}
}
return null;
}
/** @param {Event} evt */
function handleErrorFlag(evt) {
if (evt.detail.isError) {
if (htmx.config.responseTargetUnsetsError) {
evt.detail.isError = false;
}
} else if (htmx.config.responseTargetSetsError) {
evt.detail.isError = true;
}
}
htmx.defineExtension('response-targets', {
/** @param {import("../htmx").HtmxInternalApi} apiRef */
init: function (apiRef) {
api = apiRef;
if (htmx.config.responseTargetUnsetsError === undefined) {
htmx.config.responseTargetUnsetsError = true;
}
if (htmx.config.responseTargetSetsError === undefined) {
htmx.config.responseTargetSetsError = false;
}
if (htmx.config.responseTargetPrefersExisting === undefined) {
htmx.config.responseTargetPrefersExisting = false;
}
if (htmx.config.responseTargetPrefersRetargetHeader === undefined) {
htmx.config.responseTargetPrefersRetargetHeader = true;
}
},
/**
* @param {string} name
* @param {Event} evt
*/
onEvent: function (name, evt) {
if (name === "htmx:beforeSwap" &&
evt.detail.xhr &&
evt.detail.xhr.status !== 200) {
if (evt.detail.target) {
if (htmx.config.responseTargetPrefersExisting) {
evt.detail.shouldSwap = true;
handleErrorFlag(evt);
return true;
}
if (htmx.config.responseTargetPrefersRetargetHeader &&
evt.detail.xhr.getAllResponseHeaders().match(/HX-Retarget:/i)) {
evt.detail.shouldSwap = true;
handleErrorFlag(evt);
return true;
}
}
if (!evt.detail.requestConfig) {
return true;
}
var target = getRespCodeTarget(evt.detail.requestConfig.elt, evt.detail.xhr.status);
if (target) {
handleErrorFlag(evt);
evt.detail.shouldSwap = true;
evt.detail.target = target;
}
return true;
}
}
});
})();

View file

@ -0,0 +1,15 @@
htmx.defineExtension('restored', {
onEvent : function(name, evt) {
if (name === 'htmx:restored'){
var restoredElts = evt.detail.document.querySelectorAll(
"[hx-trigger='restored'],[data-hx-trigger='restored']"
);
// need a better way to do this, would prefer to just trigger from evt.detail.elt
var foundElt = Array.from(restoredElts).find(
(x) => (x.outerHTML === evt.detail.elt.outerHTML)
);
var restoredEvent = evt.detail.triggerEvent(foundElt, 'restored');
}
return;
}
})

View file

@ -0,0 +1,322 @@
/*
Server Sent Events Extension
============================
This extension adds support for Server Sent Events to htmx. See /www/extensions/sse.md for usage instructions.
*/
(function(){
/** @type {import("../htmx").HtmxInternalApi} */
var api;
htmx.defineExtension("sse", {
/**
* Init saves the provided reference to the internal HTMX API.
*
* @param {import("../htmx").HtmxInternalApi} api
* @returns void
*/
init: function(apiRef) {
// store a reference to the internal API.
api = apiRef;
// set a function in the public API for creating new EventSource objects
if (htmx.createEventSource == undefined) {
htmx.createEventSource = createEventSource;
}
},
/**
* onEvent handles all events passed to this extension.
*
* @param {string} name
* @param {Event} evt
* @returns void
*/
onEvent: function(name, evt) {
switch (name) {
// Try to remove remove an EventSource when elements are removed
case "htmx:beforeCleanupElement":
var internalData = api.getInternalData(evt.target)
if (internalData.sseEventSource) {
internalData.sseEventSource.close();
}
return;
// Try to create EventSources when elements are processed
case "htmx:afterProcessNode":
createEventSourceOnElement(evt.target);
}
}
});
///////////////////////////////////////////////
// HELPER FUNCTIONS
///////////////////////////////////////////////
/**
* createEventSource is the default method for creating new EventSource objects.
* it is hoisted into htmx.config.createEventSource to be overridden by the user, if needed.
*
* @param {string} url
* @returns EventSource
*/
function createEventSource(url) {
return new EventSource(url, {withCredentials:true});
}
function splitOnWhitespace(trigger) {
return trigger.trim().split(/\s+/);
}
function getLegacySSEURL(elt) {
var legacySSEValue = api.getAttributeValue(elt, "hx-sse");
if (legacySSEValue) {
var values = splitOnWhitespace(legacySSEValue);
for (var i = 0; i < values.length; i++) {
var value = values[i].split(/:(.+)/);
if (value[0] === "connect") {
return value[1];
}
}
}
}
function getLegacySSESwaps(elt) {
var legacySSEValue = api.getAttributeValue(elt, "hx-sse");
var returnArr = [];
if (legacySSEValue) {
var values = splitOnWhitespace(legacySSEValue);
for (var i = 0; i < values.length; i++) {
var value = values[i].split(/:(.+)/);
if (value[0] === "swap") {
returnArr.push(value[1]);
}
}
}
return returnArr;
}
/**
* createEventSourceOnElement creates a new EventSource connection on the provided element.
* If a usable EventSource already exists, then it is returned. If not, then a new EventSource
* is created and stored in the element's internalData.
* @param {HTMLElement} elt
* @param {number} retryCount
* @returns {EventSource | null}
*/
function createEventSourceOnElement(elt, retryCount) {
if (elt == null) {
return null;
}
var internalData = api.getInternalData(elt);
// get URL from element's attribute
var sseURL = api.getAttributeValue(elt, "sse-connect");
if (sseURL == undefined) {
var legacyURL = getLegacySSEURL(elt)
if (legacyURL) {
sseURL = legacyURL;
} else {
return null;
}
}
// Connect to the EventSource
var source = htmx.createEventSource(sseURL);
internalData.sseEventSource = source;
// Create event handlers
source.onerror = function (err) {
// Log an error event
api.triggerErrorEvent(elt, "htmx:sseError", {error:err, source:source});
// If parent no longer exists in the document, then clean up this EventSource
if (maybeCloseSSESource(elt)) {
return;
}
// Otherwise, try to reconnect the EventSource
if (source.readyState === EventSource.CLOSED) {
retryCount = retryCount || 0;
var timeout = Math.random() * (2 ^ retryCount) * 500;
window.setTimeout(function() {
createEventSourceOnElement(elt, Math.min(7, retryCount+1));
}, timeout);
}
};
source.onopen = function (evt) {
api.triggerEvent(elt, "htmx:sseOpen", {source: source});
}
// Add message handlers for every `sse-swap` attribute
queryAttributeOnThisOrChildren(elt, "sse-swap").forEach(function(child) {
var sseSwapAttr = api.getAttributeValue(child, "sse-swap");
if (sseSwapAttr) {
var sseEventNames = sseSwapAttr.split(",");
} else {
var sseEventNames = getLegacySSESwaps(child);
}
for (var i = 0 ; i < sseEventNames.length ; i++) {
var sseEventName = sseEventNames[i].trim();
var listener = function(event) {
// If the parent is missing then close SSE and remove listener
if (maybeCloseSSESource(elt)) {
source.removeEventListener(sseEventName, listener);
return;
}
// swap the response into the DOM and trigger a notification
swap(child, event.data);
api.triggerEvent(elt, "htmx:sseMessage", event);
};
// Register the new listener
api.getInternalData(elt).sseEventListener = listener;
source.addEventListener(sseEventName, listener);
}
});
// Add message handlers for every `hx-trigger="sse:*"` attribute
queryAttributeOnThisOrChildren(elt, "hx-trigger").forEach(function(child) {
var sseEventName = api.getAttributeValue(child, "hx-trigger");
if (sseEventName == null) {
return;
}
// Only process hx-triggers for events with the "sse:" prefix
if (sseEventName.slice(0, 4) != "sse:") {
return;
}
var listener = function(event) {
// If parent is missing, then close SSE and remove listener
if (maybeCloseSSESource(elt)) {
source.removeEventListener(sseEventName, listener);
return;
}
// Trigger events to be handled by the rest of htmx
htmx.trigger(child, sseEventName, event);
htmx.trigger(child, "htmx:sseMessage", event);
}
// Register the new listener
api.getInternalData(elt).sseEventListener = listener;
source.addEventListener(sseEventName.slice(4), listener);
});
}
/**
* maybeCloseSSESource confirms that the parent element still exists.
* If not, then any associated SSE source is closed and the function returns true.
*
* @param {HTMLElement} elt
* @returns boolean
*/
function maybeCloseSSESource(elt) {
if (!api.bodyContains(elt)) {
var source = api.getInternalData(elt).sseEventSource;
if (source != undefined) {
source.close();
// source = null
return true;
}
}
return false;
}
/**
* queryAttributeOnThisOrChildren returns all nodes that contain the requested attributeName, INCLUDING THE PROVIDED ROOT ELEMENT.
*
* @param {HTMLElement} elt
* @param {string} attributeName
*/
function queryAttributeOnThisOrChildren(elt, attributeName) {
var result = [];
// If the parent element also contains the requested attribute, then add it to the results too.
if (api.hasAttribute(elt, attributeName) || api.hasAttribute(elt, "hx-sse")) {
result.push(elt);
}
// Search all child nodes that match the requested attribute
elt.querySelectorAll("[" + attributeName + "], [data-" + attributeName + "], [hx-sse], [data-hx-sse]").forEach(function(node) {
result.push(node);
});
return result;
}
/**
* @param {HTMLElement} elt
* @param {string} content
*/
function swap(elt, content) {
api.withExtensions(elt, function(extension) {
content = extension.transformResponse(content, null, elt);
});
var swapSpec = api.getSwapSpecification(elt);
var target = api.getTarget(elt);
var settleInfo = api.makeSettleInfo(elt);
api.selectAndSwap(swapSpec.swapStyle, target, elt, content, settleInfo);
settleInfo.elts.forEach(function (elt) {
if (elt.classList) {
elt.classList.add(htmx.config.settlingClass);
}
api.triggerEvent(elt, 'htmx:beforeSettle');
});
// Handle settle tasks (with delay if requested)
if (swapSpec.settleDelay > 0) {
setTimeout(doSettle(settleInfo), swapSpec.settleDelay);
} else {
doSettle(settleInfo)();
}
}
/**
* doSettle mirrors much of the functionality in htmx that
* settles elements after their content has been swapped.
* TODO: this should be published by htmx, and not duplicated here
* @param {import("../htmx").HtmxSettleInfo} settleInfo
* @returns () => void
*/
function doSettle(settleInfo) {
return function() {
settleInfo.tasks.forEach(function (task) {
task.call();
});
settleInfo.elts.forEach(function (elt) {
if (elt.classList) {
elt.classList.remove(htmx.config.settlingClass);
}
api.triggerEvent(elt, 'htmx:afterSettle');
});
}
}
})();

View file

@ -0,0 +1,477 @@
/*
WebSockets Extension
============================
This extension adds support for WebSockets to htmx. See /www/extensions/ws.md for usage instructions.
*/
(function () {
/** @type {import("../htmx").HtmxInternalApi} */
var api;
htmx.defineExtension("ws", {
/**
* init is called once, when this extension is first registered.
* @param {import("../htmx").HtmxInternalApi} apiRef
*/
init: function (apiRef) {
// Store reference to internal API
api = apiRef;
// Default function for creating new EventSource objects
if (!htmx.createWebSocket) {
htmx.createWebSocket = createWebSocket;
}
// Default setting for reconnect delay
if (!htmx.config.wsReconnectDelay) {
htmx.config.wsReconnectDelay = "full-jitter";
}
},
/**
* onEvent handles all events passed to this extension.
*
* @param {string} name
* @param {Event} evt
*/
onEvent: function (name, evt) {
switch (name) {
// Try to close the socket when elements are removed
case "htmx:beforeCleanupElement":
var internalData = api.getInternalData(evt.target)
if (internalData.webSocket) {
internalData.webSocket.close();
}
return;
// Try to create websockets when elements are processed
case "htmx:beforeProcessNode":
var parent = evt.target;
forEach(queryAttributeOnThisOrChildren(parent, "ws-connect"), function (child) {
ensureWebSocket(child)
});
forEach(queryAttributeOnThisOrChildren(parent, "ws-send"), function (child) {
ensureWebSocketSend(child)
});
}
}
});
function splitOnWhitespace(trigger) {
return trigger.trim().split(/\s+/);
}
function getLegacyWebsocketURL(elt) {
var legacySSEValue = api.getAttributeValue(elt, "hx-ws");
if (legacySSEValue) {
var values = splitOnWhitespace(legacySSEValue);
for (var i = 0; i < values.length; i++) {
var value = values[i].split(/:(.+)/);
if (value[0] === "connect") {
return value[1];
}
}
}
}
/**
* ensureWebSocket creates a new WebSocket on the designated element, using
* the element's "ws-connect" attribute.
* @param {HTMLElement} socketElt
* @returns
*/
function ensureWebSocket(socketElt) {
// If the element containing the WebSocket connection no longer exists, then
// do not connect/reconnect the WebSocket.
if (!api.bodyContains(socketElt)) {
return;
}
// Get the source straight from the element's value
var wssSource = api.getAttributeValue(socketElt, "ws-connect")
if (wssSource == null || wssSource === "") {
var legacySource = getLegacyWebsocketURL(socketElt);
if (legacySource == null) {
return;
} else {
wssSource = legacySource;
}
}
// Guarantee that the wssSource value is a fully qualified URL
if (wssSource.indexOf("/") === 0) {
var base_part = location.hostname + (location.port ? ':' + location.port : '');
if (location.protocol === 'https:') {
wssSource = "wss://" + base_part + wssSource;
} else if (location.protocol === 'http:') {
wssSource = "ws://" + base_part + wssSource;
}
}
var socketWrapper = createWebsocketWrapper(socketElt, function () {
return htmx.createWebSocket(wssSource)
});
socketWrapper.addEventListener('message', function (event) {
if (maybeCloseWebSocketSource(socketElt)) {
return;
}
var response = event.data;
if (!api.triggerEvent(socketElt, "htmx:wsBeforeMessage", {
message: response,
socketWrapper: socketWrapper.publicInterface
})) {
return;
}
api.withExtensions(socketElt, function (extension) {
response = extension.transformResponse(response, null, socketElt);
});
var settleInfo = api.makeSettleInfo(socketElt);
var fragment = api.makeFragment(response);
if (fragment.children.length) {
var children = Array.from(fragment.children);
for (var i = 0; i < children.length; i++) {
api.oobSwap(api.getAttributeValue(children[i], "hx-swap-oob") || "true", children[i], settleInfo);
}
}
api.settleImmediately(settleInfo.tasks);
api.triggerEvent(socketElt, "htmx:wsAfterMessage", { message: response, socketWrapper: socketWrapper.publicInterface })
});
// Put the WebSocket into the HTML Element's custom data.
api.getInternalData(socketElt).webSocket = socketWrapper;
}
/**
* @typedef {Object} WebSocketWrapper
* @property {WebSocket} socket
* @property {Array<{message: string, sendElt: Element}>} messageQueue
* @property {number} retryCount
* @property {(message: string, sendElt: Element) => void} sendImmediately sendImmediately sends message regardless of websocket connection state
* @property {(message: string, sendElt: Element) => void} send
* @property {(event: string, handler: Function) => void} addEventListener
* @property {() => void} handleQueuedMessages
* @property {() => void} init
* @property {() => void} close
*/
/**
*
* @param socketElt
* @param socketFunc
* @returns {WebSocketWrapper}
*/
function createWebsocketWrapper(socketElt, socketFunc) {
var wrapper = {
socket: null,
messageQueue: [],
retryCount: 0,
/** @type {Object<string, Function[]>} */
events: {},
addEventListener: function (event, handler) {
if (this.socket) {
this.socket.addEventListener(event, handler);
}
if (!this.events[event]) {
this.events[event] = [];
}
this.events[event].push(handler);
},
sendImmediately: function (message, sendElt) {
if (!this.socket) {
api.triggerErrorEvent()
}
if (sendElt && api.triggerEvent(sendElt, 'htmx:wsBeforeSend', {
message: message,
socketWrapper: this.publicInterface
})) {
this.socket.send(message);
sendElt && api.triggerEvent(sendElt, 'htmx:wsAfterSend', {
message: message,
socketWrapper: this.publicInterface
})
}
},
send: function (message, sendElt) {
if (this.socket.readyState !== this.socket.OPEN) {
this.messageQueue.push({ message: message, sendElt: sendElt });
} else {
this.sendImmediately(message, sendElt);
}
},
handleQueuedMessages: function () {
while (this.messageQueue.length > 0) {
var queuedItem = this.messageQueue[0]
if (this.socket.readyState === this.socket.OPEN) {
this.sendImmediately(queuedItem.message, queuedItem.sendElt);
this.messageQueue.shift();
} else {
break;
}
}
},
init: function () {
if (this.socket && this.socket.readyState === this.socket.OPEN) {
// Close discarded socket
this.socket.close()
}
// Create a new WebSocket and event handlers
/** @type {WebSocket} */
var socket = socketFunc();
// The event.type detail is added for interface conformance with the
// other two lifecycle events (open and close) so a single handler method
// can handle them polymorphically, if required.
api.triggerEvent(socketElt, "htmx:wsConnecting", { event: { type: 'connecting' } });
this.socket = socket;
socket.onopen = function (e) {
wrapper.retryCount = 0;
api.triggerEvent(socketElt, "htmx:wsOpen", { event: e, socketWrapper: wrapper.publicInterface });
wrapper.handleQueuedMessages();
}
socket.onclose = function (e) {
// If socket should not be connected, stop further attempts to establish connection
// If Abnormal Closure/Service Restart/Try Again Later, then set a timer to reconnect after a pause.
if (!maybeCloseWebSocketSource(socketElt) && [1006, 1012, 1013].indexOf(e.code) >= 0) {
var delay = getWebSocketReconnectDelay(wrapper.retryCount);
setTimeout(function () {
wrapper.retryCount += 1;
wrapper.init();
}, delay);
}
// Notify client code that connection has been closed. Client code can inspect `event` field
// to determine whether closure has been valid or abnormal
api.triggerEvent(socketElt, "htmx:wsClose", { event: e, socketWrapper: wrapper.publicInterface })
};
socket.onerror = function (e) {
api.triggerErrorEvent(socketElt, "htmx:wsError", { error: e, socketWrapper: wrapper });
maybeCloseWebSocketSource(socketElt);
};
var events = this.events;
Object.keys(events).forEach(function (k) {
events[k].forEach(function (e) {
socket.addEventListener(k, e);
})
});
},
close: function () {
this.socket.close()
}
}
wrapper.init();
wrapper.publicInterface = {
send: wrapper.send.bind(wrapper),
sendImmediately: wrapper.sendImmediately.bind(wrapper),
queue: wrapper.messageQueue
};
return wrapper;
}
/**
* ensureWebSocketSend attaches trigger handles to elements with
* "ws-send" attribute
* @param {HTMLElement} elt
*/
function ensureWebSocketSend(elt) {
var legacyAttribute = api.getAttributeValue(elt, "hx-ws");
if (legacyAttribute && legacyAttribute !== 'send') {
return;
}
var webSocketParent = api.getClosestMatch(elt, hasWebSocket)
processWebSocketSend(webSocketParent, elt);
}
/**
* hasWebSocket function checks if a node has webSocket instance attached
* @param {HTMLElement} node
* @returns {boolean}
*/
function hasWebSocket(node) {
return api.getInternalData(node).webSocket != null;
}
/**
* processWebSocketSend adds event listeners to the <form> element so that
* messages can be sent to the WebSocket server when the form is submitted.
* @param {HTMLElement} socketElt
* @param {HTMLElement} sendElt
*/
function processWebSocketSend(socketElt, sendElt) {
var nodeData = api.getInternalData(sendElt);
var triggerSpecs = api.getTriggerSpecs(sendElt);
triggerSpecs.forEach(function (ts) {
api.addTriggerHandler(sendElt, ts, nodeData, function (elt, evt) {
if (maybeCloseWebSocketSource(socketElt)) {
return;
}
/** @type {WebSocketWrapper} */
var socketWrapper = api.getInternalData(socketElt).webSocket;
var headers = api.getHeaders(sendElt, api.getTarget(sendElt));
var results = api.getInputValues(sendElt, 'post');
var errors = results.errors;
var rawParameters = results.values;
var expressionVars = api.getExpressionVars(sendElt);
var allParameters = api.mergeObjects(rawParameters, expressionVars);
var filteredParameters = api.filterValues(allParameters, sendElt);
var sendConfig = {
parameters: filteredParameters,
unfilteredParameters: allParameters,
headers: headers,
errors: errors,
triggeringEvent: evt,
messageBody: undefined,
socketWrapper: socketWrapper.publicInterface
};
if (!api.triggerEvent(elt, 'htmx:wsConfigSend', sendConfig)) {
return;
}
if (errors && errors.length > 0) {
api.triggerEvent(elt, 'htmx:validation:halted', errors);
return;
}
var body = sendConfig.messageBody;
if (body === undefined) {
var toSend = Object.assign({}, sendConfig.parameters);
if (sendConfig.headers)
toSend['HEADERS'] = headers;
body = JSON.stringify(toSend);
}
socketWrapper.send(body, elt);
if (evt && api.shouldCancel(evt, elt)) {
evt.preventDefault();
}
});
});
}
/**
* getWebSocketReconnectDelay is the default easing function for WebSocket reconnects.
* @param {number} retryCount // The number of retries that have already taken place
* @returns {number}
*/
function getWebSocketReconnectDelay(retryCount) {
/** @type {"full-jitter" | ((retryCount:number) => number)} */
var delay = htmx.config.wsReconnectDelay;
if (typeof delay === 'function') {
return delay(retryCount);
}
if (delay === 'full-jitter') {
var exp = Math.min(retryCount, 6);
var maxDelay = 1000 * Math.pow(2, exp);
return maxDelay * Math.random();
}
logError('htmx.config.wsReconnectDelay must either be a function or the string "full-jitter"');
}
/**
* maybeCloseWebSocketSource checks to the if the element that created the WebSocket
* still exists in the DOM. If NOT, then the WebSocket is closed and this function
* returns TRUE. If the element DOES EXIST, then no action is taken, and this function
* returns FALSE.
*
* @param {*} elt
* @returns
*/
function maybeCloseWebSocketSource(elt) {
if (!api.bodyContains(elt)) {
api.getInternalData(elt).webSocket.close();
return true;
}
return false;
}
/**
* createWebSocket is the default method for creating new WebSocket objects.
* it is hoisted into htmx.createWebSocket to be overridden by the user, if needed.
*
* @param {string} url
* @returns WebSocket
*/
function createWebSocket(url) {
var sock = new WebSocket(url, []);
sock.binaryType = htmx.config.wsBinaryType;
return sock;
}
/**
* queryAttributeOnThisOrChildren returns all nodes that contain the requested attributeName, INCLUDING THE PROVIDED ROOT ELEMENT.
*
* @param {HTMLElement} elt
* @param {string} attributeName
*/
function queryAttributeOnThisOrChildren(elt, attributeName) {
var result = []
// If the parent element also contains the requested attribute, then add it to the results too.
if (api.hasAttribute(elt, attributeName) || api.hasAttribute(elt, "hx-ws")) {
result.push(elt);
}
// Search all child nodes that match the requested attribute
elt.querySelectorAll("[" + attributeName + "], [data-" + attributeName + "], [data-hx-ws], [hx-ws]").forEach(function (node) {
result.push(node)
})
return result
}
/**
* @template T
* @param {T[]} arr
* @param {(T) => void} func
*/
function forEach(arr, func) {
if (arr) {
for (var i = 0; i < arr.length; i++) {
func(arr[i]);
}
}
}
})();

399
themes/lean/static/js/htmx/htmx.d.ts vendored Normal file
View file

@ -0,0 +1,399 @@
// https://htmx.org/reference/#api
/**
* This method adds a class to the given element.
*
* https://htmx.org/api/#addClass
*
* @param elt the element to add the class to
* @param clazz the class to add
* @param delay the delay (in milliseconds before class is added)
*/
export function addClass(elt: Element, clazz: string, delay?: number): void;
/**
* Issues an htmx-style AJAX request
*
* https://htmx.org/api/#ajax
*
* @param verb 'GET', 'POST', etc.
* @param path the URL path to make the AJAX
* @param element the element to target (defaults to the **body**)
* @returns Promise that resolves immediately if no request is sent, or when the request is complete
*/
export function ajax(verb: string, path: string, element: Element): Promise<void>;
/**
* Issues an htmx-style AJAX request
*
* https://htmx.org/api/#ajax
*
* @param verb 'GET', 'POST', etc.
* @param path the URL path to make the AJAX
* @param selector a selector for the target
* @returns Promise that resolves immediately if no request is sent, or when the request is complete
*/
export function ajax(verb: string, path: string, selector: string): Promise<void>;
/**
* Issues an htmx-style AJAX request
*
* https://htmx.org/api/#ajax
*
* @param verb 'GET', 'POST', etc.
* @param path the URL path to make the AJAX
* @param context a context object that contains any of the following
* @returns Promise that resolves immediately if no request is sent, or when the request is complete
*/
export function ajax(
verb: string,
path: string,
context: Partial<{ source: any; event: any; handler: any; target: any; swap: any; values: any; headers: any }>
): Promise<void>;
/**
* Finds the closest matching element in the given elements parentage, inclusive of the element
*
* https://htmx.org/api/#closest
*
* @param elt the element to find the selector from
* @param selector the selector to find
*/
export function closest(elt: Element, selector: string): Element | null;
/**
* A property holding the configuration htmx uses at runtime.
*
* Note that using a [meta tag](https://htmx.org/docs/#config) is the preferred mechanism for setting these properties.
*
* https://htmx.org/api/#config
*/
export var config: HtmxConfig;
/**
* A property used to create new [Server Sent Event](https://htmx.org/docs/#sse) sources. This can be updated to provide custom SSE setup.
*
* https://htmx.org/api/#createEventSource
*/
export var createEventSource: (url: string) => EventSource;
/**
* A property used to create new [WebSocket](https://htmx.org/docs/#websockets). This can be updated to provide custom WebSocket setup.
*
* https://htmx.org/api/#createWebSocket
*/
export var createWebSocket: (url: string) => WebSocket;
/**
* Defines a new htmx [extension](https://htmx.org/extensions).
*
* https://htmx.org/api/#defineExtension
*
* @param name the extension name
* @param ext the extension definition
*/
export function defineExtension(name: string, ext: HtmxExtension): void;
/**
* Finds an element matching the selector
*
* https://htmx.org/api/#find
*
* @param selector the selector to match
*/
export function find(selector: string): Element | null;
/**
* Finds an element matching the selector
*
* https://htmx.org/api/#find
*
* @param elt the root element to find the matching element in, inclusive
* @param selector the selector to match
*/
export function find(elt: Element, selector: string): Element | null;
/**
* Finds all elements matching the selector
*
* https://htmx.org/api/#findAll
*
* @param selector the selector to match
*/
export function findAll(selector: string): NodeListOf<Element>;
/**
* Finds all elements matching the selector
*
* https://htmx.org/api/#findAll
*
* @param elt the root element to find the matching elements in, inclusive
* @param selector the selector to match
*/
export function findAll(elt: Element, selector: string): NodeListOf<Element>;
/**
* Log all htmx events, useful for debugging.
*
* https://htmx.org/api/#logAll
*/
export function logAll(): void;
/**
* The logger htmx uses to log with
*
* https://htmx.org/api/#logger
*/
export var logger: (elt: Element, eventName: string, detail: any) => void | null;
/**
* Removes an event listener from an element
*
* https://htmx.org/api/#off
*
* @param eventName the event name to remove the listener from
* @param listener the listener to remove
*/
export function off(eventName: string, listener: (evt: Event) => void): (evt: Event) => void;
/**
* Removes an event listener from an element
*
* https://htmx.org/api/#off
*
* @param target the element to remove the listener from
* @param eventName the event name to remove the listener from
* @param listener the listener to remove
*/
export function off(target: string, eventName: string, listener: (evt: Event) => void): (evt: Event) => void;
/**
* Adds an event listener to an element
*
* https://htmx.org/api/#on
*
* @param eventName the event name to add the listener for
* @param listener the listener to add
*/
export function on(eventName: string, listener: (evt: Event) => void): (evt: Event) => void;
/**
* Adds an event listener to an element
*
* https://htmx.org/api/#on
*
* @param target the element to add the listener to
* @param eventName the event name to add the listener for
* @param listener the listener to add
*/
export function on(target: string, eventName: string, listener: (evt: Event) => void): (evt: Event) => void;
/**
* Adds a callback for the **htmx:load** event. This can be used to process new content, for example initializing the content with a javascript library
*
* https://htmx.org/api/#onLoad
*
* @param callback the callback to call on newly loaded content
*/
export function onLoad(callback: (element: Element) => void): void;
/**
* Parses an interval string consistent with the way htmx does. Useful for plugins that have timing-related attributes.
*
* Caution: Accepts an int followed by either **s** or **ms**. All other values use **parseFloat**
*
* https://htmx.org/api/#parseInterval
*
* @param str timing string
*/
export function parseInterval(str: string): number;
/**
* Processes new content, enabling htmx behavior. This can be useful if you have content that is added to the DOM outside of the normal htmx request cycle but still want htmx attributes to work.
*
* https://htmx.org/api/#process
*
* @param element element to process
*/
export function process(element: Element): void;
/**
* Removes an element from the DOM
*
* https://htmx.org/api/#remove
*
* @param elt element to remove
* @param delay the delay (in milliseconds before element is removed)
*/
export function remove(elt: Element, delay?: number): void;
/**
* Removes a class from the given element
*
* https://htmx.org/api/#removeClass
*
* @param elt element to remove the class from
* @param clazz the class to remove
* @param delay the delay (in milliseconds before class is removed)
*/
export function removeClass(elt: Element, clazz: string, delay?: number): void;
/**
* Removes the given extension from htmx
*
* https://htmx.org/api/#removeExtension
*
* @param name the name of the extension to remove
*/
export function removeExtension(name: string): void;
/**
* Takes the given class from its siblings, so that among its siblings, only the given element will have the class.
*
* https://htmx.org/api/#takeClass
*
* @param elt the element that will take the class
* @param clazz the class to take
*/
export function takeClass(elt: Element, clazz: string): void;
/**
* Toggles the given class on an element
*
* https://htmx.org/api/#toggleClass
*
* @param elt the element to toggle the class on
* @param clazz the class to toggle
*/
export function toggleClass(elt: Element, clazz: string): void;
/**
* Triggers a given event on an element
*
* https://htmx.org/api/#trigger
*
* @param elt the element to trigger the event on
* @param name the name of the event to trigger
* @param detail details for the event
*/
export function trigger(elt: Element, name: string, detail: any): void;
/**
* Returns the input values that would resolve for a given element via the htmx value resolution mechanism
*
* https://htmx.org/api/#values
*
* @param elt the element to resolve values on
* @param requestType the request type (e.g. **get** or **post**) non-GET's will include the enclosing form of the element. Defaults to **post**
*/
export function values(elt: Element, requestType?: string): any;
export const version: string;
export interface HtmxConfig {
/**
* The attributes to settle during the settling phase.
* @default ["class", "style", "width", "height"]
*/
attributesToSettle?: ["class", "style", "width", "height"] | string[];
/**
* If the focused element should be scrolled into view.
* @default false
*/
defaultFocusScroll?: boolean;
/**
* The default delay between completing the content swap and settling attributes.
* @default 20
*/
defaultSettleDelay?: number;
/**
* The default delay between receiving a response from the server and doing the swap.
* @default 0
*/
defaultSwapDelay?: number;
/**
* The default swap style to use if **[hx-swap](https://htmx.org/attributes/hx-swap)** is omitted.
* @default "innerHTML"
*/
defaultSwapStyle?: "innerHTML" | string;
/**
* The number of pages to keep in **localStorage** for history support.
* @default 10
*/
historyCacheSize?: number;
/**
* Whether or not to use history.
* @default true
*/
historyEnabled?: boolean;
/**
* If true, htmx will inject a small amount of CSS into the page to make indicators invisible unless the **htmx-indicator** class is present.
* @default true
*/
includeIndicatorStyles?: boolean;
/**
* The class to place on indicators when a request is in flight.
* @default "htmx-indicator"
*/
indicatorClass?: "htmx-indicator" | string;
/**
* The class to place on triggering elements when a request is in flight.
* @default "htmx-request"
*/
requestClass?: "htmx-request" | string;
/**
* The class to temporarily place on elements that htmx has added to the DOM.
* @default "htmx-added"
*/
addedClass?: "htmx-added" | string;
/**
* The class to place on target elements when htmx is in the settling phase.
* @default "htmx-settling"
*/
settlingClass?: "htmx-settling" | string;
/**
* The class to place on target elements when htmx is in the swapping phase.
* @default "htmx-swapping"
*/
swappingClass?: "htmx-swapping" | string;
/**
* Allows the use of eval-like functionality in htmx, to enable **hx-vars**, trigger conditions & script tag evaluation. Can be set to **false** for CSP compatibility.
* @default true
*/
allowEval?: boolean;
/**
* Use HTML template tags for parsing content from the server. This allows you to use Out of Band content when returning things like table rows, but it is *not* IE11 compatible.
* @default false
*/
useTemplateFragments?: boolean;
/**
* Allow cross-site Access-Control requests using credentials such as cookies, authorization headers or TLS client certificates.
* @default false
*/
withCredentials?: boolean;
/**
* The default implementation of **getWebSocketReconnectDelay** for reconnecting after unexpected connection loss by the event code **Abnormal Closure**, **Service Restart** or **Try Again Later**.
* @default "full-jitter"
*/
wsReconnectDelay?: "full-jitter" | string | ((retryCount: number) => number);
// following don't appear in the docs
/** @default false */
refreshOnHistoryMiss?: boolean;
/** @default 0 */
timeout?: number;
/** @default "[hx-disable], [data-hx-disable]" */
disableSelector?: "[hx-disable], [data-hx-disable]" | string;
/** @default "smooth" */
scrollBehavior?: "smooth" | "auto";
}
/**
* https://htmx.org/extensions/#defining
*/
export interface HtmxExtension {
onEvent?: (name: string, evt: CustomEvent) => any;
transformResponse?: (text: any, xhr: XMLHttpRequest, elt: any) => any;
isInlineSwap?: (swapStyle: any) => any;
handleSwap?: (swapStyle: any, target: any, fragment: any, settleInfo: any) => any;
encodeParameters?: (xhr: XMLHttpRequest, parameters: any, elt: any) => any;
}

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

Binary file not shown.

View file

@ -0,0 +1,96 @@
import htmx from "./htmx";
// add the class 'myClass' to the element with the id 'demo'
htmx.addClass(htmx.find("#demo"), "myClass");
// issue a GET to /example and put the response HTML into #myDiv
htmx.ajax("GET", "/example", "#myDiv");
// find the closest enclosing div of the element with the id 'demo'
htmx.closest(htmx.find("#demo"), "div");
// update the history cache size to 30
htmx.config.historyCacheSize = 30;
// override SSE event sources to not use credentials
htmx.createEventSource = function (url) {
return new EventSource(url, { withCredentials: false });
};
// override WebSocket to use a specific protocol
htmx.createWebSocket = function (url) {
return new WebSocket(url, ["wss"]);
};
// defines a silly extension that just logs the name of all events triggered
htmx.defineExtension("silly", {
onEvent: function (name, evt) {
console.log("Event " + name + " was triggered!");
}
});
// find div with id my-div
var div = htmx.find("#my-div");
// find div with id another-div within that div
var anotherDiv = htmx.find(div, "#another-div");
// find all divs
var allDivs = htmx.findAll("div");
// find all paragraphs within a given div
var allParagraphsInMyDiv = htmx.findAll(htmx.find("#my-div"), "p");
htmx.logAll();
// remove this click listener from the body
htmx.off("click", myEventListener);
// remove this click listener from the given div
htmx.off("#my-div", "click", myEventListener);
// add a click listener to the body
var myEventListener = htmx.on("click", function (evt) {
console.log(evt);
});
// add a click listener to the given div
var myEventListener = htmx.on("#my-div", "click", function (evt) {
console.log(evt);
});
const MyLibrary: any = null;
htmx.onLoad(function (elt) {
MyLibrary.init(elt);
});
// returns 3000
var milliseconds = htmx.parseInterval("3s");
// returns 3 - Caution
var milliseconds = htmx.parseInterval("3m");
document.body.innerHTML = "<div hx-get='/example'>Get it!</div>";
// process the newly added content
htmx.process(document.body);
// removes my-div from the DOM
htmx.remove(htmx.find("#my-div"));
// removes .myClass from my-div
htmx.removeClass(htmx.find("#my-div"), "myClass");
htmx.removeExtension("my-extension");
// takes the selected class from tab2"s siblings
htmx.takeClass(htmx.find("#tab2"), "selected");
// toggles the selected class on tab2
htmx.toggleClass(htmx.find("#tab2"), "selected");
// triggers the myEvent event on #tab2 with the answer 42
htmx.trigger(htmx.find("#tab2"), "myEvent", { answer: 42 });
// gets the values associated with this form
var values = htmx.values(htmx.find("#myForm"));

View file

@ -0,0 +1,6 @@
document.addEventListener("DOMContentLoaded", function(event) {
let menuItems = document.querySelectorAll("#topmenu a");
menuItems.forEach( menuItem => {
menuItem.addEventListener('click', () => htmx.removeClass(htmx.find('#topmenu'), 'responsive'));
});
});