UNPKG

1.29 kBJavaScriptView Raw
1(function(){
2 function dependsOn(pathSpec, url) {
3 var dependencyPath = pathSpec.split("/");
4 var urlPath = url.split("/");
5 for (var i = 0; i < urlPath.length; i++) {
6 var dependencyElement = dependencyPath.shift();
7 var pathElement = urlPath[i];
8 if (dependencyElement !== pathElement && dependencyElement !== "*") {
9 return false;
10 }
11 if (dependencyPath.length === 0 || (dependencyPath.length === 1 && dependencyPath[0] === "")) {
12 return true;
13 }
14 }
15 return false;
16 }
17
18 htmx.defineExtension('path-deps', {
19 onEvent: function (name, evt) {
20 if (name === "afterRequest.htmx") {
21 var xhr = evt.detail.xhr;
22 // mutating call
23 if (xhr.method !== "GET") {
24 var eltsWithDeps = htmx.findAll("[path-deps]");
25 for (var i = 0; i < eltsWithDeps.length; i++) {
26 var elt = eltsWithDeps[i];
27 if (dependsOn(elt.getAttribute('path-deps'), xhr.url)) {
28 htmx.trigger(elt, "path-deps");
29 }
30 }
31 }
32 }
33 }
34 });
35})();