1
- // Copyright (c) QuantStack
2
- // Distributed under the terms of the Modified BSD License.
3
1
import { DOMWidgetModel , ISerializers } from '@jupyter-widgets/base' ;
4
- import TileLayer from 'ol/layer/WebGLTile.js' ;
2
+ import WebGLTileLayer from 'ol/layer/WebGLTile.js' ;
5
3
import XYZ from 'ol/source/XYZ.js' ;
6
4
import { MODULE_NAME , MODULE_VERSION } from './version' ;
7
5
import { MapView } from './widget' ;
8
6
import { LayerModel , LayerView } from './layer' ;
9
7
8
+ type WebGLEvent = {
9
+ context : WebGLRenderingContext ;
10
+ } ;
11
+
10
12
export class RasterTileLayerModel extends LayerModel {
11
13
defaults ( ) {
12
14
return {
@@ -28,7 +30,7 @@ export class RasterTileLayerModel extends LayerModel {
28
30
29
31
static serializers : ISerializers = {
30
32
...DOMWidgetModel . serializers ,
31
- // Add any extra serializers here
33
+ // Add any extra serializers ici
32
34
} ;
33
35
34
36
static model_name = 'RasterTileLayerModel' ;
@@ -41,15 +43,36 @@ export class RasterTileLayerModel extends LayerModel {
41
43
42
44
export class RasterTileLayerView extends LayerView {
43
45
map_view : MapView ;
46
+ tileLayer : WebGLTileLayer ;
47
+
48
+ private prerenderListener : ( event : WebGLEvent ) => void ;
49
+ private postrenderListener : ( event : WebGLEvent ) => void ;
50
+ private previousSwipePosition : number | undefined ;
51
+
52
+ constructor ( options : any ) {
53
+ super ( options ) ;
54
+ this . map_view = options . options . map_view ;
55
+ this . prerenderListener = this . map_view . handlePrerender . bind ( this . map_view ) ;
56
+ this . postrenderListener = this . map_view . handlePostrender . bind (
57
+ this . map_view ,
58
+ ) ;
59
+ this . previousSwipePosition = undefined ;
60
+ }
44
61
45
62
render ( ) {
46
63
super . render ( ) ;
47
64
this . urlChanged ( ) ;
48
65
this . model . on ( 'change:url' , this . urlChanged , this ) ;
66
+ this . model . on (
67
+ 'change:swipe_position' ,
68
+ this . handleSwipePositionChanged ,
69
+ this ,
70
+ ) ;
71
+ this . updateEventListeners ( ) ;
49
72
}
50
73
51
74
create_obj ( ) {
52
- this . obj = this . tileLayer = new TileLayer ( {
75
+ this . obj = this . tileLayer = new WebGLTileLayer ( {
53
76
source : new XYZ ( {
54
77
url : this . model . get ( 'url' ) ,
55
78
attributions : this . model . get ( 'attributions' ) ,
@@ -74,5 +97,27 @@ export class RasterTileLayerView extends LayerView {
74
97
}
75
98
}
76
99
77
- tileLayer : TileLayer ;
100
+ handleSwipePositionChanged ( ) {
101
+ const swipePosition = this . model . get ( 'swipe_position' ) ;
102
+ console . log ( 'Swipe Position Changed:' , swipePosition ) ;
103
+
104
+ if ( this . previousSwipePosition !== swipePosition ) {
105
+ this . previousSwipePosition = swipePosition ;
106
+ this . updateEventListeners ( ) ;
107
+ this . map_view . map . render ( ) ;
108
+ }
109
+ }
110
+
111
+ updateEventListeners ( ) {
112
+ console . log ( 'Updating event listeners' ) ;
113
+ const swipePosition = this . model . get ( 'swipe_position' ) ;
114
+ ( this . tileLayer as any ) . un ( 'precompose' , this . prerenderListener ) ;
115
+ ( this . tileLayer as any ) . un ( 'postcompose' , this . postrenderListener ) ;
116
+
117
+ if ( swipePosition >= 0 ) {
118
+ ( this . tileLayer as any ) . on ( 'precompose' , this . prerenderListener ) ;
119
+ ( this . tileLayer as any ) . on ( 'postcompose' , this . postrenderListener ) ;
120
+ }
121
+ console . log ( 'Event listeners updated' ) ;
122
+ }
78
123
}
0 commit comments