1 | // This adds the "preload" extension to htmx. By default, this will
|
2 | // preload the targets of any tags with `href` or `hx-get` attributes
|
3 | // if they also have a `preload` attribute as well. See documentation
|
4 | // for more details
|
5 | htmx.defineExtension("preload", {
|
6 |
|
7 | onEvent: function(name, event) {
|
8 |
|
9 | // Only take actions on "htmx:afterProcessNode"
|
10 | if (name !== "htmx:afterProcessNode") {
|
11 | return;
|
12 | }
|
13 |
|
14 | // SOME HELPER FUNCTIONS WE'LL NEED ALONG THE WAY
|
15 |
|
16 | // attr gets the closest non-empty value from the attribute.
|
17 | var attr = function(node, property) {
|
18 | if (node == undefined) {return undefined;}
|
19 | return node.getAttribute(property) || node.getAttribute("data-" + property) || attr(node.parentElement, property)
|
20 | }
|
21 |
|
22 | // load handles the actual HTTP fetch, and uses htmx.ajax in cases where we're
|
23 | // preloading an htmx resource (this sends the same HTTP headers as a regular htmx request)
|
24 | var load = function(node) {
|
25 |
|
26 | // Called after a successful AJAX request, to mark the
|
27 | // content as loaded (and prevent additional AJAX calls.)
|
28 | var done = function(html) {
|
29 | if (!node.preloadAlways) {
|
30 | node.preloadState = "DONE"
|
31 | }
|
32 |
|
33 | if (attr(node, "preload-images") == "true") {
|
34 | document.createElement("div").innerHTML = html // create and populate a node to load linked resources, too.
|
35 | }
|
36 | }
|
37 |
|
38 | return function() {
|
39 |
|
40 | // If this value has already been loaded, then do not try again.
|
41 | if (node.preloadState !== "READY") {
|
42 | return;
|
43 | }
|
44 |
|
45 | // Special handling for HX-GET - use built-in htmx.ajax function
|
46 | // so that headers match other htmx requests, then set
|
47 | // node.preloadState = TRUE so that requests are not duplicated
|
48 | // in the future
|
49 | var hxGet = node.getAttribute("hx-get") || node.getAttribute("data-hx-get")
|
50 | if (hxGet) {
|
51 | htmx.ajax("GET", hxGet, {
|
52 | source: node,
|
53 | handler:function(elt, info) {
|
54 | done(info.xhr.responseText);
|
55 | }
|
56 | });
|
57 | return;
|
58 | }
|
59 |
|
60 | // Otherwise, perform a standard xhr request, then set
|
61 | // node.preloadState = TRUE so that requests are not duplicated
|
62 | // in the future.
|
63 | if (node.getAttribute("href")) {
|
64 | var r = new XMLHttpRequest();
|
65 | r.open("GET", node.getAttribute("href"));
|
66 | r.onload = function() {done(r.responseText);};
|
67 | r.send();
|
68 | return;
|
69 | }
|
70 | }
|
71 | }
|
72 |
|
73 | // This function processes a specific node and sets up event handlers.
|
74 | // We'll search for nodes and use it below.
|
75 | var init = function(node) {
|
76 |
|
77 | // If this node DOES NOT include a "GET" transaction, then there's nothing to do here.
|
78 | if (node.getAttribute("href") + node.getAttribute("hx-get") + node.getAttribute("data-hx-get") == "") {
|
79 | return;
|
80 | }
|
81 |
|
82 | // Guarantee that we only initialize each node once.
|
83 | if (node.preloadState !== undefined) {
|
84 | return;
|
85 | }
|
86 |
|
87 | // Get event name from config.
|
88 | var on = attr(node, "preload") || "mousedown"
|
89 | const always = on.indexOf("always") !== -1
|
90 | if (always) {
|
91 | on = on.replace('always', '').trim()
|
92 | }
|
93 |
|
94 | // FALL THROUGH to here means we need to add an EventListener
|
95 |
|
96 | // Apply the listener to the node
|
97 | node.addEventListener(on, function(evt) {
|
98 | if (node.preloadState === "PAUSE") { // Only add one event listener
|
99 | node.preloadState = "READY"; // Required for the `load` function to trigger
|
100 |
|
101 | // Special handling for "mouseover" events. Wait 100ms before triggering load.
|
102 | if (on === "mouseover") {
|
103 | window.setTimeout(load(node), 100);
|
104 | } else {
|
105 | load(node)() // all other events trigger immediately.
|
106 | }
|
107 | }
|
108 | })
|
109 |
|
110 | // Special handling for certain built-in event handlers
|
111 | switch (on) {
|
112 |
|
113 | case "mouseover":
|
114 | // Mirror `touchstart` events (fires immediately)
|
115 | node.addEventListener("touchstart", load(node));
|
116 |
|
117 | // WHhen the mouse leaves, immediately disable the preload
|
118 | node.addEventListener("mouseout", function(evt) {
|
119 | if ((evt.target === node) && (node.preloadState === "READY")) {
|
120 | node.preloadState = "PAUSE";
|
121 | }
|
122 | })
|
123 | break;
|
124 |
|
125 | case "mousedown":
|
126 | // Mirror `touchstart` events (fires immediately)
|
127 | node.addEventListener("touchstart", load(node));
|
128 | break;
|
129 | }
|
130 |
|
131 | // Mark the node as ready to run.
|
132 | node.preloadState = "PAUSE";
|
133 | node.preloadAlways = always;
|
134 | htmx.trigger(node, "preload:init") // This event can be used to load content immediately.
|
135 | }
|
136 |
|
137 | // Search for all child nodes that have a "preload" attribute
|
138 | event.target.querySelectorAll("[preload]").forEach(function(node) {
|
139 |
|
140 | // Initialize the node with the "preload" attribute
|
141 | init(node)
|
142 |
|
143 | // Initialize all child elements that are anchors or have `hx-get` (use with care)
|
144 | node.querySelectorAll("a,[hx-get],[data-hx-get]").forEach(init)
|
145 | })
|
146 | }
|
147 | })
|