Files
threshold-ui/ui/admin/app/components/form/target/add-host-sets/index.js
T

55 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-08-27 16:38:49 -04:00
import Component from '@glimmer/component';
import { computed, action } from '@ember/object';
import { A } from '@ember/array';
export default class FormTargetAddHostSetsComponent extends Component {
// =properties
/**
* Array of selected host set IDs.
* @type {EmberArray}
*/
selectedHostSetIDs = A();
2020-09-25 21:21:27 -04:00
/**
* Checks for unassigned hostsets.
* @param {[HostSetModel]} filteredHostSets
* @type {boolean}
*/
2020-10-06 09:53:58 -04:00
@computed('filteredHostSets.length')
2020-09-25 21:21:27 -04:00
get hasAvailableHostSets() {
return this.filteredHostSets.length > 0;
}
2020-08-27 16:38:49 -04:00
/**
2020-08-27 16:52:28 -04:00
* Host sets not already added to the target.
* @type {[HostSetModel]}
2020-08-27 16:38:49 -04:00
*/
2021-09-22 19:15:09 -04:00
@computed('args.{hostSets.[],model.host_sources.[]}')
2020-08-27 16:52:28 -04:00
get filteredHostSets() {
2020-08-27 16:38:49 -04:00
// Get IDs for host sets already added to the current target
2021-09-22 19:15:09 -04:00
const alreadyAddedHostSetIDs = this.args.model.host_sources.map(
2021-09-21 16:58:25 -04:00
({ host_source_id }) => host_source_id
2020-09-16 14:33:04 -04:00
);
const notAddedHostSets = this.args.hostSets.filter(
({ id }) => !alreadyAddedHostSetIDs.includes(id)
);
2020-08-27 16:52:28 -04:00
return notAddedHostSets;
2020-08-27 16:38:49 -04:00
}
// =actions
@action
toggleHostSet(hostSet) {
if (!this.selectedHostSetIDs.includes(hostSet.id)) {
this.selectedHostSetIDs.addObject(hostSet.id);
} else {
this.selectedHostSetIDs.removeObject(hostSet.id);
}
}
@action
submit(fn) {
fn(this.selectedHostSetIDs);
}
}