1
- // Copyright (c) QuantStack
2
- // Distributed under the terms of the Modified BSD License.
3
1
import Control from 'ol/control/Control' ;
4
2
import { getRenderPixel } from 'ol/render' ;
5
3
import 'ol/ol.css' ;
@@ -10,19 +8,16 @@ import { MapView } from './widget';
10
8
interface SplitMapControlOptions {
11
9
target ?: string ;
12
10
map_view ?: MapView ;
13
- swipe_position ?: number ; // Ajoutez swipe_position en tant que nombre
11
+ swipe_position ?: number ;
14
12
}
13
+
15
14
export default class SplitMapControl extends Control {
16
15
swipe : HTMLInputElement ;
17
16
leftLayer : any ;
18
- rightLayer : any ;
19
17
map_view : MapView ;
20
- swipe_position : any ;
21
- constructor (
22
- leftLayer : any ,
23
- rightLayer : any ,
24
- options : SplitMapControlOptions = { } ,
25
- ) {
18
+ private _swipe_position : number ;
19
+
20
+ constructor ( leftLayer : any , options : SplitMapControlOptions = { } ) {
26
21
const element = document . createElement ( 'div' ) ;
27
22
element . className = 'ol-unselectable ol-control split-map-control' ;
28
23
@@ -32,98 +27,63 @@ export default class SplitMapControl extends Control {
32
27
} ) ;
33
28
34
29
this . leftLayer = leftLayer ;
35
- this . rightLayer = rightLayer ;
36
30
37
- console . log ( 'Initializing SplitMapControl...' ) ;
38
- console . log ( options ) ;
39
31
if ( options . map_view ) {
40
32
this . map_view = options . map_view ;
41
- console . log ( 'MapView initialized:' , this . map_view ) ;
42
33
} else {
43
34
throw new Error ( 'MapView is required for SplitMapControl.' ) ;
44
35
}
45
- if ( options . swipe_position ) {
46
- this . swipe_position = options . swipe_position ;
47
- console . log ( 'swipe_position initialized:' , this . swipe_position ) ;
48
- } else {
49
- throw new Error ( 'swipe_position is required for SplitMapControl.' ) ;
50
- }
36
+
37
+ this . _swipe_position = options . swipe_position ?? 0 ;
51
38
52
39
const swiperContainer = document . createElement ( 'div' ) ;
53
40
swiperContainer . className = 'swiper-container' ;
41
+ swiperContainer . style . width = '100%' ;
54
42
55
43
this . swipe = document . createElement ( 'input' ) ;
56
44
this . swipe . type = 'range' ;
57
45
this . swipe . className = 'swipe' ;
58
- ( this . swipe . value = this . swipe_position ) ,
59
- swiperContainer . appendChild ( this . swipe ) ;
60
- console . log ( 'this.map_view.map_container' , this . map_view . map_container ) ;
61
- this . map_view . map_container . appendChild ( swiperContainer ) ;
62
- this . handleSwipe ( ) ;
63
- }
64
-
65
- handleSwipe ( event ?: Event ) {
66
- console . log ( 'Handling swipe event...' ) ;
67
- console . log ( this . swipe . value ) ;
68
- if ( ! this . leftLayer . hasListener ( 'prerender' ) ) {
69
- console . log ( '1' ) ;
70
- this . leftLayer . on ( 'prerender' , this . prerender . bind ( this ) ) ;
71
- }
72
-
73
- if ( ! this . leftLayer . hasListener ( 'postrender' ) ) {
74
- console . log ( '2' ) ;
75
- //this.leftLayer.on('postrender', this.postrender.bind(this));
76
- }
77
-
78
- if ( ! this . rightLayer . hasListener ( 'prerender' ) ) {
79
- console . log ( '3' ) ;
46
+ this . swipe . style . width = '100%' ;
47
+ this . updateSwipeValue ( ) ;
48
+ swiperContainer . appendChild ( this . swipe ) ;
80
49
81
- //this.rightLayer.on('prerender', this.prerender.bind(this));
82
- }
83
-
84
- if ( ! this . rightLayer . hasListener ( 'postrender' ) ) {
85
- console . log ( '4' ) ;
86
-
87
- this . rightLayer . on ( 'postrender' , this . postrender . bind ( this ) ) ;
88
- }
50
+ this . map_view . map_container . style . position = 'relative' ;
51
+ this . map_view . map_container . appendChild ( swiperContainer ) ;
89
52
90
- console . log ( 'ok' ) ;
91
- }
53
+ const map_view = this . map_view ;
92
54
93
- prerender ( event : any ) {
94
- console . log ( 'Prerender event triggered.' ) ;
55
+ this . leftLayer . on ( 'prerender' , ( event : any ) => {
56
+ const gl = event . context ;
57
+ gl . enable ( gl . SCISSOR_TEST ) ;
95
58
96
- const gl = event . context ; // Get WebGL context
97
- gl . enable ( gl . SCISSOR_TEST ) ; // Enable scissor test
59
+ const mapSize = map_view . getSize ( ) ;
98
60
99
- const mapSize = this . map_view . getSize ( ) ; // Get the size of the map
100
- if ( ! mapSize ) {
101
- console . warn ( 'Map size is undefined.' ) ;
102
- return ;
103
- }
61
+ if ( mapSize ) {
62
+ const bottomLeft = getRenderPixel ( event , [ 0 , mapSize [ 1 ] ] ) ;
63
+ const topRight = getRenderPixel ( event , [ mapSize [ 0 ] , 0 ] ) ;
104
64
105
- // Get render pixels for the bottom left and top right corners
106
- const bottomLeft = getRenderPixel ( event , [ 0 , mapSize [ 1 ] ] ) ;
107
- const topRight = getRenderPixel ( event , [ mapSize [ 0 ] , 0 ] ) ;
65
+ const width = Math . round ( ( topRight [ 0 ] - bottomLeft [ 0 ] ) * ( this . _swipe_position / 100 ) ) ;
66
+ const height = topRight [ 1 ] - bottomLeft [ 1 ] ;
108
67
109
- // Get the swipe value from the input element
110
- const swipeValue = this . swipe . value ;
111
- console . log ( 'swipeValue' , swipeValue ) ;
68
+ gl . scissor ( bottomLeft [ 0 ] , bottomLeft [ 1 ] , width , height ) ;
69
+ }
70
+ } ) ;
112
71
113
- // Calculate the width and height for the scissor test
114
- const width = Math . round (
115
- ( topRight [ 0 ] - bottomLeft [ 0 ] ) * ( Number ( swipeValue ) / 100 ) ,
116
- ) ;
117
- const height = topRight [ 1 ] - bottomLeft [ 1 ] ;
72
+ this . leftLayer . on ( 'postrender' , ( event : any ) => {
73
+ const gl = event . context ;
74
+ gl . disable ( gl . SCISSOR_TEST ) ;
75
+ } ) ;
118
76
119
- // Define the scissor box
120
- gl . scissor ( bottomLeft [ 0 ] , bottomLeft [ 1 ] , width , height ) ;
77
+ this . swipe . addEventListener ( 'input' , ( ) => {
78
+ this . _swipe_position = parseInt ( this . swipe . value , 10 ) ;
79
+ this . updateSwipeValue ( ) ;
80
+ map_view . map . render ( ) ;
81
+ } ) ;
121
82
}
122
83
123
- postrender ( event : any ) {
124
- console . log ( 'Postrender event triggered.' ) ;
125
-
126
- const gl = event . context ; // Get WebGL context
127
- gl . disable ( gl . SCISSOR_TEST ) ; // Disable scissor test
84
+ private updateSwipeValue ( ) {
85
+ if ( this . swipe ) {
86
+ this . swipe . value = this . _swipe_position . toString ( ) ;
87
+ }
128
88
}
129
89
}
0 commit comments