htmx.org 2.0.8 → 4.0.0-alpha1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,20 +0,0 @@
1
- if (htmx.version && !htmx.version.startsWith("1.")) {
2
- console.warn("WARNING: You are using an htmx 1 extension with htmx " + htmx.version +
3
- ". It is recommended that you move to the version of this extension found on https://htmx.org/extensions")
4
- }
5
- // Disable Submit Button
6
- htmx.defineExtension('disable-element', {
7
- onEvent: function (name, evt) {
8
- let elt = evt.detail.elt;
9
- let target = elt.getAttribute("hx-disable-element");
10
- let targetElements = (target == "self") ? [ elt ] : document.querySelectorAll(target);
11
-
12
- for (var i = 0; i < targetElements.length; i++) {
13
- if (name === "htmx:beforeRequest" && targetElements[i]) {
14
- targetElements[i].disabled = true;
15
- } else if (name == "htmx:afterRequest" && targetElements[i]) {
16
- targetElements[i].disabled = false;
17
- }
18
- }
19
- }
20
- });
@@ -1,41 +0,0 @@
1
- (function(){
2
- if (htmx.version && !htmx.version.startsWith("1.")) {
3
- console.warn("WARNING: You are using an htmx 1 extension with htmx " + htmx.version +
4
- ". It is recommended that you move to the version of this extension found on https://htmx.org/extensions")
5
- }
6
- function stringifyEvent(event) {
7
- var obj = {};
8
- for (var key in event) {
9
- obj[key] = event[key];
10
- }
11
- return JSON.stringify(obj, function(key, value){
12
- if(value instanceof Node){
13
- var nodeRep = value.tagName;
14
- if (nodeRep) {
15
- nodeRep = nodeRep.toLowerCase();
16
- if(value.id){
17
- nodeRep += "#" + value.id;
18
- }
19
- if(value.classList && value.classList.length){
20
- nodeRep += "." + value.classList.toString().replace(" ", ".")
21
- }
22
- return nodeRep;
23
- } else {
24
- return "Node"
25
- }
26
- }
27
- if (value instanceof Window) return 'Window';
28
- return value;
29
- });
30
- }
31
-
32
- htmx.defineExtension('event-header', {
33
- onEvent: function (name, evt) {
34
- if (name === "htmx:configRequest") {
35
- if (evt.detail.triggeringEvent) {
36
- evt.detail.headers['Triggering-Event'] = stringifyEvent(evt.detail.triggeringEvent);
37
- }
38
- }
39
- }
40
- });
41
- })();
@@ -1,146 +0,0 @@
1
- //==========================================================
2
- // head-support.js
3
- //
4
- // An extension to htmx 1.0 to add head tag merging.
5
- //==========================================================
6
- (function(){
7
-
8
- if (htmx.version && !htmx.version.startsWith("1.")) {
9
- console.warn("WARNING: You are using an htmx 1 extension with htmx " + htmx.version +
10
- ". It is recommended that you move to the version of this extension found on https://htmx.org/extensions")
11
- }
12
-
13
- var api = null;
14
-
15
- function log() {
16
- //console.log(arguments);
17
- }
18
-
19
- function mergeHead(newContent, defaultMergeStrategy) {
20
-
21
- if (newContent && newContent.indexOf('<head') > -1) {
22
- const htmlDoc = document.createElement("html");
23
- // remove svgs to avoid conflicts
24
- var contentWithSvgsRemoved = newContent.replace(/<svg(\s[^>]*>|>)([\s\S]*?)<\/svg>/gim, '');
25
- // extract head tag
26
- var headTag = contentWithSvgsRemoved.match(/(<head(\s[^>]*>|>)([\s\S]*?)<\/head>)/im);
27
-
28
- // if the head tag exists...
29
- if (headTag) {
30
-
31
- var added = []
32
- var removed = []
33
- var preserved = []
34
- var nodesToAppend = []
35
-
36
- htmlDoc.innerHTML = headTag;
37
- var newHeadTag = htmlDoc.querySelector("head");
38
- var currentHead = document.head;
39
-
40
- if (newHeadTag == null) {
41
- return;
42
- } else {
43
- // put all new head elements into a Map, by their outerHTML
44
- var srcToNewHeadNodes = new Map();
45
- for (const newHeadChild of newHeadTag.children) {
46
- srcToNewHeadNodes.set(newHeadChild.outerHTML, newHeadChild);
47
- }
48
- }
49
-
50
-
51
-
52
- // determine merge strategy
53
- var mergeStrategy = api.getAttributeValue(newHeadTag, "hx-head") || defaultMergeStrategy;
54
-
55
- // get the current head
56
- for (const currentHeadElt of currentHead.children) {
57
-
58
- // If the current head element is in the map
59
- var inNewContent = srcToNewHeadNodes.has(currentHeadElt.outerHTML);
60
- var isReAppended = currentHeadElt.getAttribute("hx-head") === "re-eval";
61
- var isPreserved = api.getAttributeValue(currentHeadElt, "hx-preserve") === "true";
62
- if (inNewContent || isPreserved) {
63
- if (isReAppended) {
64
- // remove the current version and let the new version replace it and re-execute
65
- removed.push(currentHeadElt);
66
- } else {
67
- // this element already exists and should not be re-appended, so remove it from
68
- // the new content map, preserving it in the DOM
69
- srcToNewHeadNodes.delete(currentHeadElt.outerHTML);
70
- preserved.push(currentHeadElt);
71
- }
72
- } else {
73
- if (mergeStrategy === "append") {
74
- // we are appending and this existing element is not new content
75
- // so if and only if it is marked for re-append do we do anything
76
- if (isReAppended) {
77
- removed.push(currentHeadElt);
78
- nodesToAppend.push(currentHeadElt);
79
- }
80
- } else {
81
- // if this is a merge, we remove this content since it is not in the new head
82
- if (api.triggerEvent(document.body, "htmx:removingHeadElement", {headElement: currentHeadElt}) !== false) {
83
- removed.push(currentHeadElt);
84
- }
85
- }
86
- }
87
- }
88
-
89
- // Push the tremaining new head elements in the Map into the
90
- // nodes to append to the head tag
91
- nodesToAppend.push(...srcToNewHeadNodes.values());
92
- log("to append: ", nodesToAppend);
93
-
94
- for (const newNode of nodesToAppend) {
95
- log("adding: ", newNode);
96
- var newElt = document.createRange().createContextualFragment(newNode.outerHTML);
97
- log(newElt);
98
- if (api.triggerEvent(document.body, "htmx:addingHeadElement", {headElement: newElt}) !== false) {
99
- currentHead.appendChild(newElt);
100
- added.push(newElt);
101
- }
102
- }
103
-
104
- // remove all removed elements, after we have appended the new elements to avoid
105
- // additional network requests for things like style sheets
106
- for (const removedElement of removed) {
107
- if (api.triggerEvent(document.body, "htmx:removingHeadElement", {headElement: removedElement}) !== false) {
108
- currentHead.removeChild(removedElement);
109
- }
110
- }
111
-
112
- api.triggerEvent(document.body, "htmx:afterHeadMerge", {added: added, kept: preserved, removed: removed});
113
- }
114
- }
115
- }
116
-
117
- htmx.defineExtension("head-support", {
118
- init: function(apiRef) {
119
- // store a reference to the internal API.
120
- api = apiRef;
121
-
122
- htmx.on('htmx:afterSwap', function(evt){
123
- var serverResponse = evt.detail.xhr.response;
124
- if (api.triggerEvent(document.body, "htmx:beforeHeadMerge", evt.detail)) {
125
- mergeHead(serverResponse, evt.detail.boosted ? "merge" : "append");
126
- }
127
- })
128
-
129
- htmx.on('htmx:historyRestore', function(evt){
130
- if (api.triggerEvent(document.body, "htmx:beforeHeadMerge", evt.detail)) {
131
- if (evt.detail.cacheMiss) {
132
- mergeHead(evt.detail.serverResponse, "merge");
133
- } else {
134
- mergeHead(evt.detail.item.head, "merge");
135
- }
136
- }
137
- })
138
-
139
- htmx.on('htmx:historyItemCreated', function(evt){
140
- var historyItem = evt.detail.item;
141
- historyItem.head = document.head.outerHTML;
142
- })
143
- }
144
- });
145
-
146
- })()
@@ -1,28 +0,0 @@
1
- (function(){
2
- if (htmx.version && !htmx.version.startsWith("1.")) {
3
- console.warn("WARNING: You are using an htmx 1 extension with htmx " + htmx.version +
4
- ". It is recommended that you move to the version of this extension found on https://htmx.org/extensions")
5
- }
6
-
7
- function mergeObjects(obj1, obj2) {
8
- for (var key in obj2) {
9
- if (obj2.hasOwnProperty(key)) {
10
- obj1[key] = obj2[key];
11
- }
12
- }
13
- return obj1;
14
- }
15
-
16
- htmx.defineExtension('include-vals', {
17
- onEvent: function (name, evt) {
18
- if (name === "htmx:configRequest") {
19
- var includeValsElt = htmx.closest(evt.detail.elt, "[include-vals],[data-include-vals]");
20
- if (includeValsElt) {
21
- var includeVals = includeValsElt.getAttribute("include-vals") || includeValsElt.getAttribute("data-include-vals");
22
- var valuesToInclude = eval("({" + includeVals + "})");
23
- mergeObjects(evt.detail.parameters, valuesToInclude);
24
- }
25
- }
26
- }
27
- });
28
- })();
@@ -1,16 +0,0 @@
1
- if (htmx.version && !htmx.version.startsWith("1.")) {
2
- console.warn("WARNING: You are using an htmx 1 extension with htmx " + htmx.version +
3
- ". It is recommended that you move to the version of this extension found on https://htmx.org/extensions")
4
- }
5
- htmx.defineExtension('json-enc', {
6
- onEvent: function (name, evt) {
7
- if (name === "htmx:configRequest") {
8
- evt.detail.headers['Content-Type'] = "application/json";
9
- }
10
- },
11
-
12
- encodeParameters : function(xhr, parameters, elt) {
13
- xhr.overrideMimeType('text/json');
14
- return (JSON.stringify(parameters));
15
- }
16
- });
@@ -1,189 +0,0 @@
1
- ;(function () {
2
-
3
- if (htmx.version && !htmx.version.startsWith("1.")) {
4
- console.warn("WARNING: You are using an htmx 1 extension with htmx " + htmx.version +
5
- ". It is recommended that you move to the version of this extension found on https://htmx.org/extensions")
6
- }
7
-
8
- let loadingStatesUndoQueue = []
9
-
10
- function loadingStateContainer(target) {
11
- return htmx.closest(target, '[data-loading-states]') || document.body
12
- }
13
-
14
- function mayProcessUndoCallback(target, callback) {
15
- if (document.body.contains(target)) {
16
- callback()
17
- }
18
- }
19
-
20
- function mayProcessLoadingStateByPath(elt, requestPath) {
21
- const pathElt = htmx.closest(elt, '[data-loading-path]')
22
- if (!pathElt) {
23
- return true
24
- }
25
-
26
- return pathElt.getAttribute('data-loading-path') === requestPath
27
- }
28
-
29
- function queueLoadingState(sourceElt, targetElt, doCallback, undoCallback) {
30
- const delayElt = htmx.closest(sourceElt, '[data-loading-delay]')
31
- if (delayElt) {
32
- const delayInMilliseconds =
33
- delayElt.getAttribute('data-loading-delay') || 200
34
- const timeout = setTimeout(function () {
35
- doCallback()
36
-
37
- loadingStatesUndoQueue.push(function () {
38
- mayProcessUndoCallback(targetElt, undoCallback)
39
- })
40
- }, delayInMilliseconds)
41
-
42
- loadingStatesUndoQueue.push(function () {
43
- mayProcessUndoCallback(targetElt, function () { clearTimeout(timeout) })
44
- })
45
- } else {
46
- doCallback()
47
- loadingStatesUndoQueue.push(function () {
48
- mayProcessUndoCallback(targetElt, undoCallback)
49
- })
50
- }
51
- }
52
-
53
- function getLoadingStateElts(loadingScope, type, path) {
54
- return Array.from(htmx.findAll(loadingScope, "[" + type + "]")).filter(
55
- function (elt) { return mayProcessLoadingStateByPath(elt, path) }
56
- )
57
- }
58
-
59
- function getLoadingTarget(elt) {
60
- if (elt.getAttribute('data-loading-target')) {
61
- return Array.from(
62
- htmx.findAll(elt.getAttribute('data-loading-target'))
63
- )
64
- }
65
- return [elt]
66
- }
67
-
68
- htmx.defineExtension('loading-states', {
69
- onEvent: function (name, evt) {
70
- if (name === 'htmx:beforeRequest') {
71
- const container = loadingStateContainer(evt.target)
72
-
73
- const loadingStateTypes = [
74
- 'data-loading',
75
- 'data-loading-class',
76
- 'data-loading-class-remove',
77
- 'data-loading-disable',
78
- 'data-loading-aria-busy',
79
- ]
80
-
81
- let loadingStateEltsByType = {}
82
-
83
- loadingStateTypes.forEach(function (type) {
84
- loadingStateEltsByType[type] = getLoadingStateElts(
85
- container,
86
- type,
87
- evt.detail.pathInfo.requestPath
88
- )
89
- })
90
-
91
- loadingStateEltsByType['data-loading'].forEach(function (sourceElt) {
92
- getLoadingTarget(sourceElt).forEach(function (targetElt) {
93
- queueLoadingState(
94
- sourceElt,
95
- targetElt,
96
- function () {
97
- targetElt.style.display =
98
- sourceElt.getAttribute('data-loading') ||
99
- 'inline-block' },
100
- function () { targetElt.style.display = 'none' }
101
- )
102
- })
103
- })
104
-
105
- loadingStateEltsByType['data-loading-class'].forEach(
106
- function (sourceElt) {
107
- const classNames = sourceElt
108
- .getAttribute('data-loading-class')
109
- .split(' ')
110
-
111
- getLoadingTarget(sourceElt).forEach(function (targetElt) {
112
- queueLoadingState(
113
- sourceElt,
114
- targetElt,
115
- function () {
116
- classNames.forEach(function (className) {
117
- targetElt.classList.add(className)
118
- })
119
- },
120
- function() {
121
- classNames.forEach(function (className) {
122
- targetElt.classList.remove(className)
123
- })
124
- }
125
- )
126
- })
127
- }
128
- )
129
-
130
- loadingStateEltsByType['data-loading-class-remove'].forEach(
131
- function (sourceElt) {
132
- const classNames = sourceElt
133
- .getAttribute('data-loading-class-remove')
134
- .split(' ')
135
-
136
- getLoadingTarget(sourceElt).forEach(function (targetElt) {
137
- queueLoadingState(
138
- sourceElt,
139
- targetElt,
140
- function () {
141
- classNames.forEach(function (className) {
142
- targetElt.classList.remove(className)
143
- })
144
- },
145
- function() {
146
- classNames.forEach(function (className) {
147
- targetElt.classList.add(className)
148
- })
149
- }
150
- )
151
- })
152
- }
153
- )
154
-
155
- loadingStateEltsByType['data-loading-disable'].forEach(
156
- function (sourceElt) {
157
- getLoadingTarget(sourceElt).forEach(function (targetElt) {
158
- queueLoadingState(
159
- sourceElt,
160
- targetElt,
161
- function() { targetElt.disabled = true },
162
- function() { targetElt.disabled = false }
163
- )
164
- })
165
- }
166
- )
167
-
168
- loadingStateEltsByType['data-loading-aria-busy'].forEach(
169
- function (sourceElt) {
170
- getLoadingTarget(sourceElt).forEach(function (targetElt) {
171
- queueLoadingState(
172
- sourceElt,
173
- targetElt,
174
- function () { targetElt.setAttribute("aria-busy", "true") },
175
- function () { targetElt.removeAttribute("aria-busy") }
176
- )
177
- })
178
- }
179
- )
180
- }
181
-
182
- if (name === 'htmx:beforeOnLoad') {
183
- while (loadingStatesUndoQueue.length > 0) {
184
- loadingStatesUndoQueue.shift()()
185
- }
186
- }
187
- },
188
- })
189
- })()
@@ -1,15 +0,0 @@
1
- if (htmx.version && !htmx.version.startsWith("1.")) {
2
- console.warn("WARNING: You are using an htmx 1 extension with htmx " + htmx.version +
3
- ". It is recommended that you move to the version of this extension found on https://htmx.org/extensions")
4
- }
5
- htmx.defineExtension('method-override', {
6
- onEvent: function (name, evt) {
7
- if (name === "htmx:configRequest") {
8
- var method = evt.detail.verb;
9
- if (method !== "get" || method !== "post") {
10
- evt.detail.headers['X-HTTP-Method-Override'] = method.toUpperCase();
11
- evt.detail.verb = "post";
12
- }
13
- }
14
- }
15
- });
@@ -1,21 +0,0 @@
1
- if (htmx.version && !htmx.version.startsWith("1.")) {
2
- console.warn("WARNING: You are using an htmx 1 extension with htmx " + htmx.version +
3
- ". It is recommended that you move to the version of this extension found on https://htmx.org/extensions")
4
- }
5
- htmx.defineExtension('morphdom-swap', {
6
- isInlineSwap: function(swapStyle) {
7
- return swapStyle === 'morphdom';
8
- },
9
- handleSwap: function (swapStyle, target, fragment) {
10
- if (swapStyle === 'morphdom') {
11
- if (fragment.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
12
- // IE11 doesn't support DocumentFragment.firstElementChild
13
- morphdom(target, fragment.firstElementChild || fragment.firstChild);
14
- return [target];
15
- } else {
16
- morphdom(target, fragment.outerHTML);
17
- return [target];
18
- }
19
- }
20
- }
21
- });
@@ -1,50 +0,0 @@
1
- (function () {
2
-
3
- if (htmx.version && !htmx.version.startsWith("1.")) {
4
- console.warn("WARNING: You are using an htmx 1 extension with htmx " + htmx.version +
5
- ". It is recommended that you move to the version of this extension found on https://htmx.org/extensions")
6
- }
7
-
8
- /** @type {import("../htmx").HtmxInternalApi} */
9
- var api;
10
-
11
- htmx.defineExtension('multi-swap', {
12
- init: function (apiRef) {
13
- api = apiRef;
14
- },
15
- isInlineSwap: function (swapStyle) {
16
- return swapStyle.indexOf('multi:') === 0;
17
- },
18
- handleSwap: function (swapStyle, target, fragment, settleInfo) {
19
- if (swapStyle.indexOf('multi:') === 0) {
20
- var selectorToSwapStyle = {};
21
- var elements = swapStyle.replace(/^multi\s*:\s*/, '').split(/\s*,\s*/);
22
-
23
- elements.map(function (element) {
24
- var split = element.split(/\s*:\s*/);
25
- var elementSelector = split[0];
26
- var elementSwapStyle = typeof (split[1]) !== "undefined" ? split[1] : "innerHTML";
27
-
28
- if (elementSelector.charAt(0) !== '#') {
29
- console.error("HTMX multi-swap: unsupported selector '" + elementSelector + "'. Only ID selectors starting with '#' are supported.");
30
- return;
31
- }
32
-
33
- selectorToSwapStyle[elementSelector] = elementSwapStyle;
34
- });
35
-
36
- for (var selector in selectorToSwapStyle) {
37
- var swapStyle = selectorToSwapStyle[selector];
38
- var elementToSwap = fragment.querySelector(selector);
39
- if (elementToSwap) {
40
- api.oobSwap(swapStyle, elementToSwap, settleInfo);
41
- } else {
42
- console.warn("HTMX multi-swap: selector '" + selector + "' not found in source content.");
43
- }
44
- }
45
-
46
- return true;
47
- }
48
- }
49
- });
50
- })();
@@ -1,63 +0,0 @@
1
- (function(undefined){
2
- 'use strict';
3
- if (htmx.version && !htmx.version.startsWith("1.")) {
4
- console.warn("WARNING: You are using an htmx 1 extension with htmx " + htmx.version +
5
- ". It is recommended that you move to the version of this extension found on https://htmx.org/extensions")
6
- }
7
- // Save a reference to the global object (window in the browser)
8
- var _root = this;
9
-
10
- function dependsOn(pathSpec, url) {
11
- if (pathSpec === "ignore") {
12
- return false;
13
- }
14
- var dependencyPath = pathSpec.split("/");
15
- var urlPath = url.split("/");
16
- for (var i = 0; i < urlPath.length; i++) {
17
- var dependencyElement = dependencyPath.shift();
18
- var pathElement = urlPath[i];
19
- if (dependencyElement !== pathElement && dependencyElement !== "*") {
20
- return false;
21
- }
22
- if (dependencyPath.length === 0 || (dependencyPath.length === 1 && dependencyPath[0] === "")) {
23
- return true;
24
- }
25
- }
26
- return false;
27
- }
28
-
29
- function refreshPath(path) {
30
- var eltsWithDeps = htmx.findAll("[path-deps]");
31
- for (var i = 0; i < eltsWithDeps.length; i++) {
32
- var elt = eltsWithDeps[i];
33
- if (dependsOn(elt.getAttribute('path-deps'), path)) {
34
- htmx.trigger(elt, "path-deps");
35
- }
36
- }
37
- }
38
-
39
- htmx.defineExtension('path-deps', {
40
- onEvent: function (name, evt) {
41
- if (name === "htmx:beforeOnLoad") {
42
- var config = evt.detail.requestConfig;
43
- // mutating call
44
- if (config.verb !== "get" && evt.target.getAttribute('path-deps') !== 'ignore') {
45
- refreshPath(config.path);
46
- }
47
- }
48
- }
49
- });
50
-
51
- /**
52
- * ********************
53
- * Expose functionality
54
- * ********************
55
- */
56
-
57
- _root.PathDeps = {
58
- refresh: function(path) {
59
- refreshPath(path);
60
- }
61
- };
62
-
63
- }).call(this);
@@ -1,15 +0,0 @@
1
- if (htmx.version && !htmx.version.startsWith("1.")) {
2
- console.warn("WARNING: You are using an htmx 1 extension with htmx " + htmx.version +
3
- ". It is recommended that you move to the version of this extension found on https://htmx.org/extensions")
4
- }
5
- htmx.defineExtension('path-params', {
6
- onEvent: function(name, evt) {
7
- if (name === "htmx:configRequest") {
8
- evt.detail.path = evt.detail.path.replace(/{([^}]+)}/g, function (_, param) {
9
- var val = evt.detail.parameters[param];
10
- delete evt.detail.parameters[param];
11
- return val === undefined ? "{" + param + "}" : encodeURIComponent(val);
12
- })
13
- }
14
- }
15
- });