Skip to content

Commit 644fd96

Browse files
committed
UPDATE: Remove mainLizmap
1 parent 3962b38 commit 644fd96

File tree

2 files changed

+46
-32
lines changed

2 files changed

+46
-32
lines changed

assets/src/modules/Action.js

+45-31
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @license MPL-2.0
77
*/
88

9-
import { mainLizmap } from '../modules/Globals.js';
9+
import SelectionTool from './SelectionTool.js';
1010
import { Vector as VectorSource } from 'ol/source.js';
1111
import { Vector as VectorLayer } from 'ol/layer.js';
1212
import GeoJSON from 'ol/format/GeoJSON.js';
@@ -56,8 +56,17 @@ export default class Action {
5656

5757
/**
5858
* 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
5963
*/
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;
6170

6271
this.hasActions = true;
6372
if (typeof actionConfig === 'undefined') {
@@ -97,8 +106,9 @@ export default class Action {
97106
});
98107
}
99108

109+
const self = this;
100110
// React on the main Lizmap events
101-
mainLizmap.lizmap3.events.on({
111+
this._lizmap3.events.on({
102112

103113
// The popup has been displayed
104114
// We need to add the buttons for the action with a 'feature' scope
@@ -117,7 +127,7 @@ export default class Action {
117127
const layerId = element.dataset.layerId;
118128

119129
// Get layer lizmap config
120-
let getLayerConfig = mainLizmap.lizmap3.getLayerConfigById(layerId);
130+
let getLayerConfig = lizmap3.getLayerConfigById(layerId);
121131
if (!getLayerConfig) {
122132
return true;
123133
}
@@ -128,24 +138,24 @@ export default class Action {
128138
let action = actionConfig[i];
129139

130140
// 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) {
132142
continue;
133143
}
134144

135145
// Only add action if the layer is in the list
136146
if (action['layers'].includes(layerId)) {
137-
mainLizmap.action.addPopupActionButton(action, layerId, featureId, popupContainerId);
147+
self.addPopupActionButton(action, layerId, featureId, popupContainerId);
138148
}
139149
}
140150

141151
});
142152
}
143153
});
144154
}
145-
mainLizmap.lizmap3.events.on({
155+
this._lizmap3.events.on({
146156
minidockclosed: (event) => {
147157
if (event.id === 'action'){
148-
mainLizmap.digitizing.toolSelected = 'deactivate';
158+
this._digitizing.toolSelected = 'deactivate';
149159
}
150160
}
151161
});
@@ -179,7 +189,7 @@ export default class Action {
179189
});
180190

181191
// Add the layer inside Lizmap objects
182-
mainLizmap.map.addToolLayer(this.actionLayer);
192+
this._map.addToolLayer(this.actionLayer);
183193
}
184194

185195
/**
@@ -274,22 +284,23 @@ export default class Action {
274284

275285
if (callback['method'] == this.CallbackMethods.Zoom && features.length) {
276286
// 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});
278289
}
279290

280291
// Check the given layerId is a valid Lizmap layer
281292
// Only for the methods which gives a layerId in their configuration
282293
if (callback['method'] == this.CallbackMethods.Redraw || callback['method'] == this.CallbackMethods.Select) {
283294

284-
let getLayerConfig = mainLizmap.lizmap3.getLayerConfigById(callback['layerId']);
295+
let getLayerConfig = this._lizmap3.getLayerConfigById(callback['layerId']);
285296
if (!getLayerConfig) {
286297
continue;
287298
}
288299
let featureType = getLayerConfig[0];
289300
let layerConfig = getLayerConfig[1];
290301

291302
// Get the corresponding OpenLayers layer instance
292-
const layer = lizMap.mainLizmap.map.getLayerByName(layerConfig.name);
303+
const layer = this._map.getLayerByName(layerConfig.name);
293304

294305
if(!layer){
295306
continue;
@@ -306,7 +317,7 @@ export default class Action {
306317
// Select features in the given layer
307318
let feat = features[0];
308319
let f = feat.clone();
309-
mainLizmap.selectionTool.selectLayerFeaturesFromSelectionFeature(featureType, f);
320+
this._selectionTool.selectLayerFeaturesFromSelectionFeature(featureType, f);
310321
}
311322
}
312323
}
@@ -384,7 +395,7 @@ export default class Action {
384395

385396
const WKTformat = new WKT();
386397
const projOptions = {
387-
featureProjection: mainLizmap.projection,
398+
featureProjection: this._lizmap3.map.getProjection(),
388399
dataProjection: 'EPSG:4326'
389400
};
390401

@@ -394,8 +405,8 @@ export default class Action {
394405
this.resetLizmapAction(true, true, true, false);
395406

396407
// 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);
399410
}
400411

401412
// Set the request parameters
@@ -406,10 +417,13 @@ export default class Action {
406417
"wkt": wkt
407418
};
408419

420+
const viewExtent = this._map.getView().calculateExtent();
421+
const viewCenter = this._map.getView().getCenter();
422+
409423
// We add the map extent and center
410424
// 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);
413427

414428
// Request action and get data
415429
let url = actionConfigData.url;
@@ -431,7 +445,7 @@ export default class Action {
431445
this.resetLizmapAction(true, true, true, true);
432446

433447
// 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');
435449
console.warn(data.errors);
436450

437451
return false;
@@ -454,7 +468,7 @@ export default class Action {
454468
// Display the message if given
455469
const message = featureProperties[message_field].trim();
456470
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');
458472
}
459473

460474
// Display the HTML message if given
@@ -554,7 +568,7 @@ export default class Action {
554568

555569
// Convert the action GeoJSON data into OpenLayers features
556570
const features = (new GeoJSON()).readFeatures(data, {
557-
featureProjection: mainLizmap.projection
571+
featureProjection: this._lizmap3.map.getProjection()
558572
});
559573

560574
// Add them to the action layer
@@ -580,10 +594,10 @@ export default class Action {
580594

581595
// Get the layerId, featureId and action for this button
582596
let val = button.value;
583-
let [actionName, layerId, featureId] = mainLizmap.action.explodeActionInstanceUniqueId(val);
597+
let [actionName, layerId, featureId] = this.explodeActionInstanceUniqueId(val);
584598

585599
// 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);
587601
if (!popupAction) {
588602
console.warn('No corresponding action found in the configuration !');
589603

@@ -593,11 +607,11 @@ export default class Action {
593607
// We allow only one active action at a time.
594608
// If the action is already active for the clicked button
595609
// 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) {
599613
// Reset the action
600-
mainLizmap.action.resetLizmapAction(true, true, true, true);
614+
this.resetLizmapAction(true, true, true, true);
601615

602616
// Return
603617
return true;
@@ -607,7 +621,7 @@ export default class Action {
607621
// The action was not active, we can run it
608622
// This will override the previous actions and replace them
609623
// with this one
610-
mainLizmap.action.ACTIVE_LIZMAP_ACTION = null;
624+
this.ACTIVE_LIZMAP_ACTION = null;
611625

612626
// Display a confirm question if needed
613627
if ('confirm' in popupAction && popupAction.confirm.trim() != '') {
@@ -619,14 +633,14 @@ export default class Action {
619633
}
620634

621635
// Reset
622-
mainLizmap.action.resetLizmapAction(true, true, true, true);
636+
this.resetLizmapAction(true, true, true, true);
623637

624638
// Add the button btn-primary class
625639
button.classList.add('btn-primary');
626640

627641
// Run the Lizmap action for this feature
628642
// 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);
630644

631645
return false;
632646
}
@@ -688,7 +702,7 @@ export default class Action {
688702
}
689703

690704
// Trigger the action when clicking on button
691-
actionButton.addEventListener('click', mainLizmap.action.popupActionButtonClickHandler);
705+
actionButton.addEventListener('click', this.popupActionButtonClickHandler.bind(this));
692706
}
693707

694708
};

assets/src/modules/Lizmap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export default class Lizmap {
161161
this.proxyEvents = new ProxyEvents();
162162
this.wfs = new WFS();
163163
this.wms = new WMS();
164-
this.action = new Action();
164+
this.action = new Action(this.map, this.selectionTool, this.digitizing, this.lizmap3);
165165
this.featureStorage = new FeatureStorage();
166166
this.popup = new Popup(this.initialConfig, this.state, this.map, this.digitizing);
167167
this.legend = new Legend(this.state.layerTree);

0 commit comments

Comments
 (0)