Moka-Controllers.st 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. Smalltalk current createPackage: 'Moka-Controllers'!
  2. MKSingleAspectController subclass: #MKAnyKeyInputController
  3. instanceVariableNames: 'lastValue'
  4. package: 'Moka-Controllers'!
  5. !MKAnyKeyInputController commentStamp!
  6. I am the default controller for `MKTextAreaView`. Actions are performed on any key press if the view's value changes.!
  7. !MKAnyKeyInputController methodsFor: 'accessing'!
  8. inputText
  9. ^ self view value
  10. ! !
  11. !MKAnyKeyInputController methodsFor: 'actions'!
  12. onKeyUp: anEvent
  13. self setNewValue
  14. !
  15. setNewValue
  16. | newValue |
  17. newValue := self inputText.
  18. newValue = lastValue ifTrue: [ ^ self ].
  19. lastValue := newValue.
  20. self performAspectActionWith: newValue
  21. ! !
  22. MKAnyKeyInputController subclass: #MKEnterInputController
  23. instanceVariableNames: ''
  24. package: 'Moka-Controllers'!
  25. !MKEnterInputController commentStamp!
  26. I am the default controller for `MKInputView`.
  27. Actions are performed on 'enter' key press.!
  28. !MKEnterInputController methodsFor: 'actions'!
  29. onKeyDown: anEvent
  30. anEvent keyCode = String cr asciiValue ifTrue: [
  31. self setNewValue ]
  32. !
  33. onKeyUp: anEvent
  34. ! !
  35. MKSingleAspectController subclass: #MKButtonController
  36. instanceVariableNames: ''
  37. package: 'Moka-Controllers'!
  38. !MKButtonController commentStamp!
  39. I am the default controller for `MKButtonView`.!
  40. !MKButtonController methodsFor: 'actions'!
  41. onClick: anEvent
  42. self performAspectAction
  43. ! !
  44. MKSingleAspectController subclass: #MKCheckboxController
  45. instanceVariableNames: ''
  46. package: 'Moka-Controllers'!
  47. !MKCheckboxController commentStamp!
  48. I am the default controller for `MKCheckboxView`.!
  49. !MKCheckboxController methodsFor: 'actions'!
  50. onClick: anEvent
  51. self toggle
  52. !
  53. onKeyDown: anEvent
  54. "Avoid scrolling in scrollable views"
  55. anEvent stopPropagation
  56. !
  57. onKeyPress: anEvent
  58. anEvent charCode = ' ' asciiValue ifTrue: [
  59. self toggle.
  60. anEvent stopPropagation; preventDefault ]
  61. !
  62. toggle
  63. self performAspectActionWith: self view checked not
  64. ! !
  65. MKAspectsController subclass: #MKDropdownController
  66. instanceVariableNames: ''
  67. package: 'Moka-Controllers'!
  68. !MKDropdownController methodsFor: 'actions'!
  69. onClick: anEvent
  70. self view popupList
  71. !
  72. onKeyDown: anEvent
  73. anEvent keyCode = String cr asciiValue ifTrue: [
  74. self view popupList ]
  75. ! !
  76. MKAspectsController subclass: #MKListController
  77. instanceVariableNames: 'downRepeater upRepeater'
  78. package: 'Moka-Controllers'!
  79. !MKListController methodsFor: 'accessing'!
  80. activeItem
  81. ^ self view activeItem
  82. !
  83. collection
  84. ^ self view collection
  85. !
  86. downRepeater
  87. ^ downRepeater ifNil: [ downRepeater := MKRepeater new ]
  88. !
  89. upRepeater
  90. ^ upRepeater ifNil: [ upRepeater := MKRepeater new ]
  91. ! !
  92. !MKListController methodsFor: 'actions'!
  93. activateItem: anItem
  94. "On item activation, change the model selection"
  95. self selectItem: anItem
  96. !
  97. onClick: anEvent
  98. self selectItem: (self itemForTarget: anEvent target)
  99. !
  100. onKeyDown: anEvent
  101. "Down"
  102. anEvent keyCode = 40 ifTrue: [
  103. anEvent preventDefault; stopPropagation.
  104. self upRepeater stopRepeating.
  105. self downRepeater repeat: [
  106. self activateItem: self nextItem ] ].
  107. "Up"
  108. anEvent keyCode = 38 ifTrue: [
  109. anEvent preventDefault; stopPropagation.
  110. self downRepeater stopRepeating.
  111. self upRepeater repeat: [
  112. self activateItem: self previousItem ] ].
  113. !
  114. onKeyUp: anEvent
  115. self downRepeater stopRepeating.
  116. self upRepeater stopRepeating
  117. !
  118. selectItem: anItem
  119. self
  120. performAspectAction: self view selectionAspect
  121. with: anItem
  122. ! !
  123. !MKListController methodsFor: 'private'!
  124. itemForTarget: aDOMElement
  125. ^ self view findItemFor: aDOMElement
  126. !
  127. nextItem
  128. ^ self collection
  129. at: (self collection indexOf: self activeItem) + 1
  130. ifAbsent: [ self collection last ]
  131. !
  132. previousItem
  133. ^ self view collection
  134. at: (self view collection indexOf: self activeItem) - 1
  135. ifAbsent: [ self view collection first ]
  136. ! !
  137. MKListController subclass: #MKDropdownListController
  138. instanceVariableNames: ''
  139. package: 'Moka-Controllers'!
  140. !MKDropdownListController methodsFor: 'actions'!
  141. activateItem: anItem
  142. "Select the list item in the view.
  143. No change is done to the model"
  144. self view activateItem: anItem
  145. !
  146. onKeyDown: anEvent
  147. super onKeyDown: anEvent.
  148. anEvent keyCode = String cr asciiValue ifTrue: [
  149. self selectItem: self view activeItem ]
  150. !
  151. onMouseMove: anEvent
  152. (self upRepeater isRepeating or: [ self downRepeater isRepeating ])
  153. ifTrue: [ ^ self ].
  154. self activateItem: (self itemForTarget: anEvent target)
  155. ! !
  156. MKSingleAspectController subclass: #MKModalPaneController
  157. instanceVariableNames: ''
  158. package: 'Moka-Controllers'!
  159. !MKModalPaneController methodsFor: 'actions'!
  160. onClick: anEvent
  161. self view closeOnClick ifTrue: [ self removeView ]
  162. !
  163. onKeyDown: anEvent
  164. "ESC"
  165. anEvent keyCode = 27 ifTrue: [
  166. self removeView ].
  167. self view closeOnEnter ifTrue: [
  168. anEvent keyCode = String cr asciiValue ifTrue: [
  169. self removeView ] ]
  170. !
  171. removeView
  172. self view overlay remove
  173. ! !
  174. MKSingleAspectController subclass: #MKOverlayController
  175. instanceVariableNames: ''
  176. package: 'Moka-Controllers'!
  177. !MKOverlayController commentStamp!
  178. I am the default controller for `MKOverlayView`.
  179. On a click to the overlay, it is removed together with it's content view.!
  180. !MKOverlayController methodsFor: 'actions'!
  181. onClick: anEvent
  182. self view remove
  183. ! !
  184. Object subclass: #MKRepeater
  185. instanceVariableNames: 'repeatInterval interval delay'
  186. package: 'Moka-Controllers'!
  187. !MKRepeater commentStamp!
  188. I am an internal class used by controllers to repeat block actions after a `delay` and with an `interval`.!
  189. !MKRepeater methodsFor: 'accessing'!
  190. repeatInterval
  191. ^ repeatInterval ifNil: [ self defaultRepeatInterval ]
  192. !
  193. repeatInterval: aNumber
  194. repeatInterval := aNumber
  195. ! !
  196. !MKRepeater methodsFor: 'actions'!
  197. repeat: aBlock
  198. self isRepeating ifTrue: [ ^ self ].
  199. aBlock value.
  200. delay := [ interval := aBlock valueWithInterval: self repeatInterval ]
  201. valueWithTimeout: 300
  202. !
  203. stopRepeating
  204. interval ifNotNil: [ interval clearInterval ].
  205. delay ifNotNil: [ delay clearTimeout ].
  206. interval := delay := nil
  207. ! !
  208. !MKRepeater methodsFor: 'defaults'!
  209. defaultRepeatInterval
  210. ^ 70
  211. ! !
  212. !MKRepeater methodsFor: 'testing'!
  213. isRepeating
  214. ^ delay notNil
  215. ! !
  216. MKController subclass: #MKScrollController
  217. instanceVariableNames: ''
  218. package: 'Moka-Controllers'!
  219. !MKScrollController methodsFor: 'accessing'!
  220. position
  221. ^ self view domDecoratedSize * self scrollPercent
  222. !
  223. scrollPercent
  224. ^ self view domScrollPosition / (self view domSize - self view domScrollbarSize)
  225. ! !
  226. !MKScrollController methodsFor: 'actions'!
  227. onHorizontalDrag: anEvent
  228. (self view decorated asJQuery get: 0) at: 'scrollLeft' put: self position x
  229. !
  230. onVerticalDrag: anEvent
  231. console log: self position asString.
  232. (self view decorated asJQuery get: 0) at: 'scrollTop' put: self position y
  233. ! !