2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2022-07-29 20:33:00 +00:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import { tracked } from '@glimmer/tracking';
|
2020-09-15 18:11:24 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2022-09-13 16:06:19 +00:00
|
|
|
import { action } from '@ember/object';
|
2020-09-15 18:11:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @module InfoTableItemArray
|
|
|
|
* The `InfoTableItemArray` component handles arrays in the info-table-row component.
|
|
|
|
* If an array has more than 10 items, then only 5 are displayed and a count of total items is displayed next to the five.
|
|
|
|
* If a isLink is true than a link can be set for the use to click on the specific array item
|
|
|
|
* If a wildcard is a potential variable in the string of an item, then you can use the modelType and wildcardLabel parameters to
|
|
|
|
* return a wildcard count similar to what is done in the searchSelect component.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```js
|
2022-07-29 20:33:00 +00:00
|
|
|
* <InfoTableItemArray
|
2022-09-09 01:06:05 +00:00
|
|
|
* @label="Roles"
|
2022-07-29 20:33:00 +00:00
|
|
|
* @displayArray={{['test-1','test-2','test-3']}}
|
|
|
|
* @isLink={{true}}
|
|
|
|
* @rootRoute="vault.cluster.secrets.backend.list-root"
|
|
|
|
* @itemRoute="vault.cluster.secrets.backend.show"
|
2022-09-09 01:06:05 +00:00
|
|
|
* @modelType="transform/role"
|
2022-07-29 20:33:00 +00:00
|
|
|
* @queryParam="role"
|
|
|
|
* @backend="transform"
|
2020-09-15 18:11:24 +00:00
|
|
|
* ```
|
|
|
|
*
|
2022-09-09 01:06:05 +00:00
|
|
|
* @param {string} label - used to render lowercased display text for "View all <label>."
|
|
|
|
* @param {array} displayArray - The array of data to be displayed. (In InfoTableRow this comes from the @value arg.) If the array length > 10, and @doNotTruncate is false only 5 will show with a count of the number hidden.
|
|
|
|
* @param {boolean} [isLink] - Indicates if the item should contain a link-to component. Only setup for arrays, but this could be changed if needed.
|
|
|
|
* @param {string || array} [rootRoute="vault.cluster.secrets.backend.list-root"] - Tells what route the link should go to when selecting "view all". If the route requires more than one dynamic param, insert an array.
|
|
|
|
* @param {string || array} [itemRoute=vault.cluster.secrets.backend.show] - Tells what route the link should go to when selecting the individual item. If the route requires more than one dynamic param, insert an array.
|
2022-09-13 16:06:19 +00:00
|
|
|
* @param {string} [modelType] - Tells which model you want to query and set allOptions. Used in conjunction with the the isLink.
|
2022-09-09 01:06:05 +00:00
|
|
|
* @param {string} [wildcardLabel] - when you want the component to return a count on the model for options returned when using a wildcard you must provide a label of the count e.g. role. Should be singular.
|
|
|
|
* @param {string} [backend] - To specify which backend to point the link to.
|
2022-09-13 16:06:19 +00:00
|
|
|
* @param {boolean} [doNotTruncate=false] - Determines whether to show the View all "roles" link. Otherwise uses the ReadMore component's "See More" toggle
|
|
|
|
* @param {boolean} [renderItemName=false] - If true renders the item name instead of its id
|
2020-09-15 18:11:24 +00:00
|
|
|
*/
|
2022-07-29 20:33:00 +00:00
|
|
|
export default class InfoTableItemArray extends Component {
|
|
|
|
@service store;
|
2022-09-13 16:06:19 +00:00
|
|
|
@tracked allOptions = null;
|
|
|
|
@tracked itemNameById; // object is only created if renderItemName=true
|
|
|
|
@tracked fetchComplete = false;
|
2022-07-29 20:33:00 +00:00
|
|
|
|
|
|
|
get rootRoute() {
|
|
|
|
return this.args.rootRoute || 'vault.cluster.secrets.backend.list-root';
|
|
|
|
}
|
|
|
|
|
|
|
|
get itemRoute() {
|
|
|
|
return this.args.itemRoute || 'vault.cluster.secrets.backend.show';
|
|
|
|
}
|
|
|
|
|
2022-09-09 01:06:05 +00:00
|
|
|
get doNotTruncate() {
|
|
|
|
return this.args.doNotTruncate || false;
|
2022-07-29 20:33:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 01:06:05 +00:00
|
|
|
get displayArrayTruncated() {
|
2022-11-09 23:15:31 +00:00
|
|
|
const { displayArray } = this.args;
|
2022-07-29 20:33:00 +00:00
|
|
|
if (!displayArray) return null;
|
2022-10-20 16:54:54 +00:00
|
|
|
if (displayArray.length >= 10 && !this.args.doNotTruncate) {
|
2020-09-15 18:11:24 +00:00
|
|
|
// if array greater than 10 in length only display the first 5
|
2022-09-09 01:06:05 +00:00
|
|
|
return displayArray.slice(0, 5);
|
2020-09-15 18:11:24 +00:00
|
|
|
}
|
|
|
|
return displayArray;
|
2022-07-29 20:33:00 +00:00
|
|
|
}
|
2020-09-15 18:11:24 +00:00
|
|
|
|
2022-09-13 16:06:19 +00:00
|
|
|
@action async fetchOptions() {
|
2022-07-29 20:33:00 +00:00
|
|
|
if (this.args.isLink && this.args.modelType) {
|
2022-11-09 23:15:31 +00:00
|
|
|
const queryOptions = this.args.backend ? { backend: this.args.backend } : {};
|
2020-09-15 18:11:24 +00:00
|
|
|
|
2022-11-09 23:15:31 +00:00
|
|
|
const modelRecords = await this.store.query(this.args.modelType, queryOptions).catch((err) => {
|
2022-09-13 16:06:19 +00:00
|
|
|
if (err.httpStatus === 404) {
|
|
|
|
return [];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
2020-09-15 18:11:24 +00:00
|
|
|
|
2022-09-13 16:06:19 +00:00
|
|
|
this.allOptions = modelRecords ? modelRecords.mapBy('id') : null;
|
|
|
|
if (this.args.renderItemName && modelRecords) {
|
|
|
|
modelRecords.forEach(({ id, name }) => {
|
|
|
|
// create key/value pair { item-id: item-name } for each record
|
|
|
|
this.itemNameById = { ...this.itemNameById, [id]: name };
|
|
|
|
});
|
|
|
|
}
|
2020-09-15 18:11:24 +00:00
|
|
|
}
|
2022-09-13 16:06:19 +00:00
|
|
|
this.fetchComplete = true;
|
2022-07-29 20:33:00 +00:00
|
|
|
}
|
|
|
|
}
|