|
139 | 139 |
|
140 | 140 | if (opts.pan) { |
141 | 141 | $chartContainer.css('overflow', 'hidden'); |
142 | | - $chart.on('mousedown',function(e){ |
| 142 | + $chart.on('mousedown touchstart',function(e){ |
143 | 143 | var $this = $(this); |
144 | | - if ($(e.target).closest('.node').length) { |
| 144 | + if ($(e.target).closest('.node').length || (e.touches && e.touches.length > 1)) { |
145 | 145 | $this.data('panning', false); |
146 | 146 | return; |
147 | 147 | } else { |
|
160 | 160 | lastY = parseInt(temp[13]); |
161 | 161 | } |
162 | 162 | } |
163 | | - var startX = e.pageX - lastX; |
164 | | - var startY = e.pageY - lastY; |
165 | | - |
166 | | - $(document).on('mousemove',function(ev) { |
167 | | - var newX = ev.pageX - startX; |
168 | | - var newY = ev.pageY - startY; |
| 163 | + var startX = 0; |
| 164 | + var startY = 0; |
| 165 | + if (!e.targetTouches) { // pand on desktop |
| 166 | + startX = e.pageX - lastX; |
| 167 | + startY = e.pageY - lastY; |
| 168 | + } else if (e.targetTouches.length === 1) { // pan on mobile device |
| 169 | + startX = e.targetTouches[0].pageX - lastX; |
| 170 | + startY = e.targetTouches[0].pageY - lastY; |
| 171 | + } else if (e.targetTouches.length > 1) { |
| 172 | + return; |
| 173 | + } |
| 174 | + $chart.on('mousemove touchmove',function(e) { |
| 175 | + if (!$this.data('panning')) { |
| 176 | + return; |
| 177 | + } |
| 178 | + var newX = 0; |
| 179 | + var newY = 0; |
| 180 | + if (!e.targetTouches) { // pand on desktop |
| 181 | + newX = e.pageX - startX; |
| 182 | + newY = e.pageY - startY; |
| 183 | + } else if (e.targetTouches.length === 1) { // pan on mobile device |
| 184 | + newX = e.targetTouches[0].pageX - startX; |
| 185 | + newY = e.targetTouches[0].pageY - startY; |
| 186 | + } else if (e.targetTouches.length > 1) { |
| 187 | + return; |
| 188 | + } |
169 | 189 | var lastTf = $this.css('transform'); |
170 | 190 | if (lastTf === 'none') { |
171 | 191 | if (lastTf.indexOf('3d') === -1) { |
|
186 | 206 | } |
187 | 207 | }); |
188 | 208 | }); |
189 | | - $(document).on('mouseup',function() { |
| 209 | + $(document).on('mouseup touchend',function(e) { |
190 | 210 | if ($chart.data('panning')) { |
191 | | - $chart.css('cursor', 'default'); |
192 | | - $(this).off('mousemove'); |
| 211 | + $chart.data('panning', false).css('cursor', 'default').off('mousemove'); |
193 | 212 | } |
194 | 213 | }); |
195 | 214 | } |
196 | 215 |
|
197 | 216 | if (opts.zoom) { |
198 | 217 | $chartContainer.on('wheel', function(event) { |
199 | 218 | event.preventDefault(); |
200 | | - var lastTf = $chart.css('transform'); |
201 | 219 | var newScale = 1 + (event.originalEvent.deltaY > 0 ? -0.2 : 0.2); |
202 | | - if (lastTf === 'none') { |
203 | | - $chart.css('transform', 'scale(' + newScale + ',' + newScale + ')'); |
204 | | - } else { |
205 | | - if (lastTf.indexOf('3d') === -1) { |
206 | | - $chart.css('transform', lastTf + ' scale(' + newScale + ',' + newScale + ')'); |
207 | | - } else { |
208 | | - $chart.css('transform', lastTf + ' scale3d(' + newScale + ',' + newScale + ', 1)'); |
| 220 | + setChartScale($chart, newScale); |
| 221 | + }); |
| 222 | + |
| 223 | + $chartContainer.on('touchstart',function(e){ |
| 224 | + if(e.touches && e.touches.length === 2) { |
| 225 | + $chart.data('pinching', true); |
| 226 | + var dist = getPinchDist(e); |
| 227 | + $chart.data('pinchDistStart', dist); |
| 228 | + } |
| 229 | + }); |
| 230 | + $(document).on('touchmove',function(e) { |
| 231 | + if($chart.data('pinching')) { |
| 232 | + var dist = getPinchDist(e); |
| 233 | + $chart.data('pinchDistEnd', dist); |
| 234 | + } |
| 235 | + }) |
| 236 | + .on('touchend',function(e) { |
| 237 | + if($chart.data('pinching')) { |
| 238 | + $chart.data('pinching', false); |
| 239 | + var diff = $chart.data('pinchDistEnd') - $chart.data('pinchDistStart'); |
| 240 | + if (diff > 0) { |
| 241 | + setChartScale($chart, 1.2); |
| 242 | + } else if (diff < 0) { |
| 243 | + setChartScale($chart, 0.8); |
209 | 244 | } |
210 | 245 | } |
211 | 246 | }); |
|
214 | 249 | return $chartContainer; |
215 | 250 | }; |
216 | 251 |
|
| 252 | + function getPinchDist(e) { |
| 253 | + return Math.sqrt((e.touches[0].clientX - e.touches[1].clientX) * (e.touches[0].clientX - e.touches[1].clientX) + |
| 254 | + (e.touches[0].clientY - e.touches[1].clientY) * (e.touches[0].clientY - e.touches[1].clientY)); |
| 255 | + } |
| 256 | + |
| 257 | + function setChartScale($chart, newScale) { |
| 258 | + var lastTf = $chart.css('transform'); |
| 259 | + if (lastTf === 'none') { |
| 260 | + $chart.css('transform', 'scale(' + newScale + ',' + newScale + ')'); |
| 261 | + } else { |
| 262 | + if (lastTf.indexOf('3d') === -1) { |
| 263 | + $chart.css('transform', lastTf + ' scale(' + newScale + ',' + newScale + ')'); |
| 264 | + } else { |
| 265 | + $chart.css('transform', lastTf + ' scale3d(' + newScale + ',' + newScale + ', 1)'); |
| 266 | + } |
| 267 | + } |
| 268 | + } |
| 269 | + |
217 | 270 | function buildJsonDS($li) { |
218 | 271 | var subObj = { |
219 | 272 | 'name': $li.contents().eq(0).text().trim(), |
|
0 commit comments