Skip to content

Commit

Permalink
Merge pull request #3857 from rldhont/js-reduce-mainlizmap-dependencies
Browse files Browse the repository at this point in the history
[JavaScript] reduce mainLizmap dependencies
  • Loading branch information
rldhont authored Jan 21, 2025
2 parents 370fb57 + 8dc5a03 commit 551e001
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 151 deletions.
85 changes: 44 additions & 41 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 @@ -74,7 +74,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 @@ -280,7 +283,7 @@ export class Digitizing {
name: 'LizmapDigitizingDrawLayer'
});

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

// Constraint layer
this._constraintLayer = new VectorLayer({
Expand All @@ -307,7 +310,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 @@ -317,7 +320,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 @@ -397,8 +400,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 @@ -421,7 +424,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 @@ -521,7 +524,7 @@ export class Digitizing {
unByKey(this._listener);
});

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

this._toolSelected = tool;

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

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

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

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

this.saveFeatureDrawn();

Expand All @@ -622,9 +625,9 @@ export class Digitizing {
this.isEdited = false;
this.isSplitting = false;

mainLizmap.map.addInteraction(this._transformInteraction);
this._map.addInteraction(this._transformInteraction);
} else {
mainLizmap.map.removeInteraction(this._transformInteraction);
this._map.removeInteraction(this._transformInteraction);
}

mainEventDispatcher.dispatch('digitizing.rotate');
Expand Down Expand Up @@ -703,7 +706,7 @@ export class Digitizing {
geometry: new Polygon(parser.write(geom).getCoordinates())
});

// Add splitted polygon to vector layer
// Add splitted polygon to vector layer
this._drawSource.addFeature(splitted_polygon);
this._selectInteraction.getFeatures().push(splitted_polygon);
});
Expand Down Expand Up @@ -732,11 +735,11 @@ export class Digitizing {
}
});
});
mainLizmap.map.addInteraction(this._splitInteraction);
this._map.addInteraction(this._splitInteraction);
} else {
mainLizmap.map.removeInteraction(this._splitInteraction);
this._map.removeInteraction(this._splitInteraction);
}

mainEventDispatcher.dispatch('digitizing.split');
}
}
Expand All @@ -757,7 +760,7 @@ export class Digitizing {
this.isSplitting = 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 @@ -781,10 +784,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 @@ -824,8 +827,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 @@ -1033,7 +1036,7 @@ export class Digitizing {
* @returns {string} The formatted length.
*/
formatLength(geom) {
const length = getLength(geom, {projection: mainLizmap.map.getView().getProjection()});
const length = getLength(geom, {projection: this._map.getView().getProjection()});
let output;
if (length > 100) {
output = Math.round((length / 1000) * 100) / 100 + ' ' + 'km';
Expand All @@ -1049,7 +1052,7 @@ export class Digitizing {
* @returns {string} Formatted area.
*/
formatArea(polygon) {
const area = getArea(polygon, {projection: mainLizmap.map.getView().getProjection()});
const area = getArea(polygon, {projection: this._map.getView().getProjection()});
let output;
if (area > 10000) {
output = Math.round((area / 1000000) * 100) / 100 + ' ' + 'km<sup>2</sup>';
Expand Down Expand Up @@ -1131,8 +1134,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 @@ -1242,8 +1245,8 @@ export class Digitizing {
this.isSplitting = false;

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 @@ -1365,7 +1368,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 @@ -1428,7 +1431,7 @@ export class Digitizing {
// return (e) => {
// const buffershp = e.target.result;
// shp(buffershp).then(response => {
// let OL6features = (new GeoJSON()).readFeatures(response, {featureProjection: mainLizmap.projection});
// let OL6features = (new GeoJSON()).readFeatures(response, {featureProjection: this._lizmap3.map.getProjection()});

// if (OL6features) {
// // Add imported features to map and zoom to their extent
Expand All @@ -1448,7 +1451,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 @@ -1491,18 +1494,18 @@ export class Digitizing {
register(proj4);
}

features = reprojAll(features, projFGB, mainLizmap.projection);
features = reprojAll(features, projFGB, this._lizmap3.map.getProjection());
} else {
lizMap.addMessage(lizDict["digitizing.import.metadata.error"] + " : " +
mainLizmap.projection
this._lizmap3.addMessage(lizDict["digitizing.import.metadata.error"] + " : " +
this._lizmap3.map.getProjection()
, 'info'
, true)
}

OL6features = features;
}
} catch (error) {
lizMap.addMessage(error, 'danger', true)
this._lizmap3.addMessage(error, 'danger', true)
}

if (OL6features) {
Expand All @@ -1513,7 +1516,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
8 changes: 4 additions & 4 deletions assets/src/modules/Lizmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ export default class Lizmap {

// Create Lizmap modules
this.permalink = new Permalink();
this.map = new map();
this.map = new map('newOlMap', this.initialConfig, this.serviceURL, this.state.map, this.state.baseLayers, this.state.rootMapGroup, this.lizmap3);
this.edition = new Edition(this._lizmap3);
this.featuresTable = new FeaturesTable(this.initialConfig, this.lizmap3);
this.geolocation = new Geolocation(this.map, this.lizmap3);
this.geolocationSurvey = new GeolocationSurvey(this.geolocation, this.edition);
this.digitizing = new Digitizing();
this.digitizing = new Digitizing(this.map, this.lizmap3);
this.selectionTool = new SelectionTool(this.map, this.digitizing, this.initialConfig, this.lizmap3);
this.snapping = new Snapping();
this.snapping = new Snapping(this.edition, this.state.rootMapGroup, this.state.layerTree, this.lizmap3);
this.layers = new Layers();
this.proxyEvents = new ProxyEvents();
this.wfs = new WFS();
Expand All @@ -167,7 +167,7 @@ export default class Lizmap {
this.popup = new Popup(this.initialConfig, this.state, this.map, this.digitizing);
this.legend = new Legend(this.state.layerTree);
this.search = new Search(this.map, this.lizmap3);
this.tooltip = new Tooltip();
this.tooltip = new Tooltip(this.map, this.initialConfig.tooltipLayers, this.lizmap3);
this.locateByLayer = new LocateByLayer(
this.initialConfig.locateByLayer,
this.initialConfig.vectorLayerFeatureTypeList,
Expand Down
Loading

0 comments on commit 551e001

Please sign in to comment.