2018-05-22 15:03:45 +00:00
|
|
|
import Controller from '@ember/controller';
|
2018-05-24 08:52:07 +00:00
|
|
|
import { get, set } from '@ember/object';
|
2018-06-06 11:00:25 +00:00
|
|
|
import Changeset from 'ember-changeset';
|
|
|
|
import lookupValidator from 'ember-changeset-validations';
|
|
|
|
|
|
|
|
import validations from 'consul-ui/validations/intention';
|
2018-05-22 15:03:45 +00:00
|
|
|
|
|
|
|
export default Controller.extend({
|
|
|
|
setProperties: function(model) {
|
2018-06-06 11:00:25 +00:00
|
|
|
this.changeset = new Changeset(model.item, lookupValidator(validations), validations);
|
2018-05-22 15:03:45 +00:00
|
|
|
this._super({
|
|
|
|
...model,
|
|
|
|
...{
|
|
|
|
item: this.changeset,
|
2018-06-05 16:09:26 +00:00
|
|
|
SourceName: model.items.filterBy('Name', get(model.item, 'SourceName'))[0],
|
|
|
|
DestinationName: model.items.filterBy('Name', get(model.item, 'DestinationName'))[0],
|
2018-05-22 15:03:45 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
actions: {
|
2018-06-08 12:20:13 +00:00
|
|
|
createNewLabel: function(term) {
|
|
|
|
return `Use a future Consul Service called '${term}'`;
|
|
|
|
},
|
2018-06-05 16:09:26 +00:00
|
|
|
change: function(e, value, _target) {
|
2018-06-06 11:00:25 +00:00
|
|
|
// normalize back to standard event
|
2018-06-05 16:09:26 +00:00
|
|
|
const target = e.target || { ..._target, ...{ name: e, value: value } };
|
2018-05-22 15:03:45 +00:00
|
|
|
switch (target.name) {
|
|
|
|
case 'Action':
|
|
|
|
set(this.changeset, target.name, target.value);
|
|
|
|
break;
|
2018-05-24 08:52:07 +00:00
|
|
|
case 'SourceName':
|
2018-06-05 16:09:26 +00:00
|
|
|
case 'DestinationName':
|
2018-06-08 12:20:13 +00:00
|
|
|
let name = target.value;
|
|
|
|
let selected = target.value;
|
|
|
|
if (typeof name !== 'string') {
|
|
|
|
name = get(target.value, 'Name');
|
|
|
|
}
|
|
|
|
const match = get(this, 'items').filterBy('Name', name);
|
|
|
|
if (match.length === 0) {
|
|
|
|
selected = { Name: name };
|
|
|
|
const items = [selected].concat(this.items.toArray());
|
|
|
|
set(this, 'items', items);
|
|
|
|
}
|
|
|
|
set(this.changeset, target.name, name);
|
|
|
|
set(this, target.name, selected);
|
2018-05-24 08:52:07 +00:00
|
|
|
break;
|
2018-05-22 15:03:45 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|