2222qx . Class . define ( "osparc.component.widget.NodesSlidesTree" , {
2323 extend : qx . ui . core . Widget ,
2424
25- construct : function ( initData = { } ) {
25+ construct : function ( study ) {
2626 this . base ( arguments ) ;
2727
28+ this . __study = study ;
29+
2830 this . _setLayout ( new qx . ui . layout . VBox ( 10 ) ) ;
2931
30- this . __tree = this . _createChildControlImpl ( "tree" ) ;
31- const disable = this . _createChildControlImpl ( "disable" ) ;
32+ this . __tree = this . getChildControl ( "tree" ) ;
33+ const disable = this . getChildControl ( "disable" ) ;
3234 disable . addListener ( "execute" , ( ) => this . __disableSlides ( ) , this ) ;
33- const enable = this . _createChildControlImpl ( "enable" ) ;
35+ const enable = this . getChildControl ( "enable" ) ;
3436 enable . addListener ( "execute" , ( ) => this . __enableSlides ( ) , this ) ;
3537
36- const model = this . __initTree ( ) ;
38+ const model = this . __initRoot ( ) ;
3739 this . __tree . setModel ( model ) ;
3840
39- this . __populateTree ( ) ;
41+ this . __initTree ( ) ;
4042 this . __recalculatePositions ( ) ;
4143
42- this . __initData ( initData ) ;
44+ this . __initData ( ) ;
4345 } ,
4446
4547 events : {
4648 "finished" : "qx.event.type.Event"
4749 } ,
4850
4951 statics : {
50- convertModel : function ( nodes ) {
51- let children = [ ] ;
52- for ( let nodeId in nodes ) {
53- const node = nodes [ nodeId ] ;
54- let nodeInTree = {
55- label : "" ,
56- nodeId : node . getNodeId ( ) ,
57- skipNode : false ,
58- position : - 1
59- } ;
60- nodeInTree . label = node . getLabel ( ) ;
61- if ( node . isContainer ( ) ) {
62- nodeInTree . children = this . convertModel ( node . getInnerNodes ( ) ) ;
63- }
64- children . push ( nodeInTree ) ;
65- }
66- return children ;
67- } ,
68-
6952 getItemsInTree : function ( itemMdl , children = [ ] ) {
7053 children . push ( itemMdl ) ;
7154 for ( let i = 0 ; itemMdl . getChildren && i < itemMdl . getChildren ( ) . length ; i ++ ) {
@@ -83,6 +66,7 @@ qx.Class.define("osparc.component.widget.NodesSlidesTree", {
8366
8467 members : {
8568 __tree : null ,
69+ __study : null ,
8670
8771 _createChildControlImpl : function ( id ) {
8872 let control ;
@@ -133,22 +117,19 @@ qx.Class.define("osparc.component.widget.NodesSlidesTree", {
133117 return tree ;
134118 } ,
135119
136- __initTree : function ( ) {
137- const study = osparc . store . Store . getInstance ( ) . getCurrentStudy ( ) ;
138- const topLevelNodes = study . getWorkbench ( ) . getNodes ( ) ;
120+ __initRoot : function ( ) {
121+ const study = this . __study ;
139122 let rootData = {
140123 label : study . getName ( ) ,
141- children : this . self ( ) . convertModel ( topLevelNodes ) ,
124+ children : [ ] ,
142125 nodeId : study . getUuid ( ) ,
143126 skipNode : null ,
144127 position : null
145128 } ;
146129 return qx . data . marshal . Json . createModel ( rootData , true ) ;
147130 } ,
148131
149- __populateTree : function ( ) {
150- const study = osparc . store . Store . getInstance ( ) . getCurrentStudy ( ) ;
151-
132+ __initTree : function ( ) {
152133 this . __tree . setDelegate ( {
153134 createItem : ( ) => {
154135 const nodeSlideTreeItem = new osparc . component . widget . NodeSlideTreeItem ( ) ;
@@ -160,7 +141,7 @@ qx.Class.define("osparc.component.widget.NodesSlidesTree", {
160141 bindItem : ( c , item , id ) => {
161142 c . bindDefaultProperties ( item , id ) ;
162143 c . bindProperty ( "nodeId" , "nodeId" , null , item , id ) ;
163- const node = study . getWorkbench ( ) . getNode ( item . getModel ( ) . getNodeId ( ) ) ;
144+ const node = this . __study . getWorkbench ( ) . getNode ( item . getModel ( ) . getNodeId ( ) ) ;
164145 if ( node ) {
165146 node . bind ( "label" , item . getModel ( ) , "label" ) ;
166147 }
@@ -185,41 +166,77 @@ qx.Class.define("osparc.component.widget.NodesSlidesTree", {
185166 } ) ;
186167 } ,
187168
188- __initData : function ( initData ) {
189- if ( Object . keys ( initData ) . length ) {
190- const children = this . __tree . getModel ( ) . getChildren ( ) . toArray ( ) ;
191- children . forEach ( child => {
192- const nodeId = child . getNodeId ( ) ;
193- if ( nodeId in initData ) {
194- child . setPosition ( initData [ nodeId ] . position ) ;
195- child . setSkipNode ( false ) ;
196- } else {
197- child . setPosition ( - 1 ) ;
198- child . setSkipNode ( true ) ;
199- }
200- } ) ;
169+ /**
170+ * Converts an object of nodes into an array of children to be consumed by the tree model.
171+ * The tree is expecting to bind the children into NodeSlideTreeItem with nodeId, position and skipNode as props.
172+ * @param {Object.<Nodes> } nodes Object with nodes <nodeId: node>
173+ * @returns {Array } Array of objects with label, nodeId, position and skipNode fields.
174+ */
175+ __convertToModel : function ( nodes ) {
176+ const children = [ ] ;
177+ for ( let nodeId in nodes ) {
178+ const node = nodes [ nodeId ] ;
179+ const nodeInTree = {
180+ label : node . getLabel ( ) ,
181+ nodeId : node . getNodeId ( )
182+ } ;
183+ const pos = this . __study . getUi ( ) . getSlideshow ( ) . getPosition ( nodeId ) ;
184+ if ( pos === - 1 ) {
185+ nodeInTree . position = - 1 ;
186+ nodeInTree . skipNode = true ;
187+ } else {
188+ nodeInTree . position = pos ;
189+ nodeInTree . skipNode = false ;
190+ }
191+ if ( node . isContainer ( ) ) {
192+ nodeInTree . children = this . __convertToModel ( node . getInnerNodes ( ) ) ;
193+ }
194+ children . push ( nodeInTree ) ;
201195 }
196+ return children ;
197+ } ,
198+
199+ __initData : function ( ) {
200+ const topLevelNodes = this . __study . getWorkbench ( ) . getNodes ( ) ;
201+
202+ this . __tree . getModel ( ) . getChildren ( ) . removeAll ( ) ;
203+ const allChildren = this . __convertToModel ( topLevelNodes ) ;
204+ this . __tree . getModel ( ) . setChildren ( qx . data . marshal . Json . createModel ( allChildren ) ) ;
205+
206+ const children = this . __tree . getModel ( ) . getChildren ( ) . toArray ( ) ;
207+ children . sort ( ( a , b ) => {
208+ const aPos = a . getPosition ( ) ;
209+ const bPos = b . getPosition ( ) ;
210+ if ( aPos === - 1 ) {
211+ return 1 ;
212+ }
213+ if ( bPos === - 1 ) {
214+ return - 1 ;
215+ }
216+ return aPos - bPos ;
217+ } ) ;
218+ this . __tree . refresh ( ) ;
202219 } ,
203220
204221 __itemActioned : function ( item , action ) {
205- let fntc ;
222+ let fnct ;
206223 switch ( action ) {
207224 case "show" :
208- fntc = this . __show ;
225+ fnct = this . __show ;
209226 break ;
210227 case "hide" :
211- fntc = this . __hide ;
228+ fnct = this . __hide ;
212229 break ;
213230 case "moveUp" :
214- fntc = this . __moveUp ;
231+ fnct = this . __moveUp ;
215232 break ;
216233 case "moveDown" :
217- fntc = this . __moveDown ;
234+ fnct = this . __moveDown ;
218235 break ;
219236 }
220- if ( fntc ) {
237+ if ( fnct ) {
221238 this . __tree . setSelection ( new qx . data . Array ( [ item . getModel ( ) ] ) ) ;
222- fntc . call ( this , item . getModel ( ) ) ;
239+ fnct . call ( this , item . getModel ( ) ) ;
223240 this . __recalculatePositions ( ) ;
224241 }
225242 } ,
@@ -276,8 +293,8 @@ qx.Class.define("osparc.component.widget.NodesSlidesTree", {
276293 this . __tree . refresh ( ) ;
277294 } ,
278295
279- __enableSlides : function ( ) {
280- let slideshow = { } ;
296+ __serialize : function ( ) {
297+ const slideshow = { } ;
281298 const model = this . __tree . getModel ( ) ;
282299 const children = model . getChildren ( ) . toArray ( ) ;
283300 children . forEach ( child => {
@@ -287,14 +304,17 @@ qx.Class.define("osparc.component.widget.NodesSlidesTree", {
287304 } ;
288305 }
289306 } ) ;
290- const study = osparc . store . Store . getInstance ( ) . getCurrentStudy ( ) ;
291- study . getUi ( ) . setSlideshow ( slideshow ) ;
307+ return slideshow ;
308+ } ,
309+
310+ __enableSlides : function ( ) {
311+ const slideshow = this . __serialize ( ) ;
312+ this . __study . getUi ( ) . getSlideshow ( ) . setData ( slideshow ) ;
292313 this . fireEvent ( "finished" ) ;
293314 } ,
294315
295316 __disableSlides : function ( ) {
296- const study = osparc . store . Store . getInstance ( ) . getCurrentStudy ( ) ;
297- study . getUi ( ) . setSlideshow ( { } ) ;
317+ this . __study . getUi ( ) . getSlideshow ( ) . setData ( { } ) ;
298318 this . fireEvent ( "finished" ) ;
299319 }
300320 }
0 commit comments