Browse code

Initial htmx npm packages installation

Benjamin Roth authored on25/05/2023 09:52:13
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,60 @@
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
+        if (pathSpec === "ignore") {
9
+            return false;
10
+        }
11
+        var dependencyPath = pathSpec.split("/");
12
+        var urlPath = url.split("/");
13
+        for (var i = 0; i < urlPath.length; i++) {
14
+            var dependencyElement = dependencyPath.shift();
15
+            var pathElement = urlPath[i];
16
+            if (dependencyElement !== pathElement && dependencyElement !== "*") {
17
+                return false;
18
+            }
19
+            if (dependencyPath.length === 0 || (dependencyPath.length === 1 && dependencyPath[0] === "")) {
20
+                return true;
21
+            }
22
+        }
23
+        return false;
24
+    }
25
+
26
+    function refreshPath(path) {
27
+        var eltsWithDeps = htmx.findAll("[path-deps]");
28
+        for (var i = 0; i < eltsWithDeps.length; i++) {
29
+            var elt = eltsWithDeps[i];
30
+            if (dependsOn(elt.getAttribute('path-deps'), path)) {
31
+                htmx.trigger(elt, "path-deps");
32
+            }
33
+        }      
34
+    }    
35
+
36
+    htmx.defineExtension('path-deps', {
37
+        onEvent: function (name, evt) {
38
+            if (name === "htmx:beforeOnLoad") {
39
+                var config = evt.detail.requestConfig;
40
+                // mutating call
41
+                if (config.verb !== "get" && evt.target.getAttribute('path-deps') !== 'ignore') {
42
+                    refreshPath(config.path);
43
+                }
44
+            } 
45
+        }
46
+    });
47
+
48
+    /**
49
+     *  ********************
50
+     *  Expose functionality
51
+     *  ********************
52
+     */    
53
+
54
+    _root.PathDeps = {
55
+        refresh: function(path) {
56
+            refreshPath(path);
57
+        }
58
+    };
59
+            
60
+}).call(this);