6
6
* @license MPL-2.0
7
7
*/
8
8
9
- import { mainLizmap } from '../modules/Globals .js' ;
9
+ import SelectionTool from './SelectionTool .js' ;
10
10
import { Vector as VectorSource } from 'ol/source.js' ;
11
11
import { Vector as VectorLayer } from 'ol/layer.js' ;
12
12
import GeoJSON from 'ol/format/GeoJSON.js' ;
@@ -56,8 +56,17 @@ export default class Action {
56
56
57
57
/**
58
58
* Build the lizmap Action instance
59
+ * @param {Map } map - OpenLayers map
60
+ * @param {SelectionTool } selectionTool - The lizmap selection tool
61
+ * @param {Digitizing } digitizing - The Lizmap digitizing instance
62
+ * @param {object } lizmap3 - The old lizmap object
59
63
*/
60
- constructor ( ) {
64
+ constructor ( map , selectionTool , digitizing , lizmap3 ) {
65
+
66
+ this . _map = map ;
67
+ this . _selectionTool = selectionTool ;
68
+ this . _digitizing = digitizing ;
69
+ this . _lizmap3 = lizmap3 ;
61
70
62
71
this . hasActions = true ;
63
72
if ( typeof actionConfig === 'undefined' ) {
@@ -97,8 +106,9 @@ export default class Action {
97
106
} ) ;
98
107
}
99
108
109
+ const self = this ;
100
110
// React on the main Lizmap events
101
- mainLizmap . lizmap3 . events . on ( {
111
+ this . _lizmap3 . events . on ( {
102
112
103
113
// The popup has been displayed
104
114
// We need to add the buttons for the action with a 'feature' scope
@@ -117,7 +127,7 @@ export default class Action {
117
127
const layerId = element . dataset . layerId ;
118
128
119
129
// Get layer lizmap config
120
- let getLayerConfig = mainLizmap . lizmap3 . getLayerConfigById ( layerId ) ;
130
+ let getLayerConfig = lizmap3 . getLayerConfigById ( layerId ) ;
121
131
if ( ! getLayerConfig ) {
122
132
return true ;
123
133
}
@@ -128,24 +138,24 @@ export default class Action {
128
138
let action = actionConfig [ i ] ;
129
139
130
140
// Only add action in Popup for the scope "feature"
131
- if ( ! ( 'scope' in action ) || action [ 'scope' ] != mainLizmap . action . Scopes . Feature ) {
141
+ if ( ! ( 'scope' in action ) || action [ 'scope' ] != self . Scopes . Feature ) {
132
142
continue ;
133
143
}
134
144
135
145
// Only add action if the layer is in the list
136
146
if ( action [ 'layers' ] . includes ( layerId ) ) {
137
- mainLizmap . action . addPopupActionButton ( action , layerId , featureId , popupContainerId ) ;
147
+ self . addPopupActionButton ( action , layerId , featureId , popupContainerId ) ;
138
148
}
139
149
}
140
150
141
151
} ) ;
142
152
}
143
153
} ) ;
144
154
}
145
- mainLizmap . lizmap3 . events . on ( {
155
+ this . _lizmap3 . events . on ( {
146
156
minidockclosed : ( event ) => {
147
157
if ( event . id === 'action' ) {
148
- mainLizmap . digitizing . toolSelected = 'deactivate' ;
158
+ this . _digitizing . toolSelected = 'deactivate' ;
149
159
}
150
160
}
151
161
} ) ;
@@ -179,7 +189,7 @@ export default class Action {
179
189
} ) ;
180
190
181
191
// Add the layer inside Lizmap objects
182
- mainLizmap . map . addToolLayer ( this . actionLayer ) ;
192
+ this . _map . addToolLayer ( this . actionLayer ) ;
183
193
}
184
194
185
195
/**
@@ -274,22 +284,23 @@ export default class Action {
274
284
275
285
if ( callback [ 'method' ] == this . CallbackMethods . Zoom && features . length ) {
276
286
// Zoom to the returned features
277
- mainLizmap . extent = this . actionLayer . getSource ( ) . getExtent ( ) ;
287
+ const bounds = this . actionLayer . getSource ( ) . getExtent ( ) ;
288
+ this . _map . getView ( ) . fit ( bounds , { nearest : true } ) ;
278
289
}
279
290
280
291
// Check the given layerId is a valid Lizmap layer
281
292
// Only for the methods which gives a layerId in their configuration
282
293
if ( callback [ 'method' ] == this . CallbackMethods . Redraw || callback [ 'method' ] == this . CallbackMethods . Select ) {
283
294
284
- let getLayerConfig = mainLizmap . lizmap3 . getLayerConfigById ( callback [ 'layerId' ] ) ;
295
+ let getLayerConfig = this . _lizmap3 . getLayerConfigById ( callback [ 'layerId' ] ) ;
285
296
if ( ! getLayerConfig ) {
286
297
continue ;
287
298
}
288
299
let featureType = getLayerConfig [ 0 ] ;
289
300
let layerConfig = getLayerConfig [ 1 ] ;
290
301
291
302
// Get the corresponding OpenLayers layer instance
292
- const layer = lizMap . mainLizmap . map . getLayerByName ( layerConfig . name ) ;
303
+ const layer = this . _map . getLayerByName ( layerConfig . name ) ;
293
304
294
305
if ( ! layer ) {
295
306
continue ;
@@ -306,7 +317,7 @@ export default class Action {
306
317
// Select features in the given layer
307
318
let feat = features [ 0 ] ;
308
319
let f = feat . clone ( ) ;
309
- mainLizmap . selectionTool . selectLayerFeaturesFromSelectionFeature ( featureType , f ) ;
320
+ this . _selectionTool . selectLayerFeaturesFromSelectionFeature ( featureType , f ) ;
310
321
}
311
322
}
312
323
}
@@ -384,7 +395,7 @@ export default class Action {
384
395
385
396
const WKTformat = new WKT ( ) ;
386
397
const projOptions = {
387
- featureProjection : mainLizmap . projection ,
398
+ featureProjection : this . _lizmap3 . map . getProjection ( ) ,
388
399
dataProjection : 'EPSG:4326'
389
400
} ;
390
401
@@ -394,8 +405,8 @@ export default class Action {
394
405
this . resetLizmapAction ( true , true , true , false ) ;
395
406
396
407
// Take drawn geometry if any and if none exists as a parameter
397
- if ( ! wkt && mainLizmap . digitizing . context === "action" && mainLizmap . digitizing . featureDrawn ) {
398
- wkt = WKTformat . writeFeatures ( mainLizmap . digitizing . featureDrawn , projOptions ) ;
408
+ if ( ! wkt && this . _digitizing . context === "action" && this . _digitizing . featureDrawn ) {
409
+ wkt = WKTformat . writeFeatures ( this . _digitizing . featureDrawn , projOptions ) ;
399
410
}
400
411
401
412
// Set the request parameters
@@ -406,10 +417,13 @@ export default class Action {
406
417
"wkt" : wkt
407
418
} ;
408
419
420
+ const viewExtent = this . _map . getView ( ) . calculateExtent ( ) ;
421
+ const viewCenter = this . _map . getView ( ) . getCenter ( ) ;
422
+
409
423
// We add the map extent and center
410
424
// as WKT geometries
411
- options [ 'mapExtent' ] = WKTformat . writeGeometry ( fromExtent ( mainLizmap . extent ) , projOptions ) ;
412
- options [ 'mapCenter' ] = WKTformat . writeGeometry ( new Point ( mainLizmap . center ) , projOptions ) ;
425
+ options [ 'mapExtent' ] = WKTformat . writeGeometry ( fromExtent ( viewExtent ) , projOptions ) ;
426
+ options [ 'mapCenter' ] = WKTformat . writeGeometry ( new Point ( viewCenter ) , projOptions ) ;
413
427
414
428
// Request action and get data
415
429
let url = actionConfigData . url ;
@@ -431,7 +445,7 @@ export default class Action {
431
445
this . resetLizmapAction ( true , true , true , true ) ;
432
446
433
447
// Display the errors
434
- mainLizmap . lizmap3 . addMessage ( data . errors . title + '\n' + data . errors . detail , 'danger' , true ) . attr ( 'id' , 'lizmap-action-message' ) ;
448
+ this . _lizmap3 . addMessage ( data . errors . title + '\n' + data . errors . detail , 'danger' , true ) . attr ( 'id' , 'lizmap-action-message' ) ;
435
449
console . warn ( data . errors ) ;
436
450
437
451
return false ;
@@ -454,7 +468,7 @@ export default class Action {
454
468
// Display the message if given
455
469
const message = featureProperties [ message_field ] . trim ( ) ;
456
470
if ( message ) {
457
- mainLizmap . lizmap3 . addMessage ( message , 'info' , true ) . attr ( 'id' , 'lizmap-action-message' ) ;
471
+ this . _lizmap3 . addMessage ( message , 'info' , true ) . attr ( 'id' , 'lizmap-action-message' ) ;
458
472
}
459
473
460
474
// Display the HTML message if given
@@ -554,7 +568,7 @@ export default class Action {
554
568
555
569
// Convert the action GeoJSON data into OpenLayers features
556
570
const features = ( new GeoJSON ( ) ) . readFeatures ( data , {
557
- featureProjection : mainLizmap . projection
571
+ featureProjection : this . _lizmap3 . map . getProjection ( )
558
572
} ) ;
559
573
560
574
// Add them to the action layer
@@ -580,10 +594,10 @@ export default class Action {
580
594
581
595
// Get the layerId, featureId and action for this button
582
596
let val = button . value ;
583
- let [ actionName , layerId , featureId ] = mainLizmap . action . explodeActionInstanceUniqueId ( val ) ;
597
+ let [ actionName , layerId , featureId ] = this . explodeActionInstanceUniqueId ( val ) ;
584
598
585
599
// Get the action item data
586
- let popupAction = mainLizmap . action . getActionItemByName ( actionName , mainLizmap . action . Scopes . Feature , layerId ) ;
600
+ let popupAction = this . getActionItemByName ( actionName , this . Scopes . Feature , layerId ) ;
587
601
if ( ! popupAction ) {
588
602
console . warn ( 'No corresponding action found in the configuration !' ) ;
589
603
@@ -593,11 +607,11 @@ export default class Action {
593
607
// We allow only one active action at a time.
594
608
// If the action is already active for the clicked button
595
609
// we need to deactivate it completely
596
- if ( mainLizmap . action . ACTIVE_LIZMAP_ACTION ) {
597
- let actionUniqueId = mainLizmap . action . buildActionInstanceUniqueId ( actionName , mainLizmap . action . Scopes . Feature , layerId , featureId ) ;
598
- if ( mainLizmap . action . ACTIVE_LIZMAP_ACTION == actionUniqueId ) {
610
+ if ( this . ACTIVE_LIZMAP_ACTION ) {
611
+ let actionUniqueId = this . buildActionInstanceUniqueId ( actionName , this . Scopes . Feature , layerId , featureId ) ;
612
+ if ( this . ACTIVE_LIZMAP_ACTION == actionUniqueId ) {
599
613
// Reset the action
600
- mainLizmap . action . resetLizmapAction ( true , true , true , true ) ;
614
+ this . resetLizmapAction ( true , true , true , true ) ;
601
615
602
616
// Return
603
617
return true ;
@@ -607,7 +621,7 @@ export default class Action {
607
621
// The action was not active, we can run it
608
622
// This will override the previous actions and replace them
609
623
// with this one
610
- mainLizmap . action . ACTIVE_LIZMAP_ACTION = null ;
624
+ this . ACTIVE_LIZMAP_ACTION = null ;
611
625
612
626
// Display a confirm question if needed
613
627
if ( 'confirm' in popupAction && popupAction . confirm . trim ( ) != '' ) {
@@ -619,14 +633,14 @@ export default class Action {
619
633
}
620
634
621
635
// Reset
622
- mainLizmap . action . resetLizmapAction ( true , true , true , true ) ;
636
+ this . resetLizmapAction ( true , true , true , true ) ;
623
637
624
638
// Add the button btn-primary class
625
639
button . classList . add ( 'btn-primary' ) ;
626
640
627
641
// Run the Lizmap action for this feature
628
642
// It will set the global variable ACTIVE_LIZMAP_ACTION
629
- mainLizmap . action . runLizmapAction ( actionName , mainLizmap . action . Scopes . Feature , layerId , featureId ) ;
643
+ this . runLizmapAction ( actionName , this . Scopes . Feature , layerId , featureId ) ;
630
644
631
645
return false ;
632
646
}
@@ -688,7 +702,7 @@ export default class Action {
688
702
}
689
703
690
704
// Trigger the action when clicking on button
691
- actionButton . addEventListener ( 'click' , mainLizmap . action . popupActionButtonClickHandler ) ;
705
+ actionButton . addEventListener ( 'click' , this . popupActionButtonClickHandler . bind ( this ) ) ;
692
706
}
693
707
694
708
} ;
0 commit comments