@@ -7,9 +7,9 @@ import {ESLCarouselPlugin} from '../esl-carousel.plugin';
7
7
import { ESLCarouselSlideEvent } from '../../core/esl-carousel.events' ;
8
8
9
9
export interface ESLCarouselRelateToConfig {
10
- /** Target carousel selector */
10
+ /** Target carousel selector. Set to '' or 'none' to disable */
11
11
target : string ;
12
- /** Proactive mode to relate to the target immediately */
12
+ /** A proactive mode to relate to the target immediately */
13
13
proactive : boolean ;
14
14
}
15
15
@@ -19,37 +19,52 @@ export interface ESLCarouselRelateToConfig {
19
19
@ExportNs ( 'Carousel.RelateTo' )
20
20
export class ESLCarouselRelateToMixin extends ESLCarouselPlugin < ESLCarouselRelateToConfig > {
21
21
public static override is = 'esl-carousel-relate-to' ;
22
+ public static override DEFAULT_CONFIG : ESLCarouselRelateToConfig = {
23
+ target : 'none' ,
24
+ proactive : false
25
+ } ;
22
26
public static override DEFAULT_CONFIG_KEY = 'target' ;
23
27
28
+ /** @returns events name to listen for carousel state changes */
24
29
protected get event ( ) : string {
25
30
return [ this . config . proactive ? ESLCarouselSlideEvent . CHANGE : null , ESLCarouselSlideEvent . AFTER ] . filter ( Boolean ) . join ( ' ' ) ;
26
31
}
27
32
28
33
/** @returns ESLCarousel target to share state changes */
29
34
@memoize ( )
30
35
public get $target ( ) : ESLCarousel | null {
31
- const $target = ESLTraversingQuery . first ( this . config . target ) ;
32
- return ( $target instanceof ESLCarousel ) ? $target : null ;
36
+ const { target} = this . config ;
37
+ if ( ! target || target === 'none' ) return null ;
38
+ const $target = ESLTraversingQuery . first ( target , this . $host ) ;
39
+ // Prevent cyclic reference - target should not be the host itself
40
+ if ( ! ( $target instanceof ESLCarousel ) || $target === this . $host ) return null ;
41
+ return $target ;
33
42
}
34
43
35
44
protected override onInit ( ) : void {
36
45
if ( ! this . $target ) return ;
37
- this . $target . goTo ( this . $host . activeIndex , { activator : this } ) . catch ( console . debug ) ;
46
+ // Sync host carousel to target's current position
47
+ this . $host . goTo ( this . $target . activeIndex , { activator : this } ) . catch ( console . debug ) ;
38
48
}
39
49
40
50
@listen ( { inherit : true } )
41
51
protected override onConfigChange ( ) : void {
42
52
// Listener event change is not handled by resubscribe automatically
43
53
this . $$off ( this . _onSlideChange ) ;
54
+ super . onConfigChange ( ) ;
44
55
memoize . clear ( this , '$target' ) ;
45
56
this . $$on ( this . _onSlideChange ) ;
46
57
}
47
58
48
- /** Handles event that fires when the carousel slides state is changed. */
49
- @listen ( { event : ( $this : ESLCarouselRelateToMixin ) => $this . event } )
59
+ /** Handles event that fires when the target carousel slides state is changed. */
60
+ @listen ( {
61
+ event : ( $this : ESLCarouselRelateToMixin ) => $this . event ,
62
+ target : ( $this : ESLCarouselRelateToMixin ) => $this . $target
63
+ } )
50
64
protected _onSlideChange ( e : ESLCarouselSlideEvent ) : void {
51
65
if ( ! this . $target || e . activator === this ) return ;
52
- this . $target . goTo ( e . indexAfter , { activator : this } ) . catch ( console . debug ) ;
66
+ // Make host carousel follow the target carousel
67
+ this . $host . goTo ( e . indexAfter , { activator : this } ) . catch ( console . debug ) ;
53
68
}
54
69
}
55
70
@@ -58,3 +73,4 @@ declare global {
58
73
RelateTo : typeof ESLCarouselRelateToMixin ;
59
74
}
60
75
}
76
+
0 commit comments