Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JavaScript] reduce mainLizmap dependencies #3857

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 32 additions & 23 deletions assets/src/modules/Action.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

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 +57,15 @@ export default class Action {

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

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

this.hasActions = true;
if (typeof actionConfig === 'undefined') {
Expand Down Expand Up @@ -97,8 +105,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 +126,7 @@ export default class Action {
let layerId = val.replace('.' + featureId, '');

// Get layer lizmap config
let getLayerConfig = mainLizmap.lizmap3.getLayerConfigById(layerId);
let getLayerConfig = lizmap3.getLayerConfigById(layerId);
if (!getLayerConfig) {
return true;
}
Expand All @@ -128,13 +137,13 @@ 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);
}
}

Expand Down Expand Up @@ -173,7 +182,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 @@ -275,15 +284,15 @@ export default class Action {
// 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 @@ -300,7 +309,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 @@ -393,7 +402,7 @@ export default class Action {
// as WKT geometries
const WKTformat = new WKT();
const projOptions = {
featureProjection: mainLizmap.projection,
featureProjection: this._lizmap3.map.getProjection(),
dataProjection: 'EPSG:4326'
};
options['mapExtent'] = WKTformat.writeGeometry(fromExtent(mainLizmap.extent), projOptions);
Expand All @@ -419,7 +428,7 @@ export default class Action {
this.resetLizmapAction(true, true, true, true);

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

return false;
Expand All @@ -442,7 +451,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');
}
}
}
Expand Down Expand Up @@ -536,7 +545,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 @@ -562,10 +571,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 @@ -575,11 +584,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 @@ -589,7 +598,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 @@ -601,14 +610,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 @@ -670,7 +679,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));
}

};
55 changes: 29 additions & 26 deletions assets/src/modules/Digitizing.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @copyright 2023 3Liz
* @license MPL-2.0
*/
import { mainLizmap, mainEventDispatcher } from '../modules/Globals.js';
import { mainEventDispatcher } from '../modules/Globals.js';
import { deepFreeze } from './config/Tools.js';
import { createEnum } from './utils/Enums.js';
import Utils from '../modules/Utils.js';
Expand Down Expand Up @@ -58,7 +58,10 @@ export const DigitizingTools = createEnum({
*/
export class Digitizing {

constructor() {
constructor(map, lizmap3) {

this._map = map;
this._lizmap3 = lizmap3;

// defined a context to separate drawn features
this._context = 'draw';
Expand Down Expand Up @@ -252,7 +255,7 @@ export class Digitizing {
name: 'LizmapDigitizingDrawLayer'
});

mainLizmap.map.addToolLayer(this._drawLayer);
this._map.addToolLayer(this._drawLayer);

// Constraint layer
this._constraintLayer = new VectorLayer({
Expand All @@ -279,7 +282,7 @@ export class Digitizing {
this._constraintLayer.setProperties({
name: 'LizmapDigitizingConstraintLayer'
});
mainLizmap.map.addToolLayer(this._constraintLayer);
this._map.addToolLayer(this._constraintLayer);

// Constraints values
this._distanceConstraint = 0;
Expand All @@ -289,7 +292,7 @@ export class Digitizing {
this.loadFeatureDrawnToMap();

// Disable drawing tool when measure tool is activated
mainLizmap.lizmap3.events.on({
this._lizmap3.events.on({
minidockopened: (e) => {
if (e.id == 'measure') {
this.toolSelected = this._tools[0];
Expand Down Expand Up @@ -369,8 +372,8 @@ export class Digitizing {
}
this._isSaved = (localStorage.getItem(this._repoAndProjectString + '_' + this._context + '_drawLayer') !== null);
this._measureTooltips.forEach((measureTooltip) => {
mainLizmap.map.removeOverlay(measureTooltip[0]);
mainLizmap.map.removeOverlay(measureTooltip[1]);
this._map.removeOverlay(measureTooltip[0]);
this._map.removeOverlay(measureTooltip[1]);
this._measureTooltips.delete(measureTooltip);
});
this._drawLayer.getSource().clear();
Expand All @@ -393,7 +396,7 @@ export class Digitizing {
set toolSelected(tool) {
if (this._tools.includes(tool)) {
// Disable all tools
mainLizmap.map.removeInteraction(this._drawInteraction);
this._map.removeInteraction(this._drawInteraction);

// If tool === 'deactivate' or current selected tool is selected again => deactivate
if (tool === this._toolSelected || tool === this._tools[0]) {
Expand Down Expand Up @@ -497,7 +500,7 @@ export class Digitizing {
unByKey(this._listener);
});

mainLizmap.map.addInteraction(this._drawInteraction);
this._map.addInteraction(this._drawInteraction);

this._toolSelected = tool;

Expand Down Expand Up @@ -556,10 +559,10 @@ export class Digitizing {
this.drawColor = this.featureDrawn[0].get('color');
}

mainLizmap.map.removeInteraction(this._drawInteraction);
this._map.removeInteraction(this._drawInteraction);

mainLizmap.map.addInteraction(this._selectInteraction);
mainLizmap.map.addInteraction(this._modifyInteraction);
this._map.addInteraction(this._selectInteraction);
this._map.addInteraction(this._modifyInteraction);

this.toolSelected = 'deactivate';
this.isErasing = false;
Expand All @@ -568,8 +571,8 @@ export class Digitizing {
} else {
// Clear selection
this._selectInteraction.getFeatures().clear();
mainLizmap.map.removeInteraction(this._selectInteraction);
mainLizmap.map.removeInteraction(this._modifyInteraction);
this._map.removeInteraction(this._selectInteraction);
this._map.removeInteraction(this._modifyInteraction);

this.saveFeatureDrawn();

Expand All @@ -592,7 +595,7 @@ export class Digitizing {
this.isEdited = false;

this._erasingCallBack = event => {
const features = mainLizmap.map.getFeaturesAtPixel(event.pixel, {
const features = this._map.getFeaturesAtPixel(event.pixel, {
layerFilter: layer => {
return layer === this._drawLayer;
},
Expand All @@ -616,10 +619,10 @@ export class Digitizing {
}
};

mainLizmap.map.on('singleclick', this._erasingCallBack );
this._map.on('singleclick', this._erasingCallBack );
mainEventDispatcher.dispatch('digitizing.erasingBegins');
} else {
mainLizmap.map.un('singleclick', this._erasingCallBack );
this._map.un('singleclick', this._erasingCallBack );
mainEventDispatcher.dispatch('digitizing.erasingEnds');
}
}
Expand Down Expand Up @@ -659,8 +662,8 @@ export class Digitizing {
if (totalOverlay) {
this._measureTooltips.forEach((measureTooltip) => {
if(measureTooltip[1] === totalOverlay){
mainLizmap.map.removeOverlay(measureTooltip[0]);
mainLizmap.map.removeOverlay(measureTooltip[1]);
this._map.removeOverlay(measureTooltip[0]);
this._map.removeOverlay(measureTooltip[1]);
this._measureTooltips.delete(measureTooltip);
return;
}
Expand Down Expand Up @@ -917,8 +920,8 @@ export class Digitizing {
});

this._measureTooltips.add([segmentOverlay, totalOverlay]);
mainLizmap.map.addOverlay(segmentOverlay);
mainLizmap.map.addOverlay(totalOverlay);
this._map.addOverlay(segmentOverlay);
this._map.addOverlay(totalOverlay);
}

// Get SLD for featureDrawn[index]
Expand Down Expand Up @@ -1014,8 +1017,8 @@ export class Digitizing {

eraseAll() {
this._measureTooltips.forEach((measureTooltip) => {
mainLizmap.map.removeOverlay(measureTooltip[0]);
mainLizmap.map.removeOverlay(measureTooltip[1]);
this._map.removeOverlay(measureTooltip[0]);
this._map.removeOverlay(measureTooltip[1]);
this._measureTooltips.delete(measureTooltip);
});
this._drawSource.clear();
Expand Down Expand Up @@ -1133,7 +1136,7 @@ export class Digitizing {
download(format) {
if (this.featureDrawn) {
const options = {
featureProjection: mainLizmap.projection,
featureProjection: this._lizmap3.map.getProjection(),
dataProjection: 'EPSG:4326'
};
if (format === 'geojson') {
Expand Down Expand Up @@ -1180,7 +1183,7 @@ export class Digitizing {
// Handle GeoJSON, GPX or KML strings
try {
const options = {
featureProjection: mainLizmap.projection
featureProjection: this._lizmap3.map.getProjection()
};
// Check extension for format type
if (['json', 'geojson'].includes(fileExtension)) {
Expand Down Expand Up @@ -1208,7 +1211,7 @@ export class Digitizing {
const featuresGeometryCollection = new GeometryCollection(featuresGeometry);
const extent = featuresGeometryCollection.getExtent();

mainLizmap.map.getView().fit(extent);
this._map.getView().fit(extent);
}
};
})(this);
Expand Down
Loading
Loading