Commit dafc958f authored by Stephan Kreutzer's avatar Stephan Kreutzer

sos-grid-1 prototype: Load chilren if document.querySelectorAll() is...

sos-grid-1 prototype: Load chilren if document.querySelectorAll() is available. Allow jumping to a POI if its ID is passed in the fragment identifier.
parent 15788b9b
......@@ -32,14 +32,24 @@ it's user data that is only processed. A different license may apply.
// ".xhtml" and it might magically start to work.
function loadGrid() {
if (window.location.hash.length > 1) {
NavigatePoi(window.location.hash.substr(1));
} else {
// NavigatePoi() with empty ID string: load the "default" (first) POI.
NavigatePoi("");
}
}
window.onload = function () {
let loadLink = document.getElementById('loadLink');
loadLink.parentNode.removeChild(loadLink);
NavigatePoi("");
}
loadGrid();
// NavigatePoi() with empty ID string: load the "default" (first) POI.
document.addEventListener("DOMContentLoaded", function() { loadGrid(); });
if ("onhashchange" in window) {
window.onhashchange = loadGrid;
}
};
</script></head><body><div id="grid"><div id="causes"/><div id="current"/><div id="effects"/><div id="loadLink"><a href="#" onclick="loadGrid();">Load</a></div></div><div id="sos-input" style="display:none;"><!-- The data contained in this element and sub-elements is not part of this program, it's user data and might be under a different license than this program. This program also doesn't depend on it or link it as a library, it's only processed. --><sos:SOS Language="en" xsi:schemaLocation="http://www.untiednations.com/SOS http://www.untiednations.com/XML/SOSMeeting1.xsd">
<sos:POI ID="1" Type="Effect">
<sos:Parent Parent-ID="2"/>
......
......@@ -39,7 +39,7 @@ function SosGridEngine() {
let poiSource = null;
if (typeof document.querySelectorAll != "undefined") {
if ("querySelectorAll" in document) {
if (id.length <= 0) {
// Evil browsers/web/W3C: no support for namespaces in querySelectorAll().
// See https://www.w3.org/TR/selectors-api/#resolving-namespaces
......@@ -74,7 +74,10 @@ function SosGridEngine() {
} else {
if (id.length <= 0) {
if (_poiMapping.length > 0) {
poiSource = _poiMapping[0];
for (let item in _poiMapping) {
poiSource = _poiMapping[item];
break;
}
} else {
return null;
}
......@@ -107,19 +110,19 @@ function SosGridEngine() {
continue;
}
if (child.tagName == "sos:Title") {
if (child.localName == "Title") {
if (title != null) {
throw "POI contains more than one Title.";
}
title = child.textContent;
} else if (child.tagName == "sos:Text") {
} else if (child.localName == "Text") {
if (description != null) {
throw "POI contains more than one Text.";
}
description = child.textContent;
} else if (child.tagName == "sos:Parent") {
} else if (child.localName == "Parent") {
if (child.hasAttribute("Parent-ID") != true) {
throw "Parent of a POI is missing its 'Parent-ID' attribute.";
}
......@@ -142,6 +145,35 @@ function SosGridEngine() {
}
}
if ("querySelectorAll" in document) {
// Evil browsers/web/W3C: no support for namespaces in querySelectorAll().
// See https://www.w3.org/TR/selectors-api/#resolving-namespaces
let result = document.querySelectorAll("Parent[Parent-ID='" + poiSource.getAttribute("ID") + "']");
for (let i = 0; i < result.length; i++) {
if (result[i].namespaceURI != "http://www.untiednations.com/SOS") {
continue;
}
if (result[i].parentNode.localName != "POI" ||
result[i].parentNode.namespaceURI != "http://www.untiednations.com/SOS") {
throw "Parent of a Parent isn't a proper POI.";
}
if (result[i].parentNode.hasAttribute("ID") == true) {
children.push(result[i].parentNode.getAttribute("ID"));
} else {
throw "POI is missing its 'ID' attribute.";
}
}
} else {
// TODO: The children are not in the source for the current POI and
// no separate list concludes the children from parent references to
// add it to all read POIs afterwards, as it needs to be decided if
// the format should change to support this kind of navigation natively
// or if it is supposed to be calculated and stored in memory during runtime.
}
return new Poi(poiSource.getAttribute("ID"), title, description, parents, children);
}
......@@ -150,9 +182,11 @@ function SosGridEngine() {
return true;
}
if (typeof document.querySelectorAll != "undefined") {
if ("querySelectorAll" in document) {
_loaded = true;
return true;
} else {
console.log("Children of a POI won't be loaded.");
}
// Stupid JavaScript/web/browsers: document.getElementsByTagNameNS()
......
......@@ -35,7 +35,7 @@ function RenderPois(pois, targetId, caption, arrowPosition) {
for (let i = 0; i < pois.length; i++) {
if (arrowPosition === "pre") {
let navigationLink = document.createElement("a");
navigationLink.setAttribute("href", "#");
navigationLink.setAttribute("href", "javascript:void(0);");
// TODO: Prevent injection (any maybe at other places, too).
navigationLink.setAttribute("onclick", "NavigatePoi(\"" + pois[i].getId() + "\");");
......@@ -50,7 +50,7 @@ function RenderPois(pois, targetId, caption, arrowPosition) {
container.appendChild(title);
let navigationLink = document.createElement("a");
navigationLink.setAttribute("href", "#");
navigationLink.setAttribute("href", "javascript:void(0);");
// TODO: Prevent injection (any maybe at other places, too).
navigationLink.setAttribute("onclick", "NavigatePoi(\"" + pois[i].getId() + "\");");
......@@ -69,7 +69,7 @@ function RenderPois(pois, targetId, caption, arrowPosition) {
}
let navigationLink = document.createElement("a");
navigationLink.setAttribute("href", "#");
navigationLink.setAttribute("href", "javascript:void(0);");
// TODO: Prevent injection (any maybe at other places, too).
navigationLink.setAttribute("onclick", "NavigatePoi(\"" + pois[i].getId() + "\");");
......
......@@ -68,14 +68,24 @@ it's user data that is only processed. A different license may apply.
// ".xhtml" and it might magically start to work.
function loadGrid() {
if (window.location.hash.length > 1) {
NavigatePoi(window.location.hash.substr(1));
} else {
// NavigatePoi() with empty ID string: load the "default" (first) POI.
NavigatePoi("");
}
}
window.onload = function () {
let loadLink = document.getElementById('loadLink');
loadLink.parentNode.removeChild(loadLink);
NavigatePoi("");
}
loadGrid();
// NavigatePoi() with empty ID string: load the "default" (first) POI.
document.addEventListener("DOMContentLoaded", function() { loadGrid(); });
if ("onhashchange" in window) {
window.onhashchange = loadGrid;
}
};
</xsl:text>
</script>
</head>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment