Browse Source

Up trait

pull/5678/head
Artur Arseniev 3 years ago
parent
commit
3604563037
  1. 53
      src/trait_manager/model/Trait.ts

53
src/trait_manager/model/Trait.ts

@ -133,13 +133,17 @@ export default class Trait extends Model<TraitProperties> {
return cat instanceof Category ? cat : undefined; return cat instanceof Category ? cat : undefined;
} }
setTarget(target: Component) { get component() {
if (target) { return this.target;
}
setTarget(component: Component) {
if (component) {
const { name, changeProp, value: initValue, getValue } = this.attributes; const { name, changeProp, value: initValue, getValue } = this.attributes;
this.target = target; this.target = component;
this.unset('target'); this.unset('target');
const targetEvent = changeProp ? `change:${name}` : `change:attributes:${name}`; const targetEvent = changeProp ? `change:${name}` : `change:attributes:${name}`;
this.listenTo(target, targetEvent, this.targetUpdated); this.listenTo(component, targetEvent, this.targetUpdated);
const value = const value =
initValue || initValue ||
// Avoid the risk of loops in case the trait has a custom getValue // Avoid the risk of loops in case the trait has a custom getValue
@ -202,29 +206,31 @@ export default class Trait extends Model<TraitProperties> {
* @param {Boolean} [opts.partial] If `true` the update won't be considered complete (not stored in UndoManager). * @param {Boolean} [opts.partial] If `true` the update won't be considered complete (not stored in UndoManager).
*/ */
setValue(value: any, opts: TraitSetValueOptions = {}) { setValue(value: any, opts: TraitSetValueOptions = {}) {
const { component, em } = this;
const { partial } = opts;
const valueOpts: { avoidStore?: boolean } = {}; const valueOpts: { avoidStore?: boolean } = {};
const { setValue } = this.attributes; const { setValue } = this.attributes;
if (setValue) { if (setValue) {
setValue({ setValue({
value, value,
editor: this.em?.getEditor()!, component,
editor: em?.getEditor()!,
trait: this, trait: this,
component: this.target, partial: !!partial,
partial: !!opts.partial,
options: opts, options: opts,
emitUpdate: () => this.targetUpdated(), emitUpdate: () => this.targetUpdated(),
}); });
return; return;
} }
if (opts.partial) { if (partial) {
valueOpts.avoidStore = true; valueOpts.avoidStore = true;
} }
this.setTargetValue(value, valueOpts); this.setTargetValue(value, valueOpts);
if (opts.partial === false) { if (partial === false) {
this.setTargetValue(''); this.setTargetValue('');
this.setTargetValue(value); this.setTargetValue(value);
} }
@ -298,39 +304,40 @@ export default class Trait extends Model<TraitProperties> {
} }
targetUpdated() { targetUpdated() {
const { component, em } = this;
const value = this.getTargetValue(); const value = this.getTargetValue();
this.set({ value }, { fromTarget: 1 }); this.set({ value }, { fromTarget: 1 });
this.em?.trigger('trait:update', { em?.trigger('trait:update', {
trait: this, trait: this,
component: this.target, component,
value, value,
}); });
} }
getTargetValue() { getTargetValue() {
const { component, em } = this;
const name = this.getName(); const name = this.getName();
const target = this.target;
const getValue = this.get('getValue'); const getValue = this.get('getValue');
let value; let value;
if (getValue) { if (getValue) {
value = getValue({ value = getValue({
editor: this.em?.getEditor()!, editor: em?.getEditor()!,
trait: this, trait: this,
component: target, component,
}); });
} else if (this.get('changeProp')) { } else if (this.get('changeProp')) {
value = target.get(name); value = component.get(name);
} else { } else {
// @ts-ignore TODO update post component update // @ts-ignore TODO update post component update
value = target.getAttributes()[name]; value = component.getAttributes()[name];
} }
return !isUndefined(value) ? value : ''; return !isUndefined(value) ? value : '';
} }
setTargetValue(value: any, opts: SetOptions = {}) { setTargetValue(value: any, opts: SetOptions = {}) {
const { target, attributes } = this; const { component, attributes } = this;
const name = this.getName(); const name = this.getName();
if (isUndefined(value)) return; if (isUndefined(value)) return;
let valueToSet = value; let valueToSet = value;
@ -354,9 +361,9 @@ export default class Trait extends Model<TraitProperties> {
} }
if (this.get('changeProp')) { if (this.get('changeProp')) {
target.set(name, valueToSet, opts); component.set(name, valueToSet, opts);
} else { } else {
target.addAttributes({ [name]: valueToSet }, opts); component.addAttributes({ [name]: valueToSet }, opts);
} }
} }
@ -372,13 +379,13 @@ export default class Trait extends Model<TraitProperties> {
} }
getInitValue() { getInitValue() {
const target = this.target; const { component } = this;
const name = this.getName(); const name = this.getName();
let value; let value;
if (target) { if (component) {
const attrs = target.get('attributes')!; const attrs = component.get('attributes')!;
value = this.get('changeProp') ? target.get(name) : attrs[name]; value = this.get('changeProp') ? component.get(name) : attrs[name];
} }
return value || this.get('value') || this.get('default'); return value || this.get('value') || this.get('default');

Loading…
Cancel
Save