1- import Data from "../../modules/data.js"
2- import EventHandler from "../../modules/event-handler.js"
1+ import EventHandler from "../../modules/event-handler.js"
32
43export function init ( id , options ) {
5- const { invoke, method, isVirtualize } = options
64 const el = document . getElementById ( id )
75 if ( el === null ) {
86 return
97 }
108
11- const tree = { el, invoke, isVirtualize } ;
12- Data . set ( id , tree )
13-
9+ const { invoke, method } = options
1410 EventHandler . on ( el , 'keydown' , '.tree-root' , e => {
1511 if ( e . key === 'ArrowDown' || e . key === 'ArrowUp' || e . key === 'ArrowLeft' || e . key === 'ArrowRight' ) {
1612 const v = el . getAttribute ( 'data-bb-keyboard' ) ;
@@ -33,103 +29,89 @@ export function init(id, options) {
3329}
3430
3531export function scroll ( id , options ) {
36- const tree = Data . get ( id )
37- if ( tree ) {
38- const { el } = tree ;
39- const item = el . querySelector ( ".active .tree-node" ) ;
40- if ( item ) {
41- item . scrollIntoView ( options ) ;
42- }
32+ const el = document . getElementById ( id ) ;
33+ const item = el . querySelector ( ".active .tree-node" ) ;
34+ if ( item ) {
35+ item . scrollIntoView ( options ?? { behavior : 'smooth' , block : 'start' , inline : 'nearest' } ) ;
4336 }
4437}
4538
4639export function toggleLoading ( id , index , state ) {
47- const tree = Data . get ( id )
48- if ( tree ) {
49- const { el } = tree ;
50- const node = el . querySelector ( `[data-bb-tree-view-index="${ index } "]` ) ;
51- if ( node ) {
52- if ( state ) {
53- node . classList . add ( 'loading' ) ;
54- }
55- else {
56- node . classList . remove ( 'loading' ) ;
57- }
40+ const el = document . getElementById ( id ) ;
41+ const node = el . querySelector ( `[data-bb-tree-view-index="${ index } "]` ) ;
42+ if ( node ) {
43+ if ( state ) {
44+ node . classList . add ( 'loading' ) ;
45+ }
46+ else {
47+ node . classList . remove ( 'loading' ) ;
5848 }
5949 }
6050}
6151
6252export function setChildrenState ( id , index , state ) {
63- const tree = Data . get ( id )
64- if ( tree ) {
65- const { el } = tree ;
66- const node = el . querySelector ( `[data-bb-tree-view-index="${ index } "]` ) ;
67- const level = parseInt ( node . style . getPropertyValue ( '--bb-tree-view-level' ) ) ;
68- if ( node ) {
69- let next = node . nextElementSibling ;
70- while ( next ) {
71- const currentLevel = parseInt ( next . style . getPropertyValue ( '--bb-tree-view-level' ) ) ;
72- if ( currentLevel <= level ) {
73- break ;
74- }
75- const checkbox = next . querySelector ( '.form-check-input' ) ;
76- if ( checkbox ) {
77- checkbox . indeterminate = false ;
78- checkbox . checked = state === 1 ;
79- EventHandler . trigger ( checkbox , "statechange.bb.checkbox" , { state } ) ;
80- }
81- next = next . nextElementSibling ;
53+ const el = document . getElementById ( id ) ;
54+ const node = el . querySelector ( `[data-bb-tree-view-index="${ index } "]` ) ;
55+ const level = parseInt ( node . style . getPropertyValue ( '--bb-tree-view-level' ) ) ;
56+ if ( node ) {
57+ let next = node . nextElementSibling ;
58+ while ( next ) {
59+ const currentLevel = parseInt ( next . style . getPropertyValue ( '--bb-tree-view-level' ) ) ;
60+ if ( currentLevel <= level ) {
61+ break ;
62+ }
63+ const checkbox = next . querySelector ( '.form-check-input' ) ;
64+ if ( checkbox ) {
65+ checkbox . indeterminate = false ;
66+ checkbox . checked = state === 1 ;
67+ EventHandler . trigger ( checkbox , "statechange.bb.checkbox" , { state } ) ;
8268 }
69+ next = next . nextElementSibling ;
8370 }
8471 }
8572}
8673
87- export async function setParentState ( id , index , state ) {
88- const tree = Data . get ( id )
89- if ( tree ) {
90- const { el, invoke } = tree ;
91- const node = el . querySelector ( `[data-bb-tree-view-index="${ index } "]` ) ;
92- let level = parseInt ( node . style . getPropertyValue ( '--bb-tree-view-level' ) ) ;
93- if ( node ) {
94- const parents = [ ] ;
95- let prev = node . previousElementSibling ;
96- while ( prev ) {
97- const currentLevel = parseInt ( prev . style . getPropertyValue ( '--bb-tree-view-level' ) ) ;
98- if ( currentLevel < level ) {
99- level = currentLevel ;
100- parents . push ( prev ) ;
101- }
102- prev = prev . previousElementSibling ;
74+ export async function setParentState ( id , invoke , method , index ) {
75+ const el = document . getElementById ( id ) ;
76+ const node = el . querySelector ( `[data-bb-tree-view-index="${ index } "]` ) ;
77+ let level = parseInt ( node . style . getPropertyValue ( '--bb-tree-view-level' ) ) ;
78+ if ( node ) {
79+ const parents = [ ] ;
80+ let prev = node . previousElementSibling ;
81+ while ( prev ) {
82+ const currentLevel = parseInt ( prev . style . getPropertyValue ( '--bb-tree-view-level' ) ) ;
83+ if ( currentLevel < level ) {
84+ level = currentLevel ;
85+ parents . push ( prev ) ;
10386 }
87+ prev = prev . previousElementSibling ;
88+ }
10489
105- if ( parents . length > 0 ) {
106- const results = await invoke . invokeMethodAsync ( 'GetParentsState' , parents . map ( p => parseInt ( p . getAttribute ( 'data-bb-tree-view-index' ) ) ) , index , state ) ;
107- for ( let index = 0 ; index < parents . length ; index ++ ) {
108- const checkbox = parents [ index ] . querySelector ( '.form-check-input' ) ;
109- const result = results [ index ] ;
110- checkbox . indeterminate = false ;
111- if ( result === 0 ) {
112- checkbox . checked = false ;
113- }
114- else if ( result === 1 ) {
115- checkbox . checked = true ;
116- }
117- else {
118- checkbox . indeterminate = true ;
119- }
120- EventHandler . trigger ( checkbox , "statechange.bb.checkbox" , { state : result } ) ;
90+ if ( parents . length > 0 ) {
91+ const results = await invoke . invokeMethodAsync ( method , parents . map ( p => parseInt ( p . getAttribute ( 'data-bb-tree-view-index' ) ) ) ) ;
92+ for ( let index = 0 ; index < parents . length ; index ++ ) {
93+ const checkbox = parents [ index ] . querySelector ( '.form-check-input' ) ;
94+ const result = results [ index ] ;
95+ checkbox . indeterminate = false ;
96+ if ( result === 0 ) {
97+ checkbox . checked = false ;
98+ }
99+ else if ( result === 1 ) {
100+ checkbox . checked = true ;
101+ }
102+ else {
103+ checkbox . indeterminate = true ;
121104 }
105+ EventHandler . trigger ( checkbox , "statechange.bb.checkbox" , { state : result } ) ;
122106 }
123107 }
124108 }
125109}
126110
127111export function dispose ( id ) {
128- const tree = Data . get ( id )
129- Data . remove ( id ) ;
112+ const el = document . getElementById ( id ) ;
130113
131- if ( tree ) {
132- const { el } = tree ;
114+ if ( el ) {
133115 EventHandler . off ( el , 'keyup' , '.tree-root' ) ;
134116 }
135117}
0 commit comments