@@ -77,6 +77,7 @@ export const setOnStateOfItem = (
77
77
id : string ,
78
78
on : boolean ,
79
79
default_choice_mode : FolderModeType = 0 ,
80
+ multi_chose_folder_switch_all : boolean = false
80
81
) : IHostsListObject [ ] => {
81
82
let new_list : IHostsListObject [ ] = lodash . cloneDeep ( list )
82
83
@@ -85,13 +86,24 @@ export const setOnStateOfItem = (
85
86
86
87
item . on = on
87
88
88
- if ( ! on ) return new_list
89
+ let itemIsInTopLevel = isInTopLevel ( list , id ) ;
90
+ if ( multi_chose_folder_switch_all ) {
91
+ item = switchFolderChild ( item , on )
92
+ ! itemIsInTopLevel && switchItemParentIsON ( new_list , item , on )
93
+ }
94
+
95
+ if ( ! on ) {
96
+ return new_list
97
+ }
89
98
90
- if ( isInTopLevel ( list , id ) ) {
99
+ if ( itemIsInTopLevel ) {
91
100
if ( default_choice_mode === 1 ) {
92
101
new_list . map ( ( item ) => {
93
102
if ( item . id !== id ) {
94
103
item . on = false
104
+ if ( multi_chose_folder_switch_all ) {
105
+ item = switchFolderChild ( item , false )
106
+ }
95
107
}
96
108
} )
97
109
}
@@ -104,6 +116,9 @@ export const setOnStateOfItem = (
104
116
parent . children . map ( ( item ) => {
105
117
if ( item . id !== id ) {
106
118
item . on = false
119
+ if ( multi_chose_folder_switch_all ) {
120
+ item = switchFolderChild ( item , false )
121
+ }
107
122
}
108
123
} )
109
124
}
@@ -113,6 +128,60 @@ export const setOnStateOfItem = (
113
128
return new_list
114
129
}
115
130
131
+ export const switchItemParentIsON = (
132
+ list : IHostsListObject [ ] ,
133
+ item : IHostsListObject ,
134
+ on : boolean
135
+ ) => {
136
+ let parent = getParentOfItem ( list , item . id )
137
+
138
+ if ( parent ) {
139
+ if ( parent . folder_mode === 1 ) {
140
+ return
141
+ }
142
+ if ( ! on ) {
143
+ parent . on = on
144
+ } else if ( parent . children ) {
145
+ let parentOn = true
146
+ parent . children . forEach ( ( item ) => {
147
+ if ( ! item . on ) {
148
+ parentOn = false
149
+ }
150
+ } )
151
+ parent . on = parentOn
152
+ }
153
+
154
+ let itemIsInTopLevel = isInTopLevel ( list , parent . id )
155
+ if ( ! itemIsInTopLevel ) {
156
+ switchItemParentIsON ( list , parent , on )
157
+ }
158
+ }
159
+ }
160
+
161
+ export const switchFolderChild = (
162
+ item : IHostsListObject ,
163
+ on : boolean ,
164
+ ) : IHostsListObject => {
165
+ if ( item . type != 'folder' ) {
166
+ return item
167
+ }
168
+ let folder_mode = item . folder_mode
169
+ if ( folder_mode === 1 ) {
170
+ return item
171
+ }
172
+
173
+ if ( item . children ) {
174
+ item . children . forEach ( ( item ) => {
175
+ item . on = on
176
+ if ( item . type == 'folder' ) {
177
+ item = switchFolderChild ( item , on )
178
+ }
179
+ } )
180
+ }
181
+
182
+ return item ;
183
+ }
184
+
116
185
export const deleteItemById = ( list : IHostsListObject [ ] , id : string ) => {
117
186
let idx = list . findIndex ( ( item ) => item . id === id )
118
187
if ( idx >= 0 ) {
0 commit comments