2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2022-08-01 16:04:41 -04:00
|
|
|
import Route from '@ember/routing/route';
|
2022-08-09 16:12:56 -04:00
|
|
|
import { action } from '@ember/object';
|
|
|
|
|
import { resourceFilter } from 'core/decorators/resource-filter';
|
2022-08-01 16:04:41 -04:00
|
|
|
|
|
|
|
|
export default class ScopesScopeWorkersIndexRoute extends Route {
|
2022-08-09 16:12:56 -04:00
|
|
|
// =attributes
|
|
|
|
|
|
2022-09-06 13:45:22 -04:00
|
|
|
// This resource filter depends on the data from the list route so can't
|
|
|
|
|
// live in the same model where the list is loaded as it needs to get loaded
|
|
|
|
|
// first. If it's in the same file it might try to grab the model data before
|
|
|
|
|
// it's loaded.
|
2022-08-09 16:12:56 -04:00
|
|
|
@resourceFilter({
|
|
|
|
|
allowed: (route) => {
|
|
|
|
|
const configTags = route
|
2022-09-06 13:45:22 -04:00
|
|
|
.modelFor('scopes.scope.workers')
|
2022-08-09 16:12:56 -04:00
|
|
|
.toArray()
|
2022-09-20 20:51:06 -04:00
|
|
|
.flatMap((worker) => worker.getConfigTagList())
|
2022-08-26 11:50:53 -04:00
|
|
|
.filter(Boolean);
|
2022-08-09 16:12:56 -04:00
|
|
|
|
|
|
|
|
// Filter out duplicate tags
|
2022-09-20 20:51:06 -04:00
|
|
|
return configTags.reduce((uniqueConfigTags, currentTag) => {
|
|
|
|
|
const isDuplicateTag = uniqueConfigTags.some(
|
|
|
|
|
(tag) => tag.key === currentTag.key && tag.value === currentTag.value
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!isDuplicateTag) {
|
|
|
|
|
uniqueConfigTags.push(currentTag);
|
|
|
|
|
}
|
|
|
|
|
return uniqueConfigTags;
|
|
|
|
|
}, []);
|
2022-08-09 16:12:56 -04:00
|
|
|
},
|
2022-09-20 20:51:06 -04:00
|
|
|
findBySerialized: ({ key: itemKey, value: itemValue }, { key, value }) =>
|
|
|
|
|
itemKey === key && itemValue === value,
|
2022-09-06 13:45:22 -04:00
|
|
|
refreshRouteOnChange: false,
|
2022-08-09 16:12:56 -04:00
|
|
|
})
|
|
|
|
|
tags;
|
|
|
|
|
|
2022-08-01 16:04:41 -04:00
|
|
|
// =methods
|
|
|
|
|
|
2022-08-09 16:12:56 -04:00
|
|
|
model() {
|
2022-09-27 09:15:42 -04:00
|
|
|
const workers = this.modelFor('scopes.scope.workers').sortBy('displayName');
|
2022-08-26 11:50:53 -04:00
|
|
|
|
2022-09-06 13:45:22 -04:00
|
|
|
if (this.tags?.length) {
|
|
|
|
|
// Return workers that have config tags that have at
|
|
|
|
|
// least one intersection with the filter tags
|
2022-09-20 20:51:06 -04:00
|
|
|
return workers.filter((worker) => {
|
|
|
|
|
if (!worker.config_tags) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const workerTags = worker.getConfigTagList();
|
|
|
|
|
return this.tags.some((tag) =>
|
|
|
|
|
workerTags.some(
|
|
|
|
|
(workerTag) =>
|
|
|
|
|
tag.key === workerTag.key && tag.value === workerTag.value
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
});
|
2022-08-09 16:12:56 -04:00
|
|
|
}
|
2022-09-27 09:15:42 -04:00
|
|
|
return workers;
|
2022-08-09 16:12:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clears filter selections.
|
|
|
|
|
*/
|
|
|
|
|
@action
|
|
|
|
|
clearAllFilters() {
|
|
|
|
|
this.tags = [];
|
2022-09-06 13:45:22 -04:00
|
|
|
this.refresh();
|
2022-08-09 16:12:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the specified resource filter field to the specified value.
|
|
|
|
|
* @param {string} field
|
|
|
|
|
* @param value
|
|
|
|
|
*/
|
|
|
|
|
@action
|
|
|
|
|
filterBy(field, value) {
|
|
|
|
|
this[field] = value;
|
2022-09-06 13:45:22 -04:00
|
|
|
this.refresh();
|
2022-08-09 16:12:56 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-20 20:51:06 -04:00
|
|
|
@action
|
|
|
|
|
isEqual(firstTag, secondTag) {
|
|
|
|
|
return firstTag.key === secondTag.key && firstTag.value === secondTag.value;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-01 16:04:41 -04:00
|
|
|
setupController(controller) {
|
|
|
|
|
const scope = this.modelFor('scopes.scope');
|
|
|
|
|
super.setupController(...arguments);
|
|
|
|
|
controller.setProperties({ scope });
|
|
|
|
|
}
|
|
|
|
|
}
|