🧑💻 Improve usage of stylesheets and JavaScript
This commit is contained in:
parent
c83ae3c451
commit
5fb0a1332e
12 changed files with 126 additions and 172 deletions
|
|
@ -1,5 +1,3 @@
|
|||
pub mod headscript;
|
||||
pub mod headstyles;
|
||||
pub mod javascript;
|
||||
pub mod stylesheet;
|
||||
|
||||
|
|
@ -7,7 +5,7 @@ use crate::html::{html, Markup};
|
|||
use crate::{AutoDefault, Weight};
|
||||
|
||||
pub trait AssetsTrait {
|
||||
fn path(&self) -> &str;
|
||||
fn name(&self) -> &String;
|
||||
|
||||
fn weight(&self) -> Weight;
|
||||
|
||||
|
|
@ -15,7 +13,7 @@ pub trait AssetsTrait {
|
|||
}
|
||||
|
||||
#[derive(AutoDefault)]
|
||||
pub struct Assets<T>(Vec<T>);
|
||||
pub(crate) struct Assets<T>(Vec<T>);
|
||||
|
||||
impl<T: AssetsTrait> Assets<T> {
|
||||
pub fn new() -> Self {
|
||||
|
|
@ -23,7 +21,7 @@ impl<T: AssetsTrait> Assets<T> {
|
|||
}
|
||||
|
||||
pub fn add(&mut self, asset: T) -> &mut Self {
|
||||
match self.0.iter().position(|x| x.path() == asset.path()) {
|
||||
match self.0.iter().position(|x| x.name() == asset.name()) {
|
||||
Some(index) => {
|
||||
if self.0[index].weight() > asset.weight() {
|
||||
self.0.remove(index);
|
||||
|
|
@ -35,8 +33,8 @@ impl<T: AssetsTrait> Assets<T> {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, path: &'static str) -> &mut Self {
|
||||
if let Some(index) = self.0.iter().position(|x| x.path() == path) {
|
||||
pub fn remove(&mut self, name: &'static str) -> &mut Self {
|
||||
if let Some(index) = self.0.iter().position(|x| x.name() == name) {
|
||||
self.0.remove(index);
|
||||
};
|
||||
self
|
||||
|
|
|
|||
|
|
@ -1,44 +0,0 @@
|
|||
use crate::html::assets::AssetsTrait;
|
||||
use crate::html::{html, Markup};
|
||||
use crate::{AutoDefault, Weight};
|
||||
|
||||
#[rustfmt::skip]
|
||||
#[derive(AutoDefault)]
|
||||
pub struct HeadScript {
|
||||
path : String,
|
||||
code : String,
|
||||
weight: Weight,
|
||||
}
|
||||
|
||||
impl AssetsTrait for HeadScript {
|
||||
fn path(&self) -> &str {
|
||||
self.path.as_str()
|
||||
}
|
||||
|
||||
fn weight(&self) -> Weight {
|
||||
self.weight
|
||||
}
|
||||
|
||||
fn prepare(&self) -> Markup {
|
||||
html! { script { (self.code) }; }
|
||||
}
|
||||
}
|
||||
|
||||
impl HeadScript {
|
||||
pub fn named(path: impl Into<String>) -> Self {
|
||||
HeadScript {
|
||||
path: path.into(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_code(mut self, code: impl Into<String>) -> Self {
|
||||
self.code = code.into().trim().to_owned();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_weight(mut self, value: Weight) -> Self {
|
||||
self.weight = value;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
|
@ -1,44 +0,0 @@
|
|||
use crate::html::assets::AssetsTrait;
|
||||
use crate::html::{html, Markup};
|
||||
use crate::{AutoDefault, Weight};
|
||||
|
||||
#[rustfmt::skip]
|
||||
#[derive(AutoDefault)]
|
||||
pub struct HeadStyles {
|
||||
path : String,
|
||||
styles: String,
|
||||
weight: Weight,
|
||||
}
|
||||
|
||||
impl AssetsTrait for HeadStyles {
|
||||
fn path(&self) -> &str {
|
||||
self.path.as_str()
|
||||
}
|
||||
|
||||
fn weight(&self) -> Weight {
|
||||
self.weight
|
||||
}
|
||||
|
||||
fn prepare(&self) -> Markup {
|
||||
html! { styles { (self.styles) }; }
|
||||
}
|
||||
}
|
||||
|
||||
impl HeadStyles {
|
||||
pub fn named(path: impl Into<String>) -> Self {
|
||||
HeadStyles {
|
||||
path: path.into(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_styles(mut self, styles: impl Into<String>) -> Self {
|
||||
self.styles = styles.into().trim().to_owned();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_weight(mut self, value: Weight) -> Self {
|
||||
self.weight = value;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
|
@ -1,28 +1,35 @@
|
|||
use crate::html::assets::AssetsTrait;
|
||||
use crate::html::{html, Markup};
|
||||
use crate::{AutoDefault, Weight};
|
||||
use crate::{concat_string, AutoDefault, Weight};
|
||||
|
||||
#[derive(Default, Eq, PartialEq)]
|
||||
pub enum ModeJS {
|
||||
Async,
|
||||
#[derive(AutoDefault)]
|
||||
enum Source {
|
||||
#[default]
|
||||
Defer,
|
||||
Normal,
|
||||
From(String),
|
||||
Defer(String),
|
||||
Async(String),
|
||||
Inline(String, String),
|
||||
OnLoad(String, String),
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
#[derive(AutoDefault)]
|
||||
pub struct JavaScript {
|
||||
path : String,
|
||||
source : Source,
|
||||
prefix : &'static str,
|
||||
version: &'static str,
|
||||
weight : Weight,
|
||||
mode : ModeJS,
|
||||
}
|
||||
|
||||
impl AssetsTrait for JavaScript {
|
||||
fn path(&self) -> &str {
|
||||
self.path.as_str()
|
||||
fn name(&self) -> &String {
|
||||
match &self.source {
|
||||
Source::From(path) => path,
|
||||
Source::Defer(path) => path,
|
||||
Source::Async(path) => path,
|
||||
Source::Inline(name, _) => name,
|
||||
Source::OnLoad(name, _) => name,
|
||||
}
|
||||
}
|
||||
|
||||
fn weight(&self) -> Weight {
|
||||
|
|
@ -30,20 +37,60 @@ impl AssetsTrait for JavaScript {
|
|||
}
|
||||
|
||||
fn prepare(&self) -> Markup {
|
||||
html! {
|
||||
script type="text/javascript"
|
||||
src=(crate::concat_string!(self.path, self.prefix, self.version))
|
||||
async[self.mode == ModeJS::Async]
|
||||
defer[self.mode == ModeJS::Defer]
|
||||
{};
|
||||
match &self.source {
|
||||
Source::From(path) => html! {
|
||||
script src=(concat_string!(path, self.prefix, self.version)) {};
|
||||
},
|
||||
Source::Defer(path) => html! {
|
||||
script src=(concat_string!(path, self.prefix, self.version)) defer {};
|
||||
},
|
||||
Source::Async(path) => html! {
|
||||
script src=(concat_string!(path, self.prefix, self.version)) async {};
|
||||
},
|
||||
Source::Inline(_, code) => html! {
|
||||
script { (code) };
|
||||
},
|
||||
Source::OnLoad(_, code) => html! { (concat_string!(
|
||||
"document.addEventListener('DOMContentLoaded',function(){",
|
||||
code,
|
||||
"});"
|
||||
)) },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl JavaScript {
|
||||
pub fn at(path: impl Into<String>) -> Self {
|
||||
pub fn from(path: impl Into<String>) -> Self {
|
||||
JavaScript {
|
||||
path: path.into(),
|
||||
source: Source::From(path.into()),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn defer(path: impl Into<String>) -> Self {
|
||||
JavaScript {
|
||||
source: Source::Defer(path.into()),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn asynchronous(path: impl Into<String>) -> Self {
|
||||
JavaScript {
|
||||
source: Source::Async(path.into()),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inline(name: impl Into<String>, script: impl Into<String>) -> Self {
|
||||
JavaScript {
|
||||
source: Source::Inline(name.into(), script.into()),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_load(name: impl Into<String>, script: impl Into<String>) -> Self {
|
||||
JavaScript {
|
||||
source: Source::OnLoad(name.into(), script.into()),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
|
@ -61,9 +108,4 @@ impl JavaScript {
|
|||
self.weight = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_mode(mut self, mode: ModeJS) -> Self {
|
||||
self.mode = mode;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,13 @@
|
|||
use crate::html::assets::AssetsTrait;
|
||||
use crate::html::{html, Markup};
|
||||
use crate::{AutoDefault, Weight};
|
||||
use crate::{concat_string, AutoDefault, Weight};
|
||||
|
||||
#[derive(AutoDefault)]
|
||||
enum Source {
|
||||
#[default]
|
||||
From(String),
|
||||
Inline(String, String),
|
||||
}
|
||||
|
||||
pub enum TargetMedia {
|
||||
Default,
|
||||
|
|
@ -12,7 +19,7 @@ pub enum TargetMedia {
|
|||
#[rustfmt::skip]
|
||||
#[derive(AutoDefault)]
|
||||
pub struct StyleSheet {
|
||||
path : String,
|
||||
source : Source,
|
||||
prefix : &'static str,
|
||||
version: &'static str,
|
||||
media : Option<&'static str>,
|
||||
|
|
@ -20,8 +27,11 @@ pub struct StyleSheet {
|
|||
}
|
||||
|
||||
impl AssetsTrait for StyleSheet {
|
||||
fn path(&self) -> &str {
|
||||
self.path.as_str()
|
||||
fn name(&self) -> &String {
|
||||
match &self.source {
|
||||
Source::From(path) => path,
|
||||
Source::Inline(name, _) => name,
|
||||
}
|
||||
}
|
||||
|
||||
fn weight(&self) -> Weight {
|
||||
|
|
@ -29,19 +39,31 @@ impl AssetsTrait for StyleSheet {
|
|||
}
|
||||
|
||||
fn prepare(&self) -> Markup {
|
||||
html! {
|
||||
link
|
||||
rel="stylesheet"
|
||||
href=(crate::concat_string!(self.path, self.prefix, self.version))
|
||||
media=[self.media];
|
||||
match &self.source {
|
||||
Source::From(path) => html! {
|
||||
link
|
||||
rel="stylesheet"
|
||||
href=(concat_string!(path, self.prefix, self.version))
|
||||
media=[self.media];
|
||||
},
|
||||
Source::Inline(_, code) => html! {
|
||||
styles { (code) };
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StyleSheet {
|
||||
pub fn at(path: impl Into<String>) -> Self {
|
||||
pub fn from(path: impl Into<String>) -> Self {
|
||||
StyleSheet {
|
||||
path: path.into(),
|
||||
source: Source::From(path.into()),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inline(name: impl Into<String>, styles: impl Into<String>) -> Self {
|
||||
StyleSheet {
|
||||
source: Source::Inline(name.into(), styles.into()),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue