Skip to content

Commit

Permalink
hide some table elements on mobile, dynamic ui for /build, expose db …
Browse files Browse the repository at this point in the history
…id in api
  • Loading branch information
cowpod committed Nov 13, 2024
1 parent a71f089 commit 985aa04
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 187 deletions.
4 changes: 3 additions & 1 deletion api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@
$modq = $db->query("SELECT * FROM mods");
if ($modq) {
foreach ($modq as $mod) {
if ($mod['type']!='mod') continue;
$filesize = isset($mod['filesize']) ? $mod['filesize'] : 0;
$modentry = [
'id'=>$mod['id'],
'pretty_name'=>$mod['pretty_name'],
'name'=>$mod['name'],
'version'=>$mod['version'],
Expand All @@ -86,7 +88,7 @@
'filesize'=>$filesize
];

array_push($modslist,$modentry);
array_push($modslist, $modentry);
}
}
echo json_encode($modslist, JSON_UNESCAPED_SLASHES);
Expand Down
11 changes: 0 additions & 11 deletions functions/change_show_all.php

This file was deleted.

159 changes: 42 additions & 117 deletions index.php

Large diffs are not rendered by default.

38 changes: 35 additions & 3 deletions resources/js/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ function compareVersions(version1, version2) {
return 0; // Versions are equal
}

function isVersionInInterval(version, interval) {
// console.log(version,interval)
// Remove whitespace
interval = interval.replace(/\s+/g, '');

// Check interval boundaries for inclusivity/exclusivity
const startInclusive = interval[0] === '[';
const endInclusive = interval[interval.length - 1] === ']';

// Extract version bounds
const [startVersion, endVersion] = interval.slice(1, -1).split(',');

// Use compareVersions to check if version is within the range
const compareStart = compareVersions(version, startVersion);
const compareEnd = compareVersions(version, endVersion);

const inLowerBound = startInclusive ? compareStart >= 0 : compareStart > 0;
const inUpperBound = endInclusive ? compareEnd <= 0 : compareEnd < 0;

return inLowerBound && inUpperBound;
}

function slugify (str) {
str = str.replace(/^\s+|\s+$/g, '');
str = str.toLowerCase();
Expand Down Expand Up @@ -56,10 +78,13 @@ function isLocalStorageAvailable(){
}
}
function get_cached(key) {
if (havelocalstorage==null) {
havelocalstorage=isLocalStorageAvailable()
}
if (havelocalstorage) {
if (key in localStorage) {
tv = JSON.parse(localStorage[key]);
if (Math.round(Date.now() / 1000) < tv[0]) {
if (tv[0]==-1 || Math.round(Date.now() / 1000) < tv[0]) {
return tv[1];
} else {
localStorage.removeItem(key)
Expand All @@ -74,10 +99,17 @@ function get_cached(key) {
}
}
}
function set_cached(key,value) {
function set_cached(key,value,ttl) {
if (havelocalstorage==null) {
havelocalstorage=isLocalStorageAvailable()
}
// cache for 30 mins
if (havelocalstorage) {
timestamp = Math.round(Date.now() / 1000)+1800;
if (ttl==Infinity||ttl==-1) {
var timestamp = -1;
} else {
var timestamp = Math.round(Date.now() / 1000)+ttl;
}
val=JSON.stringify([timestamp, value]);
localStorage[key]=val
return value;
Expand Down
136 changes: 100 additions & 36 deletions resources/js/page_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,67 +59,129 @@ function add_o(id) {
request.open("GET", "./functions/add-mod.php?bid="+build_id+"&id="+id);
request.send();
}
function add(name, v, mcv) {
function add(name, id, v, mcv) {
if ($("#versionselect-"+name+' option:selected').attr('missing')=='true') {
return;
}
if ($("#versionselect-"+name).val()==null) {
return;
}
$("#btn-add-mod-"+name).attr("disabled", true);
$("#versionselect-"+name).attr("disabled", true);
$("#cog-"+name).show();
let id = $("#versionselect-"+name).val();
$("#btn-add-mod-"+name).attr("disabled", true);
// $('#btn-add-mod-'+name).html('<em class="fas fa-cog fa-spin">');

var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
if (request.responseText=="Mod added") {
// $("#mod-add-row-"+name).remove();
$("#cog-"+name).hide();
$("#check-"+name).show();
$("#mod-add-row-"+name).remove();
// $('#btn-add-mod-'+name).html('<em class="text-success fas fa-check"></em>');

// remember to modify index.php/build too
$("#mods-in-build").append(`
<tr id="mod-${id}">
<td scope="row" data-value="${name}">${name}</td>
<td data-value="${v}">${v}</td>
<td data-value="${mcv}">${mcv}</td>
<td data-value="${mcv}" class="d-none d-sm-table-cell">${mcv}</td>
<td>
<button onclick="remove_mod(${id})" class="btn btn-danger">
<svg class="svg-inline--fa fa-times fa-w-11" aria-hidden="true" data-prefix="fas" data-icon="times" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 352 512" data-fa-i2svg=""><path fill="currentColor" d="M242.72 256l100.07-100.07c12.28-12.28 12.28-32.19 0-44.48l-22.24-22.24c-12.28-12.28-32.19-12.28-44.48 0L176 189.28 75.93 89.21c-12.28-12.28-32.19-12.28-44.48 0L9.21 111.45c-12.28 12.28-12.28 32.19 0 44.48L109.28 256 9.21 356.07c-12.28 12.28-12.28 32.19 0 44.48l22.24 22.24c12.28 12.28 32.2 12.28 44.48 0L176 322.72l100.07 100.07c12.28 12.28 32.2 12.28 44.48 0l22.24-22.24c12.28-12.28 12.28-32.19 0-44.48L242.72 256z"></path></svg><!-- <em class="fas fa-times"></em> -->
<em class="fas fa-times"></em>
</button>
</td>
<td>
<svg style="font-size: 2em;display: none;" class="svg-inline--fa fa-cog fa-w-16 fa-spin" id="spinner-jei" aria-hidden="true" data-prefix="fas" data-icon="cog" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg=""><path fill="currentColor" d="M444.788 291.1l42.616 24.599c4.867 2.809 7.126 8.618 5.459 13.985-11.07 35.642-29.97 67.842-54.689 94.586a12.016 12.016 0 0 1-14.832 2.254l-42.584-24.595a191.577 191.577 0 0 1-60.759 35.13v49.182a12.01 12.01 0 0 1-9.377 11.718c-34.956 7.85-72.499 8.256-109.219.007-5.49-1.233-9.403-6.096-9.403-11.723v-49.184a191.555 191.555 0 0 1-60.759-35.13l-42.584 24.595a12.016 12.016 0 0 1-14.832-2.254c-24.718-26.744-43.619-58.944-54.689-94.586-1.667-5.366.592-11.175 5.459-13.985L67.212 291.1a193.48 193.48 0 0 1 0-70.199l-42.616-24.599c-4.867-2.809-7.126-8.618-5.459-13.985 11.07-35.642 29.97-67.842 54.689-94.586a12.016 12.016 0 0 1 14.832-2.254l42.584 24.595a191.577 191.577 0 0 1 60.759-35.13V25.759a12.01 12.01 0 0 1 9.377-11.718c34.956-7.85 72.499-8.256 109.219-.007 5.49 1.233 9.403 6.096 9.403 11.723v49.184a191.555 191.555 0 0 1 60.759 35.13l42.584-24.595a12.016 12.016 0 0 1 14.832 2.254c24.718 26.744 43.619 58.944 54.689 94.586 1.667 5.366-.592 11.175-5.459 13.985L444.788 220.9a193.485 193.485 0 0 1 0 70.2zM336 256c0-44.112-35.888-80-80-80s-80 35.888-80 80 35.888 80 80 80 80-35.888 80-80z"></path></svg>
</td>
</tr>
`);
} else {
$("#cog-"+name).hide();
$("#times-"+name).show();
// $('#btn-add-mod-'+name).html('Add to build');
}
}
};
request.open("GET", "./functions/add-mod.php?bid="+build_id+"&id="+$("#versionselect-"+name).val());
console.log("./functions/add-mod.php?bid="+build_id+"&id="+id)
request.open("GET", "./functions/add-mod.php?bid="+build_id+"&id="+id);
request.send();
}
$("#search").on('keyup',function(){
tr = document.getElementById("modstable").getElementsByTagName("tr");

for (var i = 0; i < tr.length; i++) {
function add_mod_row(id,pretty_name,name,vs,mcv) {
if (pretty_name=='' || name=='' || versions=='' || mcv=='') {
var addbutton=`<a id="btn-add-mod-${name}" href="mod?id=${name}" class="btn btn-warning">Issue(s)</a>`;
} else {
var addbutton=`<a id="btn-add-mod-${name}" onclick="add('${name}', '${id}', '${vs}','${mcv}')" class="btn btn-primary">Add to build</a>`;
}

var vs_str = ``;
for (let v of vs) {
vs_str += `<option>${v}</option>`;
}

td = tr[i].getElementsByTagName("td")[0];
if (td) {
$('#build-available-mods').append(`
<tr id="mod-add-row-${name}">
<td scope="row" data-value="${pretty_name}">${pretty_name}</td>
<td data-value="${vs}"><select id="versionselect-${name}" class="form-control">${vs_str}</select></td>
<td data-value="Add to build">
${addbutton}
</td>
</tr>
`);
}

console.log(td);
console.log(td.innerHTML.toUpperCase())
if (td.innerHTML.toUpperCase().indexOf($("#search").val().toUpperCase()) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
function parsemods(obj) {
let showall = $('#showall').is(':checked');
let added_num=0;

let vs={};
for (let mod of obj) {
if (mod['name'] in vs) {
vs[mod['name']].push(mod['version']);
} else {
vs[mod['name']]=[mod['version']];
}
}
let filter=$("#search").val();
for (let mod of obj) {
if (filter=='' || filter==undefined || mod['pretty_name'].toLowerCase().includes(filter)||mod['name'].toLowerCase().includes(filter)) {
if (mod['mcversion']=='' || mcv==mod['mcversion'] || isVersionInInterval(`'${mcv}'`, mod['mcversion']) || showall) {
add_mod_row(mod['id'], mod['pretty_name'],mod['name'],vs[mod['name']],mcv);
added_num+=1;
}
}
}
if (added_num==0) {
if (filter!='') {
$('#no-mods-available-search-results').show();
} else {
$('#no-mods-available').show();
}
}
}

async function getmods() {
return new Promise((resolve, reject) => {
$('#build-available-mods').empty();
$('#no-mods-available').hide();
$('#no-mods-available-search-results').hide();
if (get_cached('available_mods')) {
parsemods(JSON.parse(get_cached('available_mods')));
} else {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
set_cached('available_mods',request.responseText,30);
parsemods(JSON.parse(request.responseText));
resolve(true)
}
}
request.onerror = function() {
console.log('could not get mods from api');
reject(false)
}
request.open("GET", 'api/mod');
request.send();
}
});
}

$("#search").on('keyup', async function(){
await getmods(); // get mods on type

});
$("#search2").on('keyup',function(){
tr = document.getElementById("filestable").getElementsByTagName("tr");
Expand All @@ -140,18 +202,15 @@ $("#search2").on('keyup',function(){
}
});
$('#showall').change(function() {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
window.location.reload();
}
};
if ( $('#showall').is(':checked') ) {
request.open("GET", "./functions/change_show_all.php?showall=true");
if ($('#showall').is(':checked')) {
$('#mods-for-version-string').text(' for Minecraft '+mcv);
$('#mods-for-version-string').show();
} else {
request.open("GET", "./functions/change_show_all.php?showall=false");
$('#mods-for-version-string').hide();
}
request.send();

set_cached('showall', $('#showall').is(':checked'), -1);
getmods(mcv, type);
});

$(document).on('change', '.form-control', function() {
Expand All @@ -169,4 +228,9 @@ $(document).on('change', '.form-control', function() {
}
console.log('#btn-add-mod-'+modid);
}
});

$(document).ready(function() {
$('#showall').prop('checked', get_cached('showall'));
getmods();
});
29 changes: 14 additions & 15 deletions resources/js/page_lib-mods.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function sendFile(file, i) {
$('#table-available-mods').append(`
<tr id="mod-row-${name[i]}">
<td scope="row" data-value="${pretty_name[i]}">${pretty_name[i]}</td>
<td data-value="${author[i]}">${author[i]}</td>
<td data-value="${author[i]}" class="d-none d-sm-table-cell">${author[i]}</td>
<td data-value="${num_versions}">${num_versions}</td>
<td>
<div class="btn-group btn-group-sm" role="group" aria-label="Actions">
Expand Down Expand Up @@ -189,9 +189,9 @@ function addrow(hit) {
var row = `
<tr>
<td data-value='${hit['title']}'>${hit['title']}</td>
<td data-value='str${categories}...' style='overflow-wrap: break-word;' onclick="showcategories('${hit['slug']}')"><a href="javascript:void(0)"onclick="showcategories('${hit['slug']}')" style="word-wrap: break-all">${categories}</a></td>
<td data-value='str${categories}...' class="d-none d-md-table-cell" style='overflow-wrap: break-word;' onclick="showcategories('${hit['slug']}')"><a href="javascript:void(0)"onclick="showcategories('${hit['slug']}')" style="word-wrap: break-all">${categories}</a></td>
<td data-value='${description}' style='overflow-wrap: break-word;' onclick="getdescription('${hit['slug']}')"><a href="javascript:void(0)" onclick="getdescription('${hit['slug']}')" style="word-wrap: break-all">${description}</a></td>
<td data-value='${author}'>${author}</td>
<td data-value='${author}' class="d-none d-md-table-cell">${author}</td>
<td>${btn}</td>
</tr>
`;
Expand All @@ -202,7 +202,7 @@ function getdescription(id) {
if (id in details) {
console.log('got cached details');
showdetails(id);
} else if (havelocalstorage && 'details_'+id in localStorage) {
} else if (get_cached('details_'+id)) {
console.log('got cached details from localstorage');
details[id]=JSON.parse(localStorage['details_'+id]);
showdetails(id);
Expand All @@ -216,9 +216,7 @@ function getdescription(id) {
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
let obj = JSON.parse(request.responseText);
if (havelocalstorage && !(id in localStorage)) {
localStorage['details_'+id]=request.responseText;
}
set_cached('details_'+id, request.responseText,1800);
details[id]=obj;
console.log('got new description');
showdetails(id);
Expand Down Expand Up @@ -280,7 +278,7 @@ async function getversions(id) {
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
let obj = JSON.parse(request.responseText);
set_cached('versions_'+id, request.responseText);
set_cached('versions_'+id, request.responseText, 1800);
console.log('got new versions for id='+id);
versions[id] = obj;

Expand Down Expand Up @@ -348,13 +346,19 @@ function installmod() {

console.log(url);

let mcv = $('#mcv option:selected').attr('mc');
let type = $('#mcv option:selected').attr('type');

var request = new XMLHttpRequest();
var postdata=new FormData();
postdata.append('url',url);
if (url.endsWith('.jar')) {
var filename=url.substring(url.lastIndexOf('/') + 1);
postdata.append('filename', filename);
}
postdata.append('fallback_mcversion', mcv);
// postdata.append('fallback_type', type);

request.open("POST", "./functions/send_mods.php", true);
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
Expand Down Expand Up @@ -402,7 +406,7 @@ function installmod() {
$('#table-available-mods').append(`
<tr id="mod-row-${name[i]}">
<td scope="row" data-value="${pretty_name[i]}">${pretty_name[i]}</td>
<td data-value="${author[i]}">${author[i]}</td>
<td data-value="${author[i]}" class="d-none d-md-table-cell">${author[i]}</td>
<td data-value="${num_versions}">${num_versions}</td>
<td>
<div class="btn-group btn-group-sm" role="group" aria-label="Actions">
Expand Down Expand Up @@ -526,7 +530,7 @@ $('#searchbutton').on('click', async function() {

results[searchquery]=hits;

set_cached(searchquery, hits);
set_cached(searchquery, hits, 1800);

} else {
console.log('no hits');
Expand Down Expand Up @@ -561,9 +565,4 @@ $(document).ready(function() {
installed=fetch_installed()
loop_count+=1
}
let loop_count2=0;
while (havelocalstorage==null && loop_count2 < 3) {
havelocalstorage = isLocalStorageAvailable();
loop_count2+=1
}
});
2 changes: 1 addition & 1 deletion resources/js/page_modloaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ async function chf(link,name,id,mc) {
<tr id="forge-${id}">
<td scope="row" data-value="${mc}">${mc}</td>
<td data-value="${name}">${name}</td>
<td data-value="${link}" style="overflow-wrap: break-word;"><a href="${link}" style="word-break: break-all;">${link}</a></td>
<td data-value="${link}" class="d-none d-md-table-cell" style="overflow-wrap: break-word;"><a href="${link}" style="word-break: break-all;">${link}</a></td>
<td><button id="button-add-${id}" onclick="add(\'${name}\', \'${link}\', \'${mc}', \'${id}\')" class="btn btn-primary btn-sm">Add to Database</button></td>
<td><em id="cog-${id}" style="display:none" class="fas fa-spin fa-cog fa-2x"></em><em id="check-${id}" style="display:none" class="text-success fas fa-check fa-2x"></em><em id="times-${id}" style="display:none" class="text-danger fas fa-times fa-2x"></em></td>
</tr>`);
Expand Down
Loading

0 comments on commit 985aa04

Please sign in to comment.