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

Reduce mainLizmap dependencies in Search module #5034

Merged
merged 1 commit into from
Dec 4, 2024
Merged
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
2 changes: 1 addition & 1 deletion assets/src/modules/Lizmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export default class Lizmap {
this.featureStorage = new FeatureStorage();
this.popup = new Popup(this.initialConfig, this.state, this.map, this.digitizing);
this.legend = new Legend(this.state.layerTree);
this.search = new Search();
this.search = new Search(this.map, this.lizmap3);
this.tooltip = new Tooltip();

// Removed unusable button
Expand Down
42 changes: 23 additions & 19 deletions assets/src/modules/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @license MPL-2.0
*/

import { mainLizmap } from '../modules/Globals.js';
import { transformExtent } from 'ol/proj.js';

/**
Expand All @@ -14,13 +13,18 @@ import { transformExtent } from 'ol/proj.js';
*/
export default class Search {

constructor() {
/**
* Create a search instance
* @param {Map} map - OpenLayers map
* @param {object} lizmap3 - The old lizmap object
*/
constructor(map, lizmap3) {
// Attributes
this._config = lizMap.config;
this._map = lizMap.map;
this._map = map;
this._lizmap3 = lizmap3;

// Add or remove searches!
var configOptions = this._config.options;
var configOptions = this._lizmap3.config.options;
if (('searches' in configOptions) && (configOptions.searches.length > 0)) {
this._addSearches();
}
Expand All @@ -39,7 +43,7 @@ export default class Search {
$('#lizmap-search .items').html('<li class="start"><ul><li>' + lizDict['externalsearch.search'] + '</li></ul></li>');
$('#lizmap-search, #lizmap-search-close').addClass('open');
} else {
lizMap.addMessage(lizDict['externalsearch.noquery'], 'info', true).attr('id', 'lizmap-search-message');
this._lizmap3.addMessage(lizDict['externalsearch.noquery'], 'info', true).attr('id', 'lizmap-search-message');
}
}

Expand All @@ -58,8 +62,8 @@ export default class Search {
continue;
}
sqvalsn.push(sqi);
if (sqi != lizMap.cleanName(sqi)) {
sqvalsn.push(lizMap.cleanName(sqi));
if (sqi != this._lizmap3.cleanName(sqi)) {
sqvalsn.push(this._lizmap3.cleanName(sqi));
}
}
sqrex += sqvalsn.join('|');
Expand All @@ -85,8 +89,8 @@ export default class Search {

// define max extent for searches
var wgs84 = new OpenLayers.Projection('EPSG:4326');
var extent = new OpenLayers.Bounds(this._map.maxExtent.toArray());
extent.transform(this._map.getProjection(), wgs84);
var extent = new OpenLayers.Bounds(this._lizmap3.map.maxExtent.toArray());
extent.transform(this._map.getView().getProjection().getCode(), wgs84);

$('#nominatim-search').submit(() => {
this._startExternalSearch();
Expand Down Expand Up @@ -153,8 +157,8 @@ export default class Search {

// define max extent for searches
var wgs84 = new OpenLayers.Projection('EPSG:4326');
var extent = new OpenLayers.Bounds(this._map.maxExtent.toArray());
extent.transform(this._map.getProjection(), wgs84);
var extent = new OpenLayers.Bounds(this._lizmap3.map.maxExtent.toArray());
extent.transform(this._map.getView().getProjection().getCode(), wgs84);

// define external search service
var service = null;
Expand Down Expand Up @@ -225,8 +229,8 @@ export default class Search {
lizMap.addMessage(lizDict['externalsearch.ignlimit'], 'warning', true);
break;
}
let mapExtent4326 = transformExtent(mainLizmap.map.getView().calculateExtent(), mainLizmap.projection, 'EPSG:4326');
let queryParam = '?text=' + searchQuery + '&type=StreetAddress&maximumResponses=10&bbox=' + mapExtent4326
let mapExtent4326 = transformExtent(this._map.getView().calculateExtent(), this._map.getView().getProjection().getCode(), 'EPSG:4326');
let queryParam = '?text=' + $('#search-query').val() + '&type=StreetAddress&maximumResponses=10&bbox=' + mapExtent4326;
$.getJSON(encodeURI(service + queryParam), data => {
let text = '';
let count = 0;
Expand Down Expand Up @@ -306,7 +310,7 @@ export default class Search {
* {Boolean} searches added to the user interface
*/
_addSearches() {
var configOptions = this._config.options;
var configOptions = this._lizmap3.config.options;
if (!('searches' in configOptions) || (configOptions.searches.length == 0)) {
return;
}
Expand Down Expand Up @@ -353,18 +357,18 @@ export default class Search {
const linkClicked = evt.currentTarget;
var bbox = linkClicked.getAttribute('href').replace('#', '');
var bbox = OpenLayers.Bounds.fromString(bbox);
bbox.transform(wgs84, this._map.getProjectionObject());
this._map.zoomToExtent(bbox);
bbox.transform(wgs84, this._lizmap3.map.getProjectionObject());
this._lizmap3.map.zoomToExtent(bbox);

var feat = new OpenLayers.Feature.Vector(bbox.toGeometry().getCentroid());
var geomWKT = linkClicked.dataset.wkt;
if (geomWKT) {
mainLizmap.map.setHighlightFeatures(geomWKT, "wkt", "EPSG:4326");
this._map.setHighlightFeatures(geomWKT, "wkt", "EPSG:4326");
}

$('#lizmap-search, #lizmap-search-close').removeClass('open');
// trigger event containing selected feature
lizMap.events.triggerEvent('lizmapexternalsearchitemselected',
this._lizmap3.events.triggerEvent('lizmapexternalsearchitemselected',
{
'feature': feat
}
Expand Down
Loading