Merge pull request #6765 from fredj/fit_callback

Add new callback function to view.FitOptions
This commit is contained in:
Frédéric Junod
2017-05-02 10:36:37 +02:00
committed by GitHub
3 changed files with 33 additions and 2 deletions

View File

@@ -7849,7 +7849,8 @@ olx.view;
* maxZoom: (number|undefined),
* minResolution: (number|undefined),
* duration: (number|undefined),
* easing: (undefined|function(number):number)
* easing: (undefined|function(number):number),
* callback: (undefined|function(boolean))
* }}
*/
olx.view.FitOptions;
@@ -7927,6 +7928,16 @@ olx.view.FitOptions.prototype.duration;
olx.view.FitOptions.prototype.easing;
/**
* Optional function called when the view is in it's final position. The callback will be
* called with `true` if the animation series completed on its own or `false`
* if it was cancelled.
* @type {undefined|function(boolean)}
* @api
*/
olx.view.FitOptions.prototype.callback;
/* typedefs for object literals exposed by the library */

View File

@@ -882,6 +882,7 @@ ol.View.prototype.fit = function(geometryOrExtent, opt_options) {
var centerX = centerRotX * cosAngle - centerRotY * sinAngle;
var centerY = centerRotY * cosAngle + centerRotX * sinAngle;
var center = [centerX, centerY];
var callback = options.callback ? options.callback : ol.nullFunction;
if (options.duration !== undefined) {
this.animate({
@@ -889,10 +890,11 @@ ol.View.prototype.fit = function(geometryOrExtent, opt_options) {
center: center,
duration: options.duration,
easing: options.easing
});
}, callback);
} else {
this.setResolution(resolution);
this.setCenter(center);
setTimeout(callback.bind(undefined, true), 0);
}
};

View File

@@ -1179,6 +1179,24 @@ describe('ol.View', function() {
}, 50);
});
it('calls a callback when duration is not defined', function(done) {
view.fit(new ol.geom.LineString([[6000, 46000], [6000, 47100], [7000, 46000]]), {
callback: function(complete) {
expect(complete).to.be(true);
done();
}
});
});
it('calls a callback when animation completes', function(done) {
view.fit(new ol.geom.LineString([[6000, 46000], [6000, 47100], [7000, 46000]]), {
duration: 25,
callback: function(complete) {
expect(complete).to.be(true);
done();
}
});
});
});
describe('centerOn', function() {