2 changed files with 72 additions and 49 deletions
@ -0,0 +1,67 @@ |
|||||
|
/** |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
/** |
||||
|
* |
||||
|
*/ |
||||
|
const DefaultContextMenu = { |
||||
|
openContextMenuAction: function (identifier, refPos) { |
||||
|
if (!refPos) { |
||||
|
refPos = function (event) { |
||||
|
return new Sides() |
||||
|
.left(event.pageX) |
||||
|
.top(event.pageY); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return function (event) { |
||||
|
event.preventDefault(); |
||||
|
|
||||
|
if (refPos instanceof Function) { |
||||
|
refPos = refPos(event); |
||||
|
} |
||||
|
|
||||
|
let menu = document.querySelector(`[data-autocompel="${identifier}"`); |
||||
|
|
||||
|
menu.style.left = `${refPos.getByIndex(SideDirections.LEFT)}px`; |
||||
|
menu.style.top = `${refPos.getByIndex(SideDirections.TOP)}px`; |
||||
|
|
||||
|
toggleElementVisibility(menu, true); |
||||
|
toggleElementVisibility(menu); |
||||
|
|
||||
|
document.addEventListener( |
||||
|
"click", |
||||
|
DefaultContextMenu.hideOnClickOutsideBoundsAction(identifier) |
||||
|
); |
||||
|
document.addEventListener( |
||||
|
"keyup", |
||||
|
DefaultContextMenu.hideOnEscapeAction(identifier) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
, |
||||
|
hideOnEscapeAction: function (identifier) { |
||||
|
return function (event) { |
||||
|
if (event.key === "Escape") { |
||||
|
let menu = document.querySelector(`[data-autocompel="${identifier}"`); |
||||
|
toggleElementVisibility(menu, true); |
||||
|
document.removeEventListener("keyup"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
, |
||||
|
hideOnClickOutsideBoundsAction: function (identifier) { |
||||
|
return function (event) { |
||||
|
let menu = document.querySelector(`[data-autocompel="${identifier}"`); |
||||
|
|
||||
|
let area = getEnclosingBounds(menu); |
||||
|
|
||||
|
if (!areXYInArea(area, event.clientX, event.clientY)) { |
||||
|
toggleElementVisibility(menu, true); |
||||
|
document.removeEventListener("click") |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
}; |
Loading…
Reference in new issue