mj-context-menu
Version:
A generic context menu
119 lines • 2.97 kB
JavaScript
import { AbstractEntry } from './abstract_entry.js';
import * as MenuUtil from './menu_util.js';
import { HtmlClasses } from './html_classes.js';
export class AbstractItem extends AbstractEntry {
constructor(menu, type, _content, id) {
super(menu, type);
this._content = _content;
this.disabled = false;
this.callbacks = [];
this._id = id ? id : _content;
}
get content() {
return this._content;
}
set content(content) {
this._content = content;
this.generateHtml();
if (this.menu) {
this.menu.generateHtml();
}
}
get id() {
return this._id;
}
press() {
if (!this.disabled) {
this.executeAction();
this.executeCallbacks_();
}
}
executeAction() { }
registerCallback(func) {
if (this.callbacks.indexOf(func) === -1) {
this.callbacks.push(func);
}
}
unregisterCallback(func) {
const index = this.callbacks.indexOf(func);
if (index !== -1) {
this.callbacks.splice(index, 1);
}
}
mousedown(event) {
this.press();
this.stop(event);
}
mouseover(event) {
this.focus();
this.stop(event);
}
mouseout(event) {
this.deactivate();
this.stop(event);
}
generateHtml() {
super.generateHtml();
const html = this.html;
html.setAttribute('aria-disabled', 'false');
html.textContent = this.content;
}
activate() {
if (!this.disabled) {
this.html.classList.add(HtmlClasses['MENUACTIVE']);
}
}
deactivate() {
this.html.classList.remove(HtmlClasses['MENUACTIVE']);
}
focus() {
this.menu.focused = this;
super.focus();
this.activate();
}
unfocus() {
this.deactivate();
super.unfocus();
}
escape(_event) {
MenuUtil.close(this);
}
up(event) {
this.menu.up(event);
}
down(event) {
this.menu.down(event);
}
left(event) {
this.menu.left(event);
}
right(event) {
this.menu.right(event);
}
space(_event) {
this.press();
}
disable() {
this.disabled = true;
const html = this.html;
html.classList.add(HtmlClasses['MENUDISABLED']);
html.setAttribute('aria-disabled', 'true');
}
enable() {
this.disabled = false;
const html = this.html;
html.classList.remove(HtmlClasses['MENUDISABLED']);
html.removeAttribute('aria-disabled');
}
executeCallbacks_() {
for (const func of this.callbacks) {
try {
func(this);
}
catch (e) {
MenuUtil.error(e, 'Callback for menu entry ' + this.id + ' failed.');
}
}
}
}
//# sourceMappingURL=abstract_item.js.map