2018-05-22 15:03:45 +00:00
|
|
|
import Controller from '@ember/controller';
|
2018-11-19 14:53:08 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2018-05-24 08:52:07 +00:00
|
|
|
import { get, set } from '@ember/object';
|
2018-05-22 15:03:45 +00:00
|
|
|
export default Controller.extend({
|
2018-11-19 14:53:08 +00:00
|
|
|
dom: service('dom'),
|
|
|
|
builder: service('form'),
|
|
|
|
init: function() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.form = get(this, 'builder').form('intention');
|
|
|
|
},
|
2018-05-22 15:03:45 +00:00
|
|
|
setProperties: function(model) {
|
2018-06-08 13:55:23 +00:00
|
|
|
const sourceName = get(model.item, 'SourceName');
|
|
|
|
const destinationName = get(model.item, 'DestinationName');
|
|
|
|
let source = model.items.findBy('Name', sourceName);
|
|
|
|
let destination = model.items.findBy('Name', destinationName);
|
|
|
|
if (!source) {
|
|
|
|
source = { Name: sourceName };
|
|
|
|
model.items = [source].concat(model.items);
|
|
|
|
}
|
|
|
|
if (!destination) {
|
|
|
|
destination = { Name: destinationName };
|
|
|
|
model.items = [destination].concat(model.items);
|
|
|
|
}
|
2018-05-22 15:03:45 +00:00
|
|
|
this._super({
|
|
|
|
...model,
|
|
|
|
...{
|
2018-11-19 14:53:08 +00:00
|
|
|
item: this.form.setData(model.item).getData(),
|
2018-06-08 13:55:23 +00:00
|
|
|
SourceName: source,
|
|
|
|
DestinationName: destination,
|
2018-05-22 15:03:45 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
actions: {
|
2018-11-19 14:54:40 +00:00
|
|
|
createNewLabel: function(template, term) {
|
|
|
|
return template.replace(/{{term}}/g, term);
|
2018-06-08 12:20:13 +00:00
|
|
|
},
|
2018-06-11 14:53:12 +00:00
|
|
|
isUnique: function(term) {
|
|
|
|
return !get(this, 'items').findBy('Name', term);
|
|
|
|
},
|
2018-11-19 14:53:08 +00:00
|
|
|
change: function(e, value, item) {
|
|
|
|
const event = get(this, 'dom').normalizeEvent(e, value);
|
|
|
|
const form = get(this, 'form');
|
|
|
|
const target = event.target;
|
|
|
|
|
|
|
|
let name;
|
|
|
|
let selected;
|
2018-06-14 10:14:06 +00:00
|
|
|
let match;
|
2018-05-22 15:03:45 +00:00
|
|
|
switch (target.name) {
|
2018-05-24 08:52:07 +00:00
|
|
|
case 'SourceName':
|
2018-06-05 16:09:26 +00:00
|
|
|
case 'DestinationName':
|
2018-11-19 14:53:08 +00:00
|
|
|
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
|
2018-06-08 12:20:13 +00:00
|
|
|
if (typeof name !== 'string') {
|
|
|
|
name = get(target.value, 'Name');
|
|
|
|
}
|
2018-11-19 14:53:08 +00:00
|
|
|
// see if the name is already in the list
|
2018-06-14 10:14:06 +00:00
|
|
|
match = get(this, 'items').filterBy('Name', name);
|
2018-06-08 12:20:13 +00:00
|
|
|
if (match.length === 0) {
|
2018-11-19 14:53:08 +00:00
|
|
|
// 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
|
2018-06-08 12:20:13 +00:00
|
|
|
selected = { Name: name };
|
|
|
|
const items = [selected].concat(this.items.toArray());
|
|
|
|
set(this, 'items', items);
|
|
|
|
}
|
2018-11-19 14:53:08 +00:00
|
|
|
// 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`
|
2018-06-08 12:20:13 +00:00
|
|
|
set(this, target.name, selected);
|
2018-05-24 08:52:07 +00:00
|
|
|
break;
|
2018-05-22 15:03:45 +00:00
|
|
|
}
|
2018-11-19 14:53:08 +00:00
|
|
|
form.handleEvent(event);
|
2018-05-22 15:03:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|