// Use this file to configure custom decorators for your Consul-UI var httpRegexp = new RegExp('^http[s]?://[^ ]+$'); function url_decorator(key, value) { var e = document.createElement('a'); e.setAttribute('href', value); e.appendChild(document.createTextNode(value)); return e; } function usefullLinksGenerator(instance, serviceName, node_meta_info) { var top = document.createElement('div'); top.className = 'instance-links'; var usefullLinks = [ ]; var first = true; for (let usefullLink of usefullLinks) { link = document.createElement('a'); link.href = usefullLink.href; if (typeof usefullLink.iconClassName !== 'undefined') { icon = document.createElement('i'); icon.className = usefullLink.iconClassName; link.appendChild(icon); link.appendChild(document.createTextNode("\u00A0")); } if (!first) { top.appendChild(document.createTextNode(' | ')); } else { first = false; } link.appendChild(document.createTextNode(usefullLink.title)); top.appendChild(link); } return top; } /** * createNodeDisplayElement resolves and displays the node name. */ function createNodeDisplayElement(nodeName, _nodemeta, _instanceFqdn) { return document.createTextNode(nodeName); } /** * serviceInstanceDecorator is called to decorate an instance. */ function serviceInstanceDecorator(instance, element, serviceName, node_meta_info) { for (let child of element.children) { if (child.className && child.className.includes("instance-meta")) { var instanceMetaChild = child; } } element.insertBefore(usefullLinksGenerator(instance, serviceName, node_meta_info), instanceMetaChild); element.insertBefore(document.createElement("hr"), instanceMetaChild); return element; } /** * serviceMetaDecorator must return a HTML node to decorate a service instance meta. * It should return the decorated element. */ function serviceMetaDecorator(instance, key, value, serviceName, node_meta_info) { if (httpRegexp.test(value)) { return url_decorator(key, value); } else { return service_meta_semantics_decorator(instance, key, value, serviceName); } } function build_link(href, value) { var e = document.createElement('a'); e.setAttribute('href', href); e.appendChild(document.createTextNode(value)); return e; } function default_decorator(instance, key, value, serviceName) { return document.createTextNode(value); } function groups_decorator(instance, key, value, serviceName) { var values = value.split(','); var span = document.createElement('span'); var first = true; for (var i in values) { if (!first) { span.appendChild(document.createTextNode(', ')); } else { first = false; } var grp = values[i]; if (grp.indexOf('.') != -1) { var x = document.createElement('span'); x.setAttribute('class', 'badge badge-warning'); x.appendChild(document.createTextNode(grp)); span.appendChild(x); } else { var a = document.createElement('a'); a.target = 'asapi'; a.appendChild(document.createTextNode(grp)); a.href = asapi_url + encodeURIComponent(grp); span.appendChild(a); } } return span; } function decorateIsoDate(value, formated) { const parsed = Date.parse(formated); if (isNaN(parsed)) { return document.createTextNode(value); } const d = new Date(parsed); const e = document.createElement('time'); e.setAttribute('datetime', formated); e.setAttribute('title', value); e.appendChild(document.createTextNode(d.toLocaleString())); return e; } // This is the list of function called // When a service meta is found var registered_decorators = { } function service_meta_semantics_decorator(instance, key, value, serviceName) { var fun = registered_decorators[key]; if (fun == null) { fun = default_decorator; } return fun(instance, key, value); } const node_meta_decorators = { 'os': function(k, v, nodeMetaTags, serviceMetaIfAny) { var e = document.createElement('span'); e.setAttribute('class', 'badge badge-light mx-1 lookup'); e.appendChild(document.createTextNode(k + ':' + v + ' ')); var i = document.createElement('i'); i.setAttribute('class', 'fab fa-' + v); e.appendChild(i); return e; } } function default_node_meta_decorator(k, value, nodeMetaTags, serviceMetaIfAny) { if (httpRegexp.test(value)) { var e = document.createElement('a'); e.setAttribute('href', value); e.setAttribute('class', 'lookup'); e.appendChild(document.createTextNode(k + ':'+ value)); var metaTag = document.createElement('span'); metaTag.setAttribute('class', 'badge badge-secondary mx-1'); metaTag.appendChild(e); return metaTag; } else { var metaTag = document.createElement('span'); metaTag.setAttribute('class', 'badge badge-primary mx-1 lookup'); metaTag.appendChild(document.createTextNode(k + ':' + value)); return metaTag; } } function find_decorator(k, v) { const nd = node_meta_decorators[k]; if (nd != null) { return nd; } else { return default_node_meta_decorator; } } function nodeMetaGenerator(nodeMetaTags, serviceMetaIfAny) { var allMeta = document.createElement('div'); var metaTags = document.createElement('div'); metaTags.setAttribute('title', 'Node Meta') metaTags.className = 'node-meta'; for (var tagKey in nodeMetaTags) { const tagValue = nodeMetaTags[tagKey]; if (tagValue == ''){ continue; } const decorator = find_decorator(tagKey, tagValue); var clazz = 'badge badge-primary mx-1 lookup'; const elem = decorator(tagKey, tagValue, nodeMetaTags, serviceMetaIfAny); metaTags.appendChild(elem); } allMeta.appendChild(metaTags); return allMeta; } /** * navBarDecorator is called to modify to modify naviguation bar of all UI pages. * it receives the nav bar div * it does not have to return anything. */ function navBarDecorator(navbar) { } /** * fetchedResponseDecorator is called with http response when a resource is fetched by any instance of ConsulUIManager * it does not have to return anything. */ async function fetchedResponseDecorator(httpResponse) { }