Refactor bringToCenter, flyTo and zoomTo in only one function

In the same move:
- Map.options.easing is now false by default
- if zoomTo options is set, it should be honoured each time we
  use the zoomTo function

fix #679 #179
This commit is contained in:
Yohan Boniface 2023-05-05 17:35:14 +02:00
parent 89e0900148
commit 25ab9f943c
4 changed files with 22 additions and 31 deletions

View file

@ -708,11 +708,11 @@ L.U.Map.include({
}
L.DomEvent.on(zoom_to, 'click', function (e) {
e.callback = L.bind(this.view, this);
this.bringToCenter(e);
this.zoomTo(e);
}, feature);
L.DomEvent.on(title, 'click', function (e) {
e.callback = L.bind(this.view, this)
this.bringToCenter(e);
this.zoomTo(e);
}, feature);
L.DomEvent.on(edit, 'click', function () {
this.edit();

View file

@ -113,7 +113,7 @@ L.U.FeatureMixin = {
this.getAdvancedEditActions(advancedActions);
this.map.ui.openPanel({data: {html: container}, className: 'dark'});
this.map.editedFeature = this;
if (!this.isOnScreen()) this.bringToCenter(e);
if (!this.isOnScreen()) this.zoomTo(e);
},
getAdvancedEditActions: function (container) {
@ -254,28 +254,23 @@ L.U.FeatureMixin = {
return value;
},
bringToCenter: function (e) {
e = e || {};
var latlng = e.latlng || this.getCenter();
this.map.setView(latlng, e.zoomTo || this.map.getZoom());
if (e.callback) e.callback.call(this);
},
zoomTo: function (e) {
e = e || {};
var easing = e.easing !== undefined ? e.easing : this.map.options.easing;
if (easing) this.flyTo();
else this.bringToCenter({zoomTo: this.getBestZoom(), callback: e.callback});
if (easing) {
this.map.flyTo(this.getCenter(), this.getBestZoom());
}
else {
var latlng = e.latlng || this.getCenter();
this.map.setView(latlng, this.getBestZoom() || this.map.getZoom());
}
if (e.callback) e.callback.call(this);
},
getBestZoom: function () {
return this.getOption('zoomTo');
},
flyTo: function () {
this.map.flyTo(this.getCenter(), this.getBestZoom());
},
getNext: function () {
return this.datalayer.getNextFeature(this);
},
@ -587,7 +582,7 @@ L.U.Marker = L.Marker.extend({
callback: function () {
if (!this._latlng.isValid()) return this.map.ui.alert({content: L._('Invalid latitude or longitude'), level: 'error'});
this._redraw();
this.bringToCenter();
this.zoomTo({easing: false});
},
callbackContext: this
});
@ -595,12 +590,12 @@ L.U.Marker = L.Marker.extend({
fieldset.appendChild(builder.build());
},
bringToCenter: function (e) {
zoomTo: function (e) {
if (this.datalayer.isClustered() && !this._icon) {
// callback is mandatory for zoomToShowLayer
this.datalayer.layer.zoomToShowLayer(this, e.callback || function (){});
} else {
L.U.FeatureMixin.bringToCenter.call(this, e);
L.U.FeatureMixin.zoomTo.call(this, e);
}
},
@ -711,9 +706,7 @@ L.U.PathMixin = {
},
getBestZoom: function () {
if (this.options.zoomTo) return this.options.zoomTo;
var bounds = this.getBounds();
return this.map.getBoundsZoom(bounds, true);
return this.getOption("zoomTo") || this.map.getBoundsZoom(this.getBounds(), true);
},
endEdit: function () {

View file

@ -12,7 +12,6 @@ L.Map.mergeOptions({
default_fill: true,
default_weight: 3,
default_iconClass: 'Default',
default_zoomTo: 16,
default_popupContentTemplate: '# {name}\n{description}',
default_interactive: true,
default_labelDirection: 'auto',
@ -45,7 +44,7 @@ L.Map.mergeOptions({
captionMenus: true,
slideshow: {},
clickable: true,
easing: true,
easing: false,
permissions: {},
permanentCreditBackground: true,
});
@ -262,8 +261,7 @@ L.U.Map.include({
// but the control breaks if we don't specify a class here, so a fake class
// will do.
icon: 'umap-fake-class',
iconLoading: 'umap-fake-class',
flyTo: true,
iconLoading: 'umap-fake-class'
});
this._controls.fullscreen = new L.Control.Fullscreen({title: {'false': L._('View Fullscreen'), 'true': L._('Exit Fullscreen')}});
this._controls.search = new L.U.SearchControl();
@ -1287,7 +1285,7 @@ L.U.Map.include({
'options.smoothFactor',
'options.dashArray',
'options.zoomTo',
['options.easing', {handler: 'Switch', label: L._('Advanced transition')}],
['options.easing', {handler: 'Switch', label: L._('Animated transitions')}],
'options.labelKey',
['options.sortKey', {handler: 'BlurInput', helpEntries: 'sortKey', placeholder: L._('Default: name'), label: L._('Sort key'), inheritable: true}],
['options.filterKey', {handler: 'Input', helpEntries: 'filterKey', placeholder: L._('Default: name'), label: L._('Filter keys'), inheritable: true}],
@ -1416,7 +1414,7 @@ L.U.Map.include({
var slideshowFields = [
['options.slideshow.active', {handler: 'Switch', label: L._('Activate slideshow mode')}],
['options.slideshow.delay', {handler: 'SlideshowDelay', helpText: L._('Delay between two transitions when in play mode')}],
['options.slideshow.easing', {handler: 'Switch', label: L._('Smart transitions'), inheritable: true}],
['options.slideshow.easing', {handler: 'Switch', label: L._('Animated transitions'), inheritable: true}],
['options.slideshow.autoplay', {handler: 'Switch', label: L._('Autostart when map is loaded')}]
];
var slideshowHandler = function () {

View file

@ -118,13 +118,13 @@ L.U.PopupTemplate.Default = L.Class.extend({
if (prev) previousLi.title = L._('Go to «{feature}»', {feature: prev.properties.name || L._('previous')});
zoomLi.title = L._('Zoom to this feature');
L.DomEvent.on(nextLi, 'click', function () {
if (next) next.bringToCenter({zoomTo: next.getOption('zoomTo'), callback: next.view});
if (next) next.zoomTo({callback: next.view});
});
L.DomEvent.on(previousLi, 'click', function () {
if (prev) prev.bringToCenter({zoomTo: prev.getOption('zoomTo'), callback: prev.view});
if (prev) prev.zoomTo({callback: prev.view});
});
L.DomEvent.on(zoomLi, 'click', function () {
this.bringToCenter({zoomTo: this.getOption('zoomTo')});
this.zoomTo();
}, this.feature);
}
},