X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/2c96af9aeafe2d4943a76cd69679ee7dcec737a3..HEAD:/resources/js/wysiwyg/ui/framework/blocks/dropdown-button.ts diff --git a/resources/js/wysiwyg/ui/framework/blocks/dropdown-button.ts b/resources/js/wysiwyg/ui/framework/blocks/dropdown-button.ts index a75cf64fe..45cb74dd4 100644 --- a/resources/js/wysiwyg/ui/framework/blocks/dropdown-button.ts +++ b/resources/js/wysiwyg/ui/framework/blocks/dropdown-button.ts @@ -1,30 +1,47 @@ -import {el} from "../../../helpers"; -import {handleDropdown} from "../helpers/dropdowns"; import {EditorContainerUiElement, EditorUiElement} from "../core"; import {EditorBasicButtonDefinition, EditorButton} from "../buttons"; +import {el} from "../../../utils/dom"; +import {EditorMenuButton} from "./menu-button"; + +export type EditorDropdownButtonOptions = { + showOnHover?: boolean; + direction?: 'vertical'|'horizontal'; + showAside?: boolean; + hideOnAction?: boolean; + button: EditorBasicButtonDefinition|EditorButton; +}; + +const defaultOptions: EditorDropdownButtonOptions = { + showOnHover: false, + direction: 'horizontal', + showAside: undefined, + hideOnAction: true, + button: {label: 'Menu'}, +} export class EditorDropdownButton extends EditorContainerUiElement { protected button: EditorButton; protected childItems: EditorUiElement[]; protected open: boolean = false; - protected showOnHover: boolean = false; + protected options: EditorDropdownButtonOptions; - constructor(button: EditorBasicButtonDefinition|EditorButton, showOnHover: boolean, children: EditorUiElement[]) { + constructor(options: EditorDropdownButtonOptions, children: EditorUiElement[]) { super(children); this.childItems = children; - this.showOnHover = showOnHover; + this.options = Object.assign({}, defaultOptions, options); - if (button instanceof EditorButton) { - this.button = button; + if (options.button instanceof EditorButton) { + this.button = options.button; } else { - this.button = new EditorButton({ - ...button, + const type = options.button.format === 'long' ? EditorMenuButton : EditorButton; + this.button = new type({ + ...options.button, action() { return false; }, isActive: () => { return this.open; - } + }, }); } @@ -41,7 +58,7 @@ export class EditorDropdownButton extends EditorContainerUiElement { const childElements: HTMLElement[] = this.childItems.map(child => child.getDOMElement()); const menu = el('div', { - class: 'editor-dropdown-menu', + class: `editor-dropdown-menu editor-dropdown-menu-${this.options.direction}`, hidden: 'true', }, childElements); @@ -49,8 +66,9 @@ export class EditorDropdownButton extends EditorContainerUiElement { class: 'editor-dropdown-menu-container', }, [button, menu]); - handleDropdown({toggle : button, menu : menu, - showOnHover: this.showOnHover, + this.getContext().manager.dropdowns.handle({toggle: button, menu : menu, + showOnHover: this.options.showOnHover, + showAside: typeof this.options.showAside === 'boolean' ? this.options.showAside : (this.options.direction === 'vertical'), onOpen : () => { this.open = true; this.getContext().manager.triggerStateUpdateForElement(this.button); @@ -59,6 +77,12 @@ export class EditorDropdownButton extends EditorContainerUiElement { this.getContext().manager.triggerStateUpdateForElement(this.button); }}); + if (this.options.hideOnAction) { + this.onEvent('button-action', () => { + this.getContext().manager.dropdowns.closeAll(); + }, wrapper); + } + return wrapper; } } \ No newline at end of file