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) { L.DomEvent.on(zoom_to, 'click', function (e) {
e.callback = L.bind(this.view, this); e.callback = L.bind(this.view, this);
this.bringToCenter(e); this.zoomTo(e);
}, feature); }, feature);
L.DomEvent.on(title, 'click', function (e) { L.DomEvent.on(title, 'click', function (e) {
e.callback = L.bind(this.view, this) e.callback = L.bind(this.view, this)
this.bringToCenter(e); this.zoomTo(e);
}, feature); }, feature);
L.DomEvent.on(edit, 'click', function () { L.DomEvent.on(edit, 'click', function () {
this.edit(); this.edit();

View file

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

View file

@ -12,7 +12,6 @@ L.Map.mergeOptions({
default_fill: true, default_fill: true,
default_weight: 3, default_weight: 3,
default_iconClass: 'Default', default_iconClass: 'Default',
default_zoomTo: 16,
default_popupContentTemplate: '# {name}\n{description}', default_popupContentTemplate: '# {name}\n{description}',
default_interactive: true, default_interactive: true,
default_labelDirection: 'auto', default_labelDirection: 'auto',
@ -45,7 +44,7 @@ L.Map.mergeOptions({
captionMenus: true, captionMenus: true,
slideshow: {}, slideshow: {},
clickable: true, clickable: true,
easing: true, easing: false,
permissions: {}, permissions: {},
permanentCreditBackground: true, 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 // but the control breaks if we don't specify a class here, so a fake class
// will do. // will do.
icon: 'umap-fake-class', icon: 'umap-fake-class',
iconLoading: 'umap-fake-class', iconLoading: 'umap-fake-class'
flyTo: true,
}); });
this._controls.fullscreen = new L.Control.Fullscreen({title: {'false': L._('View Fullscreen'), 'true': L._('Exit Fullscreen')}}); this._controls.fullscreen = new L.Control.Fullscreen({title: {'false': L._('View Fullscreen'), 'true': L._('Exit Fullscreen')}});
this._controls.search = new L.U.SearchControl(); this._controls.search = new L.U.SearchControl();
@ -1287,7 +1285,7 @@ L.U.Map.include({
'options.smoothFactor', 'options.smoothFactor',
'options.dashArray', 'options.dashArray',
'options.zoomTo', 'options.zoomTo',
['options.easing', {handler: 'Switch', label: L._('Advanced transition')}], ['options.easing', {handler: 'Switch', label: L._('Animated transitions')}],
'options.labelKey', 'options.labelKey',
['options.sortKey', {handler: 'BlurInput', helpEntries: 'sortKey', placeholder: L._('Default: name'), label: L._('Sort key'), inheritable: true}], ['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}], ['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 = [ var slideshowFields = [
['options.slideshow.active', {handler: 'Switch', label: L._('Activate slideshow mode')}], ['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.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')}] ['options.slideshow.autoplay', {handler: 'Switch', label: L._('Autostart when map is loaded')}]
]; ];
var slideshowHandler = function () { 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')}); if (prev) previousLi.title = L._('Go to «{feature}»', {feature: prev.properties.name || L._('previous')});
zoomLi.title = L._('Zoom to this feature'); zoomLi.title = L._('Zoom to this feature');
L.DomEvent.on(nextLi, 'click', function () { 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 () { 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 () { L.DomEvent.on(zoomLi, 'click', function () {
this.bringToCenter({zoomTo: this.getOption('zoomTo')}); this.zoomTo();
}, this.feature); }, this.feature);
} }
}, },