community-apps/more/more.html
2020-05-31 21:43:12 -04:00

275 lines
12 KiB
HTML

<!DOCTYPE html>
<!--
// more.html
//
// Created by Keb Helion, February 2020.
// Copyright 2020 Vircadia contributors.
//
// App maintained in: https://github.com/kasenvr/community-apps
// App copied to: https://github.com/kasenvr/project-athena
//
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
-->
<html>
<head>
<meta charset="UTF-8">
<script src="jquery-3.5.1.min.js"></script>
<script>
//Defaults
var DEFAULT_PER_PAGE = 3;
var DEFAULT_OFFSET = 0;
//Parameters
function findGetParameter(parameterName) {
var result = null,
tmp = [];
var items = location.search.substr(1).split("&");
for (var index = 0; index < items.length; index++) {
tmp = items[index].split("=");
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
}
return result;
}
var offset = findGetParameter("offset");
if (offset === null) {
offset = DEFAULT_OFFSET;
}
offset = parseInt(offset);
var perpage = findGetParameter("perpage");
if (perpage === null) {
perpage = DEFAULT_PER_PAGE;
}
perpage = parseInt(perpage);
var search = findGetParameter("search");
if (search === null) {
search = "";
}
//Paths
var currentPath = window.location.protocol + "//" + window.location.host + window.location.pathname;
var developmentMode = window.location.toString().split("?")[1];
var rootPath;
var metadataScriptSrc = "https://cdn.vircadia.com/community-apps/applications/metadata.js";
if (developmentMode === "dev") { // Development mode loads locally, if not, load from repo.
console.info("Setting applications to local.")
rootPath = currentPath.replace("more/more.html", "applications/");
console.info("Loading metadata locally.");
metadataScriptSrc = "../applications/metadata.js";
} else {
console.info("Setting applications to remote URL.")
console.info("Loading metadata remotely.");
rootPath = "https://cdn.vircadia.com/community-apps/applications/";
}
//Search
function doSearch(keyword) {
offset = 0;
search = keyword;
listBuilder(keyword, offset, perpage);
}
function clearSearch() {
offset = 0;
document.searchbar.searchtextbox.value = "";
search = "";
listBuilder("", offset, perpage);
}
//Running scripts
var buttonList = [];
function requestRunningScriptData() {
var readyEvent = {
"action": "requestRunningScriptData"
};
EventBridge.emitWebEvent(JSON.stringify(readyEvent));
}
EventBridge.scriptEventReceived.connect(function(message){
//update the buttons
if (message.indexOf("RSL4MOREAPP:") !== -1){
buttonList.forEach(function(item){
var btn = "";
if (message.indexOf(item.url) !== -1) {
//Means already running
btn = "<button class='uninstall' onclick='uninstall(" + '"' + item.url + '"' + ", " + '"' + item.id + '"' + ");'>Uninstall</button>";
} else {
//Means not already installed
btn = "<button class='install' onclick='install(" + '"' + item.url + '"' + ", " + '"' + item.id + '"' + ");'>Install</button>";
}
document.getElementById(item.id).innerHTML = btn;
});
}
});
function install(script, btnId){
var btn = "<button class='processing' >Processing...</button>";
document.getElementById(btnId).innerHTML = btn;
var readyEvent = {
"action": "installScript",
"script": script
};
EventBridge.emitWebEvent(JSON.stringify(readyEvent));
}
function uninstall(script, btnId){
var btn = "<button class='processing' >Processing...</button>";
document.getElementById(btnId).innerHTML = btn;
var readyEvent = {
"action": "uninstallScript",
"script": script
};
EventBridge.emitWebEvent(JSON.stringify(readyEvent));
}
</script>
<link href="https://fonts.googleapis.com/css?family=Merriweather|Quicksand:400,700&display=swap" rel="stylesheet">
<link href="css/styles.css" rel="stylesheet">
</head>
<body><form name = "searchbar" onsubmit="submitForm(event);">
<font class="mainTitle">Add more functionalities...</font><br>
<table style = "width:100%;">
<tr >
<td style = "vertical-align:middle; text-align:left;">
<table>
<tr>
<td style = "vertical-align:middle; text-align:left;">
<div class = "searchbox">
<input class = "searchtextbox" name = "searchtextbox" size = "26" maxlength="32" onkeypress="monitorEnter(event, this);"> <a href="#" onclick='clearSearch();'><img src="del-x-16.png"></a>
</div>
</td>
<td style = "vertical-align:middle; text-align:left;">
<a href = "#" onclick = 'doSearch(document.searchbar.searchtextbox.value);'><img src="search-32.png"></a>
</td>
</tr>
</table>
</td>
<td style = "vertical-align:middle; text-align:right;">
<div id = 'pager_top' style = "vertical-align: middle;"></div>
</td>
</tr>
</table>
</form>
<div id = "data"></div>
<div style="width:98%; text-align:right;">
<div style = "vertical-align: middle;" id = 'pager_footer'></div>
</div>
<hr>
<p class="mainDesc">Want to contribute and add your own app?<br>
Read the <a href="https://cdn.vircadia.com/community-apps/web/index.html">guide</a>!</p>
<script>
function monitorEnter(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
doSearch(document.searchbar.searchtextbox.value);
}
}
function submitForm(event){
event.preventDefault();
}
document.searchbar.searchtextbox.value = search;
var pageContent = "";
function displayApp(item) {
pageContent = pageContent + "<a name = '" + window.btoa(item.directory) + "'><table class='item'><tr>";
pageContent = pageContent + "<td><div class='iconContainer'><img src='" + rootPath + item.icon + "' style='width:50px;'><br><font class = 'caption'>" + item.caption + "</font></div></td>";
var btn = "";
var absoluteJsFile = rootPath + item.jsfile;
var btn = "<button class='install' onclick = 'install(" + '"' + absoluteJsFile + " ," + window.btoa(item.directory) + '"' + ");'>Install</button>";
var btndata = {
"url": absoluteJsFile,
"id": window.btoa(item.directory)
};
buttonList.push(btndata);
pageContent = pageContent + "<td><font class='appname'>" + item.name + "<br></font><font class = 'appdesc'>" + item.description + "<br></font><div id = '"+ window.btoa(item.directory) +"' align='right'>" + btn + "</div></td>";
pageContent = pageContent + "</tr></table><br>";
}
function listBuilder(searchKeyword, listOffset, listPerPage) {
buttonList = [];
var counterDir = -1;
var counterDisp = 0;
var index = 0;
var lowItem = "";
var needNext = false;
pageContent = "";
for (index = 0; index < metadata.applications.length; index++) {
lowItem = metadata.applications[index].name.toLowerCase();
if (lowItem.indexOf(searchKeyword.toLowerCase()) !== -1 && metadata.applications[index].isActive == true) {
counterDir = counterDir + 1;
if ((counterDir >= listOffset) && (counterDir < (listOffset + listPerPage))) {
displayApp(metadata.applications[index]);
counterDisp = counterDisp + 1;
}
if (counterDir >= (listOffset + listPerPage)) {
needNext = true;
break;
}
}
}
var pagerPrevious = "<a href='#' onclick='pagetoPrevious();'><img src='minus-16.png'></a>";
if (listOffset <= 0) {
pagerPrevious = "<img src='blank_minus-16.png'>";
}
var pagerNext = "<a href='#' onclick='pagetoNext();'><img src='plus-16.png'></a>";
if (needNext == false) {
pagerNext = "<img src='blank_plus-16.png'>";
}
var countA = listOffset + 1;
var countB = listOffset + counterDisp;
var pagerHtml = pagerPrevious + "<font class='pager'>&nbsp;&nbsp;" + countA + " - " + countB + "&nbsp;&nbsp;</font>" + pagerNext;
if (counterDisp > 0 ) {
document.getElementById("pager_top").innerHTML = pagerHtml;
document.getElementById("data").innerHTML = pageContent;
document.getElementById("pager_footer").innerHTML = pagerHtml;
} else {
document.getElementById("data").innerHTML = "<hr><div align='center'><font class='noresult'><br><br><br><br>Sorry, no result found.<br><br><br><br><br><br></font></div><hr>";
}
requestRunningScriptData();
}
//pager
function pagetoPrevious(){
offset = offset - perpage;
if (offset < 0) {
offset = 0;
}
listBuilder(search, offset, perpage);
}
function pagetoNext(){
offset = offset + perpage;
listBuilder(search, offset, perpage);
}
$.getScript(metadataScriptSrc, function(data, textStatus, jqxhr) {
listBuilder(search, offset, perpage);
});
</script>
</body>
</html>