Files
threshold-ui/addons/core/addon/helpers/route-action.js
T
Zhihe Li bcf004b8a4 Reformat codebase with trailing commas in functions (#2006)
* style: 💄 Update trailing commas in API

* style: 💄 Update trailing commas in core

* style: 💄 Update trailing commas in rose

* style: 💄 Update trailing commas in admin

* style: 💄 Update trailing commas in desktop
2023-11-22 14:07:41 -05:00

70 lines
1.7 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Helper from '@ember/component/helper';
import { inject as service } from '@ember/service';
import { join } from '@ember/runloop';
import { runInDebug, assert } from '@ember/debug';
import { A } from '@ember/array';
/**
* This helper is a modern update on ember-route-action-helper, which is
* now unmaintained.
*
* See https://github.com/DockYard/ember-route-action-helper
*/
export default class RouteActionHelper extends Helper {
// =services
@service router;
// =attributes
get routes() {
const { currentInfos, mapBy } = this.currentInfos;
return A(currentInfos).mapBy(mapBy).reverse();
}
get currentInfos() {
// eslint-disable-next-line ember/no-private-routing-service
const routerLib = this.router._router._routerMicrolib;
return {
currentInfos:
routerLib.currentRouteInfos || routerLib.currentHandlerInfos,
mapBy: (routerLib.currentRouteInfos && 'route') || 'handler',
};
}
// =methods
compute([actionName, ...outerArgs]) {
const { action, handler } = this.getRouteWithAction(actionName);
runInDebug(() => {
assert(
`
[boundary-core-helper-route-action]
Unable to find action ${actionName}
`,
handler,
);
});
return (...innerArgs) =>
join(handler, action, ...outerArgs.concat(innerArgs));
}
getRouteWithAction(actionName) {
let action;
const handler = A(this.routes).find((route) => {
const actions = route.actions || route._actions;
action = actions[actionName];
return typeof action === 'function';
});
return { action, handler };
}
}