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';
10 button: EditorBasicButtonDefinition|EditorButton;
13 const defaultOptions: EditorDropdownButtonOptions = {
15 direction: 'horizontal',
16 button: {label: 'Menu'},
19 export class EditorDropdownButton extends EditorContainerUiElement {
20 protected button: EditorButton;
21 protected childItems: EditorUiElement[];
22 protected open: boolean = false;
23 protected options: EditorDropdownButtonOptions;
25 constructor(options: EditorDropdownButtonOptions, children: EditorUiElement[]) {
27 this.childItems = children;
28 this.options = Object.assign({}, defaultOptions, options);
30 if (options.button instanceof EditorButton) {
31 this.button = options.button;
33 const type = options.button.format === 'long' ? EditorMenuButton : EditorButton;
34 this.button = new type({
45 this.addChildren(this.button);
48 insertItems(...items: EditorUiElement[]) {
49 this.addChildren(...items);
50 this.childItems.push(...items);
53 protected buildDOM(): HTMLElement {
54 const button = this.button.getDOMElement();
56 const childElements: HTMLElement[] = this.childItems.map(child => child.getDOMElement());
57 const menu = el('div', {
58 class: `editor-dropdown-menu editor-dropdown-menu-${this.options.direction}`,
62 const wrapper = el('div', {
63 class: 'editor-dropdown-menu-container',
66 handleDropdown({toggle: button, menu : menu,
67 showOnHover: this.options.showOnHover,
70 this.getContext().manager.triggerStateUpdateForElement(this.button);
73 this.getContext().manager.triggerStateUpdateForElement(this.button);