mirror of
https://github.com/neon-mmd/websurfx.git
synced 2024-11-24 06:58:22 -05:00
🚸 chore: add code to display saved settings from the config on the settings page (#461)
This commit is contained in:
parent
34468202f9
commit
b7a23f1826
@ -1,17 +1,20 @@
|
|||||||
//! A module that handles the engines tab for setting page view in the `websurfx` frontend.
|
//! A module that handles the engines tab for setting page view in the `websurfx` frontend.
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use maud::{html, Markup};
|
use maud::{html, Markup};
|
||||||
|
|
||||||
/// A functions that handles the html code for the engines tab for the settings page for the search page.
|
/// A functions that handles the html code for the engines tab for the settings page for the search page.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `engine_names` - It takes the list of all available engine names as an argument.
|
/// * `engine_names` - It takes the key value pair list of all available engine names and there corresponding
|
||||||
|
/// selected (enabled/disabled) value as an argument.
|
||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
///
|
///
|
||||||
/// It returns the compiled html markup code for the engines tab.
|
/// It returns the compiled html markup code for the engines tab.
|
||||||
pub fn engines(engine_names: &[&String]) -> Markup {
|
pub fn engines(engine_names: &HashMap<String, bool>) -> Markup {
|
||||||
html!(
|
html!(
|
||||||
div class="engines tab"{
|
div class="engines tab"{
|
||||||
h1{"Engines"}
|
h1{"Engines"}
|
||||||
@ -20,21 +23,49 @@ pub fn engines(engine_names: &[&String]) -> Markup {
|
|||||||
"Select the search engines from the list of engines that you want results from"
|
"Select the search engines from the list of engines that you want results from"
|
||||||
}
|
}
|
||||||
.engine_selection{
|
.engine_selection{
|
||||||
.toggle_btn{
|
// Checks whether all the engines are selected or not if they are then the
|
||||||
label class="switch"{
|
// checked `select_all` button is rendered otherwise the unchecked version
|
||||||
input type="checkbox" class="select_all" onchange="toggleAllSelection()";
|
// is rendered.
|
||||||
span class="slider round"{}
|
@if engine_names.values().all(|selected| *selected == true){
|
||||||
}
|
.toggle_btn{
|
||||||
"Select All"
|
label class="switch"{
|
||||||
|
input type="checkbox" class="select_all" onchange="toggleAllSelection()" checked;
|
||||||
|
span class="slider round"{}
|
||||||
|
}
|
||||||
|
"Select All"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@else{
|
||||||
|
.toggle_btn {
|
||||||
|
label class="switch"{
|
||||||
|
input type="checkbox" class="select_all" onchange="toggleAllSelection()";
|
||||||
|
span class="slider round"{}
|
||||||
|
}
|
||||||
|
"Select All"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
hr;
|
hr;
|
||||||
@for engine_name in engine_names{
|
@for (engine_name, selected) in engine_names{
|
||||||
.toggle_btn{
|
// Checks whether the `engine_name` is selected or not if they are then the
|
||||||
label class="switch"{
|
// checked `engine` button is rendered otherwise the unchecked version is
|
||||||
input type="checkbox" class="engine";
|
// rendered.
|
||||||
span class="slider round"{}
|
@if *selected == true {
|
||||||
|
.toggle_btn{
|
||||||
|
label class="switch"{
|
||||||
|
input type="checkbox" class="engine" checked;
|
||||||
|
span class="slider round"{}
|
||||||
|
}
|
||||||
|
(format!("{}{}",engine_name[..1].to_uppercase().to_owned(), engine_name[1..].to_owned()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@else {
|
||||||
|
.toggle_btn {
|
||||||
|
label class="switch"{
|
||||||
|
input type="checkbox" class="engine";
|
||||||
|
span class="slider round"{}
|
||||||
|
}
|
||||||
|
(format!("{}{}",engine_name[..1].to_uppercase().to_owned(), engine_name[1..].to_owned()))
|
||||||
}
|
}
|
||||||
(format!("{}{}",engine_name[..1].to_uppercase().to_owned(), engine_name[1..].to_owned()))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,14 @@ const SAFE_SEARCH_LEVELS: [(u8, &str); 3] = [(0, "None"), (1, "Low"), (2, "Moder
|
|||||||
|
|
||||||
/// A functions that handles the html code for the general tab for the settings page for the search page.
|
/// A functions that handles the html code for the general tab for the settings page for the search page.
|
||||||
///
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `safe_search_level` - It takes the safe search level as an argument.
|
||||||
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
///
|
///
|
||||||
/// It returns the compiled html markup code for the general tab.
|
/// It returns the compiled html markup code for the general tab.
|
||||||
pub fn general() -> Markup {
|
pub fn general(safe_search_level: u8) -> Markup {
|
||||||
html!(
|
html!(
|
||||||
div class="general tab active"{
|
div class="general tab active"{
|
||||||
h1{"General"}
|
h1{"General"}
|
||||||
@ -18,9 +22,19 @@ pub fn general() -> Markup {
|
|||||||
p class="description"{
|
p class="description"{
|
||||||
"Select a safe search level from the menu below to filter content based on the level."
|
"Select a safe search level from the menu below to filter content based on the level."
|
||||||
}
|
}
|
||||||
select name="safe_search_levels"{
|
@if safe_search_level < 3 {
|
||||||
@for (k,v) in SAFE_SEARCH_LEVELS{
|
select name="safe_search_levels" {
|
||||||
option value=(k){(v)}
|
// Sets the user selected safe_search_level name from the config file as the first option in the selection list.
|
||||||
|
option value=(safe_search_level){(SAFE_SEARCH_LEVELS.iter().filter(|level| level.0 == safe_search_level).next().unwrap().1)}
|
||||||
|
@for (k,v) in SAFE_SEARCH_LEVELS.iter().filter(|level| level.0 != safe_search_level){
|
||||||
|
option value=(k){(v)}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@else {
|
||||||
|
p class="admin_warning" {"⚠️ This setting is being managed by the server administrator."}
|
||||||
|
select name="safe_search_levels" disabled {
|
||||||
|
option value=(SAFE_SEARCH_LEVELS[2].0){(SAFE_SEARCH_LEVELS[2].1)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,16 @@ use crate::handler::{file_path, FileType};
|
|||||||
use maud::{html, Markup};
|
use maud::{html, Markup};
|
||||||
use std::fs::read_dir;
|
use std::fs::read_dir;
|
||||||
|
|
||||||
/// A helper function that helps in building the list of all available colorscheme/theme names
|
/// A helper function that helps in building the list of all available colorscheme/theme/animation
|
||||||
/// present in the colorschemes and themes folder respectively.
|
/// names present in the colorschemes, animations and themes folder respectively by excluding the
|
||||||
|
/// ones that have already been selected via the config file.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `style_type` - It takes the style type of the values `theme` and `colorscheme` as an
|
/// * `style_type` - It takes the style type of the values `theme` and `colorscheme` as an
|
||||||
/// argument.
|
/// argument.
|
||||||
|
/// * `selected_style` - It takes the currently selected style value provided via the config file
|
||||||
|
/// as an argument.
|
||||||
///
|
///
|
||||||
/// # Error
|
/// # Error
|
||||||
///
|
///
|
||||||
@ -18,7 +21,8 @@ use std::fs::read_dir;
|
|||||||
/// returns a standard error message.
|
/// returns a standard error message.
|
||||||
fn style_option_list(
|
fn style_option_list(
|
||||||
style_type: &str,
|
style_type: &str,
|
||||||
) -> Result<Vec<(String, String)>, Box<dyn std::error::Error + '_>> {
|
selected_style: &str,
|
||||||
|
) -> Result<Vec<(String, String)>, Box<dyn std::error::Error>> {
|
||||||
let mut style_option_names: Vec<(String, String)> = Vec::new();
|
let mut style_option_names: Vec<(String, String)> = Vec::new();
|
||||||
for file in read_dir(format!(
|
for file in read_dir(format!(
|
||||||
"{}static/{}/",
|
"{}static/{}/",
|
||||||
@ -26,7 +30,13 @@ fn style_option_list(
|
|||||||
style_type,
|
style_type,
|
||||||
))? {
|
))? {
|
||||||
let style_name = file?.file_name().to_str().unwrap().replace(".css", "");
|
let style_name = file?.file_name().to_str().unwrap().replace(".css", "");
|
||||||
style_option_names.push((style_name.clone(), style_name.replace('-', " ")));
|
if selected_style != style_name {
|
||||||
|
style_option_names.push((style_name.clone(), style_name.replace('-', " ")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if style_type == "animations" {
|
||||||
|
style_option_names.push(("".to_owned(), "none".to_owned()))
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(style_option_names)
|
Ok(style_option_names)
|
||||||
@ -38,7 +48,11 @@ fn style_option_list(
|
|||||||
///
|
///
|
||||||
/// It returns the compiled html markup code for the user interface tab on success otherwise
|
/// It returns the compiled html markup code for the user interface tab on success otherwise
|
||||||
/// returns a standard error message.
|
/// returns a standard error message.
|
||||||
pub fn user_interface() -> Result<Markup, Box<dyn std::error::Error>> {
|
pub fn user_interface(
|
||||||
|
theme: &str,
|
||||||
|
colorscheme: &str,
|
||||||
|
animation: &Option<String>,
|
||||||
|
) -> Result<Markup, Box<dyn std::error::Error>> {
|
||||||
Ok(html!(
|
Ok(html!(
|
||||||
div class="user_interface tab"{
|
div class="user_interface tab"{
|
||||||
h1{"User Interface"}
|
h1{"User Interface"}
|
||||||
@ -47,7 +61,9 @@ pub fn user_interface() -> Result<Markup, Box<dyn std::error::Error>> {
|
|||||||
"Select the theme from the available themes to be used in user interface"
|
"Select the theme from the available themes to be used in user interface"
|
||||||
}
|
}
|
||||||
select name="themes"{
|
select name="themes"{
|
||||||
@for (k,v) in style_option_list("themes")?{
|
// Sets the user selected theme name from the config file as the first option in the selection list.
|
||||||
|
option value=(theme){(theme.replace('-', " "))}
|
||||||
|
@for (k,v) in style_option_list("themes", theme)?{
|
||||||
option value=(k){(v)}
|
option value=(k){(v)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,7 +72,9 @@ pub fn user_interface() -> Result<Markup, Box<dyn std::error::Error>> {
|
|||||||
"Select the color scheme for your theme to be used in user interface"
|
"Select the color scheme for your theme to be used in user interface"
|
||||||
}
|
}
|
||||||
select name="colorschemes"{
|
select name="colorschemes"{
|
||||||
@for (k,v) in style_option_list("colorschemes")?{
|
// Sets the user selected colorscheme name from the config file as the first option in the selection list.
|
||||||
|
option value=(colorscheme){(colorscheme.replace('-', " "))}
|
||||||
|
@for (k,v) in style_option_list("colorschemes", colorscheme)?{
|
||||||
option value=(k){(v)}
|
option value=(k){(v)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,12 +83,12 @@ pub fn user_interface() -> Result<Markup, Box<dyn std::error::Error>> {
|
|||||||
"Select the animation for your theme to be used in user interface"
|
"Select the animation for your theme to be used in user interface"
|
||||||
}
|
}
|
||||||
select name="animations"{
|
select name="animations"{
|
||||||
option value=""{"none"}
|
// Sets the user selected animation name from the config file as the first option in the selection list.
|
||||||
@for (k,v) in style_option_list("animations")?{
|
option value=(animation.as_ref().unwrap_or(&"".to_owned())){(animation.as_ref().unwrap_or(&"".to_owned()).replace('-'," "))}
|
||||||
|
@for (k,v) in style_option_list("animations", &animation.as_ref().unwrap_or(&"".to_owned()))?{
|
||||||
option value=(k){(v)}
|
option value=(k){(v)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
//! A module that handles the view for the settings page in the `websurfx` frontend.
|
//! A module that handles the view for the settings page in the `websurfx` frontend.
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use maud::{html, Markup};
|
use maud::{html, Markup};
|
||||||
|
|
||||||
use crate::templates::partials::{
|
use crate::templates::partials::{
|
||||||
@ -14,8 +16,10 @@ use crate::templates::partials::{
|
|||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
|
/// * `safe_search_level` - It takes the safe search level as an argument.
|
||||||
/// * `colorscheme` - It takes the colorscheme name as an argument.
|
/// * `colorscheme` - It takes the colorscheme name as an argument.
|
||||||
/// * `theme` - It takes the theme name as an argument.
|
/// * `theme` - It takes the theme name as an argument.
|
||||||
|
/// * `animation` - It takes the animation name as an argument.
|
||||||
/// * `engine_names` - It takes a list of engine names as an argument.
|
/// * `engine_names` - It takes a list of engine names as an argument.
|
||||||
///
|
///
|
||||||
/// # Error
|
/// # Error
|
||||||
@ -23,10 +27,11 @@ use crate::templates::partials::{
|
|||||||
/// This function returns a compiled html markup code on success otherwise returns a standard error
|
/// This function returns a compiled html markup code on success otherwise returns a standard error
|
||||||
/// message.
|
/// message.
|
||||||
pub fn settings(
|
pub fn settings(
|
||||||
|
safe_search_level: u8,
|
||||||
colorscheme: &str,
|
colorscheme: &str,
|
||||||
theme: &str,
|
theme: &str,
|
||||||
animation: &Option<String>,
|
animation: &Option<String>,
|
||||||
engine_names: &[&String],
|
engine_names: &HashMap<String, bool>,
|
||||||
) -> Result<Markup, Box<dyn std::error::Error>> {
|
) -> Result<Markup, Box<dyn std::error::Error>> {
|
||||||
Ok(html!(
|
Ok(html!(
|
||||||
(header(colorscheme, theme, animation))
|
(header(colorscheme, theme, animation))
|
||||||
@ -41,8 +46,8 @@ pub fn settings(
|
|||||||
.btn onclick="setActiveTab(this)"{"cookies"}
|
.btn onclick="setActiveTab(this)"{"cookies"}
|
||||||
}
|
}
|
||||||
.main_container{
|
.main_container{
|
||||||
(general())
|
(general(safe_search_level))
|
||||||
(user_interface()?)
|
(user_interface(theme, colorscheme, animation)?)
|
||||||
(engines(engine_names))
|
(engines(engine_names))
|
||||||
(cookies())
|
(cookies())
|
||||||
p class="message"{}
|
p class="message"{}
|
||||||
|
Loading…
Reference in New Issue
Block a user