/*************************************************************
*
* Copyright (c) 2015-2016 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Abstract class of menu items.
*
* @author volker.sorge@gmail.com (Volker Sorge)
*/
/// <reference path="abstract_entry.ts" />
/// <reference path="abstract_menu.ts" />
/// <reference path="item.ts" />
/// <reference path="menu.ts" />
/// <reference path="menu_util.ts" />
var ContextMenu;
(function (ContextMenu) {
class AbstractItem extends ContextMenu.AbstractEntry {
/**
* @constructor
* @implements {Item}
* @extends {AbstractEntry}
* @param {Menu} menu The context menu or sub-menu the item belongs to.
* @param {string} type The type of the entry.
* @param {string} content The content of the menu item.
* @param {string=} id Optionally the id of the menu item.
*/
constructor(menu, type, content, id) {
super(menu, type);
/**
* Flag indicating if element is disabled.
* @type {boolean}
*/
this.disabled = false;
this.callbacks = [];
this.content = content;
this.id = id ? id : content;
}
/**
* @override
*/
getContent() {
return this.content;
}
/**
* @override
*/
getId() {
return this.id;
}
/**
* @override
*/
press() {
if (!this.disabled) {
this.executeAction();
this.executeCallbacks_();
}
}
/**
* Execute the item's action if it is not disabled.
*/
executeAction() { }
/**
* Registers a callback function.
* @param {Function} func Callback that does not take any arguments.
* @final
*/
registerCallback(func) {
if (this.callbacks.indexOf(func) === -1) {
this.callbacks.push(func);
}
}
/**
* Removes a callback function.
* @param {Function} func Callback that does not take any arguments.
* @final
*/
unregisterCallback(func) {
let index = this.callbacks.indexOf(func);
if (index !== -1) {
this.callbacks.splice(index, 1);
}
}
/**
* @override
*/
mousedown(event) {
this.press();
this.stop(event);
}
/**
* @override
*/
mouseover(event) {
this.focus();
this.stop(event);
}
/**
* @override
*/
mouseout(event) {
this.deactivate();
this.stop(event);
}
/**
* @override
*/
generateHtml() {
super.generateHtml();
let html = this.getHtml();
html.setAttribute('aria-disabled', 'false');
html.textContent = this.content;
}
/**
* Sets active style for item.
*/
activate() {
if (!this.disabled) {
this.getHtml().classList.add(ContextMenu.HtmlClasses['MENUACTIVE']);
}
}
/**
* Removes active style from item.
*/
deactivate() {
this.getHtml().classList.remove(ContextMenu.HtmlClasses['MENUACTIVE']);
}
/**
* @override
*/
focus() {
this.getMenu().setFocused(this);
super.focus();
this.activate();
}
/**
* @override
*/
unfocus() {
this.deactivate();
super.unfocus();
}
/**
* @override
*/
escape(event) {
ContextMenu.MenuUtil.close(this);
}
/**
* @override
*/
up(event) {
this.getMenu().up(event);
}
/**
* @override
*/
down(event) {
this.getMenu().down(event);
}
//// TODO: RTL change of direction.
/**
* @override
*/
left(event) {
if (this.getMenu() instanceof ContextMenu.ContextMenu) {
this.getMenu().left(event);
return;
}
let menu = this.getMenu();
menu.setFocused(null);
menu.getAnchor().focus();
}
/**
* @override
*/
right(event) {
this.getMenu().right(event);
}
/**
* @override
*/
space(event) {
this.press();
}
/**
* @override
*/
disable() {
this.disabled = true;
let html = this.getHtml();
html.classList.add(ContextMenu.HtmlClasses['MENUDISABLED']);
html.setAttribute('aria-disabled', 'true');
}
/**
* @override
*/
enable() {
this.disabled = false;
let html = this.getHtml();
html.classList.remove(ContextMenu.HtmlClasses['MENUDISABLED']);
html.removeAttribute('aria-disabled');
}
/**
* Executes the additional callbacks registered with this menu item.
*/
executeCallbacks_() {
let active = ContextMenu.MenuUtil.getActiveElement(this);
for (let func of this.callbacks) {
try {
func(event);
}
catch (e) {
ContextMenu.MenuUtil.error(e, 'Callback for menu entry ' + this.getId() +
' failed.');
}
}
}
}
ContextMenu.AbstractItem = AbstractItem;
})(ContextMenu || (ContextMenu = {}));