Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nboisteault committed Dec 5, 2024
1 parent 4f2b601 commit 4cc973c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 67 deletions.
7 changes: 6 additions & 1 deletion assets/src/modules/Lizmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ export default class Lizmap {
this.legend = new Legend(this.state.layerTree);
this.search = new Search(this.map, this.lizmap3);
this.tooltip = new Tooltip();
this.locateByLayer = new LocateByLayer();
this.locateByLayer = new LocateByLayer(
this.initialConfig.locateByLayer,
this.initialConfig.vectorLayerFeatureTypeList,
this.map,
this._lizmap3
);

// Removed unusable button
if (!this.config['printTemplates'] || this.config.printTemplates.length == 0 ) {
Expand Down
135 changes: 69 additions & 66 deletions assets/src/modules/LocateByLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* @license MPL-2.0
*/

import { mainLizmap } from '../modules/Globals.js';
import DOMPurify from 'dompurify';

import GeoJSON from 'ol/format/GeoJSON.js';
Expand All @@ -16,10 +15,22 @@ import GeoJSON from 'ol/format/GeoJSON.js';
* @name LocateByLayer
*/
export default class LocateByLayer {
constructor() {
this._locateByLayerConfig = lizMap.config.locateByLayer;
/**
* Build the lizmap LocateByLayer instance
* @param {LocateByLayerConfig} locateByLayer - The lizmap locateByLayer config
* @param {WfsFeatureType[]} vectorLayerFeatureTypeList - The list of WFS feature type
* @param {Map} map - OpenLayers map
* @param {object} lizmap3 - The old lizmap object
*/
constructor(locateByLayer, vectorLayerFeatureTypeList, map, lizmap3) {
this._map = map;
this._vectorLayerFeatureTypeList = vectorLayerFeatureTypeList;

this._lizmap3 = lizmap3;
this._lizmap3LocateByLayerConfig = lizmap3.config.locateByLayer;

const locateBtn = document.getElementById('button-locate');
if (mainLizmap.initialConfig.locateByLayer) {
if (locateByLayer) {
this.addLocateByLayer();
document.querySelector('#mapmenu .locate').classList.add('active')
document.getElementById('locate').classList.remove('hide')
Expand All @@ -30,18 +41,19 @@ export default class LocateByLayer {

addLocateByLayer() {
var locateByLayerList = [];
for (var lname in this._locateByLayerConfig) {
if ('order' in this._locateByLayerConfig[lname])
locateByLayerList[this._locateByLayerConfig[lname].order] = lname;
else
for (var lname in this._lizmap3LocateByLayerConfig) {
if ('order' in this._lizmap3LocateByLayerConfig[lname]){
locateByLayerList[this._lizmap3LocateByLayerConfig[lname].order] = lname;
} else {
locateByLayerList.push(lname);
}
}
var locateContent = [];
for (var l in locateByLayerList) {
var lname = locateByLayerList[l];
var lConfig = lizMap.config.layers[lname];
var lConfig = this._lizmap3.config.layers[lname];
var html = '<div class="locate-layer">';
html += '<select id="locate-layer-' + lizMap.cleanName(lname) + '" class="label">';
html += '<select id="locate-layer-' + this._lizmap3.cleanName(lname) + '" class="label">';
html += '<option>' + lConfig.title + '...</option>';
html += '</select>';
html += '</div>';
Expand All @@ -50,22 +62,22 @@ export default class LocateByLayer {
}
$('#locate .menu-content').html(locateContent.join('<hr/>'));

var featureTypes = lizMap.mainLizmap.initialConfig.vectorLayerFeatureTypeList;
var featureTypes = this._vectorLayerFeatureTypeList;
if (featureTypes.length == 0) {
this._locateByLayerConfig = {};
this._lizmap3LocateByLayerConfig = {};
$('#button-locate').parent().remove();
$('#locate-menu').remove();
} else {
for (const featureType of featureTypes) {
var typeName = featureType.Name;
var lname = lizMap.getNameByTypeName(typeName);
var lname = this._lizmap3.getNameByTypeName(typeName);
if (!lname) {
if (typeName in this._locateByLayerConfig)
if (typeName in this._lizmap3LocateByLayerConfig)
lname = typeName
else if ((typeName in shortNameMap) && (shortNameMap[typeName] in this._locateByLayerConfig))
else if ((typeName in shortNameMap) && (shortNameMap[typeName] in this._lizmap3LocateByLayerConfig))
lname = shortNameMap[typeName];
else {
for (var lbl in this._locateByLayerConfig) {
for (var lbl in this._lizmap3LocateByLayerConfig) {
if (lbl.split(' ').join('_') == typeName) {
lname = lbl;
break;
Expand All @@ -74,25 +86,22 @@ export default class LocateByLayer {
}
}

if (!(lname in this._locateByLayerConfig))
if (!(lname in this._lizmap3LocateByLayerConfig))
continue;

var locate = this._locateByLayerConfig[lname];
var locate = this._lizmap3LocateByLayerConfig[lname];
locate['crs'] = featureType.SRS;
// loadProjDefinition(locate.crs, function () {
// new OpenLayers.Projection(locate.crs);
// });
locate['bbox'] = featureType.LatLongBoundingBox;
}

// get joins
for (var lName in this._locateByLayerConfig) {
var locate = this._locateByLayerConfig[lName];
for (var lName in this._lizmap3LocateByLayerConfig) {
var locate = this._lizmap3LocateByLayerConfig[lName];
if ('vectorjoins' in locate && locate['vectorjoins'].length != 0) {
var vectorjoin = locate['vectorjoins'][0];
locate['joinFieldName'] = vectorjoin['targetFieldName'];
for (var jName in this._locateByLayerConfig) {
var jLocate = this._locateByLayerConfig[jName];
for (var jName in this._lizmap3LocateByLayerConfig) {
var jLocate = this._lizmap3LocateByLayerConfig[jName];
if (jLocate.layerId == vectorjoin.joinLayerId) {
vectorjoin['joinLayer'] = jName;
locate['joinLayer'] = jName;
Expand All @@ -110,23 +119,23 @@ export default class LocateByLayer {
}

// get locate by layers features
for (var lname in this._locateByLayerConfig) {
for (var lname in this._lizmap3LocateByLayerConfig) {
this.getLocateFeature(lname);
}
$('.btn-locate-clear').click(function () {
lizMap.mainLizmap.map.clearHighlightFeatures();
document.getElementById('locate-clear').addEventListener('click', () => {
this._lizmap3.mainLizmap.map.clearHighlightFeatures();
$('#locate select').val('-1');
$('div.locate-layer span > input').val('');

if (lizMap.lizmapLayerFilterActive) {
lizMap.events.triggerEvent('lizmaplocatefeaturecanceled',
{ 'featureType': lizMap.lizmapLayerFilterActive }
if (this._lizmap3.lizmapLayerFilterActive) {
this._lizmap3.events.triggerEvent('lizmaplocatefeaturecanceled',
{ 'featureType': this._lizmap3.lizmapLayerFilterActive }
);
}
return false;

});
$('#locate-close').click(function () {
document.getElementById('locate-close').addEventListener('click', () => {
$('.btn-locate-clear').click(); // deactivate locate and filter
document.getElementById('button-locate')?.click();
return false;
Expand All @@ -139,7 +148,7 @@ export default class LocateByLayer {
* @param aName
*/
getLocateFeature(aName) {
var locate = this._locateByLayerConfig[aName];
var locate = this._lizmap3LocateByLayerConfig[aName];

// get fields to retrieve
var fields = ['geometry',locate.fieldName];
Expand All @@ -163,24 +172,18 @@ export default class LocateByLayer {
}

// Get WFS url and options
var getFeatureUrlData = lizMap.getVectorLayerWfsUrl( aName, null, null, 'extent' );
var getFeatureUrlData = this._lizmap3.getVectorLayerWfsUrl( aName, null, null, 'extent' );
getFeatureUrlData['options']['PROPERTYNAME'] = fields.join(',');

var layerName = lizMap.cleanName(aName);
var layerName = this._lizmap3.cleanName(aName);

// Get data
$.post( getFeatureUrlData['url'], getFeatureUrlData['options'], (data) => {
var lConfig = lizMap.config.layers[aName];
$.post( getFeatureUrlData['url'], getFeatureUrlData['options'], data => {
var lConfig = this._lizmap3.config.layers[aName];
locate['features'] = {};
if ( !data.features )
data = JSON.parse(data);
var features = data.features;
// if( locate.crs != 'EPSG:4326' && features.length != 0) {
// // load projection to be sure to have the definition
// loadProjDefinition( locate.crs, function() {
// locate.crs = 'EPSG:4326';
// });
// }

if ('filterFieldName' in locate) {
// create filter combobox for the layer
Expand Down Expand Up @@ -296,7 +299,7 @@ export default class LocateByLayer {
for (var i=0, len=filterjoins.length; i<len; i++) {
var filterjoin = filterjoins[i];
var jName = filterjoin.joinLayer;
if ( jName in this._locateByLayerConfig ) {
if ( jName in this._lizmap3LocateByLayerConfig ) {
// update joined select options
var oldVal = $('#locate-layer-'+cleanName(jName)).val();
this.updateLocateFeatureList( jName );
Expand All @@ -308,7 +311,7 @@ export default class LocateByLayer {
// zoom to parent selection
if ( 'vectorjoins' in locate && locate.vectorjoins.length == 1 ) {
var jName = locate.vectorjoins[0].joinLayer;
if ( jName in this._locateByLayerConfig ) {
if ( jName in this._lizmap3LocateByLayerConfig ) {
this.zoomToLocateFeature( jName );
return;
}
Expand All @@ -324,7 +327,7 @@ export default class LocateByLayer {
for (var i=0, len=filterjoins.length; i<len; i++) {
var filterjoin = filterjoins[i];
var jName = filterjoin.joinLayer;
if ( jName in this._locateByLayerConfig ) {
if ( jName in this._lizmap3LocateByLayerConfig ) {
// update joined select options
this.updateLocateFeatureList( jName );
$('#locate-layer-'+cleanName(jName)).val('-1');
Expand Down Expand Up @@ -355,7 +358,7 @@ export default class LocateByLayer {
$('#locate-layer-'+layerName+' ~ span > input').autocomplete('close');
if ( ('minLength' in locate) && locate.minLength > 0 )
$('#locate-layer-'+layerName).parent().addClass('no-toggle');
if(lizMap.checkMobile()){
if (this._lizmap3.checkMobile()) {
// autocompletion items for locatebylayer feature
$('div.locate-layer select').show();
$('span.custom-combobox').hide();
Expand All @@ -369,46 +372,46 @@ export default class LocateByLayer {
*/
zoomToLocateFeature(aName) {
// clear highlight layer
lizMap.mainLizmap.map.clearHighlightFeatures();
this._map.clearHighlightFeatures();

// get locate by layer val
var locate = this._locateByLayerConfig[aName];
var layerName = lizMap.cleanName(aName);
var locate = this._lizmap3LocateByLayerConfig[aName];
var layerName = this._lizmap3.cleanName(aName);
var val = $('#locate-layer-'+layerName).val();
if (val == '-1') {
// Trigger event
lizMap.events.triggerEvent('lizmaplocatefeaturecanceled', {'featureType': aName });
this._lizmap3.events.triggerEvent('lizmaplocatefeaturecanceled', {'featureType': aName });
} else {
// zoom to val
const featGeoJSON = locate.features[val];
if( featGeoJSON.geometry){
const geom = (new GeoJSON()).readGeometry(featGeoJSON.geometry, {
dataProjection: 'EPSG:4326',
featureProjection: lizMap.mainLizmap.projection
featureProjection: this._lizmap3.mainLizmap.projection
});
// Show geometry if asked
if (locate.displayGeom == 'True') {
var getFeatureUrlData = lizMap.getVectorLayerWfsUrl( aName, null, null, null );
var getFeatureUrlData = this._lizmap3.getVectorLayerWfsUrl( aName, null, null, null );
getFeatureUrlData['options']['PROPERTYNAME'] = ['geometry',locate.fieldName].join(',');
getFeatureUrlData['options']['FEATUREID'] = val;
// Get data
$.post( getFeatureUrlData['url'], getFeatureUrlData['options'], function(data) {
$.post( getFeatureUrlData['url'], getFeatureUrlData['options'], data => {
if ( !data.features ){
data = JSON.parse(data);
}
lizMap.mainLizmap.map.setHighlightFeatures(data.features[0], "geojson");
}).fail(function(){
lizMap.mainLizmap.map.setHighlightFeatures(feat, "geojson");
this._map.setHighlightFeatures(data.features[0], "geojson");
}).fail(() => {
this._.map.setHighlightFeatures(feat, "geojson");
});
}
// zoom to extent
lizMap.mainLizmap.map.zoomToGeometryOrExtent(geom);
this._map.zoomToGeometryOrExtent(geom);
}

var fid = val.split('.')[1];

// Trigger event
lizMap.events.triggerEvent('lizmaplocatefeaturechanged',
this._lizmap3.events.triggerEvent('lizmaplocatefeaturechanged',
{
'featureType': aName,
'featureId': fid
Expand All @@ -422,15 +425,15 @@ export default class LocateByLayer {
* @param aName
*/
updateLocateFeatureList(aName) {
var locate = this._locateByLayerConfig[aName];
var locate = this._lizmap3LocateByLayerConfig[aName];
// clone features reference
var features = {};
for ( var fid in locate.features ) {
features[fid] = locate.features[fid];
}
// filter by filter field name
if ('filterFieldName' in locate) {
var filterValue = $('#locate-layer-' + lizMap.cleanName(aName) + '-'+locate.filterFieldName).val();
var filterValue = $('#locate-layer-' + this._lizmap3.cleanName(aName) + '-'+locate.filterFieldName).val();
if ( filterValue != '-1' ) {
for (var fid in features) {
var feat = features[fid];
Expand All @@ -446,9 +449,9 @@ export default class LocateByLayer {
for ( var i=0, len =vectorjoins.length; i< len; i++) {
var vectorjoin = vectorjoins[i];
var jName = vectorjoin.joinLayer;
if ( jName in this._locateByLayerConfig ) {
var jLocate = this._locateByLayerConfig[jName];
var jVal = $('#locate-layer-' + lizMap.cleanName(jName)).val();
if ( jName in this._lizmap3LocateByLayerConfig ) {
var jLocate = this._lizmap3LocateByLayerConfig[jName];
var jVal = $('#locate-layer-' + this._lizmap3.cleanName(jName)).val();
if ( jVal == '-1' ) continue;
var jFeat = jLocate.features[jVal];
for (var fid in features) {
Expand All @@ -460,13 +463,13 @@ export default class LocateByLayer {
}
}
// create the option list
const placeHolder = lizMap.config.layers[aName].title;
const placeHolder = this._lizmap3.config.layers[aName].title;
var options = '<option value="-1" label="'+placeHolder+'"></option>';
for (var fid in features) {
var feat = features[fid];
options += '<option value="' + feat.id + '">' + DOMPurify.sanitize(feat.properties[locate.fieldName]) + '</option>';
}
// add option list
$('#locate-layer-'+ lizMap.cleanName(aName)).html(options);
$('#locate-layer-'+ this._lizmap3.cleanName(aName)).html(options);
}
};

0 comments on commit 4cc973c

Please sign in to comment.