Moka-Decorators.st 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. Smalltalk createPackage: 'Moka-Decorators'!
  2. MKDecorator subclass: #MKDraggableDecorator
  3. instanceVariableNames: ''
  4. package: 'Moka-Decorators'!
  5. MKDecorator subclass: #MKDroppableDecorator
  6. instanceVariableNames: 'droppableOptions'
  7. package: 'Moka-Decorators'!
  8. !MKDroppableDecorator methodsFor: 'accessing'!
  9. droppableOptions
  10. ^ droppableOptions ifNil: [ self defaultDroppableOptions ]
  11. !
  12. droppableOptions: aHashedCollection
  13. droppableOptions := aHashedCollection
  14. ! !
  15. !MKDroppableDecorator methodsFor: 'defaults'!
  16. defaultDroppableOptions
  17. ^ { 'helper' -> 'clone' }
  18. ! !
  19. !MKDroppableDecorator methodsFor: 'rendering'!
  20. renderContentOn: html
  21. super renderContentOn: html.
  22. self decorated asJQuery droppable: self droppableOptions
  23. ! !
  24. MKDecorator subclass: #MKModalDecorator
  25. instanceVariableNames: 'overlay closeOnEnter closeOnClick'
  26. package: 'Moka-Decorators'!
  27. !MKModalDecorator commentStamp!
  28. I render my `decorated` view as a modal pane.!
  29. !MKModalDecorator methodsFor: 'accessing'!
  30. closeOnClick
  31. ^ closeOnClick ifNil: [ false ]
  32. !
  33. closeOnClick: aBoolean
  34. closeOnClick := aBoolean
  35. !
  36. closeOnEnter
  37. ^ closeOnEnter ifNil: [ false ]
  38. !
  39. closeOnEnter: aBoolean
  40. closeOnEnter := aBoolean
  41. !
  42. cssClass
  43. ^ super cssClass, ' mk_modal'
  44. !
  45. overlay
  46. ^ overlay ifNil: [ overlay := MKOverlayView childView: self ]
  47. !
  48. zindex
  49. ^ 1001
  50. ! !
  51. !MKModalDecorator methodsFor: 'defaults'!
  52. defaultControllerClass
  53. ^ MKModalController
  54. !
  55. defaultLayout
  56. ^ super defaultLayout
  57. centerY: 0;
  58. centerX: 0;"
  59. width: 300;
  60. height: 200;"
  61. yourself
  62. ! !
  63. !MKModalDecorator methodsFor: 'rendering'!
  64. renderOn: html
  65. super renderOn: html.
  66. root at: 'tabindex' put: '0'.
  67. root asJQuery focus.
  68. html with: self overlay
  69. ! !
  70. MKDecorator subclass: #MKScrollDecorator
  71. instanceVariableNames: 'verticalScrollbar horizontalScrollbar'
  72. package: 'Moka-Decorators'!
  73. !MKScrollDecorator commentStamp!
  74. I decorate a view adding scrollbars around it.
  75. The `decorated` view can send `MKViewScrolled` announcement to update the scrollbars position.!
  76. !MKScrollDecorator methodsFor: 'accessing'!
  77. cssClass
  78. ^ super cssClass, ' mk_scroll'
  79. !
  80. horizontalScrollbar
  81. ^ horizontalScrollbar
  82. !
  83. scrollPercent
  84. | element |
  85. element := self decorated asJQuery get: 0.
  86. ^ (element scrollLeft / element scrollWidth) @ (element scrollTop / element scrollHeight)
  87. !
  88. scrollbarPosition
  89. | position |
  90. position := self scrollPercent * (self domSize - self domScrollbarSize).
  91. ^ position x rounded @ position y rounded
  92. !
  93. scrollbarSize
  94. | domSize overflow |
  95. domSize := self domSize.
  96. overflow := self domOverflow.
  97. ^ ((domSize x / (overflow x + domSize x)) * 100) @ ((domSize y / (overflow y + domSize y) * 100))
  98. !
  99. verticalScrollbar
  100. ^ verticalScrollbar
  101. ! !
  102. !MKScrollDecorator methodsFor: 'actions'!
  103. resized
  104. super resized.
  105. self updateScrollbars
  106. !
  107. scrollDeltaX: aNumber
  108. | scrollbar left maxLeft |
  109. scrollbar := self horizontalScrollbar asJQuery.
  110. maxLeft := self domSize x - scrollbar width.
  111. left := ((scrollbar position left + aNumber) max: 0) min: maxLeft.
  112. scrollbar css: 'left' put: left.
  113. (self decorated asJQuery get: 0) at: 'scrollLeft' put: self domScrollPosition x
  114. !
  115. scrollDeltaY: aNumber
  116. | scrollbar top maxTop |
  117. scrollbar := self verticalScrollbar asJQuery.
  118. maxTop := self domSize y - scrollbar height.
  119. top := ((scrollbar position top - aNumber) max: 0) min: maxTop.
  120. scrollbar css: 'top' put: top.
  121. (self decorated asJQuery get: 0) at: 'scrollTop' put: self domScrollPosition y
  122. ! !
  123. !MKScrollDecorator methodsFor: 'defaults'!
  124. defaultControllerClass
  125. ^ MKScrollController
  126. ! !
  127. !MKScrollDecorator methodsFor: 'dom'!
  128. domDecoratedSize
  129. | element |
  130. element := self decorated asJQuery get: 0.
  131. ^ element scrollWidth @ element scrollHeight
  132. !
  133. domOverflow
  134. | element |
  135. element := self decorated asJQuery get: 0.
  136. ^ (element scrollWidth - element clientWidth) @ (element scrollHeight - element clientHeight)
  137. !
  138. domScrollPercent
  139. ^ self domScrollbarPosition / (self domSize - self domScrollbarSize)
  140. !
  141. domScrollPosition
  142. ^ (self domDecoratedSize - self domSize) * self domScrollPercent
  143. !
  144. domScrollbarPosition
  145. ^ horizontalScrollbar asJQuery position left @ verticalScrollbar asJQuery position top
  146. !
  147. domScrollbarSize
  148. ^ horizontalScrollbar asJQuery width @ verticalScrollbar asJQuery height
  149. ! !
  150. !MKScrollDecorator methodsFor: 'observing'!
  151. observeDecorated
  152. self decorated
  153. on: MKViewScroll
  154. send: #onDecoratedScroll
  155. to: self controller
  156. ! !
  157. !MKScrollDecorator methodsFor: 'private'!
  158. setupEventHandlers
  159. super setupEventHandlers.
  160. root asJQuery mousewheel: [ :event |
  161. self controller onMousewheel: event ].
  162. (jQuery value: window) resize: [ :event |
  163. self resized ]
  164. !
  165. setupScrollbars
  166. verticalScrollbar asJQuery draggable: #{
  167. 'containment' -> 'parent'.
  168. 'axis' -> 'y'.
  169. 'drag' -> [ :event | self controller onVerticalDrag: event ]
  170. }.
  171. horizontalScrollbar asJQuery draggable: #{
  172. 'containment' -> 'parent'.
  173. 'axis' -> 'x'.
  174. 'drag' -> [ :event | self controller onHorizontalDrag: event ]
  175. }.
  176. self updateScrollbars
  177. ! !
  178. !MKScrollDecorator methodsFor: 'rendering'!
  179. renderContentOn: html
  180. html div
  181. class: 'mk_scroll_container';
  182. with: [ super renderContentOn: html ].
  183. html div
  184. class: 'mk_scroll_rail vertical';
  185. with: [
  186. verticalScrollbar := html div
  187. class: 'mk_scrollbar';
  188. yourself ].
  189. html div
  190. class: 'mk_scroll_rail horizontal';
  191. with: [
  192. horizontalScrollbar := html div
  193. class: 'mk_scrollbar';
  194. yourself ].
  195. self setupScrollbars
  196. ! !
  197. !MKScrollDecorator methodsFor: 'testing'!
  198. hasHorizontalOverflow
  199. ^ self domOverflow x > 0
  200. !
  201. hasVerticalOverflow
  202. ^ self domOverflow y > 0
  203. ! !
  204. !MKScrollDecorator methodsFor: 'updating'!
  205. updateScrollbars
  206. | width height |
  207. width := self hasHorizontalOverflow
  208. ifTrue: [ self scrollbarSize x max: 10 ]
  209. ifFalse: [ 0 ].
  210. height := self hasVerticalOverflow
  211. ifTrue: [ self scrollbarSize y max: 10 ]
  212. ifFalse: [ 0 ].
  213. horizontalScrollbar asJQuery
  214. width: width asString, '%'.
  215. verticalScrollbar asJQuery
  216. height: height asString, '%'
  217. !
  218. updateScrollbarsPosition
  219. | position |
  220. position := self scrollbarPosition.
  221. horizontalScrollbar asJQuery
  222. css: 'left' put: position x.
  223. verticalScrollbar asJQuery
  224. css: 'top' put: position y
  225. ! !