@@ -7,7 +7,6 @@ import '@vaadin/avatar/src/vaadin-avatar.js';
7
7
import './vaadin-avatar-group-menu.js' ;
8
8
import './vaadin-avatar-group-menu-item.js' ;
9
9
import './vaadin-avatar-group-overlay.js' ;
10
- import { calculateSplices } from '@polymer/polymer/lib/utils/array-splice.js' ;
11
10
import { afterNextRender } from '@polymer/polymer/lib/utils/render-status.js' ;
12
11
import { html as legacyHtml , PolymerElement } from '@polymer/polymer/polymer-element.js' ;
13
12
import { html , render } from 'lit' ;
@@ -126,6 +125,7 @@ class AvatarGroup extends ResizeMixin(OverlayClassMixin(ElementMixin(ThemableMix
126
125
*/
127
126
items : {
128
127
type : Array ,
128
+ observer : '__itemsChanged' ,
129
129
} ,
130
130
131
131
/**
@@ -188,17 +188,6 @@ class AvatarGroup extends ResizeMixin(OverlayClassMixin(ElementMixin(ThemableMix
188
188
value : ( ) => [ ] ,
189
189
} ,
190
190
191
- /** @private */
192
- __maxReached : {
193
- type : Boolean ,
194
- computed : '__computeMaxReached(items.length, maxItemsVisible)' ,
195
- } ,
196
-
197
- /** @private */
198
- __items : {
199
- type : Array ,
200
- } ,
201
-
202
191
/** @private */
203
192
__itemsInView : {
204
193
type : Number ,
@@ -214,7 +203,7 @@ class AvatarGroup extends ResizeMixin(OverlayClassMixin(ElementMixin(ThemableMix
214
203
_overflowItems : {
215
204
type : Array ,
216
205
observer : '__overflowItemsChanged' ,
217
- computed : '__computeOverflowItems(items.* , __itemsInView, maxItemsVisible)' ,
206
+ computed : '__computeOverflowItems(items, __itemsInView, maxItemsVisible)' ,
218
207
} ,
219
208
220
209
/** @private */
@@ -232,13 +221,11 @@ class AvatarGroup extends ResizeMixin(OverlayClassMixin(ElementMixin(ThemableMix
232
221
233
222
static get observers ( ) {
234
223
return [
235
- '__itemsChanged(items.splices, items.*)' ,
236
- '__i18nItemsChanged(i18n.*, items.length)' ,
224
+ '__i18nItemsChanged(i18n, items)' ,
237
225
'__updateAvatarsTheme(_overflow, _avatars, _theme)' ,
238
- '__updateAvatars(items.*, __itemsInView, maxItemsVisible, _overflow, i18n)' ,
239
- '__updateOverflowAbbr(_overflow, items.length, __itemsInView, maxItemsVisible)' ,
240
- '__updateOverflowHidden(_overflow, items.length, __itemsInView, __maxReached)' ,
241
- '__updateOverflowTooltip(_overflowTooltip, items.length, __itemsInView, maxItemsVisible)' ,
226
+ '__updateAvatars(items, __itemsInView, maxItemsVisible, _overflow, i18n)' ,
227
+ '__updateOverflowAvatar(_overflow, items, __itemsInView, maxItemsVisible)' ,
228
+ '__updateOverflowTooltip(_overflowTooltip, items, __itemsInView, maxItemsVisible)' ,
242
229
] ;
243
230
}
244
231
@@ -406,12 +393,11 @@ class AvatarGroup extends ResizeMixin(OverlayClassMixin(ElementMixin(ThemableMix
406
393
}
407
394
408
395
/** @private */
409
- __updateAvatars ( arr , itemsInView , maxItemsVisible , overflow ) {
410
- if ( ! overflow ) {
396
+ __updateAvatars ( items , itemsInView , maxItemsVisible , overflow ) {
397
+ if ( ! overflow || ! Array . isArray ( items ) ) {
411
398
return ;
412
399
}
413
400
414
- const items = arr . base || [ ] ;
415
401
const limit = this . __getLimit ( items . length , itemsInView , maxItemsVisible ) ;
416
402
417
403
this . __renderAvatars ( limit ? items . slice ( 0 , limit ) : items ) ;
@@ -420,28 +406,20 @@ class AvatarGroup extends ResizeMixin(OverlayClassMixin(ElementMixin(ThemableMix
420
406
}
421
407
422
408
/** @private */
423
- __computeOverflowItems ( arr , itemsInView , maxItemsVisible ) {
424
- const items = arr . base || [ ] ;
425
- const limit = this . __getLimit ( items . length , itemsInView , maxItemsVisible ) ;
409
+ __computeOverflowItems ( items , itemsInView , maxItemsVisible ) {
410
+ const count = Array . isArray ( items ) ? items . length : 0 ;
411
+ const limit = this . __getLimit ( count , itemsInView , maxItemsVisible ) ;
426
412
return limit ? items . slice ( limit ) : [ ] ;
427
413
}
428
414
429
415
/** @private */
430
- __computeMaxReached ( items , maxItemsVisible ) {
431
- return maxItemsVisible != null && items > this . __getMax ( maxItemsVisible ) ;
432
- }
433
-
434
- /** @private */
435
- __updateOverflowAbbr ( overflow , items , itemsInView , maxItemsVisible ) {
416
+ __updateOverflowAvatar ( overflow , items , itemsInView , maxItemsVisible ) {
436
417
if ( overflow ) {
437
- overflow . abbr = `+${ items - this . __getLimit ( items , itemsInView , maxItemsVisible ) } ` ;
438
- }
439
- }
418
+ const count = Array . isArray ( items ) ? items . length : 0 ;
419
+ const maxReached = maxItemsVisible != null && count > this . __getMax ( maxItemsVisible ) ;
440
420
441
- /** @private */
442
- __updateOverflowHidden ( overflow , items , itemsInView , maxReached ) {
443
- if ( overflow ) {
444
- overflow . toggleAttribute ( 'hidden' , ! maxReached && ! ( itemsInView && itemsInView < items ) ) ;
421
+ overflow . abbr = `+${ count - this . __getLimit ( count , itemsInView , maxItemsVisible ) } ` ;
422
+ overflow . toggleAttribute ( 'hidden' , ! maxReached && ! ( itemsInView && itemsInView < count ) ) ;
445
423
}
446
424
}
447
425
@@ -460,18 +438,18 @@ class AvatarGroup extends ResizeMixin(OverlayClassMixin(ElementMixin(ThemableMix
460
438
461
439
/** @private */
462
440
__updateOverflowTooltip ( tooltip , items , itemsInView , maxItemsVisible ) {
463
- if ( ! tooltip ) {
441
+ if ( ! tooltip || ! Array . isArray ( items ) ) {
464
442
return ;
465
443
}
466
444
467
- const limit = this . __getLimit ( items , itemsInView , maxItemsVisible ) ;
445
+ const limit = this . __getLimit ( items . length , itemsInView , maxItemsVisible ) ;
468
446
if ( limit == null ) {
469
447
return ;
470
448
}
471
449
472
450
const result = [ ] ;
473
- for ( let i = limit ; i < items ; i ++ ) {
474
- const item = this . items [ i ] ;
451
+ for ( let i = limit ; i < items . length ; i ++ ) {
452
+ const item = items [ i ] ;
475
453
if ( item ) {
476
454
result . push ( item . name || item . abbr || 'anonymous' ) ;
477
455
}
@@ -500,35 +478,32 @@ class AvatarGroup extends ResizeMixin(OverlayClassMixin(ElementMixin(ThemableMix
500
478
}
501
479
502
480
/** @private */
503
- __itemsChanged ( splices , itemsChange ) {
504
- const items = itemsChange . base ;
481
+ __itemsChanged ( items , oldItems ) {
505
482
this . __setItemsInView ( ) ;
506
483
507
- // Mutation using group.splice('items')
508
- if ( splices && Array . isArray ( splices . indexSplices ) ) {
509
- splices . indexSplices . forEach ( ( mutation ) => {
510
- this . __announceItemsChange ( items , mutation ) ;
511
- } ) ;
512
- } else if ( Array . isArray ( items ) && Array . isArray ( this . __oldItems ) ) {
513
- // Mutation using group.set('items')
514
- const diff = calculateSplices ( items , this . __oldItems ) ;
515
- diff . forEach ( ( mutation ) => {
516
- this . __announceItemsChange ( items , mutation ) ;
517
- } ) ;
484
+ let added = [ ] ;
485
+ let removed = [ ] ;
486
+
487
+ const hasNewItems = Array . isArray ( items ) ;
488
+ const hasOldItems = Array . isArray ( oldItems ) ;
489
+
490
+ if ( hasOldItems ) {
491
+ removed = oldItems . filter ( ( item ) => hasNewItems && ! items . includes ( item ) ) ;
492
+ }
493
+
494
+ if ( hasNewItems ) {
495
+ added = items . filter ( ( item ) => hasOldItems && ! oldItems . includes ( item ) ) ;
518
496
}
519
497
520
- this . __oldItems = items ;
498
+ this . __announceItemsChange ( added , removed ) ;
521
499
}
522
500
523
501
/** @private */
524
- __announceItemsChange ( items , mutation ) {
525
- const { addedCount, index, removed } = mutation ;
502
+ __announceItemsChange ( added , removed ) {
526
503
let addedMsg = [ ] ;
527
504
let removedMsg = [ ] ;
528
- if ( addedCount ) {
529
- addedMsg = items
530
- . slice ( index , index + addedCount )
531
- . map ( ( user ) => this . __getMessage ( user , this . i18n . joined || '{user} joined' ) ) ;
505
+ if ( added ) {
506
+ addedMsg = added . map ( ( user ) => this . __getMessage ( user , this . i18n . joined || '{user} joined' ) ) ;
532
507
}
533
508
534
509
if ( removed ) {
@@ -543,15 +518,15 @@ class AvatarGroup extends ResizeMixin(OverlayClassMixin(ElementMixin(ThemableMix
543
518
544
519
/** @private */
545
520
__i18nItemsChanged ( i18n , items ) {
546
- const { base } = i18n ;
547
- if ( base && base . activeUsers ) {
548
- const field = items === 1 ? 'one' : 'many' ;
549
- if ( base . activeUsers [ field ] ) {
550
- this . setAttribute ( 'aria-label' , base . activeUsers [ field ] . replace ( '{count}' , items || 0 ) ) ;
521
+ if ( i18n && i18n . activeUsers ) {
522
+ const count = Array . isArray ( items ) ? items . length : 0 ;
523
+ const field = count === 1 ? 'one' : 'many' ;
524
+ if ( i18n . activeUsers [ field ] ) {
525
+ this . setAttribute ( 'aria-label' , i18n . activeUsers [ field ] . replace ( '{count}' , count || 0 ) ) ;
551
526
}
552
527
553
528
this . _avatars . forEach ( ( avatar ) => {
554
- avatar . i18n = base ;
529
+ avatar . i18n = i18n ;
555
530
} ) ;
556
531
}
557
532
}
0 commit comments