-
Piotr Gawron authoredPiotr Gawron authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Panel.js 11.55 KiB
"use strict";
/* exported logger */
var GuiConnector = require('../GuiConnector');
var AbstractGuiElement = require('./AbstractGuiElement');
var PanelControlElementType = require('./PanelControlElementType');
var Functions = require('../Functions');
var logger = require('../logger');
function Panel(params) {
AbstractGuiElement.call(this, params);
var self = this;
self.setPanelName(params.panelName);
self.setElement(params.element);
self.setMap(params.customMap);
self._controlElements = [];
}
Panel.prototype = Object.create(AbstractGuiElement.prototype);
Panel.prototype.constructor = Panel;
Panel.prototype.disablePanel = function(message) {
var self = this;
var searchQueryElement = self.getControlElement(PanelControlElementType.SEARCH_DIV);
var searchResultsElement = self.getControlElement(PanelControlElementType.SEARCH_RESULTS_DIV);
searchQueryElement.style.visibility = "hidden";
searchResultsElement.style.visibility = "hidden";
var hideReasonDiv = document.createElement("div");
hideReasonDiv.className = "searchPanel";
var center = document.createElement("center");
var messageDiv = document.createElement("h4");
messageDiv.innerHTML = message;
center.appendChild(messageDiv);
hideReasonDiv.appendChild(center);
self.getElement().insertBefore(hideReasonDiv, searchQueryElement);
};
Panel.prototype.createLabel = function(value) {
var result = document.createElement("span");
result.innerHTML = value;
result.className = "searchDescriptionLabel";
return result;
};
Panel.prototype.createPostTranslationalModifications = function(label, value) {
var result = document.createElement("div");
if (value !== undefined) {
throw new Error("Not implemented");
}
return result;
};
Panel.prototype.createCandidates = function(label, value) {
var result = document.createElement("div");
if (value !== undefined) {
throw new Error("Not implemented");
}
return result;
};
Panel.prototype.createChebiTree = function(label, value) {
var result = document.createElement("div");
if (value !== undefined) {
throw new Error("Not implemented");
}
return result;
};
Panel.prototype.createSeparator = function() {
var result = document.createElement("hr");
return result;
};
Panel.prototype.createNewLine = function(count) {
var result = document.createElement("p");
if (count > 0) {
result.style.height = ((count - 1) * 10) + "px";
}
return result;
};
Panel.prototype.createLink = function(url, name) {
var link = document.createElement("a");
link.href = url;
link.innerHTML = name;
link.style.textDecoration = "underline";
return link;
};
Panel.prototype.createAnnotationLink = function(element, showType) {
var name, type, hint;
if (element.title !== undefined) {
hint = element.title + " " + element.authors.join(", ") + ", " + element.year + ", " + element.journal;
type = "PUBMED";
name = element.id;
} else {
name = element.name;
type = element.type;
}
var link;
if (showType) {
link = this.createLink(element.link, type + " (" + name + ")");
} else {
link = this.createLink(element.link, name);
}
if (hint !== undefined) {
var div = document.createElement("div");
div.title = hint;
div.appendChild(link);
return div;
} else {
return link;
}
};
Panel.prototype.createAnnotations = function(label, value, options) {
var showType = true;
var inline = false;
if (options !== undefined) {
if (options.showType !== undefined) {
showType = options.showType;
}
if (options.inline !== undefined) {
inline = options.inline;
}
}
var result = document.createElement("div");
if (value !== undefined && value.length > 0) {
var self = this;
result.appendChild(self.createLabel(label));
if (!inline) {
result.appendChild(self.createNewLine());
}
for (var i = 0; i < value.length; i++) {
var element = value[i];
var link = this.createAnnotationLink(element, showType);
if (inline) {
if (i > 0) {
var coma = document.createElement("span");
coma.innerHTML = ", ";
result.appendChild(coma);
}
result.appendChild(link);
} else {
var row = document.createElement("div");
row.style.height = "26px";
if (i % 2 === 0) {
row.className = "annotationRowOdd";
} else {
row.className = "annotationRowEven";
}
var header = document.createElement("div");
header.style.width = "24px";
header.style.float = "left";
header.innerHTML = "[" + (i + 1) + "]";
row.appendChild(header);
var body = document.createElement("div");
body.style.float = "left";
body.appendChild(link);
row.appendChild(body);
result.appendChild(row);
}
}
}
return result;
};
Panel.prototype.setMap = function(map) {
this._map = map;
};
Panel.prototype.getMap = function() {
return this._map;
};
Panel.prototype.setPanelName = function(panelName) {
this._panelName = panelName;
};
Panel.prototype.getPanelName = function() {
return this._panelName;
};
Panel.prototype.setElement = function(element) {
if (element === undefined || element === null) {
throw new Error("DOM Element must be defined");
}
this._element = element;
};
Panel.prototype.getElement = function() {
return this._element;
};
Panel.prototype.createLabelText = function(value) {
var result = document.createElement("span");
if (value !== undefined) {
result.innerHTML = value;
}
return result;
};
Panel.prototype.createInputText = function(value) {
var result = document.createElement("input");
result.setAttribute('type', 'text');
if (value !== undefined) {
result.setAttribute('value', value);
}
return result;
};
Panel.prototype.createTextArea = function(value) {
var result = document.createElement("textarea");
if (value !== undefined) {
result.setAttribute('value', value);
result.innerHTML = value;
}
return result;
};
Panel.prototype.createFileButton = function() {
var result = document.createElement("input");
result.setAttribute('type', 'file');
return result;
};
Panel.prototype.createParamLine = function(label, value) {
var result = document.createElement("div");
if (value !== undefined) {
var self = this;
result.appendChild(self.createLabel(label));
result.appendChild(self.createLabelText(value));
result.appendChild(self.createNewLine());
}
return result;
};
Panel.prototype.createIcon = function(icon) {
var result = document.createElement("div");
if (icon !== undefined && icon !== null) {
var img = document.createElement("img");
img.src = GuiConnector.getImgPrefix() + icon;
img.style.float = "left";
img.hspace = "5";
result.appendChild(img);
}
return result;
};
Panel.prototype.createArrayParamLine = function(label, value) {
var result = document.createElement("div");
if (value !== undefined && value.length > 0) {
var self = this;
result.appendChild(self.createLabel(label));
result.appendChild(self.createLabelText(value.join(",")));
result.appendChild(self.createNewLine());
}
return result;
};
Panel.prototype.createSubMapLink = function(label, element) {
var self = this;
var result = document.createElement("div");
if (element !== undefined) {
var button = document.createElement("button");
button.text = element.getModelId();
button.onclick = function() {
return self.getMap().openSubmodel(element.getModelId());
};
result.appendChild(this.createLabel("Submodel: "));
result.appendChild(button);
}
return result;
};
Panel.prototype.downloadFile = function(url) {
this._downloadFile = url;
window.open(url, '_blank');
};
Panel.prototype.getLastDownloadUrl = function() {
return this._downloadFile;
};
Panel.prototype.getElementByName = function(element, name) {
if (element !== undefined) {
if (element.getAttribute("name") === name) {
return element;
}
var children = element.children;
for (var i = 0; i < children.length; i++) {
var child = children[i];
var res = this.getElementByName(child, name);
if (res !== undefined) {
return res;
}
}
}
return undefined;
};
Panel.prototype.getDialogDiv = function(id) {
var dialogs = this.getElementByName(this.getElement(), "dialogs");
if (dialogs === undefined) {
dialogs = document.createElement("div");
dialogs.setAttribute("name", "dialogs");
this.getElement().appendChild(dialogs);
this._dialogs = [];
}
var dialogDiv = this._dialogs[id];
if (dialogDiv === undefined) {
dialogDiv = document.createElement("div");
dialogDiv.className = "ui-widget";
dialogDiv.setAttribute("name", "dialog-" + id);
var contentDiv = document.createElement("div");
contentDiv.setAttribute("name", "content");
dialogDiv.appendChild(contentDiv);
dialogs.appendChild(dialogDiv);
this._dialogs[id] = dialogDiv;
}
return dialogDiv;
};
Panel.prototype.assignDialogOptions = function(div, params) {
var dialog = $(div);
for ( var key in params) {
if (params.hasOwnProperty(key)) {
if (key === "id") {
div.setAttribute("name", "dialog-" + params[key]);
} else if (key === "modal") {
dialog.dialog('option', 'modal', params[key]);
} else if (key === "buttons") {
dialog.dialog('option', 'buttons', params[key]);
} else {
throw new Error("Unknown dialog param: " + key + " - " + params[key]);
}
}
}
};
Panel.prototype.openDialog = function(content, options) {
if (options === undefined) {
options = {};
}
if (options.id === undefined) {
logger.warn("Id of dialog is not defined");
}
var div = this.getDialogDiv(options.id);
var contentDiv = this.getElementByName(div, "content");
while (contentDiv.hasChildNodes()) {
contentDiv.removeChild(contentDiv.lastChild);
}
contentDiv.appendChild(content);
contentDiv.style.display = "block";
$(div).dialog({
close : function() {
contentDiv.style.display = "none";
$(this).dialog('destroy');
}
});
this.assignDialogOptions(div, options);
$(div).dialog("open");
};
Panel.prototype.setControlElement = function(type, element) {
if (type === null || type === undefined) {
throw new Error("Unknown controle element type");
}
if (PanelControlElementType[type] === undefined) {
throw new Error("Unknown controle element type: " + type);
}
this._controlElements[type] = element;
};
Panel.prototype.getControlElement = function(type) {
if (type === null || type === undefined) {
throw new Error("Unknown controle element type");
}
if (PanelControlElementType[type] === undefined) {
throw new Error("Unknown controle element type: " + type);
}
return this._controlElements[type];
};
Panel.prototype.createTableRow = function(elements) {
var row = Functions.createElement({
type : "div",
style : "display: table-row;"
});
for (var i = 0; i < elements.length; i++) {
var cell = Functions.createElement({
type : "div",
style : "display: table-cell;"
});
cell.appendChild(elements[i]);
row.appendChild(cell);
}
return row;
}
module.exports = Panel;