1 import {EditorContainerUiElement, EditorUiElement} from "../core";
2 import {EditorBasicButtonDefinition, EditorButton} from "../buttons";
3 import {el} from "../../../utils/dom";
4 import {EditorMenuButton} from "./menu-button";
6 export type EditorDropdownButtonOptions = {
8 direction?: 'vertical'|'horizontal';
10 hideOnAction?: boolean;
11 button: EditorBasicButtonDefinition|EditorButton;
14 const defaultOptions: EditorDropdownButtonOptions = {
16 direction: 'horizontal',
19 button: {label: 'Menu'},
22 export class EditorDropdownButton extends EditorContainerUiElement {
23 protected button: EditorButton;
24 protected childItems: EditorUiElement[];
25 protected open: boolean = false;
26 protected options: EditorDropdownButtonOptions;
28 constructor(options: EditorDropdownButtonOptions, children: EditorUiElement[]) {
30 this.childItems = children;
31 this.options = Object.assign({}, defaultOptions, options);
33 if (options.button instanceof EditorButton) {
34 this.button = options.button;
36 const type = options.button.format === 'long' ? EditorMenuButton : EditorButton;
37 this.button = new type({
48 this.addChildren(this.button);
51 insertItems(...items: EditorUiElement[]) {
52 this.addChildren(...items);
53 this.childItems.push(...items);
56 protected buildDOM(): HTMLElement {
57 const button = this.button.getDOMElement();
59 const childElements: HTMLElement[] = this.childItems.map(child => child.getDOMElement());
60 const menu = el('div', {
61 class: `editor-dropdown-menu editor-dropdown-menu-${this.options.direction}`,
65 const wrapper = el('div', {
66 class: 'editor-dropdown-menu-container',
69 this.getContext().manager.dropdowns.handle({toggle: button, menu : menu,
70 showOnHover: this.options.showOnHover,
71 showAside: typeof this.options.showAside === 'boolean' ? this.options.showAside : (this.options.direction === 'vertical'),
74 this.getContext().manager.triggerStateUpdateForElement(this.button);
77 this.getContext().manager.triggerStateUpdateForElement(this.button);
80 if (this.options.hideOnAction) {
81 this.onEvent('button-action', () => {
82 this.getContext().manager.dropdowns.closeAll();