ui/lib/shared/addon/components/container/form-scheduling/component.js

94 lines
2.5 KiB
JavaScript

import { computed } from '@ember/object';
import { inject as service } from '@ember/service';
import { get, set } from '@ember/object';
import Component from '@ember/component';
import layout from './template';
export default Component.extend({
layout,
scope: service(),
scheduling: null,
// Request a specific host
requestedHostId: null,
// Is requesting a specific host allowed
canRequestHost: true,
// Initial host to start with
initialHostId: null,
// Internal properties
isRequestedHost: false,
editing: true,
advanced: false,
classNames: ['accordion-wrapper'],
_allNodes: null,
init() {
this._super(...arguments);
set(this, '_allNodes', get(this, 'globalStore').all('node'));
set(this, 'advanced', !get(this, 'editing'));
if (get(this, 'initialHostId')) {
set(this, 'isRequestedHost', true);
set(this, 'requestedHostId', get(this, 'initialHostId'));
}
},
didReceiveAttrs() {
if (!get(this, 'expandFn')) {
set(this, 'expandFn', function (item) {
item.toggleProperty('expanded');
});
}
},
isRequestedHostDidChange: function () {
const scheduling = get(this, 'scheduling');
if (get(this, 'isRequestedHost')) {
var hostId = get(this, 'requestedHostId') || get(this, 'hostChoices.firstObject.id');
Object.keys(scheduling).forEach(key => {
delete scheduling.node[key];
});
set(this, 'requestedHostId', hostId);
}
else {
set(this, 'requestedHostId', null);
delete scheduling.node['nodeId'];
}
}.observes('isRequestedHost'),
requestedHostIdDidChange: function () {
var hostId = get(this, 'requestedHostId');
set(this, 'scheduling.node.nodeId', hostId);
}.observes('requestedHostId'),
selectedChoice: computed('_allNodes.@each.{id,clusterId,name,state}', function () {
return get(this, 'hostChoices').findBy('id', get(this, 'initialHostId'));
}),
hostChoices: function () {
var list = get(this, '_allNodes').filter((node) => !get(node, 'isUnschedulable')).filterBy('clusterId', get(this, 'scope.currentCluster.id')).map((host) => {
var hostLabel = get(host, 'hostname');
if (get(host, 'state') !== 'active') {
hostLabel += ' (' + get(host, 'state') + ')';
}
return {
id: get(host, 'id'),
name: hostLabel,
};
});
return list.sortBy('name', 'id');
}.property('_allNodes.@each.{id,clusterId,name,state}'),
statusClass: null,
status: null,
});