Modifica la localización de activos en el contexto

This commit is contained in:
Manuel Cillero 2022-05-16 20:44:17 +02:00
parent 658d88a383
commit f5314e9ca4
12 changed files with 269 additions and 217 deletions

View file

@ -47,6 +47,7 @@ impl ComponentTrait for Input {
classes : Classes::new_with_default("form-item"),
template : "default".to_owned(),
}
.with_classes("form-type-textfield", ClassesOp::AddFirst)
}
fn handler(&self) -> &'static str {
@ -126,31 +127,36 @@ impl Input {
}
pub fn password() -> Self {
let mut input = Input::new();
let mut input = Input::new()
.with_classes("form-type-password", ClassesOp::Replace("form-type-textfield"));
input.input_type = InputType::Password;
input
}
pub fn search() -> Self {
let mut input = Input::new();
let mut input = Input::new()
.with_classes("form-type-search", ClassesOp::Replace("form-type-textfield"));
input.input_type = InputType::Search;
input
}
pub fn email() -> Self {
let mut input = Input::new();
let mut input = Input::new()
.with_classes("form-type-email", ClassesOp::Replace("form-type-textfield"));
input.input_type = InputType::Email;
input
}
pub fn telephone() -> Self {
let mut input = Input::new();
let mut input = Input::new()
.with_classes("form-type-telephone", ClassesOp::Replace("form-type-textfield"));
input.input_type = InputType::Telephone;
input
}
pub fn url() -> Self {
let mut input = Input::new();
let mut input = Input::new()
.with_classes("form-type-url", ClassesOp::Replace("form-type-textfield"));
input.input_type = InputType::Url;
input
}
@ -256,6 +262,10 @@ impl Input {
pub fn alter_name(&mut self, name: &str) -> &mut Self {
self.name.with_value(name);
self.alter_classes(
concat_string!("form-item form-item-", name).as_str(),
ClassesOp::SetDefault
);
self
}

View file

@ -17,10 +17,9 @@ impl ThemeTrait for Aliner {
fn before_render_page(&self, page: &mut Page) {
page.context()
.with_favicon(
Favicon::new()
.with_icon("/theme/favicon.png")
)
.with_favicon(Some(Favicon::new()
.with_icon("/theme/favicon.png")
))
.add_stylesheet(
StyleSheet::with_source(
"/aliner/css/styles.css"

View file

@ -19,10 +19,9 @@ impl ThemeTrait for Bootsier {
fn before_render_page(&self, page: &mut Page) {
page.context()
.with_favicon(
Favicon::new()
.with_icon("/theme/favicon.png")
)
.with_favicon(Some(Favicon::new()
.with_icon("/theme/favicon.png")
))
.add_stylesheet(
StyleSheet::with_source(
"/bootsier/css/bootstrap.min.css?ver=5.1.3"

View file

@ -17,10 +17,9 @@ impl ThemeTrait for Bulmix {
fn before_render_page(&self, page: &mut Page) {
page.context()
.with_favicon(
Favicon::new()
.with_icon("/theme/favicon.png")
)
.with_favicon(Some(Favicon::new()
.with_icon("/theme/favicon.png")
))
.add_stylesheet(
StyleSheet::with_source(
"/bulmix/css/bulma.min.css?ver=0.9.3"

View file

@ -6,11 +6,6 @@ pub use hook::{
mod context;
pub use context::InContext;
pub use context::{
Favicon,
JavaScript, JSMode,
StyleSheet,
};
mod definition;
pub use definition::{

View file

@ -1,163 +1,11 @@
use crate::{Lazy, base, concat_string, util};
use crate::config::SETTINGS;
use crate::html::{Markup, PreEscaped, html};
use crate::core::theme::*;
// Favicon.
pub struct Favicon(Vec<String>);
impl Favicon {
pub fn new() -> Self {
Favicon(Vec::new())
}
pub fn with_icon(self, image: &str) -> Self {
self.add_item("icon", image, "", "")
}
pub fn with_icon_for_sizes(self, image: &str, sizes: &str) -> Self {
self.add_item("icon", image, sizes, "")
}
pub fn with_apple_touch_icon(self, image: &str, sizes: &str) -> Self {
self.add_item("apple-touch-icon", image, sizes, "")
}
pub fn with_mask_icon(self, image: &str, color: &str) -> Self {
self.add_item("mask-icon", image, "", color)
}
pub fn with_manifest(self, file: &str) -> Self {
self.add_item("manifest", file, "", "")
}
pub fn with_theme_color(mut self, color: &str) -> Self {
self.0.push(format!(
"<meta name=\"theme-color\" content=\"{}\">", color
));
self
}
pub fn with_ms_tile_color(mut self, color: &str) -> Self {
self.0.push(format!(
"<meta name=\"msapplication-TileColor\" content=\"{}\">", color
));
self
}
pub fn with_ms_tile_image(mut self, image: &str) -> Self {
self.0.push(format!(
"<meta name=\"msapplication-TileImage\" content=\"{}\">", image
));
self
}
fn add_item(
mut self,
rel : &str,
source: &str,
sizes : &str,
color : &str
) -> Self {
let mut link: String = format!("<link rel=\"{}\"", rel);
if let Some(i) = source.rfind('.') {
link = match source[i..].to_owned().to_lowercase().as_str() {
".gif" => format!("{} type=\"image/gif\"", link),
".ico" => format!("{} type=\"image/x-icon\"", link),
".jpg" => format!("{} type=\"image/jpg\"", link),
".png" => format!("{} type=\"image/png\"", link),
".svg" => format!("{} type=\"image/svg+xml\"", link),
_ => link
};
}
if !sizes.is_empty() {
link = format!("{} sizes=\"{}\"", link, sizes);
}
if !color.is_empty() {
link = format!("{} color=\"{}\"", link, color);
}
self.0.push(format!("{} href=\"{}\">", link, source));
self
}
fn render(&self) -> Markup {
html! {
@for item in &self.0 {
(PreEscaped(item))
}
}
}
}
// JavaScript.
#[derive(PartialEq)]
pub enum JSMode { Async, Defer, Normal }
pub struct JavaScript {
source: &'static str,
weight: isize,
mode : JSMode,
}
impl JavaScript {
pub fn with_source(s: &'static str) -> Self {
JavaScript {
source: s,
weight: 0,
mode : JSMode::Defer,
}
}
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
self
}
pub fn with_mode(mut self, mode: JSMode) -> Self {
self.mode = mode;
self
}
fn render(&self) -> Markup {
html! {
script type="text/javascript"
src=(self.source)
async[self.mode == JSMode::Async]
defer[self.mode == JSMode::Defer]
{};
}
}
}
// StyleSheet.
pub struct StyleSheet {
source: &'static str,
weight: isize,
}
impl StyleSheet {
pub fn with_source(s: &'static str) -> Self {
StyleSheet {
source: s,
weight: 0,
}
}
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
self
}
fn render(&self) -> Markup {
html! {
link rel="stylesheet" href=(self.source);
}
}
}
use crate::html::*;
use crate::core::theme::ThemeTrait;
use crate::core::theme::all::theme_by_single_name;
static DEFAULT_THEME: Lazy<&dyn ThemeTrait> = Lazy::new(|| {
match all::theme_by_single_name(&SETTINGS.app.theme) {
match theme_by_single_name(&SETTINGS.app.theme) {
Some(theme) => theme,
None => &base::theme::bootsier::Bootsier,
}
@ -167,8 +15,8 @@ pub struct InContext {
theme : &'static dyn ThemeTrait,
favicon : Option<Favicon>,
metadata : Vec<(String, String)>,
stylesheets: Vec<StyleSheet>,
javascripts: Vec<JavaScript>,
stylesheets: Assets<StyleSheet>,
javascripts: Assets<JavaScript>,
with_jquery: bool,
id_counter : usize,
}
@ -179,20 +27,20 @@ impl InContext {
theme : *DEFAULT_THEME,
favicon : None,
metadata : Vec::new(),
stylesheets: Vec::new(),
javascripts: Vec::new(),
stylesheets: Assets::<StyleSheet>::new(),
javascripts: Assets::<JavaScript>::new(),
with_jquery: false,
id_counter : 0,
}
}
pub fn using_theme(&mut self, theme_name: &str) -> &mut Self {
self.theme = all::theme_by_single_name(theme_name).unwrap_or(*DEFAULT_THEME);
self.theme = theme_by_single_name(theme_name).unwrap_or(*DEFAULT_THEME);
self
}
pub fn with_favicon(&mut self, favicon: Favicon) -> &mut Self {
self.favicon = Some(favicon);
pub fn with_favicon(&mut self, favicon: Option<Favicon>) -> &mut Self {
self.favicon = favicon;
self
}
@ -202,24 +50,12 @@ impl InContext {
}
pub fn add_stylesheet(&mut self, css: StyleSheet) -> &mut Self {
match self.stylesheets.iter().position(|x| x.source == css.source) {
Some(index) => if self.stylesheets[index].weight > css.weight {
self.stylesheets.remove(index);
self.stylesheets.push(css);
},
_ => self.stylesheets.push(css)
}
self.stylesheets.add(css);
self
}
pub fn add_javascript(&mut self, js: JavaScript) -> &mut Self {
match self.javascripts.iter().position(|x| x.source == js.source) {
Some(index) => if self.javascripts[index].weight > js.weight {
self.javascripts.remove(index);
self.javascripts.push(js);
},
_ => self.javascripts.push(js)
}
self.javascripts.add(js);
self
}
@ -246,12 +82,6 @@ impl InContext {
/// InContext RENDER.
pub fn render(&mut self) -> Markup {
let ordered_css = &mut self.stylesheets;
ordered_css.sort_by_key(|css| css.weight);
let ordered_js = &mut self.javascripts;
ordered_js.sort_by_key(|js| js.weight);
html! {
@match &self.favicon {
Some(favicon) => (favicon.render()),
@ -260,12 +90,8 @@ impl InContext {
@for (name, content) in &self.metadata {
meta name=(name) content=(content) {}
}
@for css in ordered_css {
(css.render())
}
@for js in ordered_js {
(js.render())
}
(self.stylesheets.render())
(self.javascripts.render())
}
}

View file

@ -1,7 +1,7 @@
use crate::{app, concat_string, util};
use crate::config::SETTINGS;
use crate::html::{Markup, html};
use crate::core::component::{ComponentTrait, Favicon, InContext};
use crate::html::{Favicon, Markup, html};
use crate::core::component::{ComponentTrait, InContext};
use crate::response::page::Page;
use crate::base::component::Chunck;
@ -28,10 +28,9 @@ pub trait ThemeTrait: BaseTheme + Send + Sync {
#[allow(unused_variables)]
fn before_render_page(&self, page: &mut Page) {
page.context()
.with_favicon(
Favicon::new()
.with_icon("/theme/favicon.png")
);
.with_favicon(Some(Favicon::new()
.with_icon("/theme/favicon.png")
));
}
fn render_page_head(&self, page: &mut Page) -> Markup {

View file

@ -1,8 +1,18 @@
pub use maud::{DOCTYPE, Markup, PreEscaped, html};
mod assets;
pub use assets::Assets;
pub use assets::javascript::{JavaScript, JSMode};
pub use assets::stylesheet::StyleSheet;
mod favicon;
pub use favicon::Favicon;
mod optiden;
pub use optiden::OptIden;
mod optattr;
pub use optattr::OptAttr;
mod classes;
pub use classes::{Classes, ClassesOp};

View file

@ -0,0 +1,41 @@
pub mod javascript;
pub mod stylesheet;
use crate::html::{Markup, html};
pub trait AssetsTrait {
fn source(&self) -> &'static str;
fn weight(&self) -> isize;
fn render(&self) -> Markup;
}
pub struct Assets<T>(Vec<T>);
impl<T: AssetsTrait> Assets<T> {
pub fn new() -> Self {
Assets::<T>(Vec::<T>::new())
}
pub fn add(&mut self, assets: T) -> &mut Self {
match self.0.iter().position(|x| x.source() == assets.source()) {
Some(index) => if self.0[index].weight() > assets.weight() {
self.0.remove(index);
self.0.push(assets);
},
_ => self.0.push(assets)
}
self
}
pub fn render(&mut self) -> Markup {
let assets = &mut self.0;
assets.sort_by_key(|a| a.weight());
html! {
@for a in assets {
(a.render())
}
}
}
}

View file

@ -0,0 +1,51 @@
use crate::html::{Markup, html};
use super::AssetsTrait;
#[derive(PartialEq)]
pub enum JSMode { Async, Defer, Normal }
pub struct JavaScript {
source: &'static str,
weight: isize,
mode : JSMode,
}
impl AssetsTrait for JavaScript {
fn source(&self) -> &'static str {
self.source
}
fn weight(&self) -> isize {
self.weight
}
fn render(&self) -> Markup {
html! {
script type="text/javascript"
src=(self.source)
async[self.mode == JSMode::Async]
defer[self.mode == JSMode::Defer]
{};
}
}
}
impl JavaScript {
pub fn with_source(s: &'static str) -> Self {
JavaScript {
source: s,
weight: 0,
mode : JSMode::Defer,
}
}
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
self
}
pub fn with_mode(mut self, mode: JSMode) -> Self {
self.mode = mode;
self
}
}

View file

@ -0,0 +1,37 @@
use crate::html::{Markup, html};
use super::AssetsTrait;
pub struct StyleSheet {
source: &'static str,
weight: isize,
}
impl AssetsTrait for StyleSheet {
fn source(&self) -> &'static str {
self.source
}
fn weight(&self) -> isize {
self.weight
}
fn render(&self) -> Markup {
html! {
link rel="stylesheet" href=(self.source);
}
}
}
impl StyleSheet {
pub fn with_source(s: &'static str) -> Self {
StyleSheet {
source: s,
weight: 0,
}
}
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
self
}
}

View file

@ -0,0 +1,86 @@
use crate::html::{Markup, PreEscaped, html};
pub struct Favicon(Vec<String>);
impl Favicon {
pub fn new() -> Self {
Favicon(Vec::new())
}
pub fn with_icon(self, image: &str) -> Self {
self.add_item("icon", image, "", "")
}
pub fn with_icon_for_sizes(self, image: &str, sizes: &str) -> Self {
self.add_item("icon", image, sizes, "")
}
pub fn with_apple_touch_icon(self, image: &str, sizes: &str) -> Self {
self.add_item("apple-touch-icon", image, sizes, "")
}
pub fn with_mask_icon(self, image: &str, color: &str) -> Self {
self.add_item("mask-icon", image, "", color)
}
pub fn with_manifest(self, file: &str) -> Self {
self.add_item("manifest", file, "", "")
}
pub fn with_theme_color(mut self, color: &str) -> Self {
self.0.push(format!(
"<meta name=\"theme-color\" content=\"{}\">", color
));
self
}
pub fn with_ms_tile_color(mut self, color: &str) -> Self {
self.0.push(format!(
"<meta name=\"msapplication-TileColor\" content=\"{}\">", color
));
self
}
pub fn with_ms_tile_image(mut self, image: &str) -> Self {
self.0.push(format!(
"<meta name=\"msapplication-TileImage\" content=\"{}\">", image
));
self
}
fn add_item(
mut self,
rel : &str,
source: &str,
sizes : &str,
color : &str
) -> Self {
let mut link: String = format!("<link rel=\"{}\"", rel);
if let Some(i) = source.rfind('.') {
link = match source[i..].to_owned().to_lowercase().as_str() {
".gif" => format!("{} type=\"image/gif\"", link),
".ico" => format!("{} type=\"image/x-icon\"", link),
".jpg" => format!("{} type=\"image/jpg\"", link),
".png" => format!("{} type=\"image/png\"", link),
".svg" => format!("{} type=\"image/svg+xml\"", link),
_ => link
};
}
if !sizes.is_empty() {
link = format!("{} sizes=\"{}\"", link, sizes);
}
if !color.is_empty() {
link = format!("{} color=\"{}\"", link, color);
}
self.0.push(format!("{} href=\"{}\">", link, source));
self
}
pub(crate) fn render(&self) -> Markup {
html! {
@for item in &self.0 {
(PreEscaped(item))
}
}
}
}