/** * A context menu is a kind of popover which contains a list of options. */ class ContextMenu extends Popover { /** * The constructor accepts a DOM element as a parameter. This is the content view of the popover. */ constructor(element = null) { super(element); element.addEventListener("click", this.onElementClicked.bind(this)); } /** * Used to define the view controller which should be the first responder for actions within the context menu. */ setViewController(viewController) { this.viewController = viewController; } /** * Used to define the view instance which should be the first responder for actions within the context menu. */ setView(view) { this.view = view; } onElementClicked(event) { var checkingElement = event.target; while (checkingElement != event.currentTarget) { if (checkingElement.hasAttribute("action")) { var action = checkingElement.getAttribute("action"); if (this.viewController && (action in this.viewController)) { //Call the action function on the view controller. this.viewController[action](event); } if (this.view && (action in this.view)) { //Call the action function on the view instance. this.view[action](event); } } checkingElement = checkingElement.parentNode; } this.dismiss(); } } UIKit.registerPopoverType(ContextMenu, "context-menu");