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

Commit

Permalink
fix: model forced open state as additional combinator (#477)
Browse files Browse the repository at this point in the history
* fix: model forced open state as additional combinator
  • Loading branch information
tilmx authored and marionebl committed May 29, 2018
1 parent be9df1d commit 499bce0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/container/element-list/element-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ElementContainer extends React.Component<ElementContainerProps> {
const store = ViewStore.getInstance();
const { props } = this;
const open =
props.element.getOpen() || props.element.getDescendants().some(e => e.getSelected());
props.element.getOpen() || props.element.getForcedOpen();

// Ensure mobx registers
props.element.getSelected();
Expand Down
32 changes: 31 additions & 1 deletion src/model/element/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface ElementInit {
containerId?: string;
contentIds: string[];
dragged: boolean;
forcedOpen: boolean;
highlighted: boolean;
id?: string;
name?: string;
Expand All @@ -37,6 +38,8 @@ export class Element {

@Mobx.observable private editedName: string;

@Mobx.observable private forcedOpen: boolean;

@Mobx.observable private highlighted: boolean;

private readonly id: string;
Expand Down Expand Up @@ -70,6 +73,7 @@ export class Element {
this.patternId = init.patternId;
this.containerId = init.containerId;
this.open = init.open;
this.forcedOpen = init.forcedOpen;
this.patternLibrary = context.patternLibrary;
this.project = context.project;
this.selected = init.selected;
Expand Down Expand Up @@ -128,6 +132,7 @@ export class Element {
containerId: serialized.containerId,
contentIds: serialized.contentIds,
open: serialized.open,
forcedOpen: serialized.forcedOpen,
properties: serialized.properties.map(p =>
ElementProperty.from(p, { patternLibrary: context.patternLibrary })
),
Expand Down Expand Up @@ -170,6 +175,7 @@ export class Element {
contentIds: clonedContents.map(content => content.getId()),
name: this.name,
open: false,
forcedOpen: false,
patternId: this.patternId,
placeholderHighlighted: false,
properties: this.properties.map(propertyValue => propertyValue.clone()),
Expand All @@ -190,6 +196,19 @@ export class Element {
return clone;
}

public getAncestors(init: Element[] = []): Element[] {
const parent = this.getParent();

if (!parent) {
return init;
}

init.push(parent);
parent.getAncestors(init);

return init;
}

public getContainer(): ElementContent | undefined {
if (!this.containerId) {
return;
Expand Down Expand Up @@ -281,6 +300,10 @@ export class Element {
return result;
}

public getForcedOpen(): boolean {
return this.forcedOpen;
}

public getHighlighted(): boolean {
return this.highlighted;
}
Expand Down Expand Up @@ -387,6 +410,11 @@ export class Element {
this.dragged = dragged;
}

@Mobx.action
public setForcedOpen(forcedOpen: boolean): void {
this.forcedOpen = forcedOpen;
}

@Mobx.action
public setHighlighted(highlighted: boolean): void {
this.highlighted = highlighted;
Expand Down Expand Up @@ -481,7 +509,8 @@ export class Element {

@Mobx.action
public toggleOpen(): void {
this.setOpen(!this.getOpen());
this.setOpen(!this.getOpen() && !this.getForcedOpen());
this.setForcedOpen(false);
}

@Mobx.action
Expand All @@ -498,6 +527,7 @@ export class Element {
id: this.id,
name: this.name,
open: this.open,
forcedOpen: this.forcedOpen,
patternId: this.patternId,
placeholderHighlighted: this.placeholderHighlighted,
properties: this.properties.map(elementProperty => elementProperty.toJSON()),
Expand Down
1 change: 1 addition & 0 deletions src/model/page/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class Page {
contentIds: rootContents.map(c => c.getId()),
dragged: false,
open: true,
forcedOpen: false,
patternId: rootPattern.getId(),
placeholderHighlighted: false,
properties: [],
Expand Down
1 change: 1 addition & 0 deletions src/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface SerializedElement {
containerId?: string;
contentIds: string[];
dragged: boolean;
forcedOpen: boolean;
highlighted: boolean;
id: string;
name: string;
Expand Down
20 changes: 17 additions & 3 deletions src/store/view-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class ViewStore {
private static EPHEMERAL_CONTENTS: WeakMap<
Model.Element,
Model.ElementContent[]
> = new WeakMap();
> = new WeakMap();

/**
* The store singleton instance.
Expand Down Expand Up @@ -104,7 +104,7 @@ export class ViewStore {
/**
* Creates a new store.
*/
private constructor() {}
private constructor() { }

/**
* Returns (or creates) the one global store instance.
Expand Down Expand Up @@ -227,6 +227,7 @@ export class ViewStore {
contentIds: elementContents.map(e => e.getId()),
dragged: init.dragged || false,
highlighted: false,
forcedOpen: false,
open: false,
patternId: init.pattern.getId(),
placeholderHighlighted: false,
Expand Down Expand Up @@ -921,10 +922,18 @@ export class ViewStore {

if (previousSelectedElement) {
previousSelectedElement.setSelected(false);

previousSelectedElement.getAncestors().forEach(ancestor => {
ancestor.setForcedOpen(false);
});
}

this.rightPane = null;
selectedElement.setSelected(true);

selectedElement.getAncestors().forEach(ancestor => {
ancestor.setForcedOpen(true);
});
}

@Mobx.action
Expand Down Expand Up @@ -972,7 +981,12 @@ export class ViewStore {
return;
}

this.project.getElements().forEach(element => element.setSelected(false));
this.project.getElements().forEach(element => {
element.setSelected(false);
element.getAncestors().forEach(ancestor => {
ancestor.setForcedOpen(false);
});
});
}

@Mobx.action
Expand Down

0 comments on commit 499bce0

Please sign in to comment.