htmx.org 2.0.0-beta2 → 2.0.0-beta4

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.
package/README.md CHANGED
@@ -33,7 +33,7 @@ By removing these arbitrary constraints htmx completes HTML as a
33
33
  ## quick start
34
34
 
35
35
  ```html
36
- <script src="https://unpkg.com/htmx.org@1.9.11"></script>
36
+ <script src="https://unpkg.com/htmx.org@2.0.0-beta4"></script>
37
37
  <!-- have a button POST a click via AJAX -->
38
38
  <button hx-post="/clicked" hx-swap="outerHTML">
39
39
  Click Me
@@ -70,8 +70,6 @@ No time? Then [become a sponsor](https://github.com/sponsors/bigskysoftware#spon
70
70
 
71
71
  To develop htmx locally, you will need to install the development dependencies.
72
72
 
73
- __Requires Node 15.__
74
-
75
73
  Run:
76
74
 
77
75
  ```
@@ -0,0 +1,9 @@
1
+ # Why Are These Files Here?
2
+
3
+ These are legacy extensions for htmx 1.x and are **NOT** actively maintained or guaranteed to work with htmx 2.x.
4
+ They are here because we unfortunately linked to unversioned unpkg URLs in the installation guides for them
5
+ in 1.x, so we need to keep them here to preserve those URLs and not break existing users functionality.
6
+
7
+ If you are looking for extensions for htmx 2.x, please see the [htmx 2.0 extensions site](https://extensions.htmx.org),
8
+ which has links to the new extensions repos (They have all been moved to their own NPM projects and URLs, like
9
+ they should have been from the start!)
@@ -0,0 +1,11 @@
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://extensions.htmx.org")
4
+ }
5
+ htmx.defineExtension('ajax-header', {
6
+ onEvent: function (name, evt) {
7
+ if (name === "htmx:configRequest") {
8
+ evt.detail.headers['X-Requested-With'] = 'XMLHttpRequest';
9
+ }
10
+ }
11
+ });
@@ -0,0 +1,20 @@
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://extensions.htmx.org")
4
+ }
5
+ htmx.defineExtension('alpine-morph', {
6
+ isInlineSwap: function (swapStyle) {
7
+ return swapStyle === 'morph';
8
+ },
9
+ handleSwap: function (swapStyle, target, fragment) {
10
+ if (swapStyle === 'morph') {
11
+ if (fragment.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
12
+ Alpine.morph(target, fragment.firstElementChild);
13
+ return [target];
14
+ } else {
15
+ Alpine.morph(target, fragment.outerHTML);
16
+ return [target];
17
+ }
18
+ }
19
+ }
20
+ });
@@ -0,0 +1,97 @@
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://extensions.htmx.org")
6
+ }
7
+
8
+ function splitOnWhitespace(trigger) {
9
+ return trigger.split(/\s+/);
10
+ }
11
+
12
+ function parseClassOperation(trimmedValue) {
13
+ var split = splitOnWhitespace(trimmedValue);
14
+ if (split.length > 1) {
15
+ var operation = split[0];
16
+ var classDef = split[1].trim();
17
+ var cssClass;
18
+ var delay;
19
+ if (classDef.indexOf(":") > 0) {
20
+ var splitCssClass = classDef.split(':');
21
+ cssClass = splitCssClass[0];
22
+ delay = htmx.parseInterval(splitCssClass[1]);
23
+ } else {
24
+ cssClass = classDef;
25
+ delay = 100;
26
+ }
27
+ return {
28
+ operation: operation,
29
+ cssClass: cssClass,
30
+ delay: delay
31
+ }
32
+ } else {
33
+ return null;
34
+ }
35
+ }
36
+
37
+ function performOperation(elt, classOperation, classList, currentRunTime) {
38
+ setTimeout(function () {
39
+ elt.classList[classOperation.operation].call(elt.classList, classOperation.cssClass);
40
+ }, currentRunTime)
41
+ }
42
+
43
+ function toggleOperation(elt, classOperation, classList, currentRunTime) {
44
+ setTimeout(function () {
45
+ setInterval(function () {
46
+ elt.classList[classOperation.operation].call(elt.classList, classOperation.cssClass);
47
+ }, classOperation.delay);
48
+ }, currentRunTime)
49
+ }
50
+
51
+ function processClassList(elt, classList) {
52
+ var runs = classList.split("&");
53
+ for (var i = 0; i < runs.length; i++) {
54
+ var run = runs[i];
55
+ var currentRunTime = 0;
56
+ var classOperations = run.split(",");
57
+ for (var j = 0; j < classOperations.length; j++) {
58
+ var value = classOperations[j];
59
+ var trimmedValue = value.trim();
60
+ var classOperation = parseClassOperation(trimmedValue);
61
+ if (classOperation) {
62
+ if (classOperation.operation === "toggle") {
63
+ toggleOperation(elt, classOperation, classList, currentRunTime);
64
+ currentRunTime = currentRunTime + classOperation.delay;
65
+ } else {
66
+ currentRunTime = currentRunTime + classOperation.delay;
67
+ performOperation(elt, classOperation, classList, currentRunTime);
68
+ }
69
+ }
70
+ }
71
+ }
72
+ }
73
+
74
+ function maybeProcessClasses(elt) {
75
+ if (elt.getAttribute) {
76
+ var classList = elt.getAttribute("classes") || elt.getAttribute("data-classes");
77
+ if (classList) {
78
+ processClassList(elt, classList);
79
+ }
80
+ }
81
+ }
82
+
83
+ htmx.defineExtension('class-tools', {
84
+ onEvent: function (name, evt) {
85
+ if (name === "htmx:afterProcessNode") {
86
+ var elt = evt.detail.elt;
87
+ maybeProcessClasses(elt);
88
+ if (elt.querySelectorAll) {
89
+ var children = elt.querySelectorAll("[classes], [data-classes]");
90
+ for (var i = 0; i < children.length; i++) {
91
+ maybeProcessClasses(children[i]);
92
+ }
93
+ }
94
+ }
95
+ }
96
+ });
97
+ })();
@@ -0,0 +1,100 @@
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://extensions.htmx.org")
4
+ }
5
+ htmx.defineExtension('client-side-templates', {
6
+ transformResponse : function(text, xhr, elt) {
7
+
8
+ var mustacheTemplate = htmx.closest(elt, "[mustache-template]");
9
+ if (mustacheTemplate) {
10
+ var data = JSON.parse(text);
11
+ var templateId = mustacheTemplate.getAttribute('mustache-template');
12
+ var template = htmx.find("#" + templateId);
13
+ if (template) {
14
+ return Mustache.render(template.innerHTML, data);
15
+ } else {
16
+ throw "Unknown mustache template: " + templateId;
17
+ }
18
+ }
19
+
20
+ var mustacheArrayTemplate = htmx.closest(elt, "[mustache-array-template]");
21
+ if (mustacheArrayTemplate) {
22
+ var data = JSON.parse(text);
23
+ var templateId = mustacheArrayTemplate.getAttribute('mustache-array-template');
24
+ var template = htmx.find("#" + templateId);
25
+ if (template) {
26
+ return Mustache.render(template.innerHTML, {"data": data });
27
+ } else {
28
+ throw "Unknown mustache template: " + templateId;
29
+ }
30
+ }
31
+
32
+ var handlebarsTemplate = htmx.closest(elt, "[handlebars-template]");
33
+ if (handlebarsTemplate) {
34
+ var data = JSON.parse(text);
35
+ var templateId = handlebarsTemplate.getAttribute('handlebars-template');
36
+ var templateElement = htmx.find('#' + templateId).innerHTML;
37
+ var renderTemplate = Handlebars.compile(templateElement);
38
+ if (renderTemplate) {
39
+ return renderTemplate(data);
40
+ } else {
41
+ throw "Unknown handlebars template: " + templateId;
42
+ }
43
+ }
44
+
45
+ var handlebarsArrayTemplate = htmx.closest(elt, "[handlebars-array-template]");
46
+ if (handlebarsArrayTemplate) {
47
+ var data = JSON.parse(text);
48
+ var templateId = handlebarsArrayTemplate.getAttribute('handlebars-array-template');
49
+ var templateElement = htmx.find('#' + templateId).innerHTML;
50
+ var renderTemplate = Handlebars.compile(templateElement);
51
+ if (renderTemplate) {
52
+ return renderTemplate(data);
53
+ } else {
54
+ throw "Unknown handlebars template: " + templateId;
55
+ }
56
+ }
57
+
58
+ var nunjucksTemplate = htmx.closest(elt, "[nunjucks-template]");
59
+ if (nunjucksTemplate) {
60
+ var data = JSON.parse(text);
61
+ var templateName = nunjucksTemplate.getAttribute('nunjucks-template');
62
+ var template = htmx.find('#' + templateName);
63
+ if (template) {
64
+ return nunjucks.renderString(template.innerHTML, data);
65
+ } else {
66
+ return nunjucks.render(templateName, data);
67
+ }
68
+ }
69
+
70
+ var xsltTemplate = htmx.closest(elt, "[xslt-template]");
71
+ if (xsltTemplate) {
72
+ var templateId = xsltTemplate.getAttribute('xslt-template');
73
+ var template = htmx.find("#" + templateId);
74
+ if (template) {
75
+ var content = template.innerHTML ? new DOMParser().parseFromString(template.innerHTML, 'application/xml')
76
+ : template.contentDocument;
77
+ var processor = new XSLTProcessor();
78
+ processor.importStylesheet(content);
79
+ var data = new DOMParser().parseFromString(text, "application/xml");
80
+ var frag = processor.transformToFragment(data, document);
81
+ return new XMLSerializer().serializeToString(frag);
82
+ } else {
83
+ throw "Unknown XSLT template: " + templateId;
84
+ }
85
+ }
86
+
87
+ var nunjucksArrayTemplate = htmx.closest(elt, "[nunjucks-array-template]");
88
+ if (nunjucksArrayTemplate) {
89
+ var data = JSON.parse(text);
90
+ var templateName = nunjucksArrayTemplate.getAttribute('nunjucks-array-template');
91
+ var template = htmx.find('#' + templateName);
92
+ if (template) {
93
+ return nunjucks.renderString(template.innerHTML, {"data": data});
94
+ } else {
95
+ return nunjucks.render(templateName, {"data": data});
96
+ }
97
+ }
98
+ return text;
99
+ }
100
+ });
@@ -0,0 +1,15 @@
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://extensions.htmx.org")
4
+ }
5
+ htmx.defineExtension('debug', {
6
+ onEvent: function (name, evt) {
7
+ if (console.debug) {
8
+ console.debug(name, evt);
9
+ } else if (console) {
10
+ console.log("DEBUG:", name, evt);
11
+ } else {
12
+ throw "NO CONSOLE SUPPORTED"
13
+ }
14
+ }
15
+ });
@@ -0,0 +1,20 @@
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://extensions.htmx.org")
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
+ });
@@ -0,0 +1,41 @@
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://extensions.htmx.org")
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
+ })();
@@ -0,0 +1,146 @@
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://extensions.htmx.org")
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
+ })()
@@ -0,0 +1,28 @@
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://extensions.htmx.org")
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
+ })();
@@ -0,0 +1,16 @@
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://extensions.htmx.org")
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
+ });