Skip to content

Commit

Permalink
fix: refactor the project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
NewByVector committed Dec 11, 2020
1 parent be27e2a commit 51bf7d6
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 72 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const model = [
],
]

const girdLayout = new Layout.GridLayout({
const girdLayout = new Layout({
width: 600,
height: 400,
rows: 4,
Expand Down
4 changes: 3 additions & 1 deletion __tests__/grid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Layout } from '../src'

describe('#GridLayout', () => {
it('return correct default config', () => {
const grid = new Layout.GridLayout();
const grid = new Layout({
type: 'grid'
})
expect(grid.getDefaultCfg()).toEqual({
begin: [0, 0],
cols: undefined,
Expand Down
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
import * as Layout from './layout'
import * as Registy from './registy'

export { Layout, Registy }
export * from './layout'
export * from './registy'
10 changes: 1 addition & 9 deletions src/layout/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
export * from './base'
export * from './grid'
export * from './random'
export * from './g-force'
export * from './force'
export * from './circular'
export * from './dagre'
export * from './radial'
export * from './types'
export * from './layout'
71 changes: 71 additions & 0 deletions src/layout/layout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Base } from './base'
import { Model } from './types'
import { GridLayout } from './grid'
import { RandomLayout } from './random'
import { GForceLayout } from './g-force'
import { ForceLayout } from './force'
import { CircularLayout } from './circular'
import { DagreLayout } from './dagre'
import { RadialLayout } from './radial'
import { registLayout, getLayoutByName } from '../registy'

export class Layout {
public readonly layoutInstance: Base

constructor(options: Layout.LayoutOptions) {
const layoutClass = getLayoutByName(options.type)
this.layoutInstance = new layoutClass(options)
}

layout(data: Model) {
return this.layoutInstance.layout(data)
}

updateCfg(cfg: Layout.LayoutOptions) {
this.layoutInstance.updateCfg(cfg)
}

init(data: Model) {
this.layoutInstance.init(data)
}

execute() {
this.layoutInstance.execute()
}

getDefaultCfg() {
return this.layoutInstance.getDefaultCfg()
}

destroy() {
return this.layoutInstance.destroy()
}
}

export namespace Layout {
registLayout('grid', GridLayout)
registLayout('random', RandomLayout)
registLayout('gForce', GForceLayout)
registLayout('force', ForceLayout)
registLayout('circular', CircularLayout)
registLayout('dagre', DagreLayout)
registLayout('radial', RadialLayout)

export type LayoutTypes =
| 'grid'
| 'random'
| 'gForce'
| 'force'
| 'circular'
| 'dagre'
| 'radial'

export type LayoutOptions =
| GridLayout.GridLayoutOptions
| RandomLayout.RandomLayoutOptions
| GForceLayout.GForceLayoutOptions
| ForceLayout.ForceLayoutOptions
| CircularLayout.CircularLayoutOptions
| DagreLayout.DagreLayoutOptions
| RadialLayout.RadialLayoutOptions
}
57 changes: 0 additions & 57 deletions src/registy.ts

This file was deleted.

21 changes: 21 additions & 0 deletions src/registy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const map: Map<string, any> = new Map()

export const registLayout = (name: string, customClass: any) => {
if (map.get(name)) {
throw new Error('already have a layout with the same name')
}
map.set(name, customClass)
}

export const unRegistLayout = (name: string) => {
if (map.has(name)) {
map.delete(name)
}
}

export const getLayoutByName = (name: string) => {
if (map.has(name)) {
return map.get(name)
}
return null
}

0 comments on commit 51bf7d6

Please sign in to comment.