//! The task modal's markup: the `` over the board, its sections, and the plain-text renderer for //! descriptions and comments. //! //! Text from users is never markup. [`segments`] splits a text into lines, text runs and links; only //! addresses that start with `http://` or `https://` become links, with `rel="noopener noreferrer //! nofollow"`. Everything else is escaped by `view!`, so `` and `javascript:` addresses //! reach the page as plain characters. //! //! Forms carry the CSRF token and the modal's own URL as `next`. A viewer gets the same sections with no //! form, so the read-only view and the write view differ only in controls. use topcoat::Result; use topcoat::view::{View, component, view}; use crate::i18n::task::{K, t}; use crate::pages::board::BoardPage; use crate::pages::task::{TaskModal, task_link}; use crate::views::alert; use crate::views::board::flag; use crate::views::files::task_files_section; /// One piece of a plain-text body: a run of text, a link, or a line break. pub struct Segment { /// The text of the run, or the address of the link. Empty for a line break. pub text: String, /// Whether this piece is an address that becomes a link. pub link: bool, /// Whether this piece is a line break. pub brk: bool, } /// The text run `text` as a segment. fn run(text: &str) -> Segment { Segment { text: text.to_owned(), link: false, brk: false, } } /// The first `http://` or `https://` address in `text` that is longer than its scheme, as its byte range. /// Trailing sentence punctuation and brackets are not part of the address; whitespace and `<>"'` end it. fn next_link(text: &str) -> Option<(usize, usize)> { let lower = text.to_ascii_lowercase(); let mut from = 0; loop { let (offset, scheme) = ["http://", "https://"] .iter() .filter_map(|scheme| lower[from..].find(scheme).map(|at| (at, *scheme))) .min_by_key(|(at, _)| *at)?; let start = from + offset; let tail = &text[start..]; let length = tail .find(|c: char| c.is_whitespace() || matches!(c, '<' | '>' | '"' | '\'' | '`')) .unwrap_or(tail.len()); let mut end = start + length; while end > start && text[..end] .chars() .last() .is_some_and(|c| matches!(c, '.' | ',' | ';' | ':' | '!' | '?' | ')' | ']' | '}')) { end -= 1; } if end - start > scheme.len() { return Some((start, end)); } from = start + scheme.len(); } } /// Splits a plain-text body into segments: each line, its runs of text and its links, with a line break /// between lines. A carriage return before a line feed is dropped. pub fn segments(text: &str) -> Vec { let mut out = Vec::new(); for (index, line) in text.split('\n').enumerate() { if index > 0 { out.push(Segment { text: String::new(), link: false, brk: true, }); } let mut rest = line.strip_suffix('\r').unwrap_or(line); while let Some((start, end)) = next_link(rest) { if start > 0 { out.push(run(&rest[..start])); } out.push(Segment { text: rest[start..end].to_owned(), link: true, brk: false, }); rest = &rest[end..]; } if !rest.is_empty() { out.push(run(rest)); } } out } /// A plain-text body as markup: escaped text, line breaks, and links for http and https addresses only. #[component] pub async fn rich_text(text: &str) -> Result { let pieces = segments(text); Ok(view! {
for piece in pieces.iter() { if piece.brk {
} if piece.link { (piece.text.as_str()) } if !piece.link && !piece.brk { (piece.text.as_str()) } }
}) } /// The modal: the `` over the board. Escape and the close link return to the board with its /// filters (`board.js` reads `data-close-href`). Without JavaScript the dialog is shown in place. #[component] pub async fn task_modal(page: &BoardPage, modal: &TaskModal) -> Result { let lang = page.lang; let task = &modal.detail.task; let key = task.key.as_str(); let close = modal.close.as_str(); Ok(view! {

(key)

(t(lang, K::Close))
if let Some(message) = modal.error { alert(message: message) }

(task.title.as_str())

task_details(page: page, modal: modal) task_files(page: page, modal: modal) task_people(page: page, modal: modal) task_structure(page: page, modal: modal) task_links(page: page, modal: modal) task_comments(page: page, modal: modal) task_activity(page: page, modal: modal) task_delete(page: page, modal: modal)
}) } /// The fields as read, and for a member the edit form folded under them. The form opens by itself after a /// refusal, so the typed values stay in view. #[component] pub async fn task_details(page: &BoardPage, modal: &TaskModal) -> Result { let lang = page.lang; let task = &modal.detail.task; let deadline = task.deadline.map(|day| day.to_string()).unwrap_or_default(); let deadline = if deadline.is_empty() { "-".to_owned() } else { deadline }; let time = if modal.edit.time.is_empty() { "-".to_owned() } else { modal.edit.time.clone() }; let project = modal.detail.tag.name.as_str(); let id = task.id.as_str(); let edit_action = format!("/tasks/{id}/edit"); let csrf = page.viewer.csrf.as_str(); let next = modal.base.as_str(); let edit = &modal.edit; Ok(view! {
(t(lang, K::Project))
(project)
(t(lang, K::Deadline))
(deadline.as_str())
(t(lang, K::Meeting))
(time.as_str())
rich_text(text: task.description.as_str()) if modal.can_write {
(t(lang, K::EditComment))

(t(lang, K::MeetingHint))

(t(lang, K::DescriptionHint))

}
}) } /// The attachments of the task: the viewer when a file is open, the list and the upload form. #[component] pub async fn task_files(page: &BoardPage, modal: &TaskModal) -> Result { let id = modal.detail.task.id.as_str(); let csrf = page.viewer.csrf.as_str(); let next = modal.base.as_str(); Ok(view! { task_files_section( lang: page.lang, csrf: csrf, next: next, task_id: id, can_write: modal.can_write, files: modal.files.as_slice(), open: modal.viewer.as_ref(), ) }) } /// The assignees, with a remove control each, the picker for a new one, and the watch switch. #[component] pub async fn task_people(page: &BoardPage, modal: &TaskModal) -> Result { let lang = page.lang; let id = modal.detail.task.id.as_str(); let csrf = page.viewer.csrf.as_str(); let next = modal.base.as_str(); let assign_action = format!("/tasks/{id}/assign"); let unassign_action = format!("/tasks/{id}/unassign"); let watch_action = format!("/tasks/{id}/watch"); let watch_on = if modal.watching { "0" } else { "1" }; let watch_label = if modal.watching { t(lang, K::Unwatch) } else { t(lang, K::Watch) }; let watch_text = if modal.watching { t(lang, K::Watching) } else { t(lang, K::NotWatching) }; let assignees: Vec<(String, String)> = modal .detail .assignees .iter() .map(|a| (a.sub.clone(), a.name.clone())) .collect(); let picks: Vec<(String, String)> = modal .assignable .iter() .map(|member| (member.sub.clone(), member.name.clone())) .collect(); Ok(view! {

(t(lang, K::Assignees))

if assignees.is_empty() {

(t(lang, K::NobodyAssigned))

}
    for (sub, name) in assignees.iter() {
  • (name.as_str()) if modal.can_write {
    }
  • }
if modal.can_write && !picks.is_empty() {
}

(watch_text)

if modal.can_write {
}
}) } /// The subtasks, the form that adds one, and the parent picker. #[component] pub async fn task_structure(page: &BoardPage, modal: &TaskModal) -> Result { let lang = page.lang; let task = &modal.detail.task; let id = task.id.as_str(); let csrf = page.viewer.csrf.as_str(); let next = modal.base.as_str(); let subtask_action = format!("/tasks/{id}/subtasks"); let parent_action = format!("/tasks/{id}/parent"); let subtasks: Vec<(String, String)> = modal .detail .subtasks .iter() .map(|sub| { ( task_link(&modal.close, &sub.key), format!("{} {}", sub.key, sub.title), ) }) .collect(); let parent_id = task.parent_id.clone().unwrap_or_default(); let parents: Vec<(String, String, bool)> = modal .options .iter() .filter(|other| other.parent_id.is_none()) .map(|other| { ( other.id.clone(), format!("{} {}", other.key, other.title), other.id == parent_id, ) }) .collect(); let title_max = iz_core::limits::TITLE_MAX.to_string(); Ok(view! {

(t(lang, K::Subtasks))

if subtasks.is_empty() {

(t(lang, K::NoSubtasks))

} if modal.can_write && task.parent_id.is_none() {
} if modal.can_write {
}
}) } /// The dependencies in both directions, each with a remove control, and the form that adds one. #[component] pub async fn task_links(page: &BoardPage, modal: &TaskModal) -> Result { let lang = page.lang; let id = modal.detail.task.id.as_str(); let csrf = page.viewer.csrf.as_str(); let next = modal.base.as_str(); let remove_action = format!("/tasks/{id}/links/remove"); let add_action = format!("/tasks/{id}/links"); let waits: Vec<(String, String, String)> = modal .detail .blocked_by .iter() .map(|other| { ( other.id.clone(), task_link(&modal.close, &other.key), format!("{} {}", other.key, other.title), ) }) .collect(); let blocks: Vec<(String, String, String)> = modal .detail .blocks .iter() .map(|other| { ( other.id.clone(), task_link(&modal.close, &other.key), format!("{} {}", other.key, other.title), ) }) .collect(); let options: Vec<(String, String)> = modal .options .iter() .map(|other| (other.id.clone(), format!("{} {}", other.key, other.title))) .collect(); let nothing = waits.is_empty() && blocks.is_empty(); Ok(view! {

(t(lang, K::Dependencies))

if nothing {

(t(lang, K::NoLinks))

} if !waits.is_empty() {

(t(lang, K::BlockedBy))

    for (other_id, link, label) in waits.iter() {
  • (label.as_str()) if modal.can_write {
    }
  • }
} if !blocks.is_empty() {

(t(lang, K::Blocks))

    for (other_id, link, label) in blocks.iter() {
  • (label.as_str()) if modal.can_write {
    }
  • }
} if modal.can_write && !options.is_empty() {
}
}) } /// The comments, oldest first, with edit and delete for the ones the viewer may change, and the composer. #[component] pub async fn task_comments(page: &BoardPage, modal: &TaskModal) -> Result { let lang = page.lang; let id = modal.detail.task.id.as_str(); let csrf = page.viewer.csrf.as_str(); let next = modal.base.as_str(); let comment_action = format!("/tasks/{id}/comments"); let actions: Vec<(String, String)> = modal .comments .iter() .map(|c| { ( format!("/comments/{}/edit", c.id), format!("/comments/{}/delete", c.id), ) }) .collect(); let limit = iz_core::limits::COMMENT_MAX.to_string(); Ok(view! {

(t(lang, K::Comments))

if modal.comments.is_empty() {

(t(lang, K::NoComments))

}
    for (c, (edit_action, delete_action)) in modal.comments.iter().zip(actions.iter()) {
  1. (c.author.as_str()) " " if c.edited { " " (t(lang, K::Edited)) }

    rich_text(text: c.body.as_str()) if c.can_edit {
    (t(lang, K::EditComment))
    } if c.can_delete {
    }
  2. }
if modal.can_write {
}
}) } /// The activity feed, newest first, with the link to the next older page. #[component] pub async fn task_activity(page: &BoardPage, modal: &TaskModal) -> Result { let lang = page.lang; Ok(view! {

(t(lang, K::Activity))

if modal.activity.is_empty() {

(t(lang, K::NoActivity))

}
    for row in modal.activity.iter() {
  • " " (row.actor.as_str()) " " (row.sentence)
  • }
if let Some(older) = modal.older.as_ref() {

(t(lang, K::Older))

}
}) } /// Deleting the task. The first step is a link to `?confirm=delete`; the confirmation form posts the /// delete and returns to the board, without the task. #[component] pub async fn task_delete(page: &BoardPage, modal: &TaskModal) -> Result { let lang = page.lang; let id = modal.detail.task.id.as_str(); let csrf = page.viewer.csrf.as_str(); let delete_action = format!("/tasks/{id}/delete"); let confirm_url = format!("{}&confirm=delete", modal.base); let close = modal.close.as_str(); Ok(view! { if modal.can_write {
if modal.confirm_delete {

(t(lang, K::DeleteQuestion))

(t(lang, K::DeleteConsequence))

(t(lang, K::Close))
} else { (t(lang, K::DeleteTask)) }
} }) }