2020-04-01 08:55:49 +00:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import { setProperties, set, get } from '@ember/object';
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
tagName: '',
|
2020-07-09 09:08:47 +00:00
|
|
|
ondelete: function() {
|
|
|
|
this.onsubmit(...arguments);
|
2020-04-01 08:55:49 +00:00
|
|
|
},
|
2020-07-09 09:08:47 +00:00
|
|
|
oncancel: function() {
|
|
|
|
this.onsubmit(...arguments);
|
|
|
|
},
|
|
|
|
onsubmit: function() {},
|
|
|
|
actions: {
|
|
|
|
createServices: function(item, e) {
|
|
|
|
// Services in the menus should:
|
|
|
|
// 1. Be unique (they potentially could be duplicated due to services from different namespaces)
|
|
|
|
// 2. Only include services that shold have intentions
|
|
|
|
// 3. Include an 'All Services' option
|
|
|
|
// 4. Include the current Source and Destination incase they are virtual services/don't exist yet
|
|
|
|
let items = e.data
|
|
|
|
.uniqBy('Name')
|
|
|
|
.toArray()
|
|
|
|
.filter(
|
|
|
|
item => !['connect-proxy', 'mesh-gateway', 'terminating-gateway'].includes(item.Kind)
|
|
|
|
)
|
|
|
|
.sort((a, b) => a.Name.localeCompare(b.Name));
|
|
|
|
items = [{ Name: '*' }].concat(items);
|
|
|
|
let source = items.findBy('Name', item.SourceName);
|
2020-04-01 08:55:49 +00:00
|
|
|
if (!source) {
|
2020-07-09 09:08:47 +00:00
|
|
|
source = { Name: item.SourceName };
|
|
|
|
items = [source].concat(items);
|
2020-04-01 08:55:49 +00:00
|
|
|
}
|
2020-07-09 09:08:47 +00:00
|
|
|
let destination = items.findBy('Name', item.DestinationName);
|
2020-04-01 08:55:49 +00:00
|
|
|
if (!destination) {
|
2020-07-09 09:08:47 +00:00
|
|
|
destination = { Name: item.DestinationName };
|
|
|
|
items = [destination].concat(items);
|
2020-04-01 08:55:49 +00:00
|
|
|
}
|
|
|
|
setProperties(this, {
|
2020-07-09 09:08:47 +00:00
|
|
|
services: items,
|
2020-04-01 08:55:49 +00:00
|
|
|
SourceName: source,
|
|
|
|
DestinationName: destination,
|
|
|
|
});
|
2020-07-09 09:08:47 +00:00
|
|
|
},
|
|
|
|
createNspaces: function(item, e) {
|
|
|
|
// Nspaces in the menus should:
|
|
|
|
// 1. Include an 'All Namespaces' option
|
|
|
|
// 2. Include the current SourceNS and DestinationNS incase they don't exist yet
|
|
|
|
let items = e.data.toArray().sort((a, b) => a.Name.localeCompare(b.Name));
|
|
|
|
items = [{ Name: '*' }].concat(items);
|
|
|
|
let source = items.findBy('Name', item.SourceNS);
|
|
|
|
if (!source) {
|
|
|
|
source = { Name: item.SourceNS };
|
|
|
|
items = [source].concat(items);
|
|
|
|
}
|
|
|
|
let destination = items.findBy('Name', item.DestinationNS);
|
|
|
|
if (!destination) {
|
|
|
|
destination = { Name: item.DestinationNS };
|
|
|
|
items = [destination].concat(items);
|
|
|
|
}
|
|
|
|
setProperties(this, {
|
|
|
|
nspaces: items,
|
|
|
|
SourceNS: source,
|
|
|
|
DestinationNS: destination,
|
|
|
|
});
|
|
|
|
},
|
2020-04-01 08:55:49 +00:00
|
|
|
createNewLabel: function(template, term) {
|
|
|
|
return template.replace(/{{term}}/g, term);
|
|
|
|
},
|
2020-07-09 09:08:47 +00:00
|
|
|
isUnique: function(items, term) {
|
|
|
|
return !items.findBy('Name', term);
|
2020-04-01 08:55:49 +00:00
|
|
|
},
|
2020-07-09 09:08:47 +00:00
|
|
|
change: function(e, form, item) {
|
|
|
|
const target = e.target;
|
2020-04-01 08:55:49 +00:00
|
|
|
|
|
|
|
let name, selected, match;
|
|
|
|
switch (target.name) {
|
|
|
|
case 'SourceName':
|
|
|
|
case 'DestinationName':
|
|
|
|
case 'SourceNS':
|
|
|
|
case 'DestinationNS':
|
|
|
|
name = selected = target.value;
|
|
|
|
// Names can be selected Service EmberObjects or typed in strings
|
|
|
|
// if its not a string, use the `Name` from the Service EmberObject
|
|
|
|
if (typeof name !== 'string') {
|
|
|
|
name = get(target.value, 'Name');
|
|
|
|
}
|
|
|
|
// mutate the value with the string name
|
|
|
|
// which will be handled by the form
|
|
|
|
target.value = name;
|
|
|
|
// these are 'non-form' variables so not on `item`
|
|
|
|
// these variables also exist in the template so we know
|
|
|
|
// the current selection
|
|
|
|
// basically the difference between
|
|
|
|
// `item.DestinationName` and just `DestinationName`
|
|
|
|
// see if the name is already in the list
|
2020-07-09 09:08:47 +00:00
|
|
|
match = this.services.filterBy('Name', name);
|
2020-04-01 08:55:49 +00:00
|
|
|
if (match.length === 0) {
|
|
|
|
// if its not make a new 'fake' Service that doesn't exist yet
|
|
|
|
// and add it to the possible services to make an intention between
|
|
|
|
selected = { Name: name };
|
|
|
|
switch (target.name) {
|
|
|
|
case 'SourceName':
|
|
|
|
case 'DestinationName':
|
2020-07-09 09:08:47 +00:00
|
|
|
set(this, 'services', [selected].concat(this.services.toArray()));
|
2020-04-01 08:55:49 +00:00
|
|
|
break;
|
|
|
|
case 'SourceNS':
|
|
|
|
case 'DestinationNS':
|
2020-07-09 09:08:47 +00:00
|
|
|
set(this, 'nspaces', [selected].concat(this.nspaces.toArray()));
|
2020-04-01 08:55:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set(this, target.name, selected);
|
|
|
|
break;
|
|
|
|
}
|
2020-07-09 09:08:47 +00:00
|
|
|
form.handleEvent(e);
|
2020-04-01 08:55:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|