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 detauls
|
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 | // config gets the closest non-empty value from the preload="" attribute. (default = "mousedown")
|
17 | var config = function(node) {
|
18 | if (node == undefined) {return undefined;}
|
19 | return node.getAttribute("preload") || node.getAttribute("data-preload") || config(node.parentElement) || "mousedown"
|
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 | return function() {
|
27 |
|
28 | // If this value has already been loaded, then do not try again.
|
29 | if (node.preloadState !== "READY") {
|
30 | return;
|
31 | }
|
32 |
|
33 | // This is used after a successful AJAX request, to mark the
|
34 | // content as loaded (and prevent additional AJAX calls.)
|
35 | var done = function() {
|
36 | node.preloadState = "DONE"
|
37 | }
|
38 |
|
39 | // Special handling for HX-GET - use built-in htmx.ajax function
|
40 | // so that headers match other htmx requests, then set
|
41 | // node.preloadState = TRUE so that requests are not duplicated
|
42 | // in the future
|
43 | if (node.getAttribute("hx-get")) {
|
44 | htmx.ajax("GET", node.getAttribute("hx-get"), {handler:done});
|
45 | return;
|
46 | }
|
47 |
|
48 | // Otherwise, perform a standard xhr request, then set
|
49 | // node.preloadState = TRUE so that requests are not duplicated
|
50 | // in the future.
|
51 | if (node.getAttribute("href")) {
|
52 | var r = new XMLHttpRequest();
|
53 | r.open("GET", node.getAttribute("href"));
|
54 | r.onload = done;
|
55 | r.send();
|
56 | return;
|
57 | }
|
58 | }
|
59 | }
|
60 |
|
61 | // This function processes a specific node and sets up event handlers.
|
62 | // We'll search for nodes and use it below.
|
63 | var init = function(node) {
|
64 |
|
65 | // If this node DOES NOT include a "GET" transaction, then there's nothing to do here.
|
66 | if (node.getAttribute("href") + node.getAttribute("hx-get") + node.getAttribute("data-hx-get") == "") {
|
67 | return;
|
68 | }
|
69 |
|
70 | // Guarantee that we only initialize each node once.
|
71 | if (node.preloadState !== undefined) {
|
72 | return;
|
73 | }
|
74 |
|
75 | // Get event name from config.
|
76 | var on = config(node)
|
77 |
|
78 | // FALL THROUGH to here means we need to add an EventListener
|
79 |
|
80 | // Apply the listener to the node
|
81 | node.addEventListener(on, function(evt) {
|
82 | if (node.preloadState === "PAUSE") { // Only add one event listener
|
83 | node.preloadState = "READY"; // Requred for the `load` function to trigger
|
84 |
|
85 | // Special handling for "mouseover" events. Wait 100ms before triggering load.
|
86 | if (on === "mouseover") {
|
87 | window.setTimeout(load(node), 100);
|
88 | } else {
|
89 | load(node)() // all other events trigger immediately.
|
90 | }
|
91 | }
|
92 | })
|
93 |
|
94 | // Special handling for certain built-in event handlers
|
95 | switch (on) {
|
96 |
|
97 | case "mouseover":
|
98 | // Mirror `touchstart` events (fires immediately)
|
99 | node.addEventListener("touchstart", load(node));
|
100 |
|
101 | // WHhen the mouse leaves, immediately disable the preload
|
102 | node.addEventListener("mouseout", function(evt) {
|
103 | if ((evt.target === node) && (node.preloadState === "READY")) {
|
104 | node.preloadState = "PAUSE";
|
105 | }
|
106 | })
|
107 | break;
|
108 |
|
109 | case "mousedown":
|
110 | // Mirror `touchstart` events (fires immediately)
|
111 | node.addEventListener("touchstart", load(node));
|
112 | break;
|
113 | }
|
114 |
|
115 | // Mark the node as ready to run.
|
116 | node.preloadState = "PAUSE";
|
117 | htmx.trigger(node, "preload:init") // This event can be used to load content immediately.
|
118 | }
|
119 |
|
120 | // Search for all child nodes that have a "preload" attribute
|
121 | event.target.querySelectorAll("[preload]").forEach(function(node) {
|
122 |
|
123 | // Initialize the node with the "preload" attribute
|
124 | init(node)
|
125 |
|
126 | // Initialize all child elements that are anchors or have `hx-get` (use with care)
|
127 | node.querySelectorAll("a,[hx-get],[data-hx-get").forEach(init)
|
128 | })
|
129 | }
|
130 | })
|