UNPKG

1.67 kBJavaScriptView Raw
1(function(undefined){
2 'use strict';
3
4 // Save a reference to the global object (window in the browser)
5 var _root = this;
6
7 function dependsOn(pathSpec, url) {
8 var dependencyPath = pathSpec.split("/");
9 var urlPath = url.split("/");
10 for (var i = 0; i < urlPath.length; i++) {
11 var dependencyElement = dependencyPath.shift();
12 var pathElement = urlPath[i];
13 if (dependencyElement !== pathElement && dependencyElement !== "*") {
14 return false;
15 }
16 if (dependencyPath.length === 0 || (dependencyPath.length === 1 && dependencyPath[0] === "")) {
17 return true;
18 }
19 }
20 return false;
21 }
22
23 function refreshPath(path) {
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'), path)) {
28 htmx.trigger(elt, "path-deps");
29 }
30 }
31 }
32
33 htmx.defineExtension('path-deps', {
34 onEvent: function (name, evt) {
35 if (name === "htmx:beforeOnLoad") {
36 var config = evt.detail.requestConfig;
37 // mutating call
38 if (config.verb !== "get") {
39 refreshPath(config.path);
40 }
41 }
42 }
43 });
44
45 /**
46 * ********************
47 * Expose functionality
48 * ********************
49 */
50
51 _root.PathDeps = {
52 refresh: function(path) {
53 refreshPath(path);
54 }
55 };
56
57}).call(this);