Moka-Views.st 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. Smalltalk current createPackage: 'Moka-Views'!
  2. MKAspectView subclass: #MKButtonView
  3. instanceVariableNames: 'default label'
  4. package: 'Moka-Views'!
  5. !MKButtonView commentStamp!
  6. I am a push button view. My default controller is `MKButtonController`.
  7. My controller must answer to `#onPressed`.
  8. ## API
  9. - Instances can be set a `default` button
  10. - Use `#label:` to set the label string!
  11. !MKButtonView methodsFor: 'accessing'!
  12. cssClass
  13. ^ self isDefault
  14. ifTrue: [ 'default' ]
  15. ifFalse: [ '' ]
  16. !
  17. default
  18. ^ default
  19. !
  20. default: aBoolean
  21. default := aBoolean
  22. !
  23. label
  24. ^ label ifNil: [ self defaultLabel ]
  25. !
  26. label: aString
  27. label := aString
  28. ! !
  29. !MKButtonView methodsFor: 'defaults'!
  30. defaultControllerClass
  31. ^ MKButtonController
  32. !
  33. defaultLabel
  34. ^ 'OK'
  35. ! !
  36. !MKButtonView methodsFor: 'events'!
  37. pressed
  38. self controller onPressed
  39. ! !
  40. !MKButtonView methodsFor: 'rendering'!
  41. renderContentOn: html
  42. html button
  43. class: self cssClass;
  44. with: self label;
  45. onClick: [ self pressed ]
  46. ! !
  47. !MKButtonView methodsFor: 'testing'!
  48. isDefault
  49. ^ self default ifNil: [ false ]
  50. ! !
  51. MKAspectView subclass: #MKCheckboxView
  52. instanceVariableNames: 'id'
  53. package: 'Moka-Views'!
  54. !MKCheckboxView commentStamp!
  55. I am a checkbox view. My default controller is `MKCheckboxController`.
  56. My controller must answer to `#onToggled:`.
  57. ##API
  58. - If no `aspect` is provided, the ckeckbox state will always be off.
  59. - use `#label:` to set the label string.!
  60. !MKCheckboxView methodsFor: 'accessing'!
  61. checked
  62. ^ self aspectValue ifNil: [ false ]
  63. !
  64. cssClass
  65. ^ 'mk_checkbox'
  66. !
  67. id
  68. ^ id ifNil: [ id := 1000000 atRandom asString ]
  69. ! !
  70. !MKCheckboxView methodsFor: 'defaults'!
  71. defaultControllerClass
  72. ^ MKCheckboxController
  73. ! !
  74. !MKCheckboxView methodsFor: 'events'!
  75. pressed
  76. self controller onToggled: self checked not
  77. !
  78. update
  79. | checkbox |
  80. checkbox := ('#', self id) asJQuery.
  81. self checked
  82. ifTrue: [ checkbox attr: 'checked' put: 'checked' ]
  83. ifFalse: [ checkbox removeAttr: 'checked' ]
  84. ! !
  85. !MKCheckboxView methodsFor: 'rendering'!
  86. renderContentOn: html
  87. | checkbox |
  88. checkbox := html input
  89. type: 'checkbox';
  90. class: self cssClass;
  91. id: self id;
  92. onClick: [ self pressed ].
  93. self checked ifTrue: [
  94. checkbox at: 'checked' put: 'checked' ].
  95. html label
  96. for: self id;
  97. with: [ html entity: 'nbsp' ]
  98. ! !
  99. MKCheckboxView subclass: #MKSwitchView
  100. instanceVariableNames: ''
  101. package: 'Moka-Views'!
  102. !MKSwitchView commentStamp!
  103. I am a switch view, similar to a `MKCheckboxView` but displayed as a switch.
  104. My default controller is `MKCheckboxController`.!
  105. !MKSwitchView methodsFor: 'accessing'!
  106. cssClass
  107. ^ 'mk_switch'
  108. ! !
  109. MKAspectView subclass: #MKLabelView
  110. instanceVariableNames: 'input'
  111. package: 'Moka-Views'!
  112. !MKLabelView commentStamp!
  113. I am an label view. I display a `String`.!
  114. !MKLabelView methodsFor: 'defaults'!
  115. defaultControllerClass
  116. ^ super defaultControllerClass
  117. ! !
  118. !MKLabelView methodsFor: 'rendering'!
  119. renderContentOn: html
  120. html span with: self aspectValue
  121. ! !
  122. MKAspectView subclass: #MKTextAreaView
  123. instanceVariableNames: 'input'
  124. package: 'Moka-Views'!
  125. !MKTextAreaView commentStamp!
  126. I am an text area view. My default controller is `MKAnyKeyInputController`.
  127. My controller must answer to `#onKeyPressed:`.!
  128. !MKTextAreaView methodsFor: 'accessing'!
  129. value
  130. ^ input asJQuery val
  131. ! !
  132. !MKTextAreaView methodsFor: 'defaults'!
  133. defaultControllerClass
  134. ^ MKAnyKeyInputController
  135. ! !
  136. !MKTextAreaView methodsFor: 'events'!
  137. keyUp: anEvent
  138. self controller onKeyPressed: anEvent
  139. ! !
  140. !MKTextAreaView methodsFor: 'rendering'!
  141. renderContentOn: html
  142. input := html textarea
  143. with: self aspectValue;
  144. onKeyUp: [ :event | self keyUp: event ]
  145. ! !
  146. !MKTextAreaView methodsFor: 'updating'!
  147. update
  148. input ifNotNil: [ input asJQuery val: self aspectValue ]
  149. ! !
  150. MKTextAreaView subclass: #MKInputView
  151. instanceVariableNames: ''
  152. package: 'Moka-Views'!
  153. !MKInputView commentStamp!
  154. I am an input view. My default controller is `MKEnterInputController`.
  155. My controller must answer to `#onKeyPressed:`.!
  156. !MKInputView methodsFor: 'defaults'!
  157. defaultControllerClass
  158. ^ MKEnterInputController
  159. ! !
  160. !MKInputView methodsFor: 'rendering'!
  161. renderContentOn: html
  162. input := html input
  163. value: self aspectValue;
  164. onKeyUp: [ :event |
  165. self keyUp: event ];
  166. yourself
  167. ! !
  168. !MKInputView methodsFor: 'settings'!
  169. triggerChangeOnAnyKey
  170. self controller: MKAnyKeyInputController new
  171. !
  172. triggerChangeOnEnter
  173. self controller: MKEnterInputController new
  174. ! !