Skip to content

Commit

Permalink
UPDATE: Remove mainLizmap
Browse files Browse the repository at this point in the history
  • Loading branch information
neo-garaix authored and rldhont committed Dec 3, 2024
1 parent 308f68a commit 68a84e6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
76 changes: 45 additions & 31 deletions assets/src/modules/Action.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @license MPL-2.0
*/

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

/**
* Build the lizmap Action instance
* @param {Map} map - OpenLayers map
* @param {SelectionTool} selectionTool - The lizmap selection tool
* @param {Digitizing} digitizing - The Lizmap digitizing instance
* @param {object} lizmap3 - The old lizmap object
*/
constructor() {
constructor(map, selectionTool, digitizing, lizmap3) {

this._map = map;
this._selectionTool = selectionTool;
this._digitizing = digitizing;
this._lizmap3 = lizmap3;

this.hasActions = true;
if (typeof actionConfig === 'undefined') {
Expand Down Expand Up @@ -97,8 +106,9 @@ export default class Action {
});
}

const self = this;
// React on the main Lizmap events
mainLizmap.lizmap3.events.on({
this._lizmap3.events.on({

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

// Get layer lizmap config
let getLayerConfig = mainLizmap.lizmap3.getLayerConfigById(layerId);
let getLayerConfig = lizmap3.getLayerConfigById(layerId);
if (!getLayerConfig) {
return true;
}
Expand All @@ -128,24 +138,24 @@ export default class Action {
let action = actionConfig[i];

// Only add action in Popup for the scope "feature"
if (!('scope' in action) || action['scope'] != mainLizmap.action.Scopes.Feature) {
if (!('scope' in action) || action['scope'] != self.Scopes.Feature) {
continue;
}

// Only add action if the layer is in the list
if (action['layers'].includes(layerId)) {
mainLizmap.action.addPopupActionButton(action, layerId, featureId, popupContainerId);
self.addPopupActionButton(action, layerId, featureId, popupContainerId);
}
}

});
}
});
}
mainLizmap.lizmap3.events.on({
this._lizmap3.events.on({
minidockclosed: (event) => {
if (event.id === 'action'){
mainLizmap.digitizing.toolSelected = 'deactivate';
this._digitizing.toolSelected = 'deactivate';
}
}
});
Expand Down Expand Up @@ -179,7 +189,7 @@ export default class Action {
});

// Add the layer inside Lizmap objects
mainLizmap.map.addToolLayer(this.actionLayer);
this._map.addToolLayer(this.actionLayer);
}

/**
Expand Down Expand Up @@ -274,22 +284,23 @@ export default class Action {

if (callback['method'] == this.CallbackMethods.Zoom && features.length) {
// Zoom to the returned features
mainLizmap.extent = this.actionLayer.getSource().getExtent();
const bounds = this.actionLayer.getSource().getExtent();
this._map.getView().fit(bounds, {nearest: true});
}

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

let getLayerConfig = mainLizmap.lizmap3.getLayerConfigById(callback['layerId']);
let getLayerConfig = this._lizmap3.getLayerConfigById(callback['layerId']);
if (!getLayerConfig) {
continue;
}
let featureType = getLayerConfig[0];
let layerConfig = getLayerConfig[1];

// Get the corresponding OpenLayers layer instance
const layer = lizMap.mainLizmap.map.getLayerByName(layerConfig.name);
const layer = this._map.getLayerByName(layerConfig.name);

if(!layer){
continue;
Expand All @@ -306,7 +317,7 @@ export default class Action {
// Select features in the given layer
let feat = features[0];
let f = feat.clone();
mainLizmap.selectionTool.selectLayerFeaturesFromSelectionFeature(featureType, f);
this._selectionTool.selectLayerFeaturesFromSelectionFeature(featureType, f);
}
}
}
Expand Down Expand Up @@ -384,7 +395,7 @@ export default class Action {

const WKTformat = new WKT();
const projOptions = {
featureProjection: mainLizmap.projection,
featureProjection: this._lizmap3.map.getProjection(),
dataProjection: 'EPSG:4326'
};

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

// Take drawn geometry if any and if none exists as a parameter
if (!wkt && mainLizmap.digitizing.context === "action" && mainLizmap.digitizing.featureDrawn) {
wkt = WKTformat.writeFeatures(mainLizmap.digitizing.featureDrawn, projOptions);
if (!wkt && this._digitizing.context === "action" && this._digitizing.featureDrawn) {
wkt = WKTformat.writeFeatures(this._digitizing.featureDrawn, projOptions);
}

// Set the request parameters
Expand All @@ -406,10 +417,13 @@ export default class Action {
"wkt": wkt
};

const viewExtent = this._map.getView().calculateExtent();
const viewCenter = this._map.getView().getCenter();

// We add the map extent and center
// as WKT geometries
options['mapExtent'] = WKTformat.writeGeometry(fromExtent(mainLizmap.extent), projOptions);
options['mapCenter'] = WKTformat.writeGeometry(new Point(mainLizmap.center), projOptions);
options['mapExtent'] = WKTformat.writeGeometry(fromExtent(viewExtent), projOptions);
options['mapCenter'] = WKTformat.writeGeometry(new Point(viewCenter), projOptions);

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

// Display the errors
mainLizmap.lizmap3.addMessage(data.errors.title + '\n' + data.errors.detail, 'error', true).attr('id', 'lizmap-action-message');
this._lizmap3.addMessage(data.errors.title + '\n' + data.errors.detail, 'error', true).attr('id', 'lizmap-action-message');
console.warn(data.errors);

return false;
Expand All @@ -454,7 +468,7 @@ export default class Action {
// Display the message if given
const message = featureProperties[message_field].trim();
if (message) {
mainLizmap.lizmap3.addMessage(message, 'info', true).attr('id', 'lizmap-action-message');
this._lizmap3.addMessage(message, 'info', true).attr('id', 'lizmap-action-message');
}

// Display the HTML message if given
Expand Down Expand Up @@ -554,7 +568,7 @@ export default class Action {

// Convert the action GeoJSON data into OpenLayers features
const features = (new GeoJSON()).readFeatures(data, {
featureProjection: mainLizmap.projection
featureProjection: this._lizmap3.map.getProjection()
});

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

// Get the layerId, featureId and action for this button
let val = button.value;
let [actionName, layerId, featureId] = mainLizmap.action.explodeActionInstanceUniqueId(val);
let [actionName, layerId, featureId] = this.explodeActionInstanceUniqueId(val);

// Get the action item data
let popupAction = mainLizmap.action.getActionItemByName(actionName, mainLizmap.action.Scopes.Feature, layerId);
let popupAction = this.getActionItemByName(actionName, this.Scopes.Feature, layerId);
if (!popupAction) {
console.warn('No corresponding action found in the configuration !');

Expand All @@ -593,11 +607,11 @@ export default class Action {
// We allow only one active action at a time.
// If the action is already active for the clicked button
// we need to deactivate it completely
if (mainLizmap.action.ACTIVE_LIZMAP_ACTION) {
let actionUniqueId = mainLizmap.action.buildActionInstanceUniqueId(actionName, mainLizmap.action.Scopes.Feature, layerId, featureId);
if (mainLizmap.action.ACTIVE_LIZMAP_ACTION == actionUniqueId) {
if (this.ACTIVE_LIZMAP_ACTION) {
let actionUniqueId = this.buildActionInstanceUniqueId(actionName, this.Scopes.Feature, layerId, featureId);
if (this.ACTIVE_LIZMAP_ACTION == actionUniqueId) {
// Reset the action
mainLizmap.action.resetLizmapAction(true, true, true, true);
this.resetLizmapAction(true, true, true, true);

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

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

// Reset
mainLizmap.action.resetLizmapAction(true, true, true, true);
this.resetLizmapAction(true, true, true, true);

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

// Run the Lizmap action for this feature
// It will set the global variable ACTIVE_LIZMAP_ACTION
mainLizmap.action.runLizmapAction(actionName, mainLizmap.action.Scopes.Feature, layerId, featureId);
this.runLizmapAction(actionName, this.Scopes.Feature, layerId, featureId);

return false;
}
Expand Down Expand Up @@ -688,7 +702,7 @@ export default class Action {
}

// Trigger the action when clicking on button
actionButton.addEventListener('click', mainLizmap.action.popupActionButtonClickHandler);
actionButton.addEventListener('click', this.popupActionButtonClickHandler.bind(this));
}

};
2 changes: 1 addition & 1 deletion assets/src/modules/Lizmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export default class Lizmap {
this.proxyEvents = new ProxyEvents();
this.wfs = new WFS();
this.wms = new WMS();
this.action = new Action();
this.action = new Action(this.map, this.selectionTool, this.digitizing, this.lizmap3);
this.featureStorage = new FeatureStorage();
this.popup = new Popup(this.initialConfig, this.state, this.map, this.digitizing);
this.legend = new Legend(this.state.layerTree);
Expand Down

0 comments on commit 68a84e6

Please sign in to comment.