@@ -1381,27 +1381,60 @@ VirtualSky.prototype.createSky = function(){
1381
1381
ctx . fillText ( loading , ( ctx . wide - ctx . measureText ( loading ) . width ) / 2 , ( this . tall - fs ) / 2 ) ;
1382
1382
ctx . fill ( ) ;
1383
1383
1384
- var contextMenuHandler = ( ! this . callback . contextmenu ) ? undefined : {
1385
- longPressStart : function ( x , y ) {
1386
- contextMenuHandler . longPressStop ( ) ;
1387
- this . longPressTimer = window . setTimeout ( function ( ) {
1388
- this . longPressTimer = undefined ;
1389
- this . dragging = false ;
1390
- this . x = "" ;
1391
- this . y = "" ;
1392
- this . theta = "" ;
1393
- contextMenuHandler . click ( x , y ) ;
1394
- } , 400 /** 400ms for long press */ ) ;
1384
+ var touchClickHandler = ( ! this . callback . contextmenu && ! this . callback . click ) ? undefined : {
1385
+ // Indicate an immobile press is occuring. It will lead to context menu or click depending on its duration
1386
+ clickActive : false ,
1387
+ clickDone : false ,
1388
+ // Timer that differentiate between long/short press
1389
+ longPressTimer : undefined ,
1390
+
1391
+ clickStart : function ( e ) {
1392
+ e . originalEvent . preventDefault ( ) ;
1393
+ touchClickHandler . clickCancel ( ) ;
1394
+ touchClickHandler . clickActive = true ;
1395
+ touchClickHandler . clickDone = false ;
1396
+ touchClickHandler . initialTouchEvent = e ;
1397
+ if ( this . callback . contextmenu ) {
1398
+ touchClickHandler . longPressTimer = window . setTimeout ( function ( ) {
1399
+ touchClickHandler . clickActive = false ;
1400
+ touchClickHandler . longPressTimer = undefined ;
1401
+ this . dragging = false ;
1402
+ this . x = "" ;
1403
+ this . y = "" ;
1404
+ this . theta = "" ;
1405
+
1406
+ if ( this . callback . contextmenu ) {
1407
+ touchClickHandler . clickDone = true ;
1408
+ this . callback . contextmenu . call ( e . data . sky , e ) ;
1409
+ }
1410
+ } . bind ( this ) , 400 /** 400ms for long press */ ) ;
1411
+ }
1412
+ } . bind ( this ) ,
1413
+
1414
+ clickEnd : function ( e ) {
1415
+ if ( touchClickHandler . clickActive ) {
1416
+ var initialTouchEvent = touchClickHandler . initialTouchEvent ;
1417
+ touchClickHandler . clickCancel ( ) ;
1418
+
1419
+ if ( e . data . sky . callback . click ) {
1420
+ touchClickHandler . clickDone = true ;
1421
+ e . data . sky . callback . click . call ( initialTouchEvent . data . sky , initialTouchEvent ) ;
1422
+ }
1423
+ }
1395
1424
} . bind ( this ) ,
1396
- longPressStop : function ( ) {
1397
- if ( this . longPressTimer !== undefined ) {
1398
- window . clearTimeout ( this . longPressTimer ) ;
1399
- this . longPressTimer = undefined ;
1425
+
1426
+ clickCancel : function ( ) {
1427
+ touchClickHandler . clickActive = false ;
1428
+ touchClickHandler . initialTouchEvent = undefined ;
1429
+ if ( touchClickHandler . longPressTimer !== undefined ) {
1430
+ window . clearTimeout ( touchClickHandler . longPressTimer ) ;
1431
+ touchClickHandler . longPressTimer = undefined ;
1400
1432
}
1401
- } . bind ( this )
1433
+ return ! touchClickHandler . clickDone ;
1434
+ } . bind ( this ) ,
1402
1435
} ;
1403
1436
1404
- function getXYProperties ( e , sky ) {
1437
+ getXYProperties = function ( e , sky ) {
1405
1438
e . matched = sky . whichPointer ( e . x , e . y ) ;
1406
1439
var skyPos = sky . xy2radec ( e . x , e . y ) ;
1407
1440
if ( skyPos ) {
@@ -1415,6 +1448,11 @@ VirtualSky.prototype.createSky = function(){
1415
1448
e . y = o . pageY - el . offset ( ) . top - window . scrollY ;
1416
1449
return getXYProperties ( e , sky ) ;
1417
1450
}
1451
+ function getTouchXY ( sky , o , el , e ) {
1452
+ e . x = o . touches [ 0 ] . pageX - el . offset ( ) . left - window . scrollX ;
1453
+ e . y = o . touches [ 0 ] . pageY - el . offset ( ) . top - window . scrollY ;
1454
+ return getXYProperties ( e , sky ) ;
1455
+ }
1418
1456
1419
1457
S ( "#" + this . idinner ) . on ( 'click' , { sky :this } , function ( e ) {
1420
1458
e . data . sky . debug ( 'click' ) ;
@@ -1496,7 +1534,11 @@ VirtualSky.prototype.createSky = function(){
1496
1534
if ( typeof s . callback . mouseenter == "function" ) s . callback . mouseenter . call ( s ) ;
1497
1535
} ) . on ( 'touchmove' , { sky :this } , function ( e ) {
1498
1536
e . preventDefault ( ) ;
1499
- if ( contextMenuHandler ) contextMenuHandler . longPressStop ( ) ;
1537
+ if ( touchClickHandler ) {
1538
+ if ( ! touchClickHandler . clickCancel ( ) ) {
1539
+ return ;
1540
+ }
1541
+ }
1500
1542
var s = e . data . sky ;
1501
1543
var x = e . originalEvent . touches [ 0 ] . pageX ;
1502
1544
var y = e . originalEvent . touches [ 0 ] . pageY ;
@@ -1529,25 +1571,18 @@ VirtualSky.prototype.createSky = function(){
1529
1571
} ) . on ( 'touchstart' , { sky :this } , function ( e ) {
1530
1572
e . data . sky . debug ( 'touchstart' ) ;
1531
1573
e . data . sky . dragging = true ;
1532
- if ( contextMenuHandler ) {
1533
- var x = e . originalEvent . touches [ 0 ] . pageX ;
1534
- var y = e . originalEvent . touches [ 0 ] . pageY ;
1535
- x = x - this . offset ( ) . left - window . scrollX ;
1536
- y = y - this . offset ( ) . top - window . scrollY ;
1537
- contextMenuHandler . longPressStart ( x , y ) ;
1538
- if ( e . data . sky . callback . click ) {
1539
- e . x = x ;
1540
- e . y = y ;
1541
- e . data . sky . callback . click . call ( e . data . sky , getXYProperties ( e , e . data . sky ) ) ;
1542
- }
1574
+ if ( touchClickHandler ) {
1575
+ touchClickHandler . clickStart ( getTouchXY ( e . data . sky , e . originalEvent , this , e ) ) ;
1543
1576
}
1544
1577
} ) . on ( 'touchend' , { sky :this } , function ( e ) {
1545
1578
e . data . sky . debug ( 'touchend' ) ;
1546
1579
e . data . sky . dragging = false ;
1547
1580
e . data . sky . x = "" ;
1548
1581
e . data . sky . y = "" ;
1549
1582
e . data . sky . theta = "" ;
1550
- if ( contextMenuHandler ) contextMenuHandler . longPressStop ( ) ;
1583
+ if ( touchClickHandler ) {
1584
+ touchClickHandler . clickEnd ( e ) ;
1585
+ }
1551
1586
} ) . on ( ( isEventSupported ( 'mousewheel' ) ? 'mousewheel' : 'wheel' ) , { sky :this } , function ( e ) {
1552
1587
e . preventDefault ( ) ;
1553
1588
e . data . sky . debug ( 'mousewheel' ) ;
0 commit comments