Skip to content

Commit 99ee760

Browse files
committed
Enable option zoom on mobile device
1 parent 7c8ac86 commit 99ee760

File tree

3 files changed

+74
-21
lines changed

3 files changed

+74
-21
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "orgchart",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"homepage": "https://github.com/dabeng/OrgChart",
55
"authors": [
66
"dabeng <dabeng413@gmail.com>"

dist/js/jquery.orgchart.js

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@
139139

140140
if (opts.pan) {
141141
$chartContainer.css('overflow', 'hidden');
142-
$chart.on('mousedown',function(e){
142+
$chart.on('mousedown touchstart',function(e){
143143
var $this = $(this);
144-
if ($(e.target).closest('.node').length) {
144+
if ($(e.target).closest('.node').length || (e.touches && e.touches.length > 1)) {
145145
$this.data('panning', false);
146146
return;
147147
} else {
@@ -160,12 +160,32 @@
160160
lastY = parseInt(temp[13]);
161161
}
162162
}
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+
}
169189
var lastTf = $this.css('transform');
170190
if (lastTf === 'none') {
171191
if (lastTf.indexOf('3d') === -1) {
@@ -186,26 +206,41 @@
186206
}
187207
});
188208
});
189-
$(document).on('mouseup',function() {
209+
$(document).on('mouseup touchend',function(e) {
190210
if ($chart.data('panning')) {
191-
$chart.css('cursor', 'default');
192-
$(this).off('mousemove');
211+
$chart.data('panning', false).css('cursor', 'default').off('mousemove');
193212
}
194213
});
195214
}
196215

197216
if (opts.zoom) {
198217
$chartContainer.on('wheel', function(event) {
199218
event.preventDefault();
200-
var lastTf = $chart.css('transform');
201219
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);
209244
}
210245
}
211246
});
@@ -214,6 +249,24 @@
214249
return $chartContainer;
215250
};
216251

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+
217270
function buildJsonDS($li) {
218271
var subObj = {
219272
'name': $li.contents().eq(0).text().trim(),

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "orgchart",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "Simple and direct organization chart(tree-like hierarchy) plugin based on pure DOM and jQuery.",
55
"main": "./dist/js/jquery.orgchart.js",
66
"style": [

0 commit comments

Comments
 (0)