-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
folder.ts
46 lines (38 loc) · 1.27 KB
/
folder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import IComponent from './icomponent'
export default class Folder implements IComponent {
// A composite can contain leaves and composites
referenceToParent?: Folder
name: string
components: IComponent[]
constructor(name: string) {
this.name = name
this.components = []
}
dir(indent: string): void {
console.log(`${indent}<DIR> ${this.name}`)
this.components.forEach((component) => {
component.dir(indent + '..')
})
}
attach(component: IComponent): void {
// Detach leaf / composite from any current parent reference and
// then set the parent reference to this composite(self)
component.detach()
component.referenceToParent = this
this.components.push(component)
}
delete(component: IComponent): void {
// Removes leaf/composite from this composite self.components
const index = this.components.indexOf(component)
if (index > -1) {
this.components.splice(index, 1)
}
}
detach(): void {
// Detaching this composite from its parent composite
if (this.referenceToParent) {
this.referenceToParent.delete(this)
this.referenceToParent = undefined
}
}
}