Advertisement
Guest User

Untitled

a guest
May 27th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. import m from 'mithril'
  2.  
  3. export default {
  4. tests : [
  5. [ 'offsetTop', el => el.scrollTop > 0,
  6. [ 'offsetRight', el => el.scrollWidth - ( el.scrollLeft + el.offsetWidth ) > 2,
  7. [ 'offsetBottom', el => el.scrollHeight - ( el.scrollTop + el.offsetHeight ) > 2,
  8. [ 'offsetLeft', el => el.scrollLeft > 0
  9. ],
  10. controller( { style } ){
  11. let child = m.prop( document.createElement( 'x' ) )
  12. let apply = () => {
  13. for( let [ className, test ] of tests )
  14. if( test( el ) )
  15. this.parentAttrs.className += ' ' + className
  16. }
  17.  
  18. this.parentAttrs = {
  19. style : Object.assign( {
  20. overflow : 'scroll'
  21. }, style )
  22. }
  23.  
  24. this.childAttrs = {
  25. config : ( el, init, ctxt ) => {
  26. if( !init ){
  27. child( el )
  28. apply()
  29. }
  30. },
  31. onscroll : () => {
  32. window.cancelAnimationFrame( pending )
  33.  
  34. pending = window.requestAnimationFrame( apply )
  35. }
  36. }
  37. },
  38. view : ( { parentAttrs, childAttrs }, { contents } ) =>
  39. m( '.scrollBox',
  40. Object.assign( parentAttrs, { style } ),
  41. Object.assign( contents, { attrs : childAttrs } )
  42. )
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement