Trapped-Processors.st 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. Smalltalk current createPackage: 'Trapped-Processors'!
  2. TrappedProcessor subclass: #TrappedDataExpectingProcessor
  3. instanceVariableNames: ''
  4. package: 'Trapped-Processors'!
  5. !TrappedDataExpectingProcessor commentStamp!
  6. I answer true to isExpectingModelData and serve as a base class
  7. for processor that present / change model data.
  8. When at least one of my instances is present in the chain,
  9. automatic databinding processor is added at the beginning
  10. (the data-binding scenario); otherwise, the chain
  11. is run immediately with true as data (run-once scenario).!
  12. !TrappedDataExpectingProcessor methodsFor: 'testing'!
  13. isExpectingModelData
  14. ^true
  15. ! !
  16. TrappedDataExpectingProcessor subclass: #TrappedProcessorContents
  17. instanceVariableNames: ''
  18. package: 'Trapped-Processors'!
  19. !TrappedProcessorContents commentStamp!
  20. I put data into target via contents: in toView:!
  21. !TrappedProcessorContents methodsFor: 'data transformation'!
  22. toView: aDataCarrier
  23. aDataCarrier toTargetContents
  24. ! !
  25. TrappedDataExpectingProcessor subclass: #TrappedProcessorDataAdhoc
  26. instanceVariableNames: 'toViewBlock'
  27. package: 'Trapped-Processors'!
  28. !TrappedProcessorDataAdhoc commentStamp!
  29. I put data into target via contents: in toView:!
  30. !TrappedProcessorDataAdhoc methodsFor: 'accessing'!
  31. toViewBlock: aBlock
  32. toViewBlock := aBlock
  33. ! !
  34. !TrappedProcessorDataAdhoc methodsFor: 'data transformation'!
  35. toView: aDataCarrier
  36. toViewBlock value: aDataCarrier
  37. ! !
  38. !TrappedProcessorDataAdhoc class methodsFor: 'instance creation'!
  39. newToView: aBlock
  40. ^self new
  41. toViewBlock: aBlock;
  42. yourself
  43. ! !
  44. TrappedDataExpectingProcessor subclass: #TrappedProcessorInputChecked
  45. instanceVariableNames: ''
  46. package: 'Trapped-Processors'!
  47. !TrappedProcessorInputChecked commentStamp!
  48. I bind to checkbox checked attribute.!
  49. !TrappedProcessorInputChecked methodsFor: 'data transformation'!
  50. toView: aDataCarrier
  51. aDataCarrier toTargetAttr: 'checked'
  52. ! !
  53. !TrappedProcessorInputChecked methodsFor: 'installation'!
  54. installToView: aDataCarrier toModel: anotherDataCarrier
  55. | brush |
  56. brush := aDataCarrier target.
  57. brush onChange: [ anotherDataCarrier copy value: (brush asJQuery attr: 'checked') notNil; proceed ]
  58. ! !
  59. TrappedDataExpectingProcessor subclass: #TrappedProcessorInputValue
  60. instanceVariableNames: ''
  61. package: 'Trapped-Processors'!
  62. !TrappedProcessorInputValue commentStamp!
  63. I bind to input value.!
  64. !TrappedProcessorInputValue methodsFor: 'data transformation'!
  65. toView: aDataCarrier
  66. aDataCarrier toTargetValue
  67. ! !
  68. !TrappedProcessorInputValue methodsFor: 'installation'!
  69. installToView: aDataCarrier toModel: anotherDataCarrier
  70. | brush |
  71. brush := aDataCarrier target.
  72. brush onChange: [ anotherDataCarrier copy value: brush asJQuery val; proceed ]
  73. ! !
  74. TrappedProcessor subclass: #TrappedProcessorBlackboard
  75. instanceVariableNames: ''
  76. package: 'Trapped-Processors'!
  77. !TrappedProcessorBlackboard commentStamp!
  78. I am used internally to fetch data from blackboard
  79. or write it back.
  80. I am added to the beginning of the chain
  81. when the chain contains at least one element
  82. that isExpectingModelData (see TrappedDataExpectingProcessor).!
  83. !TrappedProcessorBlackboard methodsFor: 'data transformation'!
  84. toModel: aDataCarrier
  85. aDataCarrier modifyTarget
  86. ! !
  87. !TrappedProcessorBlackboard methodsFor: 'installation'!
  88. installToView: aDataCarrier toModel: anotherDataCarrier
  89. | snap |
  90. snap := anotherDataCarrier target.
  91. snap watch: [ :data |
  92. (aDataCarrier target asJQuery closest: 'html') toArray isEmpty ifTrue: [ KeyedPubSubUnsubscribe signal ].
  93. snap do: [ aDataCarrier copy value: data; proceed ] ].
  94. aDataCarrier value: false
  95. ! !
  96. TrappedProcessor subclass: #TrappedProcessorDescend
  97. instanceVariableNames: ''
  98. package: 'Trapped-Processors'!
  99. !TrappedProcessorDescend commentStamp!
  100. I intepret data-trap in descendants of my brush.!
  101. !TrappedProcessorDescend methodsFor: 'data transformation'!
  102. toView: aDataCarrier
  103. Trapped current injectToJQuery: aDataCarrier target asJQuery children
  104. ! !
  105. TrappedProcessor subclass: #TrappedProcessorGuardBase
  106. instanceVariableNames: 'guardPath'
  107. package: 'Trapped-Processors'!
  108. !TrappedProcessorGuardBase commentStamp!
  109. I serve as base class for brush-guarding processors.
  110. I cover instantiation and subclasses have to provide
  111. implementation of toVIew: that react appropriately to guard releasing.!
  112. !TrappedProcessorGuardBase methodsFor: 'accessing'!
  113. guardPath: anArray
  114. guardPath := anArray
  115. ! !
  116. !TrappedProcessorGuardBase methodsFor: 'data transformation'!
  117. toModel: aDataCarrier
  118. "stop"
  119. !
  120. toView: aDataCarrier
  121. self subclassResponsibility
  122. ! !
  123. !TrappedProcessorGuardBase class methodsFor: 'instance creation'!
  124. new: anArray
  125. ^ self new
  126. guardPath: anArray;
  127. yourself
  128. ! !
  129. TrappedProcessorGuardBase subclass: #TrappedProcessorGuardContents
  130. instanceVariableNames: ''
  131. package: 'Trapped-Processors'!
  132. !TrappedProcessorGuardContents commentStamp!
  133. I am used to guard contents of the brush I am installed on.
  134. I save the brush contents, then I observe guard expression in the model,
  135. and when it changes to nil or false, I delete the brush contents;
  136. on the other hand, when it changes to non-nil and non-false,
  137. I restore it from remembered state and interpret all contained
  138. data-trap attributes inside.!
  139. !TrappedProcessorGuardContents methodsFor: 'data transformation'!
  140. toView: aDataCarrier
  141. | frozen contents |
  142. frozen := aDataCarrier copy.
  143. contents := frozen target asJQuery contents detach.
  144. frozen target trapGuard: guardPath contents: [ :html |
  145. html root asJQuery append: contents.
  146. Trapped current injectToJQuery: html root asJQuery ]
  147. ! !
  148. TrappedProcessorGuardBase subclass: #TrappedProcessorGuardProc
  149. instanceVariableNames: ''
  150. package: 'Trapped-Processors'!
  151. !TrappedProcessorGuardProc commentStamp!
  152. I am used to guard contents filling process of the brush I am installed on.
  153. I observe guard expression in the model,
  154. and when it changes to nil or false, I delete the brush contents;
  155. on the other hand, when it changes to non-nil and non-false,
  156. I run the rest on the chain, which should be one-time
  157. that sets up the contents,!
  158. !TrappedProcessorGuardProc methodsFor: 'data transformation'!
  159. toView: aDataCarrier
  160. | frozen |
  161. frozen := aDataCarrier copy.
  162. frozen target trapGuard: guardPath contents: [ frozen copy proceed ]
  163. ! !
  164. TrappedProcessor subclass: #TrappedProcessorSignal
  165. instanceVariableNames: 'selector'
  166. package: 'Trapped-Processors'!
  167. !TrappedProcessorSignal commentStamp!
  168. Instead of writing data directly to model,
  169. I instead modify it by sending a message specified when instantiating me.!
  170. !TrappedProcessorSignal methodsFor: 'accessing'!
  171. selector: aString
  172. selector := aString
  173. ! !
  174. !TrappedProcessorSignal methodsFor: 'data transformation'!
  175. toModel: aDataCarrier
  176. aDataCarrier modifyTargetByPerforming: selector
  177. !
  178. toView: aDataCarrier
  179. "stop"
  180. ! !
  181. !TrappedProcessorSignal class methodsFor: 'instance creation'!
  182. new: aString
  183. ^self new
  184. selector: aString;
  185. yourself
  186. ! !
  187. TrappedProcessor subclass: #TrappedProcessorTerminator
  188. instanceVariableNames: ''
  189. package: 'Trapped-Processors'!
  190. !TrappedProcessorTerminator commentStamp!
  191. I do not proceed in toView:.
  192. I am added automatically to end of chain when it does not contain
  193. any element that isExpectingModelData (see TrappedDataExpectingProcessor).!
  194. !TrappedProcessorTerminator methodsFor: 'data transformation'!
  195. toView: aDataCarrier
  196. "stop"
  197. ! !
  198. TrappedProcessor subclass: #TrappedProcessorWhenClicked
  199. instanceVariableNames: ''
  200. package: 'Trapped-Processors'!
  201. !TrappedProcessorWhenClicked commentStamp!
  202. I bind to an element and send true to blackboard when clicked.!
  203. !TrappedProcessorWhenClicked methodsFor: 'installation'!
  204. installToView: aDataCarrier toModel: anotherDataCarrier
  205. aDataCarrier target onClick: [ anotherDataCarrier copy proceed. false ]
  206. ! !
  207. TrappedProcessor subclass: #TrappedProcessorWhenSubmitted
  208. instanceVariableNames: ''
  209. package: 'Trapped-Processors'!
  210. !TrappedProcessorWhenSubmitted commentStamp!
  211. I bind to a form and send true to blackboard when submitted.!
  212. !TrappedProcessorWhenSubmitted methodsFor: 'installation'!
  213. installToView: aDataCarrier toModel: anotherDataCarrier
  214. aDataCarrier target onSubmit: [ anotherDataCarrier copy proceed. false ]
  215. ! !
  216. TrappedProcessor subclass: #TrappedProcessorWidget
  217. instanceVariableNames: 'viewName'
  218. package: 'Trapped-Processors'!
  219. !TrappedProcessorWidget commentStamp!
  220. I insert a widget instance of the class specified when creating me.!
  221. !TrappedProcessorWidget methodsFor: 'accessing'!
  222. viewName: aString
  223. viewName := aString
  224. ! !
  225. !TrappedProcessorWidget methodsFor: 'data transformation'!
  226. toView: aDataCarrier
  227. aDataCarrier target with: (Smalltalk current at: viewName) new
  228. ! !
  229. !TrappedProcessorWidget class methodsFor: 'instance creation'!
  230. new: aString
  231. ^self new
  232. viewName: aString;
  233. yourself
  234. ! !
  235. !TrappedDataCarrier methodsFor: '*Trapped-Processors'!
  236. modifyTarget
  237. self target modify: [ self value ]
  238. !
  239. modifyTargetByPerforming: aString
  240. self target modify: [ :m | m perform: aString ]
  241. !
  242. toTargetAttr: aString
  243. self target asJQuery attr: aString put: (self value ifNotNil: [ :o | o value ] ifNil: [[]])
  244. !
  245. toTargetContents
  246. self target contents: self value
  247. !
  248. toTargetValue
  249. self target asJQuery val: (self value ifNotNil: [ :o | o value ] ifNil: [[]])
  250. ! !