393 lines
15 KiB
JavaScript
393 lines
15 KiB
JavaScript
var repoindex = (function(repoindex) {
|
|
|
|
repoindex.parseHash = function(hash) {
|
|
if(hash.length > 0 && hash[0] === "#") {
|
|
hash = hash.substr(1);
|
|
}
|
|
var parts = hash.split("?", 2);
|
|
var params;
|
|
if(parts.length > 1) {
|
|
params = {};
|
|
var subParts = parts[1].split("&");
|
|
for(var i = 0; i < subParts.length; ++i) {
|
|
var pair = subParts[i].split("=", 2);
|
|
if(pair.length > 1) {
|
|
params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
|
|
}
|
|
}
|
|
}
|
|
return [parts.length > 0 ? decodeURIComponent(parts[0]) : "", params];
|
|
};
|
|
|
|
repoindex.makeHash = function(pageName, params, numberSign) {
|
|
var pairs = [];
|
|
for(var prop in params) {
|
|
if(params.hasOwnProperty(prop)) {
|
|
var val = params[prop];
|
|
if(val) {
|
|
pairs.push([encodeURIComponent(prop), encodeURIComponent(val)].join("="));
|
|
}
|
|
}
|
|
}
|
|
var res = [(numberSign ? "#" : "") + encodeURIComponent(pageName)];
|
|
if(pairs.length > 0) {
|
|
res.push(pairs.join("&"));
|
|
}
|
|
return res.join("?");
|
|
};
|
|
|
|
repoindex.escapeHtml = function(unsafe) {
|
|
return unsafe
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/"/g, """)
|
|
.replace(/'/g, "'");
|
|
};
|
|
|
|
repoindex.getElement = function(id) {
|
|
return typeof id === "string" ? document.getElementById(id) : id;
|
|
};
|
|
|
|
repoindex.pkgNamesFromDeps = function(dependencyInfos) {
|
|
if(dependencyInfos) {
|
|
var names = new Array(dependencyInfos.length);
|
|
for(var i = 0, end = dependencyInfos.length; i < end; ++i) {
|
|
names[i] = dependencyInfos[i].name;
|
|
}
|
|
return names;
|
|
} else {
|
|
return [];
|
|
}
|
|
};
|
|
|
|
repoindex.pack = function(value) {
|
|
return value ? (Array.isArray(value) ? value : [value]) : [];
|
|
};
|
|
|
|
repoindex.setText = function(id, text, htmlTags) {
|
|
var element = typeof id === "string" ? document.getElementById(id) : id;
|
|
if(htmlTags) {
|
|
element.innerHTML = text;
|
|
} else {
|
|
element.wipeChildren();
|
|
element.appendChild(document.createTextNode(text));
|
|
}
|
|
};
|
|
|
|
repoindex.setLink = function(id, text, href, action, tooltip) {
|
|
var element = typeof id === "string" ? document.getElementById(id) : id;
|
|
element.wipeChildren();
|
|
var linkElement = document.createElement("a");
|
|
linkElement.href = href;
|
|
if(tooltip) {
|
|
linkElement.title = tooltip;
|
|
}
|
|
if(action) {
|
|
linkElement.onclick = function() {
|
|
action(this.href);
|
|
return false;
|
|
};
|
|
}
|
|
linkElement.appendChild(document.createTextNode(text));
|
|
element.appendChild(linkElement);
|
|
};
|
|
|
|
repoindex.setDownloadButton = function(id, text, href, action, iconName) {
|
|
if(!iconName) {
|
|
iconName = "download";
|
|
}
|
|
var element = typeof id === "string" ? document.getElementById(id) : id;
|
|
element.wipeChildren();
|
|
var linkElement = document.createElement("a");
|
|
linkElement.href = href;
|
|
if(action) {
|
|
linkElement.onclick = function() {
|
|
action(this.href);
|
|
return false;
|
|
};
|
|
}
|
|
var buttonElement = document.createElement("button");
|
|
buttonElement.setAttribute("type", "button");
|
|
buttonElement.className = "btn btn-default btn-xs";
|
|
var spanElement = document.createElement("span");
|
|
spanElement.className = "glyphicon glyphicon-" + iconName;
|
|
spanElement.style.padding.right = "2px";
|
|
buttonElement.appendChild(spanElement);
|
|
buttonElement.appendChild(document.createTextNode(text));
|
|
linkElement.appendChild(buttonElement);
|
|
element.appendChild(linkElement);
|
|
};
|
|
|
|
repoindex.setPackageNames = function(id, packageNames, pageName) {
|
|
var element = typeof id === "string" ? document.getElementById(id) : id;
|
|
element.wipeChildren();
|
|
if(Array.isArray(packageNames)) {
|
|
if(packageNames.length > 0) {
|
|
if(!pageName) {
|
|
pageName = repoindex.Pages.Packages;
|
|
}
|
|
for(var i = 0; i < packageNames.length; ++i) {
|
|
var link = document.createElement("a");
|
|
link.appendChild(document.createTextNode(packageNames[i]));
|
|
link.href = repoindex.makeHash(pageName, {}, true);
|
|
link.onclick = function() {
|
|
repoindex.pageManager.setPage(pageName, {});
|
|
repoindex.pageManager.applySearchTerm(this.firstChild.data, true);
|
|
return false;
|
|
};
|
|
link.className = "package-link";
|
|
element.appendChild(link);
|
|
element.appendChild(document.createTextNode(" "));
|
|
}
|
|
} else {
|
|
element.appendChild(document.createTextNode("none"));
|
|
}
|
|
} else {
|
|
element.appendChild(document.createTextNode("invalid"));
|
|
}
|
|
};
|
|
|
|
repoindex.makeTree = function(list) {
|
|
var root = [];
|
|
if(Array.isArray(list) && list.length) {
|
|
for(var i1 = 0; i1 < list.length; ++i1) {
|
|
var element = list[i1];
|
|
if(typeof element.name === "string") {
|
|
var parent = root;
|
|
var parts = element.name.split("/");
|
|
for(var i2 = 0; i2 < parts.length; ++i2) {
|
|
var part = parts[i2];
|
|
if(part !== "") {
|
|
var node = null;
|
|
for(var i3 = 0; i3 < parent.length; ++i3) {
|
|
if(parent[i3].name === part) {
|
|
node = parent[i3];
|
|
break;
|
|
}
|
|
}
|
|
if(!node) {
|
|
node = {
|
|
name: part,
|
|
children: []
|
|
};
|
|
parent.push(node);
|
|
}
|
|
parent = node.children;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return root;
|
|
};
|
|
|
|
repoindex.setTree = function(id, nodes) {
|
|
var element = repoindex.getElement(id);
|
|
element.wipeChildren();
|
|
element.className = "file-tree";
|
|
// has root?
|
|
if(Array.isArray(nodes) && nodes.length) {
|
|
var makeNode = function(parentElement, nodes) {
|
|
var ulElement = document.createElement("ul");
|
|
for(var i = 0; i < nodes.length; ++i) {
|
|
var node = nodes[i];
|
|
if(typeof node.name === "string") {
|
|
var liElement = document.createElement("li");
|
|
// is parent?
|
|
if(Array.isArray(node.children) && node.children.length) {
|
|
// make child nodes
|
|
var spanElement = document.createElement("span");
|
|
spanElement.subUlElement = makeNode(liElement, node.children);
|
|
spanElement.className = "tree-link-collapsed";
|
|
spanElement.onclick = function() {
|
|
var expand = this.subUlElement.style.display === "none";
|
|
this.className = expand ? "tree-link-expanded" : "tree-link-collapsed";
|
|
this.subUlElement.style.display = expand ? "block" : "none";
|
|
};
|
|
spanElement.onclick(); // hide sub ul element in the first place
|
|
spanElement.appendChild(document.createTextNode(node.name));
|
|
liElement.insertBefore(spanElement, spanElement.subUlElement);
|
|
} else {
|
|
liElement.appendChild(document.createTextNode(node.name));
|
|
}
|
|
ulElement.appendChild(liElement);
|
|
}
|
|
}
|
|
parentElement.appendChild(ulElement);
|
|
return ulElement;
|
|
};
|
|
// make root
|
|
makeNode(element, nodes);
|
|
} else {
|
|
element.appendChild(document.createTextNode("unknown"));
|
|
}
|
|
};
|
|
|
|
repoindex.makeStr = function(str, na) {
|
|
return str ? ((typeof str === "string" || typeof str === "number") ? str : "invalid") : (na ? na : "unknown");
|
|
};
|
|
|
|
repoindex.makeArray = function(array, separator) {
|
|
return array ? (Array.isArray(array) ? (array.length ? array.join(separator ? separator : " ") : "none") : "invalid") : "unknown";
|
|
};
|
|
|
|
repoindex.makeDataSize = function(sizeInByte) {
|
|
if(sizeInByte) {
|
|
if(typeof sizeInByte === "number") {
|
|
if (sizeInByte < 1024) {
|
|
return sizeInByte + " bytes";
|
|
} else if (sizeInByte < 1048576) {
|
|
return (sizeInByte / 1024.0).toFixed(2) + " KiB";
|
|
} else if (sizeInByte < 1073741824) {
|
|
return (sizeInByte / 1048576.0).toFixed(2) + " MiB";
|
|
} else if (sizeInByte < 1099511627776) {
|
|
return (sizeInByte / 1073741824.0).toFixed(2) + " GiB";
|
|
} else {
|
|
return (sizeInByte / 1099511627776.0).toFixed(2) + " TiB";
|
|
}
|
|
} else {
|
|
return "invalid";
|
|
}
|
|
} else {
|
|
return "unknown";
|
|
}
|
|
};
|
|
|
|
repoindex.makeBool = function(bool) {
|
|
return bool ? "yes" : "no";
|
|
};
|
|
|
|
repoindex.makeQuandityInfo = function(elements) {
|
|
var infos = [];
|
|
for(var i = 0; i < elements.length; ++i) {
|
|
var element = elements[i];
|
|
if(element.count > 0) {
|
|
infos.push((element.style ? "<span style=\"" + element.style + "\"><strong>" : "<strong>")
|
|
+ element.count
|
|
+ "</strong> "
|
|
+ (element.count === 1 ? element.name : element.namePlural)
|
|
+ (element.style ? "</span>" : ""));
|
|
}
|
|
}
|
|
if(infos.length > 0) {
|
|
var last = infos.pop();
|
|
if(infos.length > 0) {
|
|
return infos.join(", ") + " and " + last;
|
|
} else {
|
|
return last;
|
|
}
|
|
}
|
|
}
|
|
|
|
repoindex.makeId = function() {
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
return v.toString(16);
|
|
});
|
|
};
|
|
|
|
repoindex.makeElementId = function(someName) {
|
|
if(someName) {
|
|
return "id_" + someName.replace(/\+/g, "--plus--")
|
|
.replace(/\?/g, "--question--")
|
|
.replace(/\\/g, "--bslash--")
|
|
.replace(/\//g, "--slash--")
|
|
.replace(/[\[\]\{\}\(\)]/g, "--bracket--")
|
|
.replace(/\./g, "--point--")
|
|
.replace(/\*/g, "--star--")
|
|
.replace(/[^0-1,a-z,A-Z,]/g, "--other--");
|
|
} else {
|
|
return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);
|
|
}
|
|
};
|
|
|
|
repoindex.addField = function(element, fieldName, value) {
|
|
var nameElement = document.createElement("th");
|
|
nameElement.appendChild(document.createTextNode(fieldName));
|
|
var valueElement = document.createElement("td");
|
|
if(value) {
|
|
valueElement.appendChild(document.createTextNode(value));
|
|
}
|
|
var trElement = document.createElement("tr");
|
|
trElement.appendChild(nameElement);
|
|
trElement.appendChild(valueElement);
|
|
element.appendChild(trElement);
|
|
return valueElement;
|
|
};
|
|
|
|
repoindex.addPackageNames = function(element, fieldName, packageNames, pageName) {
|
|
repoindex.setPackageNames(repoindex.addField(element, fieldName), packageNames, pageName);
|
|
};
|
|
|
|
repoindex.makeCloseButton = function() {
|
|
var closeButton = document.createElement("button");
|
|
closeButton.setAttribute("type", "button");
|
|
closeButton.setAttribute("class", "close");
|
|
closeButton.setAttribute("aria-label", "Close");
|
|
var closeButtonSpan = document.createElement("span");
|
|
closeButtonSpan.setAttribute("aria-hidden", "true");
|
|
closeButtonSpan.appendChild(document.createTextNode("\u00D7"));
|
|
closeButton.appendChild(closeButtonSpan);
|
|
return closeButton;
|
|
};
|
|
|
|
repoindex.makeInfoPanel = function(headingText, closeFunc) {
|
|
var panelElement = document.createElement("div");
|
|
panelElement.className = "panel panel-default info-container";
|
|
var panelHeadingElement = document.createElement("div");
|
|
panelHeadingElement.className = "panel-heading";
|
|
panelHeadingElement.appendChild(document.createTextNode(headingText));
|
|
var closeButton = repoindex.makeCloseButton();
|
|
closeButton.onclick = function() {
|
|
if(panelElement.parentNode) {
|
|
panelElement.parentNode.removeChild(panelElement);
|
|
if(closeFunc) {
|
|
closeFunc();
|
|
}
|
|
}
|
|
};
|
|
panelHeadingElement.appendChild(closeButton);
|
|
panelElement.appendChild(panelHeadingElement);
|
|
var panelBodyElement = document.createElement("div");
|
|
panelBodyElement.className = "panel-body";
|
|
panelElement.appendChild(panelBodyElement);
|
|
return {
|
|
element: panelElement,
|
|
headingElement: panelHeadingElement,
|
|
bodyElement: panelBodyElement
|
|
};
|
|
};
|
|
|
|
repoindex.makeColumnGroup = function(columnWidths) {
|
|
var colgroupElement = document.createElement("colgroup");
|
|
for(var i = 0; i < columnWidths.length; ++i) {
|
|
var colElement = document.createElement("col");
|
|
colElement.setAttribute("style", "width:" + columnWidths[i]);
|
|
colgroupElement.appendChild(colElement);
|
|
}
|
|
return colgroupElement;
|
|
};
|
|
|
|
repoindex.makeInfoTable = function() {
|
|
var tableElement = document.createElement("table");
|
|
tableElement.className = "info-table";
|
|
tableElement.appendChild(repoindex.makeColumnGroup(["30%", "70%"]));
|
|
var tbodyElement = document.createElement("tbody");
|
|
tableElement.appendChild(tbodyElement);
|
|
return {
|
|
tableElement: tableElement,
|
|
tbodyElement: tbodyElement
|
|
};
|
|
};
|
|
|
|
repoindex.bind = function(obj, func, args) {
|
|
return function() {
|
|
func.call(obj, args);
|
|
}
|
|
}
|
|
|
|
return repoindex;
|
|
|
|
})(repoindex || {});
|