From 1b4286344d867635f451c96dd13dc7592fba906a Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Mon, 1 Dec 2014 13:48:38 -0800 Subject: [PATCH] [Plot] Separate out pan/zoom stack Create a separate script/class for the pan-zoom stack, to simplify PlotController for testability and maintainability. WTD-533. --- platform/features/plot/src/PlotController.js | 43 +++++++------- .../features/plot/src/PlotPanZoomStack.js | 59 +++++++++++++++++++ 2 files changed, 79 insertions(+), 23 deletions(-) create mode 100644 platform/features/plot/src/PlotPanZoomStack.js diff --git a/platform/features/plot/src/PlotController.js b/platform/features/plot/src/PlotController.js index 177782793e..325efda885 100644 --- a/platform/features/plot/src/PlotController.js +++ b/platform/features/plot/src/PlotController.js @@ -4,8 +4,13 @@ * Module defining PlotController. Created by vwoeltje on 11/12/14. */ define( - ["./PlotPreparer", "./PlotPalette", "../lib/moment.min.js"], - function (PlotPreparer, PlotPalette) { + [ + "./PlotPreparer", + "./PlotPalette", + "./PlotPanZoomStack", + "../lib/moment.min.js" + ], + function (PlotPreparer, PlotPalette, PlotPanZoomStack) { "use strict"; var AXIS_DEFAULTS = [ @@ -22,10 +27,7 @@ define( function PlotController($scope) { var mousePosition, marqueeStart, - panZoomStack = [{ - dimensions: [], - origin: [] - }], + panZoomStack = new PlotPanZoomStack([], []), domainOffset; function formatDomainValue(v) { @@ -44,8 +46,8 @@ define( function pixelToDomainRange(x, y, width, height, domainOffset) { - var panZoom = panZoomStack[panZoomStack.length - 1], - offset = [ domainOffset || 0, 0], + var panZoom = panZoomStack.getPanZoom(), + offset = [ domainOffset || 0, 0 ], origin = panZoom.origin, dimensions = panZoom.dimensions; @@ -92,7 +94,7 @@ define( } function updateDrawingBounds() { - var panZoom = panZoomStack[panZoomStack.length - 1]; + var panZoom = panZoomStack.getPanZoom(); $scope.draw.dimensions = panZoom.dimensions; $scope.draw.origin = panZoom.origin; @@ -129,10 +131,10 @@ define( formatRangeValue ); - panZoomStack[0] = { - origin: prepared.getOrigin(), - dimensions: prepared.getDimensions() - }; + panZoomStack.setBasePanZoom( + prepared.getOrigin(), + prepared.getDimensions() + ); domainOffset = prepared.getDomainOffset(); @@ -207,10 +209,7 @@ define( Math.max(a[1], b[1]) - origin[1] ]; - panZoomStack.push({ - origin: origin, - dimensions: dimensions - }); + panZoomStack.pushPanZoom(origin, dimensions); } $scope.axes = [ {}, {} ]; @@ -249,16 +248,14 @@ define( } }, isZoomed: function () { - return panZoomStack.length > 1; + return panZoomStack.getDepth() > 1; }, stepBackPanZoom: function () { - if (panZoomStack.length > 1) { - panZoomStack.pop(); - updateDrawingBounds(); - } + panZoomStack.popPanZoom(); + updateDrawingBounds(); }, unzoom: function () { - panZoomStack = [panZoomStack[0]]; + panZoomStack.clearPanZoom(); updateDrawingBounds(); } diff --git a/platform/features/plot/src/PlotPanZoomStack.js b/platform/features/plot/src/PlotPanZoomStack.js new file mode 100644 index 0000000000..70b604bb95 --- /dev/null +++ b/platform/features/plot/src/PlotPanZoomStack.js @@ -0,0 +1,59 @@ +/*global define*/ + +define( + [], + function () { + "use strict"; + + function PlotPanZoomStack(origin, dimensions) { + var stack = [{ origin: origin, dimensions: dimensions }]; + + function getDepth() { + return stack.length; + } + + function pushPanZoom(origin, dimensions) { + stack.push({ origin: origin, dimensions: dimensions }); + } + + function popPanZoom() { + if (stack.length > 1) { + stack.pop(); + } + } + + function clearPanZoom() { + stack = [stack[0]]; + } + + function setBasePanZoom(origin, dimensions) { + stack[0] = { origin: origin, dimensions: dimensions }; + } + + function getPanZoom() { + return stack[stack.length - 1]; + } + + function getOrigin() { + return getPanZoom().origin; + } + + function getDimensions() { + return getPanZoom().dimensions; + } + + return { + getDepth: getDepth, + pushPanZoom: pushPanZoom, + popPanZoom: popPanZoom, + setBasePanZoom: setBasePanZoom, + clearPanZoom: clearPanZoom, + getPanZoom: getPanZoom, + getOrigin: getOrigin, + getDimensions: getDimensions + }; + } + + return PlotPanZoomStack; + } +); \ No newline at end of file