Skip to content
Snippets Groups Projects
Commit e81e71b0 authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

improvements to javascript client

parent 7328f74b
No related branches found
No related tags found
No related merge requests found
...@@ -72,6 +72,9 @@ function getEntityID(element) { ...@@ -72,6 +72,9 @@ function getEntityID(element) {
/** /**
* Return the id of an href attribute. * Return the id of an href attribute.
* This is needed for retrieving an ID that is contained in a link to another entity. * This is needed for retrieving an ID that is contained in a link to another entity.
*
* Some anker tags have a data-entity-id tag instead which is returned.
*
* @param el The element holding the link. * @param el The element holding the link.
* @return A String holding the ID or undefined if no href attribute could be found. * @return A String holding the ID or undefined if no href attribute could be found.
*/ */
...@@ -83,6 +86,8 @@ function getIDfromHREF(el) { ...@@ -83,6 +86,8 @@ function getIDfromHREF(el) {
if (idstr.length == 0) if (idstr.length == 0)
return undefined; return undefined;
return idstr; return idstr;
} else if (el.hasAttribute("data-entity-id")) {
return el.getAttribute("data-entity-id");
} }
return undefined; return undefined;
} }
...@@ -199,21 +204,27 @@ function getPropertyFromElement(propertyelement, names=undefined) { ...@@ -199,21 +204,27 @@ function getPropertyFromElement(propertyelement, names=undefined) {
// This is set to true, when there is a reference or a list of references: // This is set to true, when there is a reference or a list of references:
property.reference = (valel.getElementsByClassName("caosdb-id").length > 0); property.reference = (valel.getElementsByClassName("caosdb-id").length > 0);
if (!(property.datatype === undefined) && property.datatype.substring(0, 4).toLowerCase() == "list") { if (!(property.datatype === undefined) && property.datatype.substring(0, 4).toLowerCase() == "list") {
property.list = true;
property.value = [];
let listel; let listel;
if (property.reference) { if (property.reference) {
// TODO: Fix list preivew here. Fixed, but untested. // TODO: Fix list preivew here. Fixed, but untested.
listel = findElementByConditions(valel, x => x.classList.contains("caosdb-id"), listel = findElementByConditions(valel, x => x.classList.contains("caosdb-resolvable-reference"),
x => x.classList.contains("caosdb-preview-container")); x => x.classList.contains("caosdb-preview-container"));
for (var j=0; j<listel.length; j++) {
property.value.push(getIDfromHREF(listel[j]));
}
} else { } else {
// TODO: Fix list preivew here. Fixed, but untested. // TODO: Fix list preivew here. Fixed, but untested.
listel = findElementByConditions(valel, x => x.classList.contains("list-group-item"), listel = findElementByConditions(valel, x => x.classList.contains("list-group-item"),
x => x.classList.contains("caosdb-preview-container")); x => x.classList.contains("caosdb-preview-container"));
}
property.list = true;
property.value = [];
for (var j=0; j<listel.length; j++) { for (var j=0; j<listel.length; j++) {
property.value.push(listel[j].textContent); property.value.push(listel[j].textContent);
} }
}
} else if (property.reference) { } else if (property.reference) {
// let el = findElementByConditions(valel, x => x.classList.contains("caosdb-id"), // let el = findElementByConditions(valel, x => x.classList.contains("caosdb-id"),
// x => x.classList.contains("caosdb-preview-container")); // x => x.classList.contains("caosdb-preview-container"));
...@@ -302,7 +313,7 @@ function setPropertySafe(valueelement, property, propold) { ...@@ -302,7 +313,7 @@ function setPropertySafe(valueelement, property, propold) {
} else { } else {
finalstring = ''; finalstring = '';
for (var i=0; i<property.value.length; i++) { for (var i=0; i<property.value.length; i++) {
finalstring += '<a class="btn btn-default btn-sm" href="' + serverstring + property.value[i] + '"><span class="caosdb-id">'+property.value[i]+'</span></a>'; finalstring += '<a class="btn btn-default btn-sm caosdb-resolvable-reference" href="' + serverstring + property.value[i] + '"><span class="caosdb-id">'+property.value[i]+'</span></a>';
} }
} }
valueelement.getElementsByClassName("caosdb-value-list")[0].getElementsByClassName("caosdb-overflow-content")[0].innerHTML = finalstring; valueelement.getElementsByClassName("caosdb-value-list")[0].getElementsByClassName("caosdb-overflow-content")[0].innerHTML = finalstring;
...@@ -316,7 +327,7 @@ function setPropertySafe(valueelement, property, propold) { ...@@ -316,7 +327,7 @@ function setPropertySafe(valueelement, property, propold) {
ael.setAttribute("href", serverstring + property.value); ael.setAttribute("href", serverstring + property.value);
ael.innerHTML = '<span class="caosdb-id">'+property.value+'</span>'; ael.innerHTML = '<span class="caosdb-id">'+property.value+'</span>';
} else { } else {
finalstring = '<a class="btn btn-default btn-sm" href="' + serverstring + property.value + '"><span class="caosdb-id">'+property.value+'</span></a>'; finalstring = '<a class="btn btn-default btn-sm caosdb-resolvable-reference" href="' + serverstring + property.value + '"><span class="caosdb-id">'+property.value+'</span></a>';
valueelement.innerHTML = finalstring; valueelement.innerHTML = finalstring;
preview.init(); preview.init();
} }
...@@ -362,9 +373,14 @@ function setProperty(element, property) { ...@@ -362,9 +373,14 @@ function setProperty(element, property) {
* @param element The element holding the entity. * @param element The element holding the entity.
* @param property_name The name of the property. * @param property_name The name of the property.
* @return The value of the the property with property_name. * @return The value of the the property with property_name.
* This function returns undefined when this property is not available for this entity.
*/ */
function getProperty(element, property_name) { function getProperty(element, property_name) {
return getProperties(element).filter(el => el.name == property_name)[0].value; var props = getProperties(element).filter(el => el.name == property_name);
if (props.length == 0) {
return undefined;
}
return props[0].value;
} }
/** /**
...@@ -403,9 +419,9 @@ function appendParent(doc, element, parent) { ...@@ -403,9 +419,9 @@ function appendParent(doc, element, parent) {
*/ */
function appendValueNode(doc, element, name, value) { function appendValueNode(doc, element, name, value) {
let el = doc.createElement(name); let el = doc.createElement(name);
let valel = doc.createTextNode(property.value); let valel = doc.createTextNode(value);
el.appendChild(valel); el.appendChild(valel);
property.appendChild(el); element.appendChild(el);
} }
/** /**
...@@ -415,9 +431,12 @@ function appendValueNode(doc, element, name, value) { ...@@ -415,9 +431,12 @@ function appendValueNode(doc, element, name, value) {
* @param element The element to append to. * @param element The element to append to.
* @param property An object specifying a property. * @param property An object specifying a property.
*/ */
function appendProperty(doc, element, property) { function appendProperty(doc, element, property, append_datatype=false) {
var propertyElement = doc.createElement("Property"); var propertyElement = doc.createElement("Property");
setNameID(propertyElement, property); setNameID(propertyElement, property);
if (append_datatype == true) {
propertyElement.setAttribute("datatype", property.datatype);
}
if (!(property.value === undefined)) { if (!(property.value === undefined)) {
if (("list" in property && property.list) || property.value instanceof Array) { if (("list" in property && property.list) || property.value instanceof Array) {
...@@ -451,7 +470,8 @@ function appendProperty(doc, element, property) { ...@@ -451,7 +470,8 @@ function appendProperty(doc, element, property) {
* @return A document holding the newly created entity. * @return A document holding the newly created entity.
* *
*/ */
function createEntityXML(role, name, id, properties, parents) { function createEntityXML(role, name, id, properties, parents,
append_datatypes=false) {
var doc = document.implementation.createDocument(null, role, null); var doc = document.implementation.createDocument(null, role, null);
var nelnode = doc.children[0] var nelnode = doc.children[0]
setNameID(nelnode, {name: name, id: id}); setNameID(nelnode, {name: name, id: id});
...@@ -464,7 +484,7 @@ function createEntityXML(role, name, id, properties, parents) { ...@@ -464,7 +484,7 @@ function createEntityXML(role, name, id, properties, parents) {
if (!(properties === undefined)) { if (!(properties === undefined)) {
for (var i=0; i<properties.length; i++) { for (var i=0; i<properties.length; i++) {
appendProperty(doc, nelnode, properties[i]); appendProperty(doc, nelnode, properties[i], append_datatypes);
} }
} }
return doc; return doc;
......
...@@ -291,13 +291,14 @@ ...@@ -291,13 +291,14 @@
<xsl:choose> <xsl:choose>
<xsl:when test="$reference='true' and $value!=''"> <xsl:when test="$reference='true' and $value!=''">
<!-- this is a reference --> <!-- this is a reference -->
<a class="btn btn-default btn-sm"> <a class="btn btn-default btn-sm caosdb-resolvable-reference">
<xsl:attribute name="href"> <xsl:attribute name="href">
<xsl:value-of select="concat($entitypath,$value)" /> <xsl:value-of select="concat($entitypath,$value)" />
</xsl:attribute> </xsl:attribute>
<span class="caosdb-id caosdb-id-button caosdb-resolvable-reference"> <span class="caosdb-id caosdb-id-button" style="margin:5px;background-color:orange;display:none;">
<xsl:value-of select="$value" /> <xsl:value-of select="$value" />
</span> </span>
<span class="caosdb-resolve-reference-target" />
</a> </a>
</xsl:when> </xsl:when>
<xsl:when test="$boolean='true'"> <xsl:when test="$boolean='true'">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment