Skip to content

Commit 63bcfd0

Browse files
committed
Major code refactor to improve code clarity and fixed some bugs
1 parent 7110487 commit 63bcfd0

11 files changed

Lines changed: 500 additions & 425 deletions

File tree

src/index.html

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,14 @@ <h5 style="color: var(--color-h1); margin:0">
417417
</div>
418418

419419
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
420-
<script src="js/jszip.min.js" type="text/javascript"></script>
421-
<script src="js/qrious.min.js" type="text/javascript"></script>
422-
<script src="js/crypto-js.js" type="text/javascript"></script>
423-
<script src="js/peerjs.min.js" type="text/javascript"></script>
424-
<script src="js/webrtc/turn.js" type="text/javascript"></script>
425-
<script src="js/webrtc/user.js" type="text/javascript"></script>
426-
<script src="js/webrtc/file.js" type="text/javascript"></script>
427-
<script src="js/script.js" type="text/javascript"></script>
420+
<script src="js/vendors/jszip.min.js" type="text/javascript"></script>
421+
<script src="js/vendors/qrious.min.js" type="text/javascript"></script>
422+
<script src="js/vendors/crypto-js.js" type="text/javascript"></script>
423+
<script src="js/vendors/peerjs.min.js" type="text/javascript"></script>
424+
<script type="module" src="js/modules/dom.js" type="text/javascript"></script>
425+
<script type="module" src="js/modules/script.js" type="text/javascript"></script>
426+
<script type="module" src="js/modules/webrtc/user.js" type="text/javascript"></script>
427+
<script type="module" src="js/modules/webrtc/file.js" type="text/javascript"></script>
428+
<script type="module" src="js/modules/script.js" type="text/javascript"></script>
428429
</body>
429430
</html>

src/js/modules/dom.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
export const dom = {
2+
// TOP BAR
3+
theme_text: document.getElementById('theme-text'),
4+
about_text: document.getElementById('about-text'),
5+
6+
// ABOUT
7+
about_div: document.getElementById('about-div'),
8+
comic_img: document.getElementById('comic-img'),
9+
10+
// ERROR
11+
error_div: document.getElementById('error-div'),
12+
error_message: document.getElementById('error-message'),
13+
14+
// PASSWORD
15+
password_div: document.getElementById('password-div'),
16+
password_alert: document.getElementById('password-alert'),
17+
password_input: document.getElementById('password-input'),
18+
password_hide: document.getElementById('password-hide'),
19+
password_show: document.getElementById('password-show'),
20+
password_error: document.getElementById('password-error'),
21+
password_submit: document.getElementById('password-submit'),
22+
password_loading: document.getElementById('password-loading'),
23+
24+
// CONNECT
25+
connect_div: document.getElementById('connect-div'),
26+
27+
// TRANSFER
28+
transfer_div: document.getElementById('transfer-div'),
29+
30+
transfer_qr_code: document.getElementById('transfer-qr-code'),
31+
transfer_status_protected: document.getElementById('transfer-status-protected'),
32+
transfer_status_wait: document.getElementById('transfer-status-wait'),
33+
transfer_status_success: document.getElementById('transfer-status-success'),
34+
35+
transfer_url_value: document.getElementById('transfer-url-value'),
36+
transfer_url_copy: document.getElementById('transfer-url-copy'),
37+
transfer_url_success: document.getElementById('transfer-url-success'),
38+
39+
transfer_select_file: document.getElementById('transfer-select-file'),
40+
transfer_select_file_input: document.getElementById('transfer-select-file-input'),
41+
transfer_add_password: document.getElementById('transfer-add-password'),
42+
43+
transfer_users_div: document.getElementById('transfer-users-div'),
44+
transfer_users_count: document.getElementById('transfer-users-count'),
45+
transfer_users_list: document.getElementById('transfer-users-list'),
46+
transfer_users_list_host: document.getElementById('transfer-users-list-host'),
47+
transfer_users_list_host_name: document.getElementById('transfer-users-list-host-name'),
48+
49+
transfer_files_div: document.getElementById('transfer-files-div'),
50+
transfer_files_count: document.getElementById('transfer-files-count'),
51+
transfer_files_download: document.getElementById('transfer-files-download'),
52+
transfer_files_list: document.getElementById('transfer-files-list'),
53+
transfer_files_list_empty: document.getElementById('transfer-files-list-empty'),
54+
55+
// PASSWORD MODAL
56+
password_modal: document.getElementById('password-modal'),
57+
password_modal_value: document.getElementById('password-modal-value'),
58+
59+
// NAME MODAL
60+
name_modal: document.getElementById('name-modal'),
61+
name_modal_value: document.getElementById('name-modal-value'),
62+
name_modal_error: document.getElementById('name-modal-error'),
63+
64+
// FILE MODAL
65+
file_modal: document.getElementById('file-modal'),
66+
file_modal_table: document.getElementById('file-modal-table'),
67+
file_modal_table_empty: document.getElementById('file-modal-table-empty'),
68+
file_modal_refresh: document.getElementById('file-modal-refresh'),
69+
70+
// DOWNLOAD MODAL
71+
download_modal: document.getElementById('download-modal'),
72+
download_modal_value: document.getElementById('download-modal-value'),
73+
download_modal_active: document.getElementById('download-modal-active'),
74+
download_modal_success: document.getElementById('download-modal-success'),
75+
download_modal_error: document.getElementById('download-modal-error'),
76+
download_modal_cancel: document.getElementById('download-modal-cancel'),
77+
download_modal_cancel_spinner: document.getElementById('download-modal-cancel-spinner'),
78+
download_modal_close: document.getElementById('download-modal-close'),
79+
80+
// NOTIFICATION
81+
notification_modal: document.getElementById('notification-modal'),
82+
notification_modal_value: document.getElementById('notification-modal-value'),
83+
}
84+
85+
// Display a confirmation dialog when the user attempts to refresh or navigate away from the page.
86+
window.addEventListener("beforeunload", (event) => {
87+
event.preventDefault();
88+
});
89+
90+
// Focus the password input after the fade animation
91+
dom.password_modal.addEventListener('shown.bs.modal', () => {
92+
dom.password_modal_value.focus()
93+
});
94+
95+
// Focus the name input after the fade animation
96+
dom.name_modal.addEventListener('shown.bs.modal', () => {
97+
dom.name_modal_value.focus()
98+
});

src/js/modules/script.js

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
import { dom } from './dom.js';
2+
import { User } from './webrtc/user.js';
3+
4+
// Room ID
5+
const room_id = window.location.pathname.substring(1) // window.location.search ? window.location.search.substring(4) : ''
6+
7+
// Store current user
8+
var user;
9+
10+
// QR Code
11+
var qr = new QRious({
12+
element: document.getElementById('transfer-qr-code'),
13+
background: 'transparent',
14+
size: 220,
15+
foreground: '#adb5db',
16+
level: 'H',
17+
})
18+
19+
// Get theme mode
20+
if (window.localStorage.getItem('mode') == 'light') {
21+
dom.theme_text.innerHTML = 'Light'
22+
dom.comic_img.src = "assets/comic.png"
23+
qr.set({foreground: '#212529'});
24+
}
25+
26+
// On Load
27+
async function onLoad() {
28+
// Create current user
29+
user = new User(room_id)
30+
31+
// Host
32+
if (room_id.length == 0) {
33+
// Generate Room ID
34+
const new_room_id = generateRoomID()
35+
36+
// Init UI Components
37+
dom.transfer_div.style.display = 'block'
38+
dom.transfer_url_value.innerHTML = `${window.location.origin}/${new_room_id}` // `${window.location.origin}${window.location.pathname}?id=${new_room_id}`
39+
dom.transfer_users_list_host_name.innerHTML = user.name + ' (You)'
40+
dom.transfer_users_count.innerHTML = ' (1)'
41+
dom.transfer_add_password.style.display = 'block';
42+
qr.set({value: dom.transfer_url_value.innerHTML});
43+
44+
// Init peer connection
45+
await user.init(new_room_id)
46+
}
47+
// Peer
48+
else {
49+
// Init UI Componente
50+
dom.connect_div.style.display = 'block'
51+
dom.transfer_url_value.innerHTML = `${window.location.origin}/${room_id}` // `${window.location.origin}${window.location.pathname}?id=${room_id}`
52+
qr.set({value: dom.transfer_url_value.innerHTML});
53+
54+
// Init peer connection
55+
await user.init()
56+
57+
// Connect to the room
58+
await user.connect(room_id)
59+
}
60+
}
61+
62+
// Theme
63+
function themeClick() {
64+
if (dom.theme_text.innerHTML == 'Dark') {
65+
dom.theme_text.innerHTML = 'Light'
66+
document.documentElement.classList.remove("dark")
67+
document.documentElement.classList.add("light")
68+
document.documentElement.setAttribute('data-bs-theme', 'light')
69+
window.localStorage.setItem('mode', 'light')
70+
dom.comic_img.src = "assets/comic.png"
71+
qr.set({foreground: '#212529'});
72+
}
73+
else if (dom.theme_text.innerHTML == 'Light') {
74+
dom.theme_text.innerHTML = 'Dark'
75+
document.documentElement.classList.remove("light")
76+
document.documentElement.classList.add("dark")
77+
document.documentElement.setAttribute('data-bs-theme', 'dark')
78+
window.localStorage.setItem('mode', 'dark')
79+
dom.comic_img.src = "assets/comic-dark.png"
80+
qr.set({foreground: '#adb5db'});
81+
}
82+
}
83+
window.themeClick = themeClick;
84+
85+
// About
86+
function aboutClick() {
87+
if (dom.about_text.innerHTML == 'About') {
88+
dom.transfer_div.style.display = 'none'
89+
dom.about_div.style.display = 'block'
90+
dom.about_text.innerHTML = 'Go back'
91+
}
92+
else {
93+
dom.about_div.style.display = 'none'
94+
dom.about_text.innerHTML = 'About'
95+
dom.transfer_div.style.display = 'block'
96+
}
97+
}
98+
window.aboutClick = aboutClick;
99+
100+
function addPassword() {
101+
const modal = new bootstrap.Modal(dom.password_modal)
102+
modal.show()
103+
}
104+
window.addPassword = addPassword;
105+
106+
// Show / Hide password
107+
function togglePasswordVisibility(input_name, button_show_name, button_hide_name) {
108+
var password_input = document.getElementById(input_name)
109+
var password_button_show = document.getElementById(button_show_name)
110+
var password_button_hide = document.getElementById(button_hide_name)
111+
if (password_input.type === "password") {
112+
password_input.type = "text"
113+
password_button_show.style.display = 'block'
114+
password_button_hide.style.display = 'none'
115+
} else {
116+
password_input.type = "password"
117+
password_button_show.style.display = 'none'
118+
password_button_hide.style.display = 'block'
119+
}
120+
password_input.focus()
121+
}
122+
window.togglePasswordVisibility = togglePasswordVisibility;
123+
124+
// Confirm change password
125+
function addPasswordSubmit() {
126+
user.password = dom.password_modal_value.value.trim()
127+
const modal = bootstrap.Modal.getInstance(dom.password_modal);
128+
modal.hide()
129+
dom.transfer_status_protected.style.display = user.password.length == 0 ? 'none' : 'inline-block'
130+
}
131+
window.addPasswordSubmit = addPasswordSubmit;
132+
133+
function connectWithPassword() {
134+
if (dom.password_input.value.trim().length == 0) {
135+
dom.password_error.style.display = 'block'
136+
}
137+
else {
138+
user.password = dom.password_input.value
139+
dom.password_error.style.display = 'none'
140+
dom.password_submit.setAttribute("disabled", "")
141+
dom.password_loading.style.display = 'inline-block'
142+
user.connect(room_id)
143+
}
144+
}
145+
window.connectWithPassword = connectWithPassword;
146+
147+
// Change name
148+
function changeName() {
149+
dom.name_modal_value.value = ''
150+
dom.name_modal_error.style.display = 'none'
151+
152+
const modal = new bootstrap.Modal(dom.name_modal)
153+
modal.show()
154+
}
155+
window.changeName = changeName;
156+
157+
function changeNameSubmit() {
158+
// Update name
159+
user.changeName(dom.name_modal_value.value.trim())
160+
}
161+
window.changeNameSubmit = changeNameSubmit;
162+
163+
// Copy Room url
164+
function copyURL() {
165+
const url = dom.transfer_url_value.innerHTML;
166+
navigator.clipboard.writeText(url)
167+
168+
const modal = new bootstrap.Modal(dom.notification_modal)
169+
dom.notification_modal_value.innerHTML = "URL copied."
170+
dom.transfer_url_copy.style.display = 'none'
171+
dom.transfer_url_success.style.display = 'flex'
172+
modal.show()
173+
174+
setTimeout(() => {
175+
dom.transfer_url_success.style.display = 'none'
176+
dom.transfer_url_copy.style.display = 'flex'
177+
modal.hide()
178+
}, 1000)
179+
}
180+
window.copyURL = copyURL;
181+
182+
// Send File
183+
function sendFiles(event) {
184+
user.addFiles(event.files)
185+
}
186+
window.sendFiles = sendFiles;
187+
188+
// Remove file
189+
function removeFile(fileId) {
190+
user.removeFile(fileId)
191+
}
192+
window.removeFile = removeFile;
193+
194+
// Download File
195+
function downloadFile(id) {
196+
user.downloadFile(id)
197+
}
198+
window.downloadFile = downloadFile;
199+
200+
// Abort File (Stop file download)
201+
function abortFile(fileId) {
202+
user.abortFile(fileId)
203+
}
204+
window.abortFile = abortFile;
205+
206+
// See details
207+
function showFileDetails(fileId) {
208+
user.showFileDetails(fileId)
209+
}
210+
window.showFileDetails = showFileDetails;
211+
212+
// Download all
213+
function downloadAll() {
214+
user.downloadAll()
215+
}
216+
window.downloadAll = downloadAll;
217+
218+
// Cancel download all
219+
function cancelDownloadAll() {
220+
user.downloadAllCancel()
221+
}
222+
window.cancelDownloadAll = cancelDownloadAll;
223+
224+
// Function to generate a random string in the format XXX-XXXX-XXX.
225+
function generateRoomID() {
226+
const length = 10
227+
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
228+
const random = crypto.getRandomValues(new Uint8Array(length));
229+
let room_id = "";
230+
for (let i = 0; i < length; i++) {
231+
room_id += alphabet[random[i] % alphabet.length];
232+
}
233+
return `${room_id.slice(0, 3)}-${room_id.slice(3, 7)}-${room_id.slice(7, 10)}`;
234+
}
235+
236+
// On document loaded, execute onLoad() method.
237+
(() => onLoad())();

0 commit comments

Comments
 (0)