🏷️ Introduce Weight type for element weights

This commit is contained in:
Manuel Cillero 2023-07-16 07:45:09 +02:00
parent 6df57a0c12
commit 941b7ae57b
28 changed files with 118 additions and 110 deletions

View file

@ -1,4 +1,4 @@
use crate::Handle;
use crate::{Handle, Weight};
use std::any::Any;
@ -13,7 +13,7 @@ pub trait ActionTrait: BaseAction + Send + Sync {
fn handle(&self) -> Handle;
fn weight(&self) -> isize {
fn weight(&self) -> Weight {
0
}
}

View file

@ -32,7 +32,7 @@ macro_rules! actions_for_component {
pub struct [<BeforePrepare $Component>] {
action: Option<[<Action $Component>]>,
weight: isize,
weight: Weight,
}
impl ActionTrait for [<BeforePrepare $Component>] {
@ -47,7 +47,7 @@ macro_rules! actions_for_component {
[<ACTION_BEFORE_PREPARE_ $Component:upper>]
}
fn weight(&self) -> isize {
fn weight(&self) -> Weight {
self.weight
}
}
@ -60,8 +60,8 @@ macro_rules! actions_for_component {
}
#[allow(dead_code)]
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
pub fn with_weight(mut self, value: Weight) -> Self {
self.weight = value;
self
}
@ -91,7 +91,7 @@ macro_rules! actions_for_component {
pub struct [<AfterPrepare $Component>] {
action: Option<[<Action $Component>]>,
weight: isize,
weight: Weight,
}
impl ActionTrait for [<AfterPrepare $Component>] {
@ -106,7 +106,7 @@ macro_rules! actions_for_component {
[<ACTION_AFTER_PREPARE_ $Component:upper>]
}
fn weight(&self) -> isize {
fn weight(&self) -> Weight {
self.weight
}
}
@ -119,8 +119,8 @@ macro_rules! actions_for_component {
}
#[allow(dead_code)]
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
pub fn with_weight(mut self, value: Weight) -> Self {
self.weight = value;
self
}

View file

@ -1,6 +1,6 @@
use crate::core::component::Context;
use crate::html::{html, Markup, PrepareMarkup};
use crate::{util, Handle};
use crate::{util, Handle, Weight};
use std::any::Any;
@ -31,7 +31,7 @@ pub trait ComponentTrait: BaseComponent + Send + Sync {
None
}
fn weight(&self) -> isize {
fn weight(&self) -> Weight {
0
}

View file

@ -30,7 +30,9 @@ impl ThemeTrait for Basic {
fn before_prepare_body(&self, page: &mut Page) {
page.alter_favicon(Some(Favicon::new().with_icon("/theme/favicon.ico")))
.alter_context(ContextOp::AddStyleSheet(
StyleSheet::at("/theme/css/normalize.min.css").with_version("8.0.1"),
StyleSheet::at("/theme/css/normalize.min.css")
.with_version("8.0.1")
.with_weight(-99),
));
}
}

View file

@ -4,11 +4,12 @@ pub mod javascript;
pub mod stylesheet;
use crate::html::{html, Markup};
use crate::Weight;
pub trait AssetsTrait {
fn path(&self) -> &str;
fn weight(&self) -> isize;
fn weight(&self) -> Weight;
fn prepare(&self) -> Markup;
}

View file

@ -1,12 +1,13 @@
use crate::html::assets::AssetsTrait;
use crate::html::{html, Markup};
use crate::Weight;
#[rustfmt::skip]
#[derive(Default)]
pub struct HeadScript {
path : String,
code : String,
weight: isize,
weight: Weight,
}
impl AssetsTrait for HeadScript {
@ -14,7 +15,7 @@ impl AssetsTrait for HeadScript {
self.path.as_str()
}
fn weight(&self) -> isize {
fn weight(&self) -> Weight {
self.weight
}
@ -39,8 +40,8 @@ impl HeadScript {
self
}
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
pub fn with_weight(mut self, value: Weight) -> Self {
self.weight = value;
self
}
}

View file

@ -1,12 +1,13 @@
use crate::html::assets::AssetsTrait;
use crate::html::{html, Markup};
use crate::Weight;
#[rustfmt::skip]
#[derive(Default)]
pub struct HeadStyles {
path : String,
styles: String,
weight: isize,
weight: Weight,
}
impl AssetsTrait for HeadStyles {
@ -14,7 +15,7 @@ impl AssetsTrait for HeadStyles {
self.path.as_str()
}
fn weight(&self) -> isize {
fn weight(&self) -> Weight {
self.weight
}
@ -39,8 +40,8 @@ impl HeadStyles {
self
}
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
pub fn with_weight(mut self, value: Weight) -> Self {
self.weight = value;
self
}
}

View file

@ -1,5 +1,6 @@
use crate::html::assets::AssetsTrait;
use crate::html::{html, Markup};
use crate::Weight;
#[derive(Default, Eq, PartialEq)]
pub enum ModeJS {
@ -15,7 +16,7 @@ pub struct JavaScript {
path : String,
prefix : &'static str,
version: &'static str,
weight : isize,
weight : Weight,
mode : ModeJS,
}
@ -24,7 +25,7 @@ impl AssetsTrait for JavaScript {
self.path.as_str()
}
fn weight(&self) -> isize {
fn weight(&self) -> Weight {
self.weight
}
@ -59,8 +60,8 @@ impl JavaScript {
self
}
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
pub fn with_weight(mut self, value: Weight) -> Self {
self.weight = value;
self
}

View file

@ -1,5 +1,6 @@
use crate::html::assets::AssetsTrait;
use crate::html::{html, Markup};
use crate::Weight;
pub enum TargetMedia {
Default,
@ -15,7 +16,7 @@ pub struct StyleSheet {
prefix : &'static str,
version: &'static str,
media : Option<&'static str>,
weight : isize,
weight : Weight,
}
impl AssetsTrait for StyleSheet {
@ -23,7 +24,7 @@ impl AssetsTrait for StyleSheet {
self.path.as_str()
}
fn weight(&self) -> isize {
fn weight(&self) -> Weight {
self.weight
}
@ -57,8 +58,8 @@ impl StyleSheet {
self
}
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
pub fn with_weight(mut self, value: Weight) -> Self {
self.weight = value;
self
}

View file

@ -1,9 +1,10 @@
//! The PageTop Prelude.
// Re-exports.
pub use crate::{
concat_string, fn_builder, paste, Handle, HashMapResources, LazyStatic, ResultExt,
};
// Re-exported macros.
pub use crate::{concat_string, fn_builder, paste};
// Global.
pub use crate::{Handle, HashMapResources, LazyStatic, ResultExt, Weight};
// Funciones y macros útiles.
pub use crate::util;
@ -11,7 +12,7 @@ pub use crate::{action, actions_for_component};
pub use crate::{create_handle, default_settings, kv};
pub use crate::{serve_static_files, static_files, static_locales};
// *************************************************************************************************
// API.
pub use crate::config;

View file

@ -1,13 +1,13 @@
use crate::core::action::{action_ref, run_actions, ActionTrait};
use crate::response::page::action::ActionPage;
use crate::response::page::Page;
use crate::{create_handle, Handle};
use crate::{create_handle, Handle, Weight};
create_handle!(ACTION_AFTER_PREPARE_BODY for Action);
pub struct ActionAfterPrepareBody {
action: Option<ActionPage>,
weight: isize,
weight: Weight,
}
impl ActionTrait for ActionAfterPrepareBody {
@ -22,7 +22,7 @@ impl ActionTrait for ActionAfterPrepareBody {
ACTION_AFTER_PREPARE_BODY
}
fn weight(&self) -> isize {
fn weight(&self) -> Weight {
self.weight
}
}
@ -33,8 +33,8 @@ impl ActionAfterPrepareBody {
self
}
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
pub fn with_weight(mut self, value: Weight) -> Self {
self.weight = value;
self
}

View file

@ -1,13 +1,13 @@
use crate::core::action::{action_ref, run_actions, ActionTrait};
use crate::response::page::action::ActionPage;
use crate::response::page::Page;
use crate::{create_handle, Handle};
use crate::{create_handle, Handle, Weight};
create_handle!(ACTION_BEFORE_PREPARE_BODY for Action);
pub struct ActionBeforePrepareBody {
action: Option<ActionPage>,
weight: isize,
weight: Weight,
}
impl ActionTrait for ActionBeforePrepareBody {
@ -22,7 +22,7 @@ impl ActionTrait for ActionBeforePrepareBody {
ACTION_BEFORE_PREPARE_BODY
}
fn weight(&self) -> isize {
fn weight(&self) -> Weight {
self.weight
}
}
@ -33,8 +33,8 @@ impl ActionBeforePrepareBody {
self
}
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
pub fn with_weight(mut self, value: Weight) -> Self {
self.weight = value;
self
}