Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions django_unicorn/static/unicorn/js/messageSender.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
import { $, getCsrfToken, hasValue, isFunction } from "./utils.js";
import { getMorphdomOptions } from "./morphdom/2.6.1/options.js";
import { components } from "./store.js";
import { componentInit } from "./unicorn.js";

function createNewJsonScriptTag(jsonData) {
// check if the component already exists
if (Object.prototype.hasOwnProperty.call(components, jsonData.id)) {
return;
}
const jsonString = JSON.stringify(jsonData);
const scriptTag = document.createElement("script");
scriptTag.id = `unicorn:data:${jsonData.id}`;
scriptTag.type = "application/json";
scriptTag.text = jsonString;
document.body.appendChild(scriptTag);
componentInit(jsonData);
}

function traverseChildNodes(node, responseJson) {
if (node.attributes && node.hasAttribute("unicorn:name")) {
const jsonData = {
id: node.getAttribute("unicorn:id"),
name: node.getAttribute("unicorn:name"),
key: node.getAttribute("unicorn:key"),
data: {}, // TODO: get data from component to validate checksum
calls: [],
hash: responseJson.hash,
};
createNewJsonScriptTag(jsonData);
}

// iterate over each child node
for (let i = 0; i < node.childNodes.length; i++) {
// recursively call traverseChildNodes()
traverseChildNodes(node.childNodes[i], responseJson);
}
}

function updateRootScripts(responseJson) {
const parser = new DOMParser();
const responseDoc = parser.parseFromString(responseJson.dom, "text/html");

// traverse the DOM tree starting from the root node
traverseChildNodes(responseDoc.documentElement, responseJson);
}

/**
* Calls the message endpoint and merges the results into the document.
Expand Down Expand Up @@ -282,6 +326,8 @@ export function send(component, callback) {
if (isFunction(callback)) {
callback(triggeringElements, forceModelUpdate, null);
}

updateRootScripts(responseJson);
})
.catch((err) => {
// Make sure to clear the current queues in case of an error
Expand Down