mirror of
https://github.com/bigskysoftware/htmx.git
synced 2025-11-03 06:53:37 +00:00
Update htmx.js
- implement new SSE syntax - add comments to delimit the start and end of SSE code block - update splitOnWhitespace to allow leading/trailing whitespace - reorganize maybeCloseSSESource() to bottom of SSE code block
This commit is contained in:
parent
8bad3b61f5
commit
411e8fd09e
75
src/htmx.js
75
src/htmx.js
@ -218,7 +218,7 @@ return (function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function splitOnWhitespace(trigger) {
|
function splitOnWhitespace(trigger) {
|
||||||
return trigger.split(/\s+/);
|
return trigger.trim().split(/\s+/);
|
||||||
}
|
}
|
||||||
|
|
||||||
function mergeObjects(obj1, obj2) {
|
function mergeObjects(obj1, obj2) {
|
||||||
@ -869,12 +869,9 @@ return (function () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function maybeCloseSSESource(elt) {
|
//====================================================================
|
||||||
if (!bodyContains(elt)) {
|
// Server Sent Events
|
||||||
getInternalData(elt).sseEventSource.close();
|
//====================================================================
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function processSSEInfo(elt, nodeData, info) {
|
function processSSEInfo(elt, nodeData, info) {
|
||||||
var values = info.split(",");
|
var values = info.split(",");
|
||||||
@ -883,6 +880,10 @@ return (function () {
|
|||||||
if (value[0] === "connect") {
|
if (value[0] === "connect") {
|
||||||
processSSESource(elt, value[1]);
|
processSSESource(elt, value[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((value[0] == "swap") && (value[1] == "on")) {
|
||||||
|
processSSESwap(elt, value[2])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -895,10 +896,51 @@ return (function () {
|
|||||||
getInternalData(elt).sseEventSource = source;
|
getInternalData(elt).sseEventSource = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
function processSSETrigger(elt, verb, path, sseEventName) {
|
function processSSESwap(elt, sseEventName) {
|
||||||
var sseSourceElt = getClosestMatch(elt, function (parent) {
|
var sseSourceElt = getClosestMatch(elt, hasEventSource);
|
||||||
return getInternalData(parent).sseEventSource != null;
|
if (sseSourceElt) {
|
||||||
|
var sseEventSource = getInternalData(sseSourceElt).sseEventSource;
|
||||||
|
var sseListener = function (event) {
|
||||||
|
if (maybeCloseSSESource(sseSourceElt)) {
|
||||||
|
sseEventSource.removeEventListener(sseEventName, sseListener);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////
|
||||||
|
// TODO: merge this code with AJAX and WebSockets code in the future.
|
||||||
|
|
||||||
|
var response = event.data;
|
||||||
|
withExtensions(elt, function(extension){
|
||||||
|
response = extension.transformResponse(response, null, elt);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var swapSpec = getSwapSpecification(elt)
|
||||||
|
var target = getTarget(elt)
|
||||||
|
var settleInfo = makeSettleInfo(elt);
|
||||||
|
|
||||||
|
selectAndSwap(swapSpec.swapStyle, elt, target, response, settleInfo)
|
||||||
|
triggerEvent(elt, "htmx:sseMessage", event)
|
||||||
|
|
||||||
|
// This is a placeholder before a larger refactor. This code does not (yet?) handle:
|
||||||
|
//
|
||||||
|
// * This syntax breaks if there are commas in the URL
|
||||||
|
// * hx-oob-swap
|
||||||
|
// * hx-settle
|
||||||
|
// * hx-trigger (header values)
|
||||||
|
//
|
||||||
|
///////////////////////////
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
getInternalData(elt).sseListener = sseListener;
|
||||||
|
sseEventSource.addEventListener(sseEventName, sseListener);
|
||||||
|
} else {
|
||||||
|
triggerErrorEvent(elt, "htmx:noSSESourceError");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function processSSETrigger(elt, verb, path, sseEventName) {
|
||||||
|
var sseSourceElt = getClosestMatch(elt, hasEventSource);
|
||||||
if (sseSourceElt) {
|
if (sseSourceElt) {
|
||||||
var sseEventSource = getInternalData(sseSourceElt).sseEventSource;
|
var sseEventSource = getInternalData(sseSourceElt).sseEventSource;
|
||||||
var sseListener = function () {
|
var sseListener = function () {
|
||||||
@ -917,6 +959,19 @@ return (function () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function maybeCloseSSESource(elt) {
|
||||||
|
if (!bodyContains(elt)) {
|
||||||
|
getInternalData(elt).sseEventSource.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function hasEventSource(node) {
|
||||||
|
return getInternalData(node).sseEventSource != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//====================================================================
|
||||||
|
|
||||||
function loadImmediately(elt, verb, path, nodeData, delay) {
|
function loadImmediately(elt, verb, path, nodeData, delay) {
|
||||||
var load = function(){
|
var load = function(){
|
||||||
if (!nodeData.loaded) {
|
if (!nodeData.loaded) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user