From b603208d4864f7d2aa06fe6188a5a033e8045847 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Wed, 21 Jul 2021 10:42:25 +0530 Subject: [PATCH] add-data rendering & clipboard takes element --- src/pages/auth/sudo.rs | 66 ++++--------------- .../components/additional-data/index.html | 12 +++- templates/components/clipboard/index.ts | 16 ++--- templates/panel/settings/index.ts | 2 +- templates/panel/sitekey/list/ts/index.ts | 19 +++--- 5 files changed, 37 insertions(+), 78 deletions(-) diff --git a/src/pages/auth/sudo.rs b/src/pages/auth/sudo.rs index 7d81e2d0..964db8fc 100644 --- a/src/pages/auth/sudo.rs +++ b/src/pages/auth/sudo.rs @@ -17,69 +17,29 @@ use std::fmt::Display; +use sailfish::runtime::Render; use sailfish::TemplateOnce; #[derive(Clone, TemplateOnce)] #[template(path = "auth/sudo/index.html")] -pub struct SudoPage<'a> { +pub struct SudoPage<'a, K, V> +where + K: Display + Render, + V: Display + Render, +{ url: &'a str, - data: Option, + data: Option>, } pub const PAGE: &str = "Confirm Access"; -impl<'a> SudoPage<'a> { +impl<'a, K, V> SudoPage<'a, K, V> +where + K: Display + Render, + V: Display + Render, +{ //pub fn new(url: &'a str, data: Option>) -> Self { - pub fn new(url: &'a str, data: Option>) -> Self - where - K: Display, - V: Display, - { - let data = if let Some(data) = data { - if !data.is_empty() { - let mut s = String::new(); - for (k, v) in data.iter() { - s.push_str(&format!(" data-{}={}", k, v)); - } - Some(s) - } else { - None - } - } else { - None - }; - + pub fn new(url: &'a str, data: Option>) -> Self { Self { url, data } } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn sudo_page_works() { - let data = vec![ - ("firefox", "mozilla"), - ("chrome", "google"), - ("servo", "mozilla"), - ]; - assert!(SudoPage::new::("foo", None).data.is_none()); - - let sudo = SudoPage::new("foo", Some(data.clone())); - - data.iter().for_each(|(k, v)| { - assert!( - sudo.data.as_ref().unwrap().contains(k) - && sudo.data.as_ref().unwrap().contains(v) - ) - }); - - let data_str = " data-firefox=mozilla data-chrome=google data-servo=mozilla"; - assert_eq!(sudo.data.as_ref().unwrap(), data_str); - - assert!(SudoPage::new::("foo", Some(Vec::default())) - .data - .is_none()); - } -} diff --git a/templates/components/additional-data/index.html b/templates/components/additional-data/index.html index eda10e56..cd873ec8 100644 --- a/templates/components/additional-data/index.html +++ b/templates/components/additional-data/index.html @@ -1,3 +1,9 @@ -<. if data.is_some() && !data.as_ref().unwrap().is_empty() { .> -
>
-<. } .> +<. let data = if let Some(data) = data { .> +<. if !data.is_empty() { .> +
+ data-<.= k .>=<.= v .> +<. } .> + >
+ <. } .> +<. }; .> diff --git a/templates/components/clipboard/index.ts b/templates/components/clipboard/index.ts index 31844109..02ca75e8 100644 --- a/templates/components/clipboard/index.ts +++ b/templates/components/clipboard/index.ts @@ -16,16 +16,16 @@ */ class CopyIcon { - copyIconClass: string; + copyIcon: HTMLElement; copyDoneIconClass: string; writeText: string; constructor( writeText: string, - copyIconClass: string, + copyIcon: HTMLElement, copyDoneIconClass: string, ) { - this.copyIconClass = copyIconClass; + this.copyIcon = copyIcon; this.copyDoneIconClass = copyDoneIconClass; this.writeText = writeText; @@ -33,10 +33,7 @@ class CopyIcon { } __registerHandlers() { - const icons = document.querySelectorAll(`.${this.copyIconClass}`); - icons.forEach(icon => { - icon.addEventListener('click', e => this.copySitekey(e)); - }); + this.copyIcon.addEventListener('click', e => this.copySitekey(e)); } /* @@ -44,11 +41,6 @@ class CopyIcon { */ async copySitekey(e: Event) { const image = e.target; - if (!image.classList.contains(this.copyIconClass)) { - throw new Error( - 'This method should only be called when sitekey copy button/icon is clicked', - ); - } const copyDoneIcon = ( image.parentElement.querySelector(`.${this.copyDoneIconClass}`) ); diff --git a/templates/panel/settings/index.ts b/templates/panel/settings/index.ts index 870ad342..a223c754 100644 --- a/templates/panel/settings/index.ts +++ b/templates/panel/settings/index.ts @@ -28,7 +28,7 @@ const index = () => { document.querySelector(`.${SECRET_COPY_ICON}`) ); const writeText = secretElement.dataset.secret; - new CopyIcon(writeText, SECRET_COPY_ICON, SECRET_COPY_DONE_ICON); + new CopyIcon(writeText, secretElement, SECRET_COPY_DONE_ICON); }; export default index; diff --git a/templates/panel/sitekey/list/ts/index.ts b/templates/panel/sitekey/list/ts/index.ts index 8eca0c9f..0a0ad9f1 100644 --- a/templates/panel/sitekey/list/ts/index.ts +++ b/templates/panel/sitekey/list/ts/index.ts @@ -20,13 +20,14 @@ const SITEKEY_COPY_ICON = `sitekey__copy-icon`; const SITEKEY_COPY_DONE_ICON = `sitekey__copy-done-icon`; export const index = () => { - const image = document.querySelector(`.${SITEKEY_COPY_ICON}`); - if (!image.classList.contains(SITEKEY_COPY_ICON)) { - throw new Error( - 'This method should only be called when sitekey copy button/icon is clicked', - ); - } - const sitekey = image.dataset.sitekey; - - new CopyIcon(sitekey, SITEKEY_COPY_ICON, SITEKEY_COPY_DONE_ICON); + const image = document.querySelectorAll(`.${SITEKEY_COPY_ICON}`); + image.forEach((img: HTMLElement) => { + if (!img.classList.contains(SITEKEY_COPY_ICON)) { + throw new Error( + 'This method should only be called when sitekey copy button/icon is clicked', + ); + } + const sitekey = img.dataset.sitekey; + new CopyIcon(sitekey, img, SITEKEY_COPY_DONE_ICON); + }); };