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

Add onPan and onZoom to the PanZoom interaction #3087

Merged
merged 3 commits into from
Jul 21, 2016
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
32 changes: 32 additions & 0 deletions plottable-npm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4577,6 +4577,8 @@ declare namespace Plottable.Interactions {
}
}
declare namespace Plottable.Interactions {
type PanCallback = () => void;
type ZoomCallback = () => void;
class PanZoom extends Interaction {
/**
* The number of pixels occupied in a line.
Expand All @@ -4595,6 +4597,8 @@ declare namespace Plottable.Interactions {
private _touchCancelCallback;
private _minDomainExtents;
private _maxDomainExtents;
private _panEndCallbacks;
private _zoomEndCallbacks;
/**
* A PanZoom Interaction updates the domains of an x-scale and/or a y-scale
* in response to the user panning or zooming.
Expand Down Expand Up @@ -4707,6 +4711,34 @@ declare namespace Plottable.Interactions {
* @returns {Interactions.PanZoom} The calling PanZoom Interaction.
*/
maxDomainExtent<D>(quantitativeScale: QuantitativeScale<D>, maxDomainExtent: D): this;
/**
* Adds a callback to be called when panning ends.
*
* @param {PanCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
onPanEnd(callback: PanCallback): this;
/**
* Removes a callback that would be called when panning ends.
*
* @param {PanCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
offPanEnd(callback: PanCallback): this;
/**
* Adds a callback to be called when zooming ends.
*
* @param {ZoomCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
onZoomEnd(callback: ZoomCallback): this;
/**
* Removes a callback that would be called when zooming ends.
*
* @param {ZoomCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
offZoomEnd(callback: ZoomCallback): this;
}
}
declare namespace Plottable {
Expand Down
32 changes: 32 additions & 0 deletions plottable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4576,6 +4576,8 @@ declare namespace Plottable.Interactions {
}
}
declare namespace Plottable.Interactions {
type PanCallback = () => void;
type ZoomCallback = () => void;
class PanZoom extends Interaction {
/**
* The number of pixels occupied in a line.
Expand All @@ -4594,6 +4596,8 @@ declare namespace Plottable.Interactions {
private _touchCancelCallback;
private _minDomainExtents;
private _maxDomainExtents;
private _panEndCallbacks;
private _zoomEndCallbacks;
/**
* A PanZoom Interaction updates the domains of an x-scale and/or a y-scale
* in response to the user panning or zooming.
Expand Down Expand Up @@ -4706,6 +4710,34 @@ declare namespace Plottable.Interactions {
* @returns {Interactions.PanZoom} The calling PanZoom Interaction.
*/
maxDomainExtent<D>(quantitativeScale: QuantitativeScale<D>, maxDomainExtent: D): this;
/**
* Adds a callback to be called when panning ends.
*
* @param {PanCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
onPanEnd(callback: PanCallback): this;
/**
* Removes a callback that would be called when panning ends.
*
* @param {PanCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
offPanEnd(callback: PanCallback): this;
/**
* Adds a callback to be called when zooming ends.
*
* @param {ZoomCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
onZoomEnd(callback: ZoomCallback): this;
/**
* Removes a callback that would be called when zooming ends.
*
* @param {ZoomCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
offZoomEnd(callback: ZoomCallback): this;
}
}
declare namespace Plottable {
Expand Down
47 changes: 47 additions & 0 deletions plottable.js
Original file line number Diff line number Diff line change
Expand Up @@ -11222,6 +11222,8 @@ var Plottable;
this._touchMoveCallback = function (ids, idToPoint, e) { return _this._handlePinch(ids, idToPoint, e); };
this._touchEndCallback = function (ids, idToPoint, e) { return _this._handleTouchEnd(ids, idToPoint, e); };
this._touchCancelCallback = function (ids, idToPoint, e) { return _this._handleTouchEnd(ids, idToPoint, e); };
this._panEndCallbacks = new Plottable.Utils.CallbackSet();
this._zoomEndCallbacks = new Plottable.Utils.CallbackSet();
this._xScales = new Plottable.Utils.Set();
this._yScales = new Plottable.Utils.Set();
this._dragInteraction = new Interactions.Drag();
Expand Down Expand Up @@ -11335,6 +11337,9 @@ var Plottable;
ids.forEach(function (id) {
_this._touchIds.remove(id.toString());
});
if (this._touchIds.size() > 0) {
this._zoomEndCallbacks.callCallbacks();
}
};
PanZoom.prototype._magnifyScale = function (scale, magnifyAmount, centerValue) {
var magnifyTransform = function (rangeValue) { return scale.invert(centerValue - (centerValue - rangeValue) * magnifyAmount); };
Expand Down Expand Up @@ -11363,6 +11368,7 @@ var Plottable;
this.yScales().forEach(function (yScale) {
_this._magnifyScale(yScale, zoomAmount_1, translatedP.y);
});
this._zoomEndCallbacks.callCallbacks();
}
};
PanZoom.prototype._constrainedZoomAmount = function (scale, zoomAmount) {
Expand Down Expand Up @@ -11395,6 +11401,7 @@ var Plottable;
});
lastDragPoint = endPoint;
});
this._dragInteraction.onDragEnd(function () { return _this._panEndCallbacks.callCallbacks(); });
};
PanZoom.prototype._nonLinearScaleWithExtents = function (scale) {
return this.minDomainExtent(scale) != null && this.maxDomainExtent(scale) != null &&
Expand Down Expand Up @@ -11508,6 +11515,46 @@ var Plottable;
this._maxDomainExtents.set(quantitativeScale, maxDomainExtent);
return this;
};
/**
* Adds a callback to be called when panning ends.
*
* @param {PanCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
PanZoom.prototype.onPanEnd = function (callback) {
this._panEndCallbacks.add(callback);
return this;
};
/**
* Removes a callback that would be called when panning ends.
*
* @param {PanCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
PanZoom.prototype.offPanEnd = function (callback) {
this._panEndCallbacks.delete(callback);
return this;
};
/**
* Adds a callback to be called when zooming ends.
*
* @param {ZoomCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
PanZoom.prototype.onZoomEnd = function (callback) {
this._zoomEndCallbacks.add(callback);
return this;
};
/**
* Removes a callback that would be called when zooming ends.
*
* @param {ZoomCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
PanZoom.prototype.offZoomEnd = function (callback) {
this._zoomEndCallbacks.delete(callback);
return this;
};
/**
* The number of pixels occupied in a line.
*/
Expand Down
57 changes: 57 additions & 0 deletions src/interactions/panZoomInteraction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
namespace Plottable.Interactions {

export type PanCallback = () => void;
export type ZoomCallback = () => void;

export class PanZoom extends Interaction {
/**
* The number of pixels occupied in a line.
Expand All @@ -22,6 +26,9 @@ namespace Plottable.Interactions {
private _minDomainExtents: Utils.Map<QuantitativeScale<any>, any>;
private _maxDomainExtents: Utils.Map<QuantitativeScale<any>, any>;

private _panEndCallbacks = new Utils.CallbackSet<PanCallback>();
private _zoomEndCallbacks = new Utils.CallbackSet<ZoomCallback>();

/**
* A PanZoom Interaction updates the domains of an x-scale and/or a y-scale
* in response to the user panning or zooming.
Expand Down Expand Up @@ -171,6 +178,10 @@ namespace Plottable.Interactions {
ids.forEach((id) => {
this._touchIds.remove(id.toString());
});

if (this._touchIds.size() > 0) {
this._zoomEndCallbacks.callCallbacks();
}
}

private _magnifyScale<D>(scale: QuantitativeScale<D>, magnifyAmount: number, centerValue: number) {
Expand Down Expand Up @@ -205,6 +216,7 @@ namespace Plottable.Interactions {
this.yScales().forEach((yScale) => {
this._magnifyScale(yScale, zoomAmount, translatedP.y);
});
this._zoomEndCallbacks.callCallbacks();
}
}

Expand Down Expand Up @@ -242,6 +254,7 @@ namespace Plottable.Interactions {
});
lastDragPoint = endPoint;
});
this._dragInteraction.onDragEnd(() => this._panEndCallbacks.callCallbacks());
}

private _nonLinearScaleWithExtents(scale: QuantitativeScale<any>) {
Expand Down Expand Up @@ -424,5 +437,49 @@ namespace Plottable.Interactions {
this._maxDomainExtents.set(quantitativeScale, maxDomainExtent);
return this;
}

/**
* Adds a callback to be called when panning ends.
*
* @param {PanCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
public onPanEnd(callback: PanCallback) {
this._panEndCallbacks.add(callback);
return this;
}

/**
* Removes a callback that would be called when panning ends.
*
* @param {PanCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
public offPanEnd(callback: PanCallback) {
this._panEndCallbacks.delete(callback);
return this;
}

/**
* Adds a callback to be called when zooming ends.
*
* @param {ZoomCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
public onZoomEnd(callback: ZoomCallback) {
this._zoomEndCallbacks.add(callback);
return this;
}

/**
* Removes a callback that would be called when zooming ends.
*
* @param {ZoomCallback} callback
* @returns {this} The calling PanZoom Interaction.
*/
public offZoomEnd(callback: ZoomCallback) {
this._zoomEndCallbacks.delete(callback);
return this;
}
}
}
Loading