1 import {handleDropdown} from "../helpers/dropdowns";
2 import {EditorContainerUiElement, EditorUiElement} from "../core";
3 import {EditorBasicButtonDefinition, EditorButton} from "../buttons";
4 import {el} from "../../../utils/dom";
5 import {EditorMenuButton} from "./menu-button";
7 export type EditorDropdownButtonOptions = {
9 direction?: 'vertical'|'horizontal';
11 button: EditorBasicButtonDefinition|EditorButton;
14 const defaultOptions: EditorDropdownButtonOptions = {
16 direction: 'horizontal',
18 button: {label: 'Menu'},
21 export class EditorDropdownButton extends EditorContainerUiElement {
22 protected button: EditorButton;
23 protected childItems: EditorUiElement[];
24 protected open: boolean = false;
25 protected options: EditorDropdownButtonOptions;
27 constructor(options: EditorDropdownButtonOptions, children: EditorUiElement[]) {
29 this.childItems = children;
30 this.options = Object.assign({}, defaultOptions, options);
32 if (options.button instanceof EditorButton) {
33 this.button = options.button;
35 const type = options.button.format === 'long' ? EditorMenuButton : EditorButton;
36 this.button = new type({
47 this.addChildren(this.button);
50 insertItems(...items: EditorUiElement[]) {
51 this.addChildren(...items);
52 this.childItems.push(...items);
55 protected buildDOM(): HTMLElement {
56 const button = this.button.getDOMElement();
58 const childElements: HTMLElement[] = this.childItems.map(child => child.getDOMElement());
59 const menu = el('div', {
60 class: `editor-dropdown-menu editor-dropdown-menu-${this.options.direction}`,
64 const wrapper = el('div', {
65 class: 'editor-dropdown-menu-container',
68 handleDropdown({toggle: button, menu : menu,
69 showOnHover: this.options.showOnHover,
70 showAside: typeof this.options.showAside === 'boolean' ? this.options.showAside : (this.options.direction === 'vertical'),
73 this.getContext().manager.triggerStateUpdateForElement(this.button);
76 this.getContext().manager.triggerStateUpdateForElement(this.button);