use cfg_if::cfg_if;
use cfg_match::cfg_match;
cfg_if! {
if #[cfg(feature = "std_web")] {
use stdweb::Value;
#[allow(unused_imports)]
use stdweb::{_js_impl, js};
} else if #[cfg(feature = "web_sys")] {
use crate::utils;
}
}
#[derive(Default, Debug)]
pub struct DialogService {}
impl DialogService {
pub fn alert(message: &str) {
cfg_match! {
feature = "std_web" => js! { @(no_return) alert(@{message}); },
feature = "web_sys" => utils::window().alert_with_message(message).unwrap(),
};
}
pub fn confirm(message: &str) -> bool {
cfg_match! {
feature = "std_web" => ({
let value: Value = js! { return confirm(@{message}); };
match value {
Value::Bool(result) => result,
_ => false,
}
}),
feature = "web_sys" => utils::window().confirm_with_message(message).unwrap(),
}
}
#[cfg_attr(
feature = "web_sys",
doc = "A default value can be supplied which will be returned if the user doesn't input anything."
)]
#[cfg_attr(
feature = "web_sys",
doc = "This function will return `None` if the value of `default` is `None` and the user \
cancels the operation. (normally a 'cancel' button will be displayed to the user, \
clicking which cancels the operation)."
)]
#[cfg_attr(
feature = "std_web",
doc = "This function will return `None` if the user cancels the operation (normally a \
'cancel' button will be displayed to the user, clicking which cancels the operation)."
)]
pub fn prompt(
message: &str,
#[cfg(feature = "web_sys")] default: Option<&str>,
) -> Option<String> {
cfg_if! {
if #[cfg(feature="web_sys")] {
if let Some(default) = default {
utils::window()
.prompt_with_message_and_default(message, default)
.expect("Couldn't read input.")
}
else {
utils::window()
.prompt_with_message(message)
.expect("Couldn't read input.")
}
} else if #[cfg(feature="std_web")] {
let value: Value = js! { return prompt(@{message}); };
match value {
Value::String(result) => Some(result),
_ => None,
}
}
}
}
}