Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
fix: set defaults for element properties
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Sep 10, 2018
1 parent 60bdc19 commit 4cfc89b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
28 changes: 15 additions & 13 deletions src/model/element/element-property/element-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import * as uuid from 'uuid';
export interface ElementPropertyInit {
id: string;
patternPropertyId: string;
setDefault: boolean;
}

export interface ElementPropertyContext {
Expand All @@ -26,7 +25,6 @@ export class ElementProperty {

@Mobx.observable private id: string;
@Mobx.observable private patternPropertyId: string;
@Mobx.observable private setDefault: boolean;
@Mobx.observable private value: Types.ElementPropertyValue;

@Mobx.computed
Expand Down Expand Up @@ -60,11 +58,10 @@ export class ElementProperty {

this.id = init.id;
this.patternPropertyId = init.patternPropertyId;
this.setDefault = init.setDefault;

const patternProperty = this.project.getPatternPropertyById(this.patternPropertyId);

if (typeof this.value === 'undefined' && this.setDefault && patternProperty) {
if (typeof this.value === 'undefined' && patternProperty) {
this.value = patternProperty.getDefaultValue();
}
}
Expand All @@ -76,8 +73,7 @@ export class ElementProperty {
return new ElementProperty(
{
id: serialized.id,
patternPropertyId: serialized.patternPropertyId,
setDefault: serialized.setDefault
patternPropertyId: serialized.patternPropertyId
},
context
);
Expand All @@ -90,8 +86,7 @@ export class ElementProperty {
return new ElementProperty(
{
id: uuid.v4(),
patternPropertyId: patternProperty.getId(),
setDefault: patternProperty.getRequired()
patternPropertyId: patternProperty.getId()
},
context
);
Expand All @@ -101,8 +96,7 @@ export class ElementProperty {
return new ElementProperty(
{
id: uuid.v4(),
patternPropertyId: this.patternPropertyId,
setDefault: this.setDefault
patternPropertyId: this.patternPropertyId
},
{
project: this.project,
Expand Down Expand Up @@ -147,7 +141,17 @@ export class ElementProperty {
return this.patternProperty.coerceValue(referencedValue);
}

return this.element.getPropertyValue(this.patternPropertyId);
const concreteValue = this.element.getPropertyValue(this.patternPropertyId);

if (typeof concreteValue !== 'undefined' && concreteValue !== null) {
return concreteValue;
}

if (this.patternProperty) {
return this.patternProperty.getDefaultValue();
}

return;
}

public getUserStoreReference(): UserStoreReference | undefined {
Expand Down Expand Up @@ -176,7 +180,6 @@ export class ElementProperty {
model: this.model,
id: this.id,
patternPropertyId: this.patternPropertyId,
setDefault: this.setDefault,
value: this.value
};
}
Expand All @@ -185,7 +188,6 @@ export class ElementProperty {
public update(b: ElementProperty): void {
this.id = b.id;
this.patternPropertyId = b.patternPropertyId;
this.setDefault = b.setDefault;
this.value = b.value;
}
}
4 changes: 4 additions & 0 deletions src/model/pattern-property/property-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export abstract class PatternPropertyBase<T> {
return _.isEqual(this.toJSON(), b.toJSON());
}

public hasDefault(): boolean {
return typeof this.defaultValue !== 'undefined';
}

public getContextId(): string {
return this.contextId;
}
Expand Down
1 change: 0 additions & 1 deletion src/types/serialized-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ export interface LegacySerializedElementProperty {
model: Types.ModelName.ElementProperty;
id: string;
patternPropertyId: string;
setDefault: boolean;
value: Types.ElementPropertyValue;
}

Expand Down

0 comments on commit 4cfc89b

Please sign in to comment.