import { isString, isFunction } from '../core/util';
import { on, createEl, addClass, setStyle, preventDefault } from '../core/util/dom';
import Point from '../geo/Point';
import type { Geometry } from '../geometry';
import type { Map } from '../map';
import UIComponent, { UIComponentOptionsType } from './UIComponent';
/**
* @property {Object} options
* @property {Boolean} [options.autoPan=false] - set it to false if you don't want the map to do panning animation to fit the opened menu.
* @property {Number} [options.width=160] - default width
* @property {Number} [options.maxHeight=0] - default max-height
* @property {String|HTMLElement} [options.custom=false] - set it to true if you want a customized menu, customized html codes or a HTMLElement is set to items.
* @property {Object[]|String|HTMLElement} options.items - html code or a html element is options.custom is true. Or a menu items array, containing: item objects, "-" as a splitor line
* @memberOf ui.Menu
* @instance
*/
const defaultOptions: MenuOptionsType = {
'containerClass': 'maptalks-menu',
'animation': null,
'animationDelay': 10,
'animationOnHide': false,
'autoPan': false,
'width': 160,
'maxHeight': 0,
'custom': false,
'items': []
};
/**
* @classdesc
* Class for context menu, useful for interactions with right clicks on the map.
* @category ui
* @extends ui.UIComponent
* @memberOf ui
*/
class Menu extends UIComponent {
options: MenuOptionsType;
/**
* Menu items is set to options.items or by setItems method.
*
* Normally items is a object array, containing:
* 1. item object: {'item': 'This is a menu text', 'click': function() {alert('oops! You clicked!');)}}
* 2. minus string "-", which will draw a splitor line on the menu.
*
* If options.custom is set to true, the menu is considered as a customized one. Then items is the customized html codes or HTMLElement.
* @param {Object} options - options defined in [ui.Menu]{@link ui.Menu#options}
*/
constructor(options: MenuOptionsType) {
super(options);
}
// TODO: obtain class in super
//@internal
_getClassName() {
return 'Menu';
}
addTo(owner: Geometry | Map) {
if (owner._menu && owner._menu !== this) {
owner.removeMenu();
}
owner._menu = this;
this._owner = owner;
return UIComponent.prototype.addTo.apply(this, [owner]);
}
/**
* Set the items of the menu.
* @param {Object[]|String|HTMLElement} items - items of the menu
* return {ui.Menu} this
* @example
* menu.setItems([
* //return false to prevent event propagation
* {'item': 'Query', 'click': function() {alert('Query Clicked!'); return false;}},
* '-',
* {'item': 'Edit', 'click': function() {alert('Edit Clicked!')}},
* {'item': 'About', 'click': function() {alert('About Clicked!')}}
* ]);
*/
setItems(items: Array