2020-07-09 09:08:47 +00:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { set, get } from '@ember/object';
|
|
|
|
import Slotted from 'block-slots';
|
|
|
|
|
|
|
|
export default Component.extend(Slotted, {
|
|
|
|
tagName: '',
|
|
|
|
dom: service('dom'),
|
|
|
|
builder: service('form'),
|
|
|
|
create: false,
|
|
|
|
ondelete: function() {
|
|
|
|
return this.onsubmit(...arguments);
|
|
|
|
},
|
|
|
|
oncancel: function() {
|
|
|
|
return this.onsubmit(...arguments);
|
|
|
|
},
|
|
|
|
onsubmit: function() {},
|
|
|
|
onchange: function(e, form) {
|
|
|
|
return form.handleEvent(e);
|
|
|
|
},
|
|
|
|
didReceiveAttrs: function() {
|
|
|
|
this._super(...arguments);
|
|
|
|
try {
|
|
|
|
this.form = this.builder.form(this.type);
|
|
|
|
} catch (e) {
|
|
|
|
// passthrough
|
|
|
|
// this lets us load view only data that doesn't have a form
|
|
|
|
}
|
|
|
|
},
|
2020-07-20 17:04:43 +00:00
|
|
|
willRender: function() {
|
|
|
|
this._super(...arguments);
|
|
|
|
set(this, 'hasError', this._isRegistered('error'));
|
|
|
|
},
|
2020-07-09 09:08:47 +00:00
|
|
|
willDestroyElement: function() {
|
|
|
|
this._super(...arguments);
|
|
|
|
if (get(this, 'data.isNew')) {
|
|
|
|
this.data.rollbackAttributes();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
setData: function(data) {
|
|
|
|
let changeset = data;
|
|
|
|
// convert to a real changeset
|
|
|
|
if (typeof this.form !== 'undefined') {
|
|
|
|
changeset = this.form.setData(data).getData();
|
|
|
|
}
|
|
|
|
// mark as creating
|
|
|
|
// and autofill the new record if required
|
|
|
|
if (get(changeset, 'isNew')) {
|
|
|
|
set(this, 'create', true);
|
|
|
|
changeset = Object.entries(this.autofill || {}).reduce(function(prev, [key, value]) {
|
|
|
|
set(prev, key, value);
|
|
|
|
return prev;
|
|
|
|
}, changeset);
|
|
|
|
}
|
|
|
|
set(this, 'data', changeset);
|
|
|
|
return this.data;
|
|
|
|
},
|
|
|
|
change: function(e, value, item) {
|
|
|
|
this.onchange(this.dom.normalizeEvent(e, value), this.form, this.form.getData());
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|