From 3c7033f92edd034d1ec1ed4cbbddab62974d95a8 Mon Sep 17 00:00:00 2001 From: jdotzki Date: Mon, 23 Jun 2014 17:16:13 +0200 Subject: [PATCH] feat(features/touch): add touch gesture support Allow to navigate i.e. scroll/zoom on the diagram via touch gestures. Closes #46 --- lib/Viewer.js | 7 +- lib/features/movecanvas/MoveCanvas.js | 24 +-- lib/features/touch/TouchInteraction.js | 172 ++++++++++++++++++ lib/features/touch/index.js | 4 + lib/features/zoomscroll/ZoomScroll.js | 1 + test/spec/browser/ViewerSpec.js | 1 + .../features/touch/TouchInteractionSpec.js | 37 ++++ .../features/zoomscoll/ZoomScrollSpec.js | 2 +- 8 files changed, 233 insertions(+), 15 deletions(-) create mode 100644 lib/features/touch/TouchInteraction.js create mode 100644 lib/features/touch/index.js create mode 100644 test/spec/browser/features/touch/TouchInteractionSpec.js diff --git a/lib/Viewer.js b/lib/Viewer.js index 1a5e222b..2c150ecf 100644 --- a/lib/Viewer.js +++ b/lib/Viewer.js @@ -10,11 +10,12 @@ var Importer = require('./import/Importer'), function getSvgContents(diagram) { - var paper = diagram.get('canvas').getPaper(); - var outerNode = paper.node.parentNode; + var outerNode = diagram.get('canvas').getContainer(); var svg = outerNode.innerHTML; - return svg.replace(/^]*>|<\/svg>$/g, ''); + return svg.replace(/^]*>|<\/svg>$/g, '') + .replace('Created with Snap', '') + .replace(/ 2) { + contextInit(); + } + + event.preventDefault(); + } + + $(element).on('touchstart', handleTouchStart); + $(element).on('touchmove', handleTouchMove); + $(document).on('touchend', handleTouchEnd); + } + + eventBus.on('canvas.init', function(e) { + init(e.paper.node); + }); +} + +TouchInteraction.$inject = ['eventBus', 'canvas']; + +module.exports = TouchInteraction; \ No newline at end of file diff --git a/lib/features/touch/index.js b/lib/features/touch/index.js new file mode 100644 index 00000000..86309fa9 --- /dev/null +++ b/lib/features/touch/index.js @@ -0,0 +1,4 @@ +module.exports = { + __init__: [ 'touchInteraction' ], + touchInteraction: [ 'type', require('./TouchInteraction') ] +}; \ No newline at end of file diff --git a/lib/features/zoomscroll/ZoomScroll.js b/lib/features/zoomscroll/ZoomScroll.js index d5bdb4fd..3f774706 100644 --- a/lib/features/zoomscroll/ZoomScroll.js +++ b/lib/features/zoomscroll/ZoomScroll.js @@ -51,6 +51,7 @@ function ZoomScroll(events, canvas) { canvas.scroll(delta); } else { + // zoom in relative to diagram {x,y} coordinates zoom(y, { x: event.offsetX, y: event.offsetY }); } diff --git a/test/spec/browser/ViewerSpec.js b/test/spec/browser/ViewerSpec.js index b7271eaf..1b912514 100644 --- a/test/spec/browser/ViewerSpec.js +++ b/test/spec/browser/ViewerSpec.js @@ -127,6 +127,7 @@ describe('viewer', function() { // expect header to be written only once expect(svg.indexOf('')).toBe(-1); + expect(svg.indexOf('