-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
141 lines (126 loc) · 4.38 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const dialogConfig = {
title: `Select your Prism Launcher directory (usually ${electron.env.USERPROFILE}\\AppData\\Roaming\\PrismLauncher)`,
buttonLabel: 'Yep this is the one, choose this one',
properties: ['openDirectory'],
};
let chooseFolder = document.getElementById('chooseFolder');
let folderChosen = document.getElementById('folderChosen');
let info = document.getElementById('info');
let modpackSelector = document.getElementById('modpackSelector');
let modpackVersionSelector = document.getElementById('modpackVersionSelector');
let downloadBtn = document.getElementById('downloadBtn');
let overlayModal = document.querySelector('[data-modal]');
window.onload = function () {
modpackSelector.disabled = true;
modpackVersionSelector.disabled = true;
downloadBtn.classList.add('disabled');
// make overlayModal unclosable
overlayModal.addEventListener('cancel', function (event) {
event.preventDefault();
});
main();
};
function main() {
chooseFolder.addEventListener('click', function () {
electron.openDialog('showOpenDialog', dialogConfig).then((result) => {
console.log(result);
if (result.canceled) return;
electron.checkPathForInstances(result.filePaths[0]).then((valid) => {
if (valid) {
info.innerHTML = 'Valid Prism Launcher directory selected!';
info.style.color = 'green';
modpackSelector.disabled = false;
} else {
info.innerHTML =
'Invalid Prism Launcher directory selected! (Does not contain a "instances" folder)';
info.style.color = 'red';
modpackSelector.disabled = true;
modpackVersionSelector.disabled = true;
downloadBtn.classList.add('disabled');
}
});
folderChosen.innerHTML = result.filePaths[0];
});
});
document.getElementById('checkForUpdatesBtn').addEventListener('click', function () {
electron.checkForUpdates();
});
electron.prismLauncherExists.then((exists) => {
if (exists) {
folderChosen.innerHTML = `${electron.env.USERPROFILE}\\AppData\\Roaming\\PrismLauncher`;
chooseFolder.disabled = true;
info.innerHTML = 'Prism Launcher automatically detected!';
info.style.color = 'green';
modpackSelector.disabled = false;
}
});
// fill modpackSelector select with options
fetch('https://api.gangnamstyle.dev/geronimo/downloads/modpacks').then(
(response) => {
response.json().then((data) => {
data.forEach((modpack) => {
let option = document.createElement('option');
option.value = modpack;
option.innerHTML = modpack;
modpackSelector.appendChild(option);
});
});
},
);
//! MODPACK SELECTOR
modpackSelector.addEventListener('change', function () {
document.getElementById('infoButton').classList.add('disabled');
downloadBtn.classList.add('disabled');
instanceInfo.innerHTML = '>';
modpackVersionSelector.innerHTML =
'<option hidden disabled selected value> -- select a version -- </option>';
fetch(
`https://api.gangnamstyle.dev/geronimo/downloads/modpacks/${modpackSelector.value}`,
).then((response) => {
response.json().then((data) => {
data
.sort((a, b) =>
b.version.localeCompare(a.version, undefined, {
numeric: true,
sensitivity: 'base',
}),
)
.forEach((modpack) => {
let option = document.createElement('option');
option.value = modpack.version;
option.innerHTML = modpack.version;
modpackVersionSelector.appendChild(option);
});
});
});
modpackVersionSelector.disabled = false;
});
//! VERSION SELECTOR
modpackVersionSelector.addEventListener('change', function () {
let instanceInfo = document.getElementById('instanceInfo');
fetch(
`https://api.gangnamstyle.dev/geronimo/downloads/modpacks/${modpackSelector.value}/${modpackVersionSelector.value}`,
).then((response) => {
response.json().then((data) => {
electron.checkInstancesForModpack(data.modpack).then((exists) => {
if (exists) {
instanceInfo.innerHTML = `"${data.modpack}" found! Ready to update?`;
instanceInfo.style.color = 'green';
} else {
instanceInfo.innerHTML = `"${data.modpack}" not found! Ready to download?`;
instanceInfo.style.color = 'red';
}
downloadBtn.classList.remove('disabled');
downloadBtn.addEventListener('click', function () {
overlayModal.showModal();
electron.downloadModpack(
modpackSelector.value,
modpackVersionSelector.value,
exists,
);
});
});
});
});
});
}