DEV: allows to get a menu by its identifier (#26968)

Usage:

```javascript
this.menu.getByIdentifier("foo");
```
This commit is contained in:
Joffrey JAFFEUX 2024-05-10 12:00:58 +02:00 committed by GitHub
parent 38c1312dfb
commit 46371fe9e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 2 deletions

View File

@ -265,4 +265,17 @@ module("Integration | Component | FloatKit | d-menu", function (hooks) {
assert.dom(".fk-d-menu__content.test-content").doesNotExist();
});
test("get a menu by identifier", async function (assert) {
await render(hbs`<DMenu @inline={{true}} @identifier="test">test</DMenu>`);
await open();
const activeMenu = getOwner(this)
.lookup("service:menu")
.getByIdentifier("test");
await activeMenu.close();
assert.dom(".fk-d-menu__content.test-content").doesNotExist();
});
});

View File

@ -78,8 +78,22 @@ export default class Menu extends Service {
}
/**
* Closes the active menu
* @param {DMenuInstance} [menu] - the menu to close, if not provider will close any active menu
* Returns an existing menu by its identifier if found
*
* @param {String} identifier - the menu identifier to retrieve
*
* @returns {Promise<DMenuInstance>}
*/
getByIdentifier(identifier) {
return this.registeredMenus.find(
(registeredMenu) => registeredMenu.options.identifier === identifier
);
}
/**
* Closes the given menu
*
* @param {DMenuInstance | String} [menu | identifier] - the menu to close, can accept an instance or an identifier
*/
@action
async close(menu) {