Moka-Decorators.st 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. Smalltalk current createPackage: 'Moka-Decorators'!
  2. MKDecorator subclass: #MKScrollDecorator
  3. instanceVariableNames: 'verticalScrollbar horizontalScrollbar'
  4. package: 'Moka-Decorators'!
  5. !MKScrollDecorator methodsFor: 'accessing'!
  6. cssClass
  7. ^ super cssClass, ' mk_scroll'
  8. !
  9. scrollbarSize
  10. | domSize overflow |
  11. domSize := self domSize.
  12. overflow := self domOverflow.
  13. ^ ((domSize x / (overflow x + domSize x)) * 100) @ ((domSize y / (overflow y + domSize y) * 100))
  14. ! !
  15. !MKScrollDecorator methodsFor: 'defaults'!
  16. defaultControllerClass
  17. ^ MKScrollController
  18. ! !
  19. !MKScrollDecorator methodsFor: 'dom'!
  20. domDecoratedSize
  21. | element |
  22. element := self decorated asJQuery get: 0.
  23. ^ element scrollWidth @ element scrollHeight
  24. !
  25. domOverflow
  26. | element |
  27. element := self decorated asJQuery get: 0.
  28. ^ (element scrollWidth - element clientWidth) @ (element scrollHeight - element clientHeight)
  29. !
  30. domScrollPosition
  31. ^ horizontalScrollbar asJQuery position left @ verticalScrollbar asJQuery position top
  32. !
  33. domScrollbarSize
  34. ^ horizontalScrollbar asJQuery width @ verticalScrollbar asJQuery height
  35. ! !
  36. !MKScrollDecorator methodsFor: 'private'!
  37. setupScrollbars
  38. verticalScrollbar asJQuery draggable: #{
  39. 'containment' -> 'parent'.
  40. 'axis' -> 'y'.
  41. 'drag' -> [ :event | self controller onVerticalDrag: event ]
  42. }.
  43. horizontalScrollbar asJQuery draggable: #{
  44. 'containment' -> 'parent'.
  45. 'axis' -> 'x'.
  46. 'drag' -> [ :event | self controller onHorizontalDrag: event ]
  47. }.
  48. self updateScrollbars
  49. ! !
  50. !MKScrollDecorator methodsFor: 'rendering'!
  51. renderContentOn: html
  52. html div
  53. class: 'mk_scroll_container';
  54. with: [ super renderContentOn: html ].
  55. html div
  56. class: 'mk_scroll_rail vertical';
  57. with: [
  58. verticalScrollbar := html div
  59. class: 'mk_scrollbar';
  60. yourself ].
  61. html div
  62. class: 'mk_scroll_rail horizontal';
  63. with: [
  64. horizontalScrollbar := html div
  65. class: 'mk_scrollbar';
  66. yourself ].
  67. self setupScrollbars
  68. ! !
  69. !MKScrollDecorator methodsFor: 'testing'!
  70. hasHorizontalOverflow
  71. ^ self domOverflow x > 0
  72. !
  73. hasVerticalOverflow
  74. ^ self domOverflow y > 0
  75. ! !
  76. !MKScrollDecorator methodsFor: 'updating'!
  77. updateScrollbars
  78. | width height |
  79. width := self hasHorizontalOverflow
  80. ifTrue: [ self scrollbarSize x max: 10 ]
  81. ifFalse: [ 0 ].
  82. height := self hasVerticalOverflow
  83. ifTrue: [ self scrollbarSize y max: 10 ]
  84. ifFalse: [ 0 ].
  85. horizontalScrollbar asJQuery width: width asString, '%'.
  86. verticalScrollbar asJQuery height: height asString, '%'
  87. ! !