Skip to content

Commit fbc4f29

Browse files
Floflobelflorian-bellencontrelovasoa
authored
fix cards that have embedded contents (#744)
* fix #743 * fix #743 * handle icons separately * remove description test, since descriptions are overwritten by card embeds anyway --------- Co-authored-by: Florian Bellencontre <[email protected]> Co-authored-by: lovasoa <[email protected]>
1 parent 28e21b4 commit fbc4f29

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

examples/official-site/sqlpage/migrations/31_card_docs_update.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
2424
('footer_link', 'An URL to which the user should be taken when they click on the footer.', 'URL', FALSE, TRUE),
2525
('style', 'Inline style property to your iframe embed code. For example "background-color: #FFFFFF"', 'TEXT', FALSE, TRUE),
2626
('icon', 'Name of an icon to display on the left side of the card.', 'ICON', FALSE, TRUE),
27-
('color', 'The name of a color, to be displayed on the left of the card to highlight it.', 'COLOR', FALSE, TRUE),
27+
('color', 'The name of a color, to be displayed on the left of the card to highlight it. If the embed parameter is enabled and you don''t have a title or description, this parameter won''t apply.', 'COLOR', FALSE, TRUE),
2828
('background_color', 'The background color of the card.', 'COLOR', FALSE, TRUE),
2929
('active', 'Whether this item in the grid is considered "active". Active items are displayed more prominently.', 'BOOLEAN', FALSE, TRUE),
3030
('width', 'The width of the card, between 1 (smallest) and 12 (full-width). The default width is 3, resulting in 4 cards per line.', 'INTEGER', FALSE, TRUE)

sqlpage/templates/card.handlebars

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@
2525
{{~#if id}} id="{{id}}"{{/if}}>
2626
<div class="card h-100
2727
{{~#if active}} card-active {{/if~}}
28-
{{~#if background_color}} bg-{{background_color}} text-{{background_color}}-fg
29-
{{~else}}{{#if embed}} bg-transparent border-0{{/if}}
30-
{{~/if~}}
28+
{{~#if background_color}}
29+
bg-{{background_color}} text-{{background_color}}-fg
30+
{{~else}}
31+
{{#if (all embed (not title) (not icon))}}
32+
bg-transparent border-0
33+
{{/if}}
34+
{{/if}}
3135
"
3236
{{#if (and embed (ne embed_mode "iframe"))}}data-pre-init="card" data-embed="{{embed}}"{{/if}}>
3337
{{#if link}}
@@ -37,9 +41,11 @@
3741
<img src="{{top_image}}" class="card-img-top" />
3842
{{/if}}
3943
{{#if color}}
40-
<div class="card-status-start bg-{{color}}"></div>
44+
{{#if (not embed)}}
45+
<div class="card-status-start bg-{{color}}"></div>
46+
{{/if}}
4147
{{/if}}
42-
<div class="card-body {{#if embed}}p-0{{/if}}">
48+
<div class="card-body {{#if (all embed (not title) (not icon))}}p-0{{/if}}">
4349
{{#if title}}<h2 class="card-title fs-3 me-3">{{title}}</h2>{{/if}}
4450
<div class="card-content remove-bottom-margin{{#if (and icon (not title))}} pe-4{{/if}}">
4551
{{~description~}}

src/template_helpers.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::borrow::Cow;
33
use crate::{app_config::AppConfig, utils::static_filename};
44
use anyhow::Context as _;
55
use handlebars::{
6-
handlebars_helper, Context, Handlebars, HelperDef, PathAndJson, RenderError, RenderErrorReason,
7-
Renderable, ScopedJson,
6+
handlebars_helper, Context, Handlebars, HelperDef, JsonTruthy, PathAndJson, RenderError,
7+
RenderErrorReason, Renderable, ScopedJson,
88
};
99
use serde_json::Value as JsonValue;
1010

@@ -19,6 +19,10 @@ type HH = fn(&JsonValue, &JsonValue) -> JsonValue;
1919

2020
pub fn register_all_helpers(h: &mut Handlebars<'_>, config: &AppConfig) {
2121
let site_prefix = config.site_prefix.clone();
22+
23+
register_helper(h, "all", HelperCheckTruthy(false));
24+
register_helper(h, "any", HelperCheckTruthy(true));
25+
2226
register_helper(h, "stringify", stringify_helper as H);
2327
register_helper(h, "parse_json", parse_json_helper as EH);
2428
register_helper(h, "default", default_helper as HH);
@@ -400,6 +404,26 @@ fn sum_helper<'reg, 'rc>(
400404
Ok(())
401405
}
402406

407+
/// Helper that returns the first argument with the given truthiness, or the last argument if none have it.
408+
/// Equivalent to a && b && c && ... if the truthiness is false,
409+
/// or a || b || c || ... if the truthiness is true.
410+
pub struct HelperCheckTruthy(bool);
411+
412+
impl CanHelp for HelperCheckTruthy {
413+
fn call(&self, args: &[PathAndJson]) -> Result<JsonValue, String> {
414+
for arg in args {
415+
if arg.value().is_truthy(false) == self.0 {
416+
return Ok(arg.value().clone());
417+
}
418+
}
419+
if let Some(last) = args.last() {
420+
Ok(last.value().clone())
421+
} else {
422+
Err("expected at least one argument".to_string())
423+
}
424+
}
425+
}
426+
403427
trait CanHelp: Send + Sync + 'static {
404428
fn call(&self, v: &[PathAndJson]) -> Result<JsonValue, String>;
405429
}

0 commit comments

Comments
 (0)