use std::path::PathBuf;
use crate::phighlighter::PLocation;
use crate::plectureparser::PLabelId;
use crate::plectureparser::plabeler::PLabeler;
use crate::plectureparser::{
pcontenttitle::PContentTitle,
pcontenttext::PContentText,
pveccontent::{
PContentType,
PVecContent
},
pabstractcontent::{PAbstractContent, PAbstractLectureBackend},
};
#[derive(Debug)]
pub struct PLectureMenu{
p_is_expanded: bool,
p_is_first_expanded: bool ,
p_title: PLabeler<PContentTitle>,
p_vec_child: Vec<PLectureMenu>,
}
impl PLectureMenu {
pub fn new(title: &PLabeler<PContentTitle>) -> Self {
PLectureMenu {
p_is_expanded: false,
p_is_first_expanded: false,
p_title: title.clone(),
p_vec_child: vec![]
}
}
pub fn from(vec_content: &PVecContent) -> Self {
let mut main_title = PContentTitle::new(0, false, &PLocation::new(&PathBuf::from("index.md"), 1, 1));
main_title.get_title_mut().add_child(&PContentType::Text(PLabeler::new(0, &PContentText::new(&String::from("Main Page")))));
let mut other = PLectureMenu::new(&PLabeler::new(0, &main_title));
for content in vec_content.get_vec_child().iter(){
match content {
PContentType::Title(title) => {
other.add_child(title);
},
_ => {} }
}
return other;
}
fn add_child(&mut self, title: &PLabeler<PContentTitle>){
let title_level: usize = title.get_data().get_level();
match self.p_vec_child.last_mut() {
Some(child) => {
if child.p_title.get_data().get_level() < title_level {
child.add_child(title);
}else{
self.p_vec_child.push(PLectureMenu::new(title))
}
},
None => self.p_vec_child.push(PLectureMenu::new(title)), };
}
pub fn to_html<TLectureBackend>(&mut self, backend: &mut TLectureBackend, current_link: &String)
where TLectureBackend: PAbstractLectureBackend
{
self.update_expanded(current_link);
let indentation = String::from("\t\t");
backend.write(&format!("{}<nav id=\"sidebar\" class=\"sidebar\" aria-label=\"Table of contents\">\n", indentation));
backend.write(&format!("{}\t<div class=\"sidebar-scrollbox\">\n", indentation));
backend.write(&format!("{}\t\t<ol class=\"chapter\">\n", indentation));
backend.write(&format!("{}\t\t\t<li class=\"chapter-item \"><a href=\"index.html\">Main Page</a></li>\n", indentation));
let child_indentation: String = String::from(format!("\t\t\t{}", indentation));
for submenu in self.p_vec_child.iter() {
submenu.to_html_sub_menu(backend, &child_indentation);
}
backend.write(&format!("{}\t\t</ol>\n", indentation));
backend.write(&format!("{}\t</div>\n", indentation));
backend.write(&format!("{}\t<div id=\"sidebar-resize-handle\" class=\"sidebar-resize-handle\"></div>\n", indentation));
backend.write(&format!("{}</nav>\n", indentation));
}
fn to_html_sub_menu<TLectureBackend>(&self, backend: &mut TLectureBackend, indentation: &String)
where TLectureBackend: PAbstractLectureBackend
{
backend.write(&format!("{}<li class=\"chapter-item ", indentation));
if self.p_is_expanded {
backend.write(&String::from("expanded"));
if self.p_is_first_expanded {
backend.write(&String::from(" active"));
}
}
backend.write(&format!("\"><a href=\"{}\">", self.p_title.get_data().get_output_filename()));
let mut str_title_number = self.p_title.get_data().get_str_title_number().clone();
if !str_title_number.is_empty() {
str_title_number += &String::from(" ");
}
backend.write(&str_title_number);
self.p_title.get_data().get_title().to_html(backend, &PLabelId::new(0));
backend.write(&String::from("</a>"));
if self.p_vec_child.len() != 0 { backend.write(&String::from("<a class=\"toggle\"><div>❱</div></a></li>\n"));
backend.write(&format!("{}<li>\n", indentation));
backend.write(&format!("{}\t<ol class=\"section\">\n", indentation));
let sub_indentation = String::from(format!("{}\t\t", indentation));
for child in self.p_vec_child.iter() {
child.to_html_sub_menu(backend, &sub_indentation);
}
backend.write(&format!("{}\t</ol>\n", indentation));
backend.write(&format!("{}</li>\n", indentation));
}else{
backend.write(&String::from("</li>\n"));
}
}
fn update_expanded(&mut self, current_link: &String){
self.p_is_first_expanded = current_link == self.p_title.get_data().get_output_filename();
let mut is_extended = self.p_is_first_expanded;
for child in self.p_vec_child.iter_mut(){
if is_extended { break;
}
child.update_expanded(current_link);
is_extended = child.p_is_expanded;
}
self.p_is_expanded = is_extended;
}
}