use crate::StaticCowStr;
#[derive(Debug, PartialEq, Eq, Default)]
pub struct ContextMenu {
pub id: &'static str,
pub content: StaticCowStr,
pub script: StaticCowStr,
pub styles: StaticCowStr,
}
impl ContextMenu {
pub fn new() -> Self {
ContextMenu::default()
}
pub fn add_id(mut self, id: &'static str) -> Self {
self.id = id;
self
}
pub fn add_content(mut self, content: StaticCowStr) -> Self {
self.content = content;
self
}
pub fn add_script(mut self, script: StaticCowStr) -> Self {
self.script = script;
self
}
pub fn add_style(mut self, styles: StaticCowStr) -> Self {
self.styles = styles;
self
}
pub fn build_script(&self) -> StaticCowStr {
StaticCowStr::Borrowed(
"
<script>
function hideCustomContextMenu() {
document.getElementById('",
) + self.id
+ "').style.display = \"none\";
}
// toggling the menu on right click to the page
function showCustomContextMenu(event) {
event.preventDefault();
var myContextMenu = document.getElementById('"
+ self.id
+ "');
if (myContextMenu.style.display == \"block\") {
myContextMenu.style.display = \"none\";
}
else {
myContextMenu.style.display = \"block\";
myContextMenu.style.left = event.pageX + \"px\";
myContextMenu.style.top = event.pageY + \"px\";
}
}
document.onclick = hideCustomContextMenu;
document.oncontextmenu = showCustomContextMenu;
</script>"
}
}