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

Commit

Permalink
refactor: make getObject utility reusabble
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Sep 10, 2018
1 parent 81d82e1 commit 9a7a9c3
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 129 deletions.
1 change: 1 addition & 0 deletions src/model-tree/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './model-tree';
39 changes: 39 additions & 0 deletions src/model-tree/model-tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as Model from '../model';
import * as Types from '../types';

export function getModelByName(modelName?: Types.ModelName): Types.ModelSurface | undefined {
switch (modelName) {
case Types.ModelName.AlvaApp:
return Model.AlvaApp;
case Types.ModelName.Element:
return Model.Element;
case Types.ModelName.ElementAction:
return Model.ElementAction;
case Types.ModelName.ElementContent:
return Model.ElementContent;
case Types.ModelName.ElementProperty:
return Model.ElementProperty;
case Types.ModelName.Page:
return Model.Page;
case Types.ModelName.Pattern:
return Model.Pattern;
case Types.ModelName.PatternLibrary:
return Model.PatternLibrary;
case Types.ModelName.PatternProperty:
return Model.PatternProperty;
case Types.ModelName.PatternSlot:
return Model.PatternSlot;
case Types.ModelName.UserStore:
return Model.UserStore;
case Types.ModelName.UserStoreAction:
return Model.UserStoreAction;
case Types.ModelName.UserStoreEnhancer:
return Model.UserStoreEnhancer;
case Types.ModelName.UserStoreProperty:
return Model.UserStoreProperty;
case Types.ModelName.UserStoreReference:
return Model.UserStoreReference;
default:
return;
}
}
30 changes: 30 additions & 0 deletions src/model/any-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { AlvaApp } from './alva-app';
import { Element, ElementContent, ElementProperty } from './element';
import { ElementAction } from './element-action';
import { Page } from './page';
import { PatternLibrary } from './pattern-library';
import { Project } from './project';
import { AnyPatternProperty } from './pattern-property';
import { Pattern } from './pattern';
import { UserStore } from './user-store';
import { UserStoreAction } from './user-store-action';
import { UserStoreEnhancer } from './user-store-enhancer';
import { UserStoreProperty } from './user-store-property';
import { UserStoreReference } from './user-store-reference';

export type AnyModel =
| AlvaApp
| AnyPatternProperty
| Element
| ElementContent
| ElementAction
| ElementProperty
| Page
| Pattern
| PatternLibrary
| Project
| UserStore
| UserStoreAction
| UserStoreEnhancer
| UserStoreProperty
| UserStoreReference;
32 changes: 1 addition & 31 deletions src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,4 @@ export * from './user-store-action';
export * from './user-store-enhancer';
export * from './user-store-property';
export * from './user-store-reference';

import { AlvaApp } from './alva-app';
import { Element, ElementContent, ElementProperty } from './element';
import { ElementAction } from './element-action';
import { Page } from './page';
import { PatternLibrary } from './pattern-library';
import { Project } from './project';
import { AnyPatternProperty } from './pattern-property';
import { Pattern } from './pattern';
import { UserStore } from './user-store';
import { UserStoreAction } from './user-store-action';
import { UserStoreEnhancer } from './user-store-enhancer';
import { UserStoreProperty } from './user-store-property';
import { UserStoreReference } from './user-store-reference';

export type AnyModel =
| AlvaApp
| AnyPatternProperty
| Element
| ElementContent
| ElementAction
| ElementProperty
| Page
| Pattern
| PatternLibrary
| Project
| UserStore
| UserStoreAction
| UserStoreEnhancer
| UserStoreProperty
| UserStoreReference;
export * from './any-model';
57 changes: 57 additions & 0 deletions src/model/project.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AnyModel } from './any-model';
import { Element, ElementContent, ElementProperty } from './element';
import { ElementAction } from './element-action';
import * as Mobx from 'mobx';
Expand Down Expand Up @@ -346,6 +347,62 @@ export class Project {
return this.pages[nextIndex];
}

public getObject(constructorName: string, id: string): AnyModel | undefined {
if (constructorName === 'Element') {
return this.getElementById(id);
}

if (constructorName === 'ElementAction') {
return this.getElementActionById(id);
}

if (constructorName === 'ElementContent') {
return this.getElementContentById(id);
}

if (constructorName === 'ElementProperty') {
return this.getElementPropertyById(id);
}

if (constructorName === 'Page') {
return this.getPageById(id);
}

if (constructorName === 'Pattern') {
return this.getPatternById(id);
}

if (constructorName === 'PatternLibrary') {
return this.getPatternLibraryById(id);
}

if (constructorName === 'Project') {
return this;
}

if (constructorName === 'UserStore') {
return this.getUserStore();
}

if (constructorName === 'UserStoreAction') {
return this.getUserStore().getActionById(id);
}

if (constructorName === 'UserStoreProperty') {
return this.getUserStore().getPropertyById(id);
}

if (constructorName === 'UserStoreEnhancer') {
return this.getUserStore().getEnhancer();
}

if (constructorName === 'UserStoreReference') {
return this.getUserStore().getReferenceById(id);
}

return;
}

public getPageById(id: string): Page | undefined {
return this.pages.find(page => page.getId() === id);
}
Expand Down
93 changes: 0 additions & 93 deletions src/preview/preview-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,99 +140,6 @@ export class PreviewStore<V> {
return this.metaDown;
}

public getModel(modelName?: Types.ModelName): Types.ModelSurface | undefined {
switch (modelName) {
case Types.ModelName.AlvaApp:
return Model.AlvaApp;
case Types.ModelName.Element:
return Model.Element;
case Types.ModelName.ElementAction:
return Model.ElementAction;
case Types.ModelName.ElementContent:
return Model.ElementContent;
case Types.ModelName.ElementProperty:
return Model.ElementProperty;
case Types.ModelName.Page:
return Model.Page;
case Types.ModelName.Pattern:
return Model.Pattern;
case Types.ModelName.PatternLibrary:
return Model.PatternLibrary;
case Types.ModelName.PatternProperty:
return Model.PatternProperty;
case Types.ModelName.PatternSlot:
return Model.PatternSlot;
case Types.ModelName.UserStore:
return Model.UserStore;
case Types.ModelName.UserStoreAction:
return Model.UserStoreAction;
case Types.ModelName.UserStoreEnhancer:
return Model.UserStoreEnhancer;
case Types.ModelName.UserStoreProperty:
return Model.UserStoreProperty;
case Types.ModelName.UserStoreReference:
return Model.UserStoreReference;
default:
return;
}
}

public getObject(constructorName: string, id: string): Model.AnyModel | undefined {
if (constructorName === 'Element') {
return this.project.getElementById(id);
}

if (constructorName === 'ElementAction') {
return this.project.getElementActionById(id);
}

if (constructorName === 'ElementContent') {
return this.project.getElementContentById(id);
}

if (constructorName === 'ElementProperty') {
return this.project.getElementPropertyById(id);
}

if (constructorName === 'Page') {
return this.project.getPageById(id);
}

if (constructorName === 'Pattern') {
return this.project.getPatternById(id);
}

if (constructorName === 'PatternLibrary') {
return this.project.getPatternLibraryById(id);
}

if (constructorName === 'Project') {
return this.project;
}

if (constructorName === 'UserStore') {
return this.project.getUserStore();
}

if (constructorName === 'UserStoreAction') {
return this.project.getUserStore().getActionById(id);
}

if (constructorName === 'UserStoreProperty') {
return this.project.getUserStore().getPropertyById(id);
}

if (constructorName === 'UserStoreEnhancer') {
return this.project.getUserStore().getEnhancer();
}

if (constructorName === 'UserStoreReference') {
return this.project.getUserStore().getReferenceById(id);
}

return;
}

public getProperties<T>(
element: Model.Element
): { [propName: string]: Types.ElementPropertyValue } {
Expand Down
11 changes: 6 additions & 5 deletions src/preview/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getInitialData } from './get-initial-data';
import * as Message from '../message';
import * as Mobx from 'mobx';
import * as Model from '../model';
import * as ModelTree from '../model-tree';
import { PreviewStore, SyntheticComponents } from './preview-store';
import { Sender } from '../sender/client';
import * as Types from '../types';
Expand Down Expand Up @@ -109,7 +110,7 @@ function main(): void {
const change = message.payload.change as
| Message.MobxObjectUpdatePayload
| Message.MobxMapUpdatePayload;
const object = store.getObject(message.payload.name, message.payload.id);
const object = project.getObject(message.payload.name, message.payload.id);

if (!object) {
// console.log(message);
Expand All @@ -130,8 +131,8 @@ function main(): void {
});

sender.match<Message.MobxAddMessage>(Message.MessageType.MobxAdd, message => {
const parent = store.getObject(message.payload.name, message.payload.id);
const ValueModel = store.getModel(message.payload.valueModel);
const parent = project.getObject(message.payload.name, message.payload.id);
const ValueModel = ModelTree.getModelByName(message.payload.valueModel);

if (!parent) {
console.log(message);
Expand All @@ -157,7 +158,7 @@ function main(): void {
});

sender.match<Message.MobxDeleteMessage>(Message.MessageType.MobxDelete, message => {
const parent = store.getObject(message.payload.name, message.payload.id);
const parent = project.getObject(message.payload.name, message.payload.id);

if (!parent) {
console.log(message);
Expand All @@ -175,7 +176,7 @@ function main(): void {
});

sender.match<Message.MobxSpliceMessage>(Message.MessageType.MobxSplice, message => {
const parent = store.getObject(message.payload.name, message.payload.id);
const parent = project.getObject(message.payload.name, message.payload.id);

if (!parent) {
console.log(message);
Expand Down

0 comments on commit 9a7a9c3

Please sign in to comment.