2024-07-12 21:11:52 -06:00
const { invoke } = window . _ _TAURI _ _ . tauri ;
const { listen } = window . _ _TAURI _ _ . event ;
2024-09-28 09:12:45 -06:00
const { ask , open , message } = window . _ _TAURI _ _ . dialog ;
2024-07-12 21:11:52 -06:00
const downBar = document . querySelector ( ".progressFinished" ) ;
//import { listen } from '@tauri-apps/api';
2024-09-28 09:12:45 -06:00
const error = listen ( "Error" , ( error ) => {
message ( error . payload , { title : "Error" , type : "error" } ) ;
} ) ;
2024-07-13 17:00:08 -06:00
var selected _pack = "" ;
2024-07-13 22:26:03 -06:00
var update _menu = document . getElementById ( "update" ) ;
var create _menu = document . getElementById ( "create" ) ;
2024-07-13 17:00:08 -06:00
2024-07-12 21:11:52 -06:00
const download _progress = listen ( "download_progress" , ( progress ) => {
console . log ( "Downloading" ) ;
//console.log("Downloaded "+progress.payload.downloaded/(1024*1024) +"MB / " + progress.payload.total/(1024*1024) + "MB");
let downProgress = ( progress . payload . downloaded / ( 1024 * 1024 ) ) . toFixed ( 2 ) ;
let downTotal = ( progress . payload . total / ( 1024 * 1024 ) ) . toFixed ( 2 ) ;
downBar . style . width = ` ${ ( progress . payload . downloaded / progress . payload . total ) * 100 } % ` ;
document . querySelector ( ".progress" ) . style . visibility = "visible" ;
} ) ;
const download _finished = listen ( "download_finished" , ( event ) => {
downBar . style . width = 0 ;
document . querySelector ( ".progress" ) . style . visibility = "hidden" ;
} ) ;
window . addEventListener ( "DOMContentLoaded" , ( ) => {
2024-07-13 22:26:03 -06:00
document . getElementById ( "browse" ) . addEventListener ( "click" , browse ) ;
document . getElementById ( "update_pack" ) . addEventListener ( "click" , update _pack ) ;
2024-07-13 17:00:08 -06:00
document . getElementById ( "up" ) . addEventListener ( "click" , up ) ;
document . getElementById ( "down" ) . addEventListener ( "click" , down ) ;
2024-07-13 22:26:03 -06:00
document . getElementById ( "add" ) . addEventListener ( "click" , add ) ;
document . getElementById ( "remove" ) . addEventListener ( "click" , remove ) ;
2024-07-12 21:11:52 -06:00
document . getElementById ( "back" ) . addEventListener ( "click" , back ) ;
} ) ;
2024-07-13 17:00:08 -06:00
window . onload = async function ( ) {
refresh ( ) ;
}
function up ( ) {
invoke ( "shift_up" , { id : selected _pack } ) . then ( refresh ) ;
}
function down ( ) {
invoke ( "shift_down" , { id : selected _pack } ) . then ( refresh ) ;
}
2024-07-12 21:11:52 -06:00
function back ( ) {
2024-07-13 17:00:08 -06:00
invoke ( "drop_session" ) ;
2024-07-12 21:11:52 -06:00
window . location . href = "index.html" ;
2024-07-13 17:00:08 -06:00
}
function refresh ( ) {
2024-07-13 22:26:03 -06:00
update _menu . style . display = "none" ;
create _menu . style . display = "none" ;
2024-07-13 17:00:08 -06:00
invoke ( "get_modpacks" ) . then ( addModpacks ) ;
}
function addModpacks ( modpacks ) {
var modpacks _list = document . getElementById ( "modpacks" ) ;
while ( modpacks _list . firstChild ) {
modpacks _list . removeChild ( modpacks _list . lastChild ) ;
}
for ( let i = 0 ; i < modpacks . length ; i ++ ) {
var div = document . createElement ( "div" ) ;
div . textContent = modpacks [ i ] . name ;
div . className = "modpack" ;
div . id = modpacks [ i ] . id ;
if ( modpacks [ i ] . id == selected _pack ) {
div . classList . add ( "modpack-selected" ) ;
2024-07-13 22:26:03 -06:00
update _menu . style . display = "flex" ;
invoke ( "get_latest_version" , { id : selected _pack } ) . then ( update _version ) ;
2024-07-13 17:00:08 -06:00
}
div . addEventListener ( "click" , function ( ) { modpackClick ( modpacks [ i ] . id ) } ) ;
modpacks _list . appendChild ( div ) ;
}
2024-07-13 22:26:03 -06:00
var div = document . createElement ( "div" ) ;
div . textContent = "<Create New Pack>" ;
div . className = "modpack" ;
div . id = "*new*" ;
div . addEventListener ( "click" , function ( ) { modpackClick ( "*new*" ) } ) ;
modpacks _list . appendChild ( div ) ;
2024-07-13 17:00:08 -06:00
}
function modpackClick ( id ) {
var old = selected _pack ;
document . getElementById ( id ) . classList . add ( "modpack-selected" ) ;
selected _pack = id ;
2024-07-13 22:26:03 -06:00
if ( old == id ) {
selected _pack = "" ;
update _menu . style . display = "none" ;
create _menu . style . display = "none" ;
} else if ( id == "*new*" ) {
update _menu . style . display = "none" ;
create _menu . style . display = "flex" ;
} else {
update _menu . style . display = "flex" ;
create _menu . style . display = "none" ;
invoke ( "get_latest_version" , { id : selected _pack } ) . then ( update _version ) ;
}
2024-07-13 17:00:08 -06:00
document . getElementById ( old ) . classList . remove ( "modpack-selected" ) ;
2024-07-13 22:26:03 -06:00
}
function add ( ) {
var id = document . getElementById ( "pack_id" ) . value ;
var name = document . getElementById ( "pack_name" ) . value ;
selected _pack = id ;
invoke ( "add_pack" , { id : id , name : name } ) . then ( refresh ) ;
}
function remove ( ) {
2024-07-15 11:08:31 -06:00
ask ( "Are you sure you want to remove " + document . getElementById ( selected _pack ) . textContent + "?" , { title : "Are you sure?" , type : "Message" } ) . then ( ( value ) => { if ( value ) { invoke ( "remove_pack" , { id : selected _pack } ) . then ( refresh ) ; } } ) ;
2024-07-13 22:26:03 -06:00
}
async function browse ( ) {
const selected = await open ( {
multiple : false ,
filters : [ {
name : 'Modrinth Modpack' ,
extensions : [ 'mrpack' ]
} ]
} ) ;
if ( selected != null ) {
document . getElementById ( "file_path" ) . value = selected ;
}
}
function update _version ( version ) {
document . getElementById ( "pack_version" ) . value = version ;
}
function update _pack ( ) {
var version = document . getElementById ( "pack_version" ) . value ;
var path = document . getElementById ( "file_path" ) . value ;
invoke ( "update_pack" , { id : selected _pack , path : path , version : version } ) . then ( refresh ) ;
2024-07-12 21:11:52 -06:00
}