1- // j2sApplet.js BH = Bob Hanson hansonr@stolaf.edu
1+ // j2sApplet.js BH = Bob Hanson hansonr@stolaf.edu
22
33// J2S._version set to "3.2.4.07" 2019.01.04; 2019.02.06
44
5+ // BH 2019.09.13 fixes touchend canceling click
56// BH 2019.08.29 fixes mouseupoutjsmol not firing MouseEvent.MOUSE_UP
67// BH 5/16/2019 fixes POST method for OuputStream
78// BH 2/6/2019 adds check for non-DOM event handler in getXY
@@ -1532,41 +1533,111 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
15321533 } ) ;
15331534 }
15341535
1535- J2S . setMouse = function ( who , isSwingJS ) {
1536- // swingjs.api.J2SInterface
1536+ // set to ignore touches if a mouse is found. Will break gestures on touch-screen laptops, but
1537+ // it enables click in touch-only devices. What a pain!
1538+
1539+ J2S . _haveMouse ;
1540+ J2S . _firstTouch ; // three-position switch: undefined, true, false
15371541
1542+ J2S . $bind ( 'body' , 'mousedown mousemove mouseup' , function ( ev ) {
1543+ J2S . _haveMouse = true ;
1544+ } ) ;
1545+
1546+ J2S . $bind ( 'body' , 'mouseup touchend' , function ( ev ) {
1547+ mouseup ( null , ev ) ;
1548+ return true ;
1549+ } ) ;
15381550
1539- var checkStopPropagation = function ( ev , ui , handled , target ) {
1540- if ( ui && ui . checkStopPropagation$O$Z ) {
1541- handled = ui . checkStopPropagation$O$Z ( ev , handled ) ;
1542- } else if ( ! ui || ! handled || ! ev . target . getAttribute ( "role" ) ) {
1543- if ( ! target || ! target . ui . buttonListener ) {
1544- ev . preventDefault ( ) ;
1545- ev . stopPropagation ( ) ;
1546- }
1551+ var checkStopPropagation = function ( ev , ui , handled , target ) {
1552+ if ( ui && ui . checkStopPropagation$O$Z ) {
1553+ handled = ui . checkStopPropagation$O$Z ( ev , handled ) ;
1554+ } else if ( ! ui || ! handled || ! ev . target . getAttribute ( "role" ) ) {
1555+ if ( ! target || ! target . ui . buttonListener ) {
1556+ ev . preventDefault ( ) ;
1557+ ev . stopPropagation ( ) ;
15471558 }
1548- // handled -- we are done here
1549- return handled ;
1550- } ;
1559+ }
1560+ // handled -- we are done here
1561+ return handled ;
1562+ } ;
15511563
1564+ var mouseup = function ( who , ev ) {
1565+ if ( J2S . _traceMouse )
1566+ J2S . traceMouse ( who , "UP" , ev ) ;
15521567
1553- J2S . traceMouse = function ( what , ev ) {
1554- System . out . println ( [ "tracemouse:" + what
1555- , "type:" , ev . type , ev . pageX , ev . pageY
1556- , "target.id:" , ev . target . id
1557- , "\n relatedtarget.id:" , ( ev . originalEvent . relatedTarget && ev . originalEvent . relatedTarget . id )
1558- , "\n who:" , who . id
1559- , "\n dragging:" , ( J2S . _mouseOwner && J2S . _mouseOwner . isDragging )
1560- , "doignore:" , doIgnore ( ev , 1 )
1561- , "role:" , ev . target . getAttribute && ev . target . getAttribute ( "role" )
1562- , "data-ui:" , ev . target [ "data-ui" ]
1563- , "data-component:" , ev . target [ "data-component" ]
1564- , "mouseOwner:" , J2S . _mouseOwner && J2S . _mouseOwner . id
1565- ] . join ( ) . replace ( ":," , ":" ) ) ;
1568+ // If we have a touchend, ignore it if we have found a mouse or it is a first touch,
1569+ // and set J2S.firstTouch false:
1570+
1571+ if ( ev . type == "touchend" ) {
1572+ if ( J2S . _haveMouse ) return ;
1573+ if ( J2S . _firstTouch ) {
1574+ J2S . _firstTouch = false ;
1575+ return ;
1576+ }
15661577 }
15671578
1568- J2S . $bind ( who , 'mousemove touchmove' , function ( ev ) { // touchmove
1579+ if ( doIgnore ( ev ) )
1580+ return true ;
1581+
1582+ if ( J2S . _mouseOwner )
1583+ who = J2S . _mouseOwner ;
1584+
1585+ // if (ev.target.getAttribute("role")) { // JSButtonUI adds
1586+ // // role=menucloser to icon
1587+ // // and text
1588+ // var m = (ev.target._menu || ev.target.parentElement._menu);
1589+ // m && m._hideJSMenu();
1590+ // }
1591+
1592+ J2S . setMouseOwner ( null ) ;
1593+
1594+ if ( ! who )
1595+ return true ;
1596+
1597+ var ui = ev . target [ "data-ui" ] ; // e.g., a textbox
1598+ var target = ev . target [ "data-component" ] ; // e.g., a button
1599+ var handled = ( ui && ui . handleJSEvent$O$I$O ( who , 502 , ev ) ) ;
1600+ if ( checkStopPropagation ( ev , ui , handled ) )
1601+ return true ;
1602+
1603+ who . isDragging = false ;
1604+
1605+ if ( ev . type != "touchend" || ! J2S . _gestureUpdate ( who , ev ) ) {
1606+ var xym = getXY ( who , ev , 502 ) ;
1607+ if ( xym )
1608+ who . applet . _processEvent ( 502 , xym , ev , who . _frameViewer ) ; // MouseEvent.MOUSE_RELEASED
1609+ }
1610+
1611+ return ! ! ( ui || target ) ;
1612+ }
1613+
1614+ J2S . traceMouse = function ( who , what , ev ) {
1615+ System . out . println ( [ "tracemouse:" + what
1616+ , "type:" , ev . type , ev . pageX , ev . pageY
1617+ , "target.id:" , ev . target . id
1618+ , "\n relatedtarget.id:" , ( ev . originalEvent . relatedTarget && ev . originalEvent . relatedTarget . id )
1619+ , "\n who:" , who . id
1620+ , "\n dragging:" , ( J2S . _mouseOwner && J2S . _mouseOwner . isDragging )
1621+ , "doignore:" , doIgnore ( ev , 1 )
1622+ , "role:" , ev . target . getAttribute && ev . target . getAttribute ( "role" )
1623+ , "data-ui:" , ev . target [ "data-ui" ]
1624+ , "data-component:" , ev . target [ "data-component" ]
1625+ , "mouseOwner:" , J2S . _mouseOwner && J2S . _mouseOwner . id
1626+ ] . join ( ) . replace ( ":," , ":" ) ) ;
1627+ }
1628+
1629+ J2S . setMouse = function ( who , isSwingJS ) {
1630+ // swingjs.api.J2SInterface
1631+
1632+
1633+ J2S . $bind ( who , ( J2S . _haveMouse ? 'mousemove' : 'mousemove touchmove' ) , function ( ev ) {
1634+
1635+ // ignore touchmove if J2S._haveMouse
15691636
1637+ if ( ev . type == "touchmove" &&
1638+ ( J2S . _firstTouch || J2S . _haveMouse ) ) {
1639+ return ;
1640+ }
15701641
15711642 if ( J2S . _dmouseOwner ) {
15721643 if ( J2S . _dmouseDrag )
@@ -1576,7 +1647,7 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
15761647 }
15771648
15781649 if ( J2S . _traceMouseMove )
1579- J2S . traceMouse ( "MOVE" , ev ) ;
1650+ J2S . traceMouse ( who , "MOVE" , ev ) ;
15801651
15811652 if ( doIgnore ( ev ) )
15821653 return true ;
@@ -1599,7 +1670,7 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
15991670
16001671 J2S . $bind ( who , 'click' , function ( ev ) {
16011672 if ( J2S . _traceMouse )
1602- J2S . traceMouse ( "CLICK" , ev ) ;
1673+ J2S . traceMouse ( who , "CLICK" , ev ) ;
16031674
16041675 if ( doIgnore ( ev ) )
16051676 return true ;
@@ -1622,7 +1693,7 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
16221693 // return true;
16231694
16241695 if ( J2S . _traceMouse )
1625- J2S . traceMouse ( "SCROLL" , ev ) ;
1696+ J2S . traceMouse ( who , "SCROLL" , ev ) ;
16261697
16271698 if ( ev . target . getAttribute ( "role" ) ) {
16281699 return true ;
@@ -1647,10 +1718,23 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
16471718 return ! ! ( ui || target ) ;
16481719 } ) ;
16491720
1650- J2S . $bind ( who , 'mousedown touchstart' , function ( ev ) {
1651-
1721+ J2S . $bind ( who , ( J2S . _haveMouse ? 'mousedown' : 'mousedown touchstart' ) , function ( ev ) {
16521722 if ( J2S . _traceMouse )
1653- J2S . traceMouse ( "DOWN" , ev ) ;
1723+ J2S . traceMouse ( who , "DOWN" , ev ) ;
1724+
1725+ // If we have a mousedown on the applet, then disable touch;
1726+ // otherwise, if J2S._firstTouch is undefined (!!x != x), set J2S._firstTouch
1727+ // and ignore future touch events (through the first touchend):
1728+
1729+ if ( ev . type == "mousedown" ) {
1730+ J2S . _haveMouse = true ;
1731+ } else {
1732+ if ( J2S . _haveMouse ) return ;
1733+ if ( ! ! J2S . _firstTouch != J2S . _firstTouch ) {
1734+ J2S . _firstTouch = true ;
1735+ return ;
1736+ }
1737+ }
16541738
16551739 lastDragx = lastDragy = 99999 ;
16561740
@@ -1681,57 +1765,13 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
16811765// return !!target || ui && ui.j2sDoPropagate;
16821766 } ) ;
16831767
1684- J2S . $bind ( who , 'mouseup touchend' , function ( ev ) {
1768+ J2S . $bind ( who , ( J2S . _haveMouse ? 'mouseup' : 'mouseup touchend') , function ( ev ) {
16851769 return mouseup ( who , ev ) ;
16861770 } ) ;
16871771
1688- J2S . $bind ( 'body' , 'mouseup touchend' , function ( ev ) {
1689- mouseup ( null , ev ) ;
1690- return true ;
1691- } ) ;
1692-
1693- var mouseup = function ( who , ev ) {
1694- if ( J2S . _traceMouse )
1695- J2S . traceMouse ( "UP" , ev ) ;
1696-
1697- if ( doIgnore ( ev ) )
1698- return true ;
1699-
1700- if ( J2S . _mouseOwner )
1701- who = J2S . _mouseOwner ;
1702-
1703- // if (ev.target.getAttribute("role")) { // JSButtonUI adds
1704- // // role=menucloser to icon
1705- // // and text
1706- // var m = (ev.target._menu || ev.target.parentElement._menu);
1707- // m && m._hideJSMenu();
1708- // }
1709-
1710- J2S . setMouseOwner ( null ) ;
1711-
1712- if ( ! who )
1713- return true ;
1714-
1715- var ui = ev . target [ "data-ui" ] ; // e.g., a textbox
1716- var target = ev . target [ "data-component" ] ; // e.g., a button
1717- var handled = ( ui && ui . handleJSEvent$O$I$O ( who , 502 , ev ) ) ;
1718- if ( checkStopPropagation ( ev , ui , handled ) )
1719- return true ;
1720-
1721- who . isDragging = false ;
1722-
1723- if ( ev . type != "touchend" || ! J2S . _gestureUpdate ( who , ev ) ) {
1724- var xym = getXY ( who , ev , 502 ) ;
1725- if ( xym )
1726- who . applet . _processEvent ( 502 , xym , ev , who . _frameViewer ) ; // MouseEvent.MOUSE_RELEASED
1727- }
1728-
1729- return ! ! ( ui || target ) ;
1730- }
1731-
17321772 J2S . $bind ( who , 'mouseenter' , function ( ev ) {
17331773 if ( J2S . _traceMouse )
1734- J2S . traceMouse ( "ENTER" , ev ) ;
1774+ J2S . traceMouse ( who , "ENTER" , ev ) ;
17351775
17361776 if ( doIgnore ( ev ) )
17371777 return true ;
@@ -1752,7 +1792,7 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
17521792
17531793 J2S . $bind ( who , 'mouseleave' , function ( ev ) {
17541794 if ( J2S . _traceMouse )
1755- J2S . traceMouse ( "OUT" , ev ) ;
1795+ J2S . traceMouse ( who , "OUT" , ev ) ;
17561796
17571797 if ( doIgnore ( ev ) )
17581798 return true ;
@@ -1784,7 +1824,7 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
17841824 return ;
17851825
17861826 if ( J2S . _traceMouse )
1787- J2S . traceMouse ( "OUTJSMOL" , ev ) ;
1827+ J2S . traceMouse ( who , "OUTJSMOL" , ev ) ;
17881828
17891829 return J2S . _drag ( who , ev , 503 ) ;
17901830 } ) ;
@@ -1794,7 +1834,7 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
17941834 if ( ! who . isDragging || who != J2S . _mouseOwner )
17951835 return true ;
17961836 if ( J2S . _traceMouse )
1797- J2S . traceMouse ( "UPJSMOL" , ev ) ;
1837+ J2S . traceMouse ( who , "UPJSMOL" , ev ) ;
17981838
17991839 return J2S . _drag ( who , ev , 502 ) ;
18001840 } ) ;
@@ -1947,8 +1987,6 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
19471987 }
19481988
19491989 J2S . _gestureUpdate = function ( who , ev ) {
1950- ev . stopPropagation ( ) ;
1951- ev . preventDefault ( ) ;
19521990 var oe = ev . originalEvent ;
19531991 switch ( ev . type ) {
19541992 case "touchstart" :
@@ -1981,6 +2019,8 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
19812019 who . applet . _processGesture ( who . _touches , who . _frameViewer ) ;
19822020 break ;
19832021 }
2022+ ev . stopPropagation ( ) ;
2023+ ev . preventDefault ( ) ;
19842024 return true ;
19852025 }
19862026
0 commit comments