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
/***************************************
Auteur : Pierre Aubert
Mail : pierre.aubert@lapp.in2p3.fr
Licence : CeCILL-C
****************************************/
use crate::pcontent::{
PLabelId, pabstractcontent::{PAbstractContent, PAbstractLectureBackend},
preferenceurl::PReferenceUrl,
};
///Content object of the lecture
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PContentCheckBox{
///True if the checkbox is checked, false if not
p_is_checked: bool,
}
impl PContentCheckBox {
///Constructor of the PContentCheckBox
/// # Parameters
/// - `is_checked` : true if the checkbox is checked, false if not
/// # Returns
/// Initialised PContentCheckBox
pub fn new(is_checked: bool) -> Self{
PContentCheckBox {
p_is_checked: is_checked,
}
}
}
impl PAbstractContent for PContentCheckBox{
///Say if the PContentCheckBox has an embeded label
/// # Returns
/// True if the PContentCheckBox has an embeded label, false otherwise
fn has_embeded_label(&self) -> bool{
false
}
///Get the reference url of the current PContentCheckBox
/// # Parameters
/// - `current_file` : current output file of the PContentCheckBox
/// - `id` : id of the PContentCheckBox
/// # Returns
/// Corresponding PReferenceUrl
fn get_reference_url(&self, current_file: &String, id: usize) -> PReferenceUrl{
PReferenceUrl::from_text(current_file, id, &String::from(&format!("PContentCheckBox {}", id)))
}
///Convert the current struct into html
/// # Parameters
/// - `backend` : backend which write a lecture in files
/// - `id` : id of the PContentCheckBox
fn to_html<TLectureBackend>(&self, backend: &mut TLectureBackend, id: &PLabelId)
where TLectureBackend: PAbstractLectureBackend
{
let style: String = if self.p_is_checked {
String::from("checkbox_checked")
}else{
String::from("checkbox_unchecked")
};
// backend.write(&String::from(format!("<input id=\"{}\" type=\"checkbox\" class=\"{}\" disabled ", id.get_id(), style)));
// backend.write(&String::from(format!("<span id=\"{}\" class=\"{}\">", id.get_id(), style)));
backend.write(&String::from(format!("<img id=\"{}\" src=\"book/images/{}.png\" class=\"checkbox_style\"/>", id.get_id(), style)));
// if self.p_is_checked {
// backend.write(&String::from("checked "));
// }
// backend.write(&String::from("/>"));
// backend.write(&String::from("</span>"));
}
}
#[cfg(test)]
mod tests{
use super::*;
use crate::pcontent::pstrbackend::PStrBackend;
///Test the PContentCheckBox
#[test]
fn test_content_text(){
let checkbox: PContentCheckBox = PContentCheckBox::new(false);
let mut backend = PStrBackend::new();
checkbox.to_html(&mut backend, &PLabelId::new(42));
// assert_eq!(backend.get_body(), &String::from("<input id=\"42\" type=\"checkbox\" disabled />"));
// assert_eq!(backend.get_body(), &String::from("<span id=\"42\" class=\"checkbox_unchecked\"></span>"));
assert_eq!(backend.get_body(), &String::from("<img id=\"42\" src=\"book/images/checkbox_unchecked.png\" class=\"checkbox_style\"/>"));
}
///Test the PContentCheckBox
#[test]
fn test_content_text_checked(){
let checkbox: PContentCheckBox = PContentCheckBox::new(true);
let mut backend = PStrBackend::new();
checkbox.to_html(&mut backend, &PLabelId::new(42));
// assert_eq!(backend.get_body(), &String::from("<input id=\"42\" type=\"checkbox\" disabled checked />"));
// assert_eq!(backend.get_body(), &String::from("<span id=\"42\" class=\"checkbox_checked\"></span>"));
assert_eq!(backend.get_body(), &String::from("<img id=\"42\" src=\"book/images/checkbox_checked.png\" class=\"checkbox_style\"/>"));
}
}