Spaces:
Runtime error
Runtime error
Merge pull request #22 from alamin655/alamin655-patch-3
Browse files- public/static/settings.js +36 -10
public/static/settings.js
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
3 |
function toggleAllSelection() {
|
4 |
document
|
5 |
.querySelectorAll('.engine')
|
@@ -10,25 +12,36 @@ function toggleAllSelection() {
|
|
10 |
)
|
11 |
}
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
15 |
function setActiveTab(current_tab) {
|
|
|
16 |
document
|
17 |
.querySelectorAll('.tab')
|
18 |
.forEach((tab) => tab.classList.remove('active'))
|
19 |
document
|
20 |
.querySelectorAll('.btn')
|
21 |
.forEach((tab) => tab.classList.remove('active'))
|
|
|
|
|
22 |
current_tab.classList.add('active')
|
23 |
document
|
24 |
.querySelector(`.${current_tab.innerText.toLowerCase().replace(' ', '_')}`)
|
25 |
.classList.add('active')
|
26 |
}
|
27 |
|
28 |
-
|
29 |
-
|
|
|
|
|
30 |
function setClientSettings() {
|
|
|
31 |
let cookie_dictionary = new Object()
|
|
|
|
|
32 |
document.querySelectorAll('select').forEach((select_tag) => {
|
33 |
if (select_tag.name === 'themes') {
|
34 |
cookie_dictionary['theme'] = select_tag.value
|
@@ -36,6 +49,8 @@ function setClientSettings() {
|
|
36 |
cookie_dictionary['colorscheme'] = select_tag.value
|
37 |
}
|
38 |
})
|
|
|
|
|
39 |
let engines = []
|
40 |
document.querySelectorAll('.engine').forEach((engine_checkbox) => {
|
41 |
if (engine_checkbox.checked === true) {
|
@@ -43,33 +58,44 @@ function setClientSettings() {
|
|
43 |
}
|
44 |
})
|
45 |
cookie_dictionary['engines'] = engines
|
|
|
|
|
46 |
let expiration_date = new Date()
|
47 |
expiration_date.setFullYear(expiration_date.getFullYear() + 1)
|
|
|
|
|
48 |
document.cookie = `appCookie=${JSON.stringify(
|
49 |
cookie_dictionary
|
50 |
)}; expires=${expiration_date.toUTCString()}`
|
51 |
|
|
|
52 |
document.querySelector('.message').innerText =
|
53 |
'β
The settings have been saved sucessfully!!'
|
54 |
|
|
|
55 |
setTimeout(() => {
|
56 |
document.querySelector('.message').innerText = ''
|
57 |
}, 10000)
|
58 |
}
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
64 |
function getClientSettings() {
|
|
|
65 |
let cookie = decodeURIComponent(document.cookie)
|
66 |
|
|
|
67 |
if (cookie !== '') {
|
68 |
let cookie_value = decodeURIComponent(document.cookie)
|
69 |
.split(';')
|
70 |
.map((item) => item.split('='))
|
71 |
.reduce((acc, [_, v]) => (acc = JSON.parse(v)) && acc, {})
|
72 |
|
|
|
73 |
let links = Array.from(document.querySelectorAll('link')).forEach(
|
74 |
(item) => {
|
75 |
if (item.href.includes('static/themes')) {
|
|
|
1 |
+
/**
|
2 |
+
* This function handles the toggling of selections of all upstream search engines
|
3 |
+
* options in the settings page under the tab engines.
|
4 |
+
*/
|
5 |
function toggleAllSelection() {
|
6 |
document
|
7 |
.querySelectorAll('.engine')
|
|
|
12 |
)
|
13 |
}
|
14 |
|
15 |
+
/**
|
16 |
+
* This function adds the functionality to sidebar buttons to only show settings
|
17 |
+
* related to that tab.
|
18 |
+
* @param {HTMLElement} current_tab - The current tab that was clicked.
|
19 |
+
*/
|
20 |
function setActiveTab(current_tab) {
|
21 |
+
// Remove the active class from all tabs and buttons
|
22 |
document
|
23 |
.querySelectorAll('.tab')
|
24 |
.forEach((tab) => tab.classList.remove('active'))
|
25 |
document
|
26 |
.querySelectorAll('.btn')
|
27 |
.forEach((tab) => tab.classList.remove('active'))
|
28 |
+
|
29 |
+
// Add the active class to the current tab and its corresponding settings
|
30 |
current_tab.classList.add('active')
|
31 |
document
|
32 |
.querySelector(`.${current_tab.innerText.toLowerCase().replace(' ', '_')}`)
|
33 |
.classList.add('active')
|
34 |
}
|
35 |
|
36 |
+
/**
|
37 |
+
* This function adds the functionality to save all the user selected preferences
|
38 |
+
* to be saved in a cookie on the users machine.
|
39 |
+
*/
|
40 |
function setClientSettings() {
|
41 |
+
// Create an object to store the user's preferences
|
42 |
let cookie_dictionary = new Object()
|
43 |
+
|
44 |
+
// Loop through all select tags and add their values to the cookie dictionary
|
45 |
document.querySelectorAll('select').forEach((select_tag) => {
|
46 |
if (select_tag.name === 'themes') {
|
47 |
cookie_dictionary['theme'] = select_tag.value
|
|
|
49 |
cookie_dictionary['colorscheme'] = select_tag.value
|
50 |
}
|
51 |
})
|
52 |
+
|
53 |
+
// Loop through all engine checkboxes and add their values to the cookie dictionary
|
54 |
let engines = []
|
55 |
document.querySelectorAll('.engine').forEach((engine_checkbox) => {
|
56 |
if (engine_checkbox.checked === true) {
|
|
|
58 |
}
|
59 |
})
|
60 |
cookie_dictionary['engines'] = engines
|
61 |
+
|
62 |
+
// Set the expiration date for the cookie to 1 year from the current date
|
63 |
let expiration_date = new Date()
|
64 |
expiration_date.setFullYear(expiration_date.getFullYear() + 1)
|
65 |
+
|
66 |
+
// Save the cookie to the user's machine
|
67 |
document.cookie = `appCookie=${JSON.stringify(
|
68 |
cookie_dictionary
|
69 |
)}; expires=${expiration_date.toUTCString()}`
|
70 |
|
71 |
+
// Display a success message to the user
|
72 |
document.querySelector('.message').innerText =
|
73 |
'β
The settings have been saved sucessfully!!'
|
74 |
|
75 |
+
// Clear the success message after 10 seconds
|
76 |
setTimeout(() => {
|
77 |
document.querySelector('.message').innerText = ''
|
78 |
}, 10000)
|
79 |
}
|
80 |
|
81 |
+
/**
|
82 |
+
* This functions gets the saved cookies if it is present on the user's machine If it
|
83 |
+
* is available then it is parsed and converted to an object which is then used to
|
84 |
+
* retrieve the preferences that the user had selected previously and is then loaded in the
|
85 |
+
* website otherwise the function does nothing and the default server side settings are loaded.
|
86 |
+
*/
|
87 |
function getClientSettings() {
|
88 |
+
// Get the appCookie from the user's machine
|
89 |
let cookie = decodeURIComponent(document.cookie)
|
90 |
|
91 |
+
// If the cookie is not empty, parse it and use it to set the user's preferences
|
92 |
if (cookie !== '') {
|
93 |
let cookie_value = decodeURIComponent(document.cookie)
|
94 |
.split(';')
|
95 |
.map((item) => item.split('='))
|
96 |
.reduce((acc, [_, v]) => (acc = JSON.parse(v)) && acc, {})
|
97 |
|
98 |
+
// Loop through all link tags and update their href values to match the user's preferences
|
99 |
let links = Array.from(document.querySelectorAll('link')).forEach(
|
100 |
(item) => {
|
101 |
if (item.href.includes('static/themes')) {
|