prettier tags in table

session-crate
Ondřej Hruška 5 years ago
parent 21baa4810c
commit 52676cf227
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 2
      src/main.rs
  2. 14
      src/store/form.rs
  3. 2
      templates/_form_macros.html.tera
  4. 14
      templates/index.html.tera
  5. 13
      templates/static/style.css

@ -54,7 +54,7 @@ fn route_index(store: State<RwLock<Store>>, page: Option<usize>) -> Template {
let mut page = page.unwrap_or_default(); let mut page = page.unwrap_or_default();
let n_pages = (rg.data.cards.len() as f64 / PER_PAGE as f64).ceil() as usize; let n_pages = (rg.data.cards.len() as f64 / PER_PAGE as f64).ceil() as usize;
if page >= n_pages { if page >= n_pages && page != 0 {
page = n_pages - 1; page = n_pages - 1;
} }

@ -23,6 +23,7 @@ pub struct RenderedField<'a> {
pub max: String, pub max: String,
pub all_tags_json: String, pub all_tags_json: String,
pub tags_json: String, pub tags_json: String,
pub tags: Option<Vec<String>>,
pub options: Option<Vec<&'a String>>, pub options: Option<Vec<&'a String>>,
pub value: Cow<'a, str>, pub value: Cow<'a, str>,
pub checked: bool, pub checked: bool,
@ -118,7 +119,6 @@ impl<'a> RenderedField<'a> {
FieldKind::Tags { options } => { FieldKind::Tags { options } => {
rendered.kind = "tags"; rendered.kind = "tags";
rendered.options = Some(options.iter().collect()); rendered.options = Some(options.iter().collect());
rendered.all_tags_json = serde_json::to_string(options).unwrap(); rendered.all_tags_json = serde_json::to_string(options).unwrap();
} }
FieldKind::FreeTags { tag_group } => { FieldKind::FreeTags { tag_group } => {
@ -145,12 +145,12 @@ impl<'a> RenderedField<'a> {
} }
FieldKind::Tags { .. } | FieldKind::FreeTags { .. } => { FieldKind::Tags { .. } | FieldKind::FreeTags { .. } => {
if let Some(v) = value { if let Some(v) = value {
rendered.value = serde_json::from_value::<Vec<String>>(v.clone()) let tags = serde_json::from_value::<Vec<String>>(v.clone()).unwrap();
.unwrap() rendered.value = tags.join(",").into();
.join(" ") rendered.tags = Some(tags);
.into();
rendered.tags_json = serde_json::to_string(v).unwrap(); rendered.tags_json = serde_json::to_string(v).unwrap();
} else { } else {
rendered.tags = Some(vec![]);
rendered.tags_json = "[]".into(); rendered.tags_json = "[]".into();
} }
} }
@ -274,7 +274,7 @@ pub fn collect_card_form(store: &Store, mut form: MapFromForm) -> IndexMap<Strin
} }
FieldKind::Tags { options } => { FieldKind::Tags { options } => {
let mut tags: Vec<String> = input let mut tags: Vec<String> = input
.split(' ') .split(',')
.map(ToOwned::to_owned) .map(ToOwned::to_owned)
.filter_map(|tag| { .filter_map(|tag| {
if options.contains(&tag) { if options.contains(&tag) {
@ -291,7 +291,7 @@ pub fn collect_card_form(store: &Store, mut form: MapFromForm) -> IndexMap<Strin
} }
FieldKind::FreeTags { .. } => { FieldKind::FreeTags { .. } => {
let mut tags: Vec<String> = input let mut tags: Vec<String> = input
.split(' ') .split(',')
.map(str::trim) .map(str::trim)
.filter(|s| !s.is_empty()) .filter(|s| !s.is_empty())
.map(ToOwned::to_owned) .map(ToOwned::to_owned)

@ -104,7 +104,7 @@
let hiden_field = document.querySelector('#field-{{field.key}}'); let hiden_field = document.querySelector('#field-{{field.key}}');
let onchange = () => { let onchange = () => {
let values = taggle.tag.values.join(" "); let values = taggle.tag.values.join(",");
hiden_field.setAttribute("value", values); hiden_field.setAttribute("value", values);
}; };

@ -28,17 +28,27 @@
<a href="/delete/{{card.id}}" onclick="return confirm('Delete card?')">Delete</a> <a href="/delete/{{card.id}}" onclick="return confirm('Delete card?')">Delete</a>
</td> </td>
{%- for field in card.fields %} {%- for field in card.fields %}
<td>
{%- if field.kind == "bool" -%} {%- if field.kind == "bool" -%}
<td>
{% if field.checked %} {% if field.checked %}
{% else %} {% else %}
{% endif %} {% endif %}
</td>
{%- elif field.kind == "tags" or field.kind == "free_tags" -%}
<td class="tags">
{% for tag in field.tags %}
<span class="tag">{{ tag }}</span>
{% endfor %}
</td>
{%- else -%} {%- else -%}
<td>
{{ field.value }} {{ field.value }}
{%- endif -%}
</td> </td>
{%- endif -%}
{%- endfor %} {%- endfor %}
</tr> </tr>
{% endfor %} {% endfor %}

@ -143,6 +143,18 @@ textarea:focus,
border-bottom: 2px solid silver; border-bottom: 2px solid silver;
} }
.cards-table td.tags {
padding: .25rem;
}
.cards-table .tag {
background: #E2E1DF;
font-size: 90%;
padding: 0.25rem .45rem;
border-radius: 3px;
display: inline-block;
}
.paginate { .paginate {
margin: 1rem auto; margin: 1rem auto;
width: 300px; width: 300px;
@ -182,3 +194,4 @@ textarea:focus,
color: gray; color: gray;
background: transparent; background: transparent;
} }

Loading…
Cancel
Save