UNPKG

4.56 kBPlain TextView Raw
1import { TreeComponent, TreeAPI, CheckboxValueConsistency } from '../types/Tree'
2import { State } from '../types/State'
3import { TreeNode } from '../types/Node'
4import { IEyzyTreeAPI, IEyzyNodeAPI } from '../types/Api'
5
6import EyzyNodeAPI from './EyzyNodeAPI'
7
8const callMethod = (api: IEyzyTreeAPI, name: string, criteria: any, args?: any[]): boolean => {
9 return api._operate(criteria, (node: IEyzyNodeAPI) => {
10 return args ? node[name].apply(node, args) : node[name]()
11 })
12}
13
14export default class EyzyTreeAPI implements IEyzyTreeAPI {
15 _tree: TreeComponent
16 _state: State
17 _api: TreeAPI
18
19 isMultiple: boolean
20
21 constructor(api: TreeAPI, isMultiple: boolean = false) {
22 this._tree = api.tree
23 this._state = api.state
24 this._api = api
25 this.isMultiple = isMultiple
26 }
27
28 useMultiple(isMultiple: boolean): EyzyTreeAPI {
29 this.isMultiple = isMultiple
30 return this
31 }
32
33 _operate(criteria: any, operator: (node: IEyzyNodeAPI) => any): boolean {
34 if (!Array.isArray(criteria)) {
35 criteria = [criteria]
36 }
37
38 const nodes: TreeNode[] | (TreeNode | null) = this.isMultiple
39 ? this._api.findAll(...criteria)
40 : this._api.find(...criteria)
41
42 if (!nodes || Array.isArray(nodes) && !nodes.length) {
43 return false
44 }
45
46 return operator(new EyzyNodeAPI(nodes, this._api))
47 }
48
49 find(...criteria: any): IEyzyNodeAPI | null {
50 const result: TreeNode | null = this._api.find(...criteria)
51
52 if (!result) {
53 return null
54 }
55
56 return new EyzyNodeAPI(result, this._api)
57 }
58
59 findAll(...criteria: any): IEyzyNodeAPI | null {
60 const result: TreeNode[] = this._api.findAll(...criteria)
61
62 if (!result || !result.length) {
63 return null
64 }
65
66 return new EyzyNodeAPI(result, this._api)
67 }
68
69 remove(criteria: any): boolean {
70 return callMethod(this, 'remove', criteria)
71 }
72
73 empty(criteria: any): boolean {
74 return callMethod(this, 'empty', criteria)
75 }
76
77 selected(): TreeNode | TreeNode[] | null {
78 return this._api.selected()
79 }
80
81 select(criteria: any, extendSelection?: boolean): boolean {
82 return callMethod(this, 'select', criteria, [extendSelection])
83 }
84
85 unselectAll(): boolean {
86 this._tree.unselectAll()
87 return true
88 }
89
90 unselect(criteria: any): boolean {
91 return callMethod(this, 'unselect', criteria)
92 }
93
94 checked(valueConsistsOf?: CheckboxValueConsistency, ignoreDisabled?: boolean): TreeNode[] {
95 return this._api.checked(valueConsistsOf, ignoreDisabled)
96 }
97
98 check(criteria: any): boolean {
99 return callMethod(this, 'check', criteria)
100 }
101
102 uncheck(criteria: any): boolean {
103 return callMethod(this, 'uncheck', criteria)
104 }
105
106 // exclusive method that not implemented in component;
107 uncheckAll(): boolean {
108 if (!this._tree.props.checkable) {
109 return false
110 }
111
112 const tree = this._tree
113 const state = tree.getState()
114
115 tree.checked = tree.checked.filter((id: string) => {
116 state.set(id, 'checked', false)
117 return false
118 })
119
120 tree.indeterminate = tree.indeterminate.filter((id: string) => {
121 state.set(id, 'indeterminate', false)
122 return false
123 })
124
125 tree.updateState(state)
126
127 return true
128 }
129
130 disable(criteria: any): boolean {
131 return callMethod(this, 'disable', criteria)
132 }
133
134 enable(criteria: any): boolean {
135 return callMethod(this, 'enable', criteria)
136 }
137
138 disableCheckbox(criteria: any): boolean {
139 return callMethod(this, 'disableCheckbox', criteria)
140 }
141
142 enableCheckbox(criteria: any): boolean {
143 return callMethod(this, 'enableCheckbox', criteria)
144 }
145
146 // TODO: expandAll - api.findAll({ expanded: true }).expand()
147 expand(criteria: any): boolean {
148 return callMethod(this, 'expand', criteria)
149 }
150
151 // TODO: collapseAll - api.find(node => new EyzyNodeAPI(node).hasChild())
152 collapse(criteria: any): boolean {
153 return callMethod(this, 'collapse', criteria)
154 }
155
156 data(criteria: any, key: any, value?: any): any {
157 return callMethod(this, 'data', criteria, [key, value])
158 }
159
160 hasClass(criteria: any, className: string): boolean {
161 return callMethod(this, 'hasClass', criteria, [className])
162 }
163
164 addClass(criteria: any, ...classNames: string[]): any {
165 return callMethod(this, 'addClass', criteria, classNames)
166 }
167
168 removeClass(criteria: any, ...classNames: string[]): any {
169 return callMethod(this, 'removeClass', criteria, classNames)
170 }
171}
\No newline at end of file