add-data rendering & clipboard takes element

This commit is contained in:
realaravinth 2021-07-21 10:42:25 +05:30
parent 4b18992f6a
commit b603208d48
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
5 changed files with 37 additions and 78 deletions

View file

@ -17,69 +17,29 @@
use std::fmt::Display; use std::fmt::Display;
use sailfish::runtime::Render;
use sailfish::TemplateOnce; use sailfish::TemplateOnce;
#[derive(Clone, TemplateOnce)] #[derive(Clone, TemplateOnce)]
#[template(path = "auth/sudo/index.html")] #[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, url: &'a str,
data: Option<String>, data: Option<Vec<(K, V)>>,
} }
pub const PAGE: &str = "Confirm Access"; 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<Vec<(&'a str, &'a str)>>) -> Self { //pub fn new(url: &'a str, data: Option<Vec<(&'a str, &'a str)>>) -> Self {
pub fn new<K, V>(url: &'a str, data: Option<Vec<(K, V)>>) -> Self pub fn new(url: &'a str, data: Option<Vec<(K, V)>>) -> 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
};
Self { url, data } 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::<String, String>("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::<String, String>("foo", Some(Vec::default()))
.data
.is_none());
}
}

View file

@ -1,3 +1,9 @@
<. if data.is_some() && !data.as_ref().unwrap().is_empty() { .> <. let data = if let Some(data) = data { .>
<div id="additional-data" <.= data.unwrap() .>></div> <. if !data.is_empty() { .>
<. } .> <div id='additional-data'
<. for (k, v) in data.iter() { .>
data-<.= k .>=<.= v .>
<. } .>
></div>
<. } .>
<. }; .>

View file

@ -16,16 +16,16 @@
*/ */
class CopyIcon { class CopyIcon {
copyIconClass: string; copyIcon: HTMLElement;
copyDoneIconClass: string; copyDoneIconClass: string;
writeText: string; writeText: string;
constructor( constructor(
writeText: string, writeText: string,
copyIconClass: string, copyIcon: HTMLElement,
copyDoneIconClass: string, copyDoneIconClass: string,
) { ) {
this.copyIconClass = copyIconClass; this.copyIcon = copyIcon;
this.copyDoneIconClass = copyDoneIconClass; this.copyDoneIconClass = copyDoneIconClass;
this.writeText = writeText; this.writeText = writeText;
@ -33,10 +33,7 @@ class CopyIcon {
} }
__registerHandlers() { __registerHandlers() {
const icons = document.querySelectorAll(`.${this.copyIconClass}`); this.copyIcon.addEventListener('click', e => this.copySitekey(e));
icons.forEach(icon => {
icon.addEventListener('click', e => this.copySitekey(e));
});
} }
/* /*
@ -44,11 +41,6 @@ class CopyIcon {
*/ */
async copySitekey(e: Event) { async copySitekey(e: Event) {
const image = <HTMLElement>e.target; const image = <HTMLElement>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 = <HTMLElement>( const copyDoneIcon = <HTMLElement>(
image.parentElement.querySelector(`.${this.copyDoneIconClass}`) image.parentElement.querySelector(`.${this.copyDoneIconClass}`)
); );

View file

@ -28,7 +28,7 @@ const index = () => {
document.querySelector(`.${SECRET_COPY_ICON}`) document.querySelector(`.${SECRET_COPY_ICON}`)
); );
const writeText = secretElement.dataset.secret; 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; export default index;

View file

@ -20,13 +20,14 @@ const SITEKEY_COPY_ICON = `sitekey__copy-icon`;
const SITEKEY_COPY_DONE_ICON = `sitekey__copy-done-icon`; const SITEKEY_COPY_DONE_ICON = `sitekey__copy-done-icon`;
export const index = () => { export const index = () => {
const image = <HTMLElement>document.querySelector(`.${SITEKEY_COPY_ICON}`); const image = document.querySelectorAll(`.${SITEKEY_COPY_ICON}`);
if (!image.classList.contains(SITEKEY_COPY_ICON)) { image.forEach((img: HTMLElement) => {
throw new Error( if (!img.classList.contains(SITEKEY_COPY_ICON)) {
'This method should only be called when sitekey copy button/icon is clicked', throw new Error(
); 'This method should only be called when sitekey copy button/icon is clicked',
} );
const sitekey = image.dataset.sitekey; }
const sitekey = img.dataset.sitekey;
new CopyIcon(sitekey, SITEKEY_COPY_ICON, SITEKEY_COPY_DONE_ICON); new CopyIcon(sitekey, img, SITEKEY_COPY_DONE_ICON);
});
}; };