@@ -20,29 +20,43 @@ window.ripples = {
2020 rippleStartTime = 500 ;
2121
2222 // Helper to bind events on dynamically created elements
23- var bind = function ( event , selector , callback ) {
24- document . addEventListener ( event , function ( e ) {
25- var target = ( typeof e . detail !== "number" ) ? e . detail : e . target ;
26-
27- if ( matchesSelector ( target , selector ) ) {
28- callback ( e , target ) ;
29- }
23+ var bind = function ( events , selector , callback ) {
24+ if ( typeof events === "string" ) {
25+ events = [ events ] ;
26+ }
27+ events . forEach ( function ( event ) {
28+ document . addEventListener ( event , function ( e ) {
29+ var target = ( typeof e . detail !== "number" ) ? e . detail : e . target ;
30+
31+ if ( matchesSelector ( target , selector ) ) {
32+ callback ( e , target ) ;
33+ }
34+ } ) ;
3035 } ) ;
3136 } ;
3237
33- var rippleStart = function ( e , target ) {
38+ var rippleStart = function ( e , target , callback ) {
3439
3540 // Init variables
3641 var $rippleWrapper = target ,
3742 $el = $rippleWrapper . parentNode ,
3843 $ripple = document . createElement ( "div" ) ,
3944 elPos = $el . getBoundingClientRect ( ) ,
40- mousePos = { x : e . clientX - elPos . left , y : e . clientY - elPos . top } ,
45+ // Mouse pos in most cases
46+ mousePos = { x : e . clientX - elPos . left , y : ( ( window . ontouchstart ) ? e . clientY - window . scrollY : e . clientY ) - elPos . top } ,
4147 scale = "scale(" + Math . round ( $rippleWrapper . offsetWidth / 5 ) + ")" ,
4248 rippleEnd = new CustomEvent ( "rippleEnd" , { detail : $ripple } ) ,
4349 _rippleOpacity = 0.1 ,
4450 refreshElementStyle ;
4551
52+
53+ // If multitouch is detected or some other black magic suff is happening...
54+ if ( e . touches ) {
55+ mousePos = { x : e . touches [ 0 ] . clientX - elPos . left , y : e . touches [ 0 ] . clientY - elPos . top } ;
56+ }
57+
58+ console . log ( mousePos ) ;
59+
4660 $ripplecache = $ripple ;
4761
4862 // Set ripple class
@@ -88,6 +102,9 @@ window.ripples = {
88102 // Let know to other functions that this element has finished the animation
89103 $ripple . dataset . animating = 0 ;
90104 document . dispatchEvent ( rippleEnd ) ;
105+ if ( callback ) {
106+ callback ( ) ;
107+ }
91108
92109 } , rippleStartTime ) ;
93110
@@ -105,12 +122,12 @@ window.ripples = {
105122
106123 // Helper, need to know if mouse is up or down
107124 var mouseDown = false ;
108- document . body . onmousedown = function ( ) {
125+ bind ( [ "mousedown" , "touchstart" ] , "*" , function ( ) {
109126 mouseDown = true ;
110- } ;
111- document . body . onmouseup = function ( ) {
127+ } ) ;
128+ bind ( [ "mouseup" , "touchend" ] , "*" , function ( ) {
112129 mouseDown = false ;
113- } ;
130+ } ) ;
114131
115132 // Append ripple wrapper if not exists already
116133 var rippleInit = function ( e , target ) {
@@ -119,20 +136,25 @@ window.ripples = {
119136 var $rippleWrapper = document . createElement ( "div" ) ;
120137 $rippleWrapper . className = "ripple-wrapper" ;
121138 target . appendChild ( $rippleWrapper ) ;
139+ if ( window . ontouchstart === null ) {
140+ rippleStart ( e , $rippleWrapper , function ( ) {
141+ // FIXME: ugly fix for first touchstart event on mobile devices...
142+ $rippleWrapper . getElementsByClassName ( "ripple" ) [ 0 ] . remove ( ) ;
143+ } ) ;
144+ }
122145 }
123146 } ;
124147
125-
126148 var $ripplecache ;
127149
128150 // Events handler
129151 // init RippleJS and start ripple effect on mousedown
130- bind ( "mouseover" , withRipple , rippleInit ) ;
152+ bind ( [ "mouseover" , "touchstart" ] , withRipple , rippleInit ) ;
131153
132154 // start ripple effect on mousedown
133- bind ( "mousedown" , ".ripple-wrapper" , function ( e , $ripple ) {
134- // Start ripple only on left or middle mouse click
135- if ( e . which === 1 || e . which === 2 ) {
155+ bind ( [ "mousedown" , "touchstart" ] , ".ripple-wrapper" , function ( e , $ripple ) {
156+ // Start ripple only on left or middle mouse click and touch click
157+ if ( e . which === 0 || e . which === 1 || e . which === 2 ) {
136158 rippleStart ( e , $ripple ) ;
137159 }
138160 } ) ;
@@ -148,7 +170,7 @@ window.ripples = {
148170 } ) ;
149171
150172 // Destroy ripple when mouse is not holded anymore if the ripple still exists
151- bind ( "mouseup" , ".ripple-wrapper" , function ( ) {
173+ bind ( [ "mouseup" , "touchend" ] , ".ripple-wrapper" , function ( ) {
152174 var $ripple = $ripplecache ;
153175 if ( $ripple && $ripple . dataset . animating != 1 ) {
154176 rippleOut ( $ripple ) ;
0 commit comments