import Component from "@glimmer/component"; import { tracked } from "@glimmer/tracking"; import { getOwner } from "@ember/application"; import { concat } from "@ember/helper"; import { action } from "@ember/object"; import { inject as service } from "@ember/service"; import { modifier } from "ember-modifier"; import { and } from "truth-helpers"; import DButton from "discourse/components/d-button"; import DModal from "discourse/components/d-modal"; import concatClass from "discourse/helpers/concat-class"; import { isTesting } from "discourse-common/config/environment"; import DFloatBody from "float-kit/components/d-float-body"; import { MENU } from "float-kit/lib/constants"; import DMenuInstance from "float-kit/lib/d-menu-instance"; export default class DMenu extends Component { @service menu; @service site; @tracked menuInstance = null; registerTrigger = modifier((element, [properties]) => { const options = { ...properties, ...{ autoUpdate: true, listeners: true, beforeTrigger: () => { this.menu.close(); }, }, }; const instance = new DMenuInstance(getOwner(this), element, options); this.menuInstance = instance; this.options.onRegisterApi?.(this.menuInstance); return () => { instance.destroy(); if (this.isDestroying) { this.menuInstance = null; } }; }); get menuId() { return `d-menu-${this.menuInstance.id}`; } get options() { return this.menuInstance?.options ?? {}; } get componentArgs() { return { close: this.menuInstance.close, data: this.options.data, }; } @action allowedProperties() { const properties = {}; Object.keys(MENU.options).forEach((key) => { const value = MENU.options[key]; properties[key] = this.args[key] ?? value; }); return properties; } }