77// LICENSE file in the root directory of this source tree.
88// -----------------------------------------------------------------------
99
10+
11+ function getElementOrThrow ( id ) {
12+ const el = document . getElementById ( id ) ;
13+ if ( ! el ) throw new Error ( `Element with ID ${ id } not found` ) ;
14+ return el ;
15+ }
16+
17+ function createElement ( tag , props , children = [ ] ) {
18+ const el = document . createElement ( tag ) ;
19+ Object . assign ( el , props ) ;
20+ children . forEach ( child => el . appendChild ( child ) ) ;
21+ return el ;
22+ }
23+
1024export function setTreeModel ( id , treeModel ) {
1125 try {
12- const element = document . getElementById ( id ) ;
13- if ( ! element ) {
14- throw new Error ( `Element with ID ${ id } not found` ) ;
15- }
26+ const element = getElementOrThrow ( id ) ;
27+ element . renderItem = ( _ , item , __ , context , update ) => {
28+ const { icon : iconName , name : itemName } = item . data || { } ;
29+ const children = [ ] ;
30+
31+ if ( iconName ) {
32+ children . push ( createElement ( 'ix-icon' , {
33+ name : iconName ,
34+ style : 'margin-right: 0.5rem'
35+ } ) ) ;
36+ }
37+
38+ let nameEl ;
39+ if ( itemName ) {
40+ nameEl = createElement ( 'span' , { innerText : itemName } ) ;
41+ children . push ( nameEl ) ;
42+ }
43+
44+ const el = createElement ( 'ix-tree-item' , {
45+ hasChildren : item . hasChildren ,
46+ context : context [ item . id ]
47+ } , children ) ;
48+
49+ update ( updateTreeItem => {
50+ const uData = updateTreeItem . data || { } ;
51+ if ( nameEl && uData . name ) nameEl . innerText = uData . name ;
52+ } ) ;
53+
54+ return el ;
55+ } ;
1656 element . model = JSON . parse ( treeModel ) ;
17- } catch {
18- console . error ( " Failed to set tree model:" , error ) ;
57+ } catch ( error ) {
58+ console . error ( ' Failed to set tree model:' , error ) ;
1959 }
2060}
2161
2262export function setTreeContext ( id , treeContext ) {
2363 try {
24- const element = document . getElementById ( id ) ;
25- if ( ! element ) {
26- throw new Error ( `Element with ID ${ id } not found` ) ;
27- }
64+ const element = getElementOrThrow ( id ) ;
2865 element . context = JSON . parse ( treeContext ) ;
29- } catch {
30- console . error ( " Failed to set tree context:" , error ) ;
66+ } catch ( error ) {
67+ console . error ( ' Failed to set tree context:' , error ) ;
3168 }
3269}
3370
3471export function markItemAsDirty ( treeId , itemIdentifiers ) {
35- console . log ( 'markItemAsDirty called:' , treeId , itemIdentifiers ) ;
36-
37- const treeElement = document . getElementById ( treeId ) ;
38- if ( ! treeElement ) {
39- console . warn ( `Tree element with id '${ treeId } ' not found` ) ;
40- return ;
41- }
42-
43- if ( typeof treeElement . markItemAsDirty === 'function' ) {
44- treeElement . markItemAsDirty ( itemIdentifiers ) ;
45- console . log ( 'Items marked as dirty:' , itemIdentifiers ) ;
46- } else {
47- console . warn ( 'markItemAsDirty method not available on tree element' ) ;
48- }
72+ console . log ( 'markItemAsDirty called:' , treeId , itemIdentifiers ) ;
73+ const treeElement = document . getElementById ( treeId ) ;
74+ if ( ! treeElement ) {
75+ console . warn ( `Tree element with id '${ treeId } ' not found` ) ;
76+ return ;
77+ }
78+ if ( typeof treeElement . markItemAsDirty === 'function' ) {
79+ treeElement . markItemAsDirty ( itemIdentifiers ) ;
80+ console . log ( 'Items marked as dirty:' , itemIdentifiers ) ;
81+ } else {
82+ console . warn ( 'markItemAsDirty method not available on tree element' ) ;
83+ }
4984}
0 commit comments