From 01c9adc5bf551da61157886a683bf8b37b07d1a3 Mon Sep 17 00:00:00 2001 From: Jake Swenson Date: Wed, 6 Nov 2013 22:19:52 -0800 Subject: [PATCH] fix issues with transformation parsing and issues with touch items when touch is applied to a parent element --- src/mouse.js | 58 ++++++++++++++++++++++++++++++---------------------- src/svg.js | 2 +- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/mouse.js b/src/mouse.js index a52cff5..eeadd98 100644 --- a/src/mouse.js +++ b/src/mouse.js @@ -45,27 +45,37 @@ Snap.plugin(function (Snap, Element, Paper, glob) { if (glob.doc.addEventListener) { return function (obj, type, fn, element) { var realName = supportsTouch && touchMap[type] ? touchMap[type] : type, - f = function (e) { + f = function (e) { var scrollY = getScroll("y"), - scrollX = getScroll("x"), - x = e.clientX + scrollX, - y = e.clientY + scrollY; - if (supportsTouch && touchMap[has](type)) { - for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) { - if (e.targetTouches[i].target == obj) { - var olde = e; - e = e.targetTouches[i]; - e.originalEvent = olde; - e.preventDefault = preventTouch; - e.stopPropagation = stopTouch; - break; - } + scrollX = getScroll("x"); + if (supportsTouch && touchMap[has](type)) { + for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) { + if (e.targetTouches[i].target == obj || obj.contains(e.targetTouches[i].target)) { + var olde = e; + e = e.targetTouches[i]; + e.originalEvent = olde; + e.preventDefault = preventTouch; + e.stopPropagation = stopTouch; + break; + } + } } - } - return fn.call(element, e, x, y); - }; + var x = e.clientX + scrollX, + y = e.clientY + scrollY; + return fn.call(element, e, x, y); + }; + + if (type !== realName) { + obj.addEventListener(type, f, false); + } + obj.addEventListener(realName, f, false); + return function () { + if (type !== realName) { + obj.removeEventListener(type, f, false); + } + obj.removeEventListener(realName, f, false); return true; }; @@ -102,11 +112,11 @@ Snap.plugin(function (Snap, Element, Paper, glob) { while (j--) { dragi = drag[j]; if (supportsTouch) { - var i = e.touches.length, + var i = e.touches && e.touches.length, touch; while (i--) { touch = e.touches[i]; - if (touch.identifier == dragi.el._drag.id) { + if (touch.identifier == dragi.el._drag.id || dragi.el.node.contains(touch.target)) { x = touch.clientX; y = touch.clientY; (e.originalEvent ? e.originalEvent : e).preventDefault(); @@ -430,19 +440,17 @@ Snap.plugin(function (Snap, Element, Paper, glob) { origTransform = this.transform().local; }); } - function start(e) { + function start(e, x, y) { (e.originalEvent || e).preventDefault(); - var scrollY = getScroll("y"), - scrollX = getScroll("x"); - this._drag.x = e.clientX + scrollX; - this._drag.y = e.clientY + scrollY; + this._drag.x = x; + this._drag.y = y; this._drag.id = e.identifier; !drag.length && Snap.mousemove(dragMove).mouseup(dragUp); drag.push({el: this, move_scope: move_scope, start_scope: start_scope, end_scope: end_scope}); onstart && eve.on("snap.drag.start." + this.id, onstart); onmove && eve.on("snap.drag.move." + this.id, onmove); onend && eve.on("snap.drag.end." + this.id, onend); - eve("snap.drag.start." + this.id, start_scope || move_scope || this, e.clientX + scrollX, e.clientY + scrollY, e); + eve("snap.drag.start." + this.id, start_scope || move_scope || this, x, y, e); } this._drag = {}; draggable.push({el: this, start: start}); diff --git a/src/svg.js b/src/svg.js index eff2c56..c648aa9 100644 --- a/src/svg.js +++ b/src/svg.js @@ -1124,7 +1124,7 @@ var parseTransformString = Snap.parseTransformString = function (TString) { function svgTransform2string(tstr) { var res = []; tstr = tstr.replace(/(?:^|\s)(\w+)\(([^)]+)\)/g, function (all, name, params) { - params = params.split(/\s*,\s*/); + params = params.split(/\s*,\s*|\s+/); if (name == "rotate" && params.length == 1) { params.push(0, 0); }