@@ -195,6 +195,12 @@ class Dashboard extends DashboardLayoutMixin(ElementMixin(ThemableMixin(PolylitM
195
195
} ;
196
196
} ,
197
197
} ,
198
+
199
+ /** @private */
200
+ __childCount : {
201
+ type : Number ,
202
+ value : 0 ,
203
+ } ,
198
204
} ;
199
205
}
200
206
@@ -221,11 +227,14 @@ class Dashboard extends DashboardLayoutMixin(ElementMixin(ThemableMixin(PolylitM
221
227
222
228
/** @protected */
223
229
render ( ) {
224
- return html `< div id ="grid "> < slot > </ slot > </ div > ` ;
230
+ return html `< div id ="grid ">
231
+ ${ [ ...Array ( this . __childCount ) ] . map ( ( _ , index ) => html `< slot name ="slot- ${ index } "> </ slot > ` ) }
232
+ </ div > ` ;
225
233
}
226
234
227
235
/** @private */
228
236
__itemsOrRendererChanged ( items , renderer ) {
237
+ this . __childCount = items ? items . length : 0 ;
229
238
this . __renderItemWrappers ( items || [ ] ) ;
230
239
231
240
this . querySelectorAll ( WRAPPER_LOCAL_NAME ) . forEach ( ( wrapper ) => {
@@ -257,38 +266,25 @@ class Dashboard extends DashboardLayoutMixin(ElementMixin(ThemableMixin(PolylitM
257
266
__renderItemWrappers ( items , hostElement = this ) {
258
267
// Get all the wrappers in the host element
259
268
let wrappers = [ ...hostElement . children ] . filter ( ( el ) => el . localName === WRAPPER_LOCAL_NAME ) ;
260
- let previousWrapper = null ;
261
269
262
270
const focusedWrapper = wrappers . find ( ( wrapper ) => wrapper . querySelector ( ':focus' ) ) ;
263
271
const focusedWrapperWillBeRemoved = focusedWrapper && ! this . __isActiveWrapper ( focusedWrapper ) ;
264
272
const wrapperClosestToRemovedFocused =
265
273
focusedWrapperWillBeRemoved && this . __getClosestActiveWrapper ( focusedWrapper ) ;
266
274
267
- items . forEach ( ( item ) => {
275
+ items . forEach ( ( item , index ) => {
268
276
// Find the wrapper for the item or create a new one
269
277
const wrapper = wrappers . find ( ( el ) => itemsEqual ( getElementItem ( el ) , item ) ) || this . __createWrapper ( item ) ;
270
278
wrappers = wrappers . filter ( ( el ) => el !== wrapper ) ;
279
+ if ( ! wrapper . isConnected ) {
280
+ hostElement . appendChild ( wrapper ) ;
281
+ }
271
282
272
283
// Update the wrapper style
273
284
this . __updateWrapper ( wrapper , item ) ;
274
285
275
- if ( wrapper !== focusedWrapper ) {
276
- if ( previousWrapper ) {
277
- // Append the wrapper after the previous one if it's not already there
278
- if ( wrapper . previousElementSibling !== previousWrapper ) {
279
- previousWrapper . after ( wrapper ) ;
280
- }
281
- } else if ( hostElement . firstChild ) {
282
- // Insert the wrapper as the first child of the host element if it's not already there
283
- if ( wrapper !== hostElement . firstChild ) {
284
- hostElement . insertBefore ( wrapper , hostElement . firstChild ) ;
285
- }
286
- } else {
287
- // Append the wrapper to the empty host element
288
- hostElement . appendChild ( wrapper ) ;
289
- }
290
- }
291
- previousWrapper = wrapper ;
286
+ // Update the wrapper slot
287
+ wrapper . slot = `slot-${ index } ` ;
292
288
293
289
// Render section if the item has subitems
294
290
if ( item . items ) {
@@ -307,6 +303,7 @@ class Dashboard extends DashboardLayoutMixin(ElementMixin(ThemableMixin(PolylitM
307
303
section . __i18n = this . i18n ;
308
304
309
305
// Render the subitems
306
+ section . __childCount = item . items . length ;
310
307
this . __renderItemWrappers ( item . items , section ) ;
311
308
}
312
309
} ) ;
@@ -368,20 +365,35 @@ class Dashboard extends DashboardLayoutMixin(ElementMixin(ThemableMixin(PolylitM
368
365
return getItemsArrayOfItem ( getElementItem ( wrapper ) , this . items ) ;
369
366
}
370
367
368
+ /**
369
+ * Parses the slot name to get the index of the item in the dashboard
370
+ * For example, slot name "slot-12" will return 12
371
+ * @private
372
+ */
373
+ __parseSlotIndex ( slotName ) {
374
+ return parseInt ( slotName . split ( '-' ) [ 1 ] ) ;
375
+ }
376
+
371
377
/** @private */
372
378
__getClosestActiveWrapper ( wrapper ) {
373
379
if ( ! wrapper || this . __isActiveWrapper ( wrapper ) ) {
374
380
return wrapper ;
375
381
}
376
382
383
+ // Sibling wrappers sorted by their slot name
384
+ const siblingWrappers = [ ...wrapper . parentElement . children ] . sort ( ( a , b ) => {
385
+ return this . __parseSlotIndex ( a . slot ) - this . __parseSlotIndex ( b . slot ) ;
386
+ } ) ;
387
+
377
388
// Starting from the given wrapper element, iterates through the siblings in the given direction
378
389
// to find the closest wrapper that represents an item in the dashboard's items array
379
390
const findSiblingWrapper = ( wrapper , dir ) => {
380
391
while ( wrapper ) {
381
392
if ( this . __isActiveWrapper ( wrapper ) ) {
382
393
return wrapper ;
383
394
}
384
- wrapper = dir === 1 ? wrapper . nextElementSibling : wrapper . previousElementSibling ;
395
+ const currentIndex = siblingWrappers . indexOf ( wrapper ) ;
396
+ wrapper = dir === 1 ? siblingWrappers [ currentIndex + 1 ] : siblingWrappers [ currentIndex - 1 ] ;
385
397
}
386
398
} ;
387
399
0 commit comments