(function () {
"use strict";
var RECONNECT_DELAY_MS = 2000;
function connect(root) {
var channel = root.getAttribute("data-live-channel");
var url =
(location.protocol === "https:" ? "wss://" : "ws://") +
location.host +
"/__larust_push/" +
encodeURIComponent(channel);
var socket = new WebSocket(url);
socket.addEventListener("message", function (event) {
applyFragment(root, event.data);
});
socket.addEventListener("close", function () {
setTimeout(function () {
connect(root);
}, RECONNECT_DELAY_MS);
});
}
function applyFragment(root, html) {
var template = document.createElement("template");
template.innerHTML = html.trim();
var newRoot = template.content.firstElementChild;
if (!newRoot) return;
larustPushPatch(root, newRoot);
}
function larustPushPatch(oldEl, newEl) {
if (isIgnored(oldEl)) return;
if (oldEl.tagName !== newEl.tagName) {
oldEl.replaceWith(newEl);
return;
}
patchAttributes(oldEl, newEl);
var oldChildren = Array.prototype.slice.call(oldEl.childNodes);
var newChildren = Array.prototype.slice.call(newEl.childNodes);
var max = Math.max(oldChildren.length, newChildren.length);
for (var i = 0; i < max; i++) {
var oldChild = oldChildren[i];
var newChild = newChildren[i];
if (oldChild && isIgnored(oldChild)) continue;
if (oldChild && !newChild) {
oldEl.removeChild(oldChild);
continue;
}
if (!oldChild && newChild) {
oldEl.appendChild(newChild);
continue;
}
patchNode(oldEl, oldChild, newChild);
}
}
function isIgnored(node) {
return (
node.nodeType === Node.ELEMENT_NODE &&
node.hasAttribute &&
node.hasAttribute("wire:ignore")
);
}
function patchNode(parent, oldChild, newChild) {
if (oldChild.nodeType !== newChild.nodeType) {
parent.replaceChild(newChild, oldChild);
return;
}
if (oldChild.nodeType === Node.TEXT_NODE) {
if (oldChild.data !== newChild.data) oldChild.data = newChild.data;
return;
}
if (oldChild.nodeType !== Node.ELEMENT_NODE) {
parent.replaceChild(newChild, oldChild);
return;
}
if (oldChild.tagName !== newChild.tagName || oldChild.id !== newChild.id) {
parent.replaceChild(newChild, oldChild);
return;
}
larustPushPatch(oldChild, newChild);
}
function patchAttributes(oldEl, newEl) {
var oldAttrs = oldEl.attributes;
var newAttrs = newEl.attributes;
for (var i = oldAttrs.length - 1; i >= 0; i--) {
var name = oldAttrs[i].name;
if (!newEl.hasAttribute(name)) oldEl.removeAttribute(name);
}
for (var j = 0; j < newAttrs.length; j++) {
var attr = newAttrs[j];
if (oldEl.getAttribute(attr.name) !== attr.value) {
oldEl.setAttribute(attr.name, attr.value);
}
}
var isEditingThis =
typeof document !== "undefined" && document.activeElement === oldEl;
if (!isEditingThis) {
if ("value" in newEl && oldEl.value !== newEl.value) {
oldEl.value = newEl.value;
}
if ("checked" in newEl && oldEl.checked !== newEl.checked) {
oldEl.checked = newEl.checked;
}
}
}
document.querySelectorAll("[data-live-channel]").forEach(connect);
})();