Compare commits

..

No commits in common. "78c017ede783f78dc553b8f0bd212b40e842f8d5" and "b3f88a49aae43a6add7be552d5eeb86111eaf99e" have entirely different histories.

3 changed files with 72 additions and 167 deletions

View File

@ -1,40 +1,8 @@
use build_html::{self, Html, HtmlContainer};
#[derive(Clone, Debug, Default)]
pub struct Attributes(Vec<(String, String)>);
/*
impl FromIterator<(String, String)> for Attributes {
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = (String, String)>,
{
Attributes(iter.collect::<Vec<(String, String)>>())
}
}
impl FromIterator<(&str, &str)> for Attributes {
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = (&str, &str)>,
{
unimplemented!()
}
}
*/
impl ToString for Attributes {
fn to_string(&self) -> String {
self.0
.iter()
.map(|(key, value)| format!("{}=\"{}\"", key, value))
.collect::<Vec<String>>()
.join(" ")
}
}
#[derive(Clone, Debug)]
pub struct Form {
classes: Option<String>,
path: String,
method: String,
encoding: Option<String>,
@ -44,6 +12,7 @@ pub struct Form {
impl Form {
pub fn new() -> Self {
Self {
classes: None,
path: "/".to_owned(),
method: "get".to_owned(),
encoding: None,
@ -69,16 +38,21 @@ impl Form {
impl Html for Form {
fn to_html_string(&self) -> String {
let classes = match self.classes {
Some(ref classes) => format!("class=\"{}\"", classes),
None => "".to_owned(),
};
let encoding = match self.encoding {
Some(ref encoding) => format!("enctype=\"{encoding}\"", encoding = encoding),
None => "".to_owned(),
};
format!(
"<form action=\"{path}\" method=\"{method}\" {encoding}\n{elements}\n</form>\n",
"<form action=\"{path}\" method=\"{method}\" {encoding} {classes}>\n{elements}\n</form>\n",
path = self.path,
method = self.method,
encoding = encoding,
elements = self.elements.to_html_string(),
classes = classes,
)
}
}
@ -95,7 +69,7 @@ pub struct Input {
name: String,
id: Option<String>,
value: Option<String>,
attributes: Attributes,
attributes: Vec<(String, String)>,
}
impl Html for Input {
@ -108,7 +82,12 @@ impl Html for Input {
Some(ref value) => format!("value=\"{}\"", value),
None => "".to_owned(),
};
let attrs = self.attributes.to_string();
let attrs = self
.attributes
.iter()
.map(|(key, value)| format!("{}=\"{}\"", key, value))
.collect::<Vec<String>>()
.join(" ");
format!(
"<input type=\"{ty}\" name=\"{name}\" {id} {value} {attrs} />\n",
@ -128,7 +107,7 @@ impl Input {
name: name.to_owned(),
id: None,
value: None,
attributes: Attributes::default(),
attributes: vec![],
}
}
@ -146,12 +125,10 @@ impl Input {
mut self,
values: impl IntoIterator<Item = (&'a str, &'a str)>,
) -> Self {
self.attributes = Attributes(
values
self.attributes = values
.into_iter()
.map(|(a, b)| (a.to_owned(), b.to_owned()))
.collect::<Vec<(String, String)>>(),
);
.collect::<Vec<(String, String)>>();
self
}
}
@ -186,7 +163,6 @@ pub struct Button {
ty: Option<String>,
name: Option<String>,
label: String,
attributes: Attributes,
}
impl Button {
@ -195,7 +171,6 @@ impl Button {
ty: None,
name: None,
label: label.to_owned(),
attributes: Attributes::default(),
}
}
@ -203,19 +178,6 @@ impl Button {
self.ty = Some(ty.to_owned());
self
}
pub fn with_attributes<'a>(
mut self,
values: impl IntoIterator<Item = (&'a str, &'a str)>,
) -> Self {
self.attributes = Attributes(
values
.into_iter()
.map(|(a, b)| (a.to_owned(), b.to_owned()))
.collect::<Vec<(String, String)>>(),
);
self
}
}
impl Html for Button {
@ -229,10 +191,9 @@ impl Html for Button {
None => "".to_owned(),
};
format!(
"<button {ty} {name} {attrs}>{label}</button>",
"<button {ty} {name}>{label}</button>",
name = name,
label = self.label,
attrs = self.attributes.to_string()
label = self.label
)
}
}
@ -240,37 +201,18 @@ impl Html for Button {
#[derive(Clone, Debug)]
pub struct Image {
path: String,
attributes: Attributes,
}
impl Image {
pub fn new(path: &str) -> Self {
Self {
path: path.to_owned(),
attributes: Attributes::default(),
}
}
pub fn with_attributes<'a>(
mut self,
values: impl IntoIterator<Item = (&'a str, &'a str)>,
) -> Self {
self.attributes = Attributes(
values
.into_iter()
.map(|(a, b)| (a.to_owned(), b.to_owned()))
.collect::<Vec<(String, String)>>(),
);
self
}
}
impl Html for Image {
fn to_html_string(&self) -> String {
format!(
"<img src={path} {attrs} />",
path = self.path,
attrs = self.attributes.to_string()
)
format!("<img src={path} />", path = self.path,)
}
}

View File

@ -11,7 +11,7 @@ pub fn auth(_message: Option<String>) -> build_html::HtmlPage {
.with_attributes([("class", "authentication-page")])
.with_container(
Container::new(ContainerType::Div)
.with_attributes([("class", "card authentication-form")])
.with_attributes([("class", "authentication-form")])
.with_html(
Form::new()
.with_path("/auth")
@ -37,16 +37,22 @@ pub fn auth(_message: Option<String>) -> build_html::HtmlPage {
pub fn gallery(handles: Vec<Result<FileHandle, ReadFileError>>) -> build_html::HtmlPage {
let mut page = build_html::HtmlPage::new()
.with_title("Gallery")
.with_title("Admin list of files")
.with_stylesheet("/css")
.with_header(1, "Admin list of files")
.with_html(
Form::new()
.with_path("/upload")
.with_method("post")
.with_encoding("multipart/form-data")
.with_container(
Container::new(ContainerType::Div)
.with_attributes([("class", "gallery-page")])
.with_header(1, "Gallery")
.with_html(upload_form()),
.with_html(Input::new("file", "file").with_id("for-selector-input"))
.with_html(Label::new("for-selector-input", "Select a file")),
)
.with_html(Button::new("Upload file").with_type("submit")),
);
let mut gallery = Container::new(ContainerType::Div).with_attributes([("class", "gallery")]);
for handle in handles {
let container = match handle {
Ok(ref handle) => thumbnail(&handle.id).with_html(
@ -61,42 +67,19 @@ pub fn gallery(handles: Vec<Result<FileHandle, ReadFileError>>) -> build_html::H
.with_attributes(vec![("class", "file")])
.with_paragraph(format!("{:?}", err)),
};
gallery.add_container(container);
page.add_container(container)
}
page.add_container(gallery);
page
}
pub fn upload_form() -> Form {
Form::new()
.with_path("/upload")
.with_method("post")
.with_encoding("multipart/form-data")
.with_container(
Container::new(ContainerType::Div)
.with_attributes([("class", "card upload-form")])
.with_html(Input::new("file", "file").with_attributes([
("id", "for-selector-input"),
("placeholder", "select file"),
("class", "upload-form__selector"),
]))
.with_html(
Button::new("Upload file")
.with_attributes([("class", "upload-form__button")])
.with_type("submit"),
),
)
}
pub fn thumbnail(id: &FileId) -> Container {
Container::new(ContainerType::Div)
.with_attributes(vec![("class", "card thumbnail")])
.with_html(
Container::new(ContainerType::Div).with_link(
let mut container = Container::new(ContainerType::Div).with_attributes(vec![("class", "file")]);
let tn = Container::new(ContainerType::Div)
.with_attributes(vec![("class", "thumbnail")])
.with_link(
format!("/{}", **id),
Image::new(&format!("{}/tn", **id))
.with_attributes([("class", "thumbnail__image")])
.to_html_string(),
),
)
Image::new(&format!("{}/tn", **id)).to_html_string(),
);
container.add_html(tn);
container
}

View File

@ -2,9 +2,9 @@
--main-bg-color: #e5f0fc;
--fg-color: #449dfc;
--space-small: 4px;
--space-medium: 8px;
--space-large: 12px;
--px-4: 4px;
--px-8: 8px;
--px-12: 12px;
--hover-low: 4px 4px 4px gray;
}
@ -14,71 +14,50 @@ body {
background-color: var(--main-bg-color);
}
.card {
border: 1px solid black;
border-radius: 5px;
border-shadow: var(--hover-low);
padding: var(--px-12);
margin: var(--space-large);
padding: var(--space-medium);
}
.authentication-page {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 200px;
}
.authentication-form {
border: 1px solid black;
border-radius: 5px;
border-shadow: var(--hover-low);
padding: var(--px-12);
}
.authentication-form__label {
margin: var(--space-small);
margin: var(--px-4);
}
.authentication-form__input {
margin: var(--space-small);
margin: var(--px-4);
}
.gallery-page {
display: flex;
flex-direction: column;
}
.upload-form {
display: flex;
flex-direction: column;
}
.upload-form__selector {
margin: var(--space-small);
}
.upload-form__button {
margin: var(--space-small);
}
.gallery {
.files {
display: flex;
flex-wrap: wrap;
}
.thumbnail {
max-width: 300px;
.file {
display: flex;
flex-direction: column;
justify-content: space-between;
margin: 1em;
border: 1px solid #449dfc;
border-radius: 5px;
padding: 1em;
}
.thumbnail__image {
.thumbnail {
max-width: 320px;
margin: 1em;
}
img {
max-width: 100%;
border: none;
}
/*
[type="submit"] {
border-radius: 1em;
margin: 1em;
@ -86,10 +65,12 @@ body {
}
.uploadform {
display: flex;
margin: 1em;
padding: 1em;
}
*/
/*
/* https://benmarshall.me/styling-file-inputs/ */
[type="file"] {
border: 0;
clip: rect(0, 0, 0, 0);
@ -121,7 +102,6 @@ body {
outline: 1px dotted #000;
outline: -webkit-focus-ring-color auto 5px;
}
*/
/*
@media screen and (max-width: 980px) { /* This is the screen width of a OnePlus 5t