Trapped-Processors.st 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. Smalltalk current createPackage: 'Trapped-Processors'!
  2. TrappedDataExpectingProcessor subclass: #TrappedProcessorAttribute
  3. instanceVariableNames: 'attrName'
  4. package: 'Trapped-Processors'!
  5. !TrappedProcessorAttribute commentStamp!
  6. I set the data into an attribute speciried when creating me.
  7. No observing and sending back, atm.!
  8. !TrappedProcessorAttribute methodsFor: 'accessing'!
  9. attrName: aString
  10. attrName := aString
  11. ! !
  12. !TrappedProcessorAttribute methodsFor: 'data transformation'!
  13. toView: aDataCarrier
  14. aDataCarrier toTargetAttr: attrName
  15. ! !
  16. !TrappedProcessorAttribute class methodsFor: 'instance creation'!
  17. new: aString
  18. ^self new
  19. attrName: aString;
  20. yourself
  21. ! !
  22. TrappedDataExpectingProcessor subclass: #TrappedProcessorDataAdhoc
  23. instanceVariableNames: 'toViewBlock'
  24. package: 'Trapped-Processors'!
  25. !TrappedProcessorDataAdhoc commentStamp!
  26. I put data into target via contents: in toView:!
  27. !TrappedProcessorDataAdhoc methodsFor: 'accessing'!
  28. toViewBlock: aBlock
  29. toViewBlock := aBlock
  30. ! !
  31. !TrappedProcessorDataAdhoc methodsFor: 'data transformation'!
  32. toView: aDataCarrier
  33. toViewBlock value: aDataCarrier
  34. ! !
  35. !TrappedProcessorDataAdhoc class methodsFor: 'instance creation'!
  36. newToView: aBlock
  37. ^self new
  38. toViewBlock: aBlock;
  39. yourself
  40. ! !
  41. TrappedProcessor subclass: #TrappedProcessorDescend
  42. instanceVariableNames: ''
  43. package: 'Trapped-Processors'!
  44. !TrappedProcessorDescend commentStamp!
  45. I intepret data-trap in descendants of my brush.!
  46. !TrappedProcessorDescend methodsFor: 'data transformation'!
  47. toView: aDataCarrier
  48. Trapped current injectToJQuery: aDataCarrier target asJQuery children
  49. ! !
  50. TrappedProcessor subclass: #TrappedProcessorGuardBase
  51. instanceVariableNames: 'guardPath'
  52. package: 'Trapped-Processors'!
  53. !TrappedProcessorGuardBase commentStamp!
  54. I serve as base class for brush-guarding processors.
  55. I cover instantiation and subclasses have to provide
  56. implementation of toVIew: that react appropriately to guard releasing.!
  57. !TrappedProcessorGuardBase methodsFor: 'accessing'!
  58. guardPath: anArray
  59. guardPath := anArray
  60. ! !
  61. !TrappedProcessorGuardBase methodsFor: 'data transformation'!
  62. toModel: aDataCarrier
  63. "stop"
  64. !
  65. toView: aDataCarrier
  66. self subclassResponsibility
  67. ! !
  68. !TrappedProcessorGuardBase class methodsFor: 'instance creation'!
  69. new: anArray
  70. ^ self new
  71. guardPath: anArray;
  72. yourself
  73. ! !
  74. TrappedProcessorGuardBase subclass: #TrappedProcessorGuardContents
  75. instanceVariableNames: ''
  76. package: 'Trapped-Processors'!
  77. !TrappedProcessorGuardContents commentStamp!
  78. I am used to guard contents of the brush I am installed on.
  79. I save the brush contents, then I observe guard expression in the model,
  80. and when it changes to nil or false, I delete the brush contents;
  81. on the other hand, when it changes to non-nil and non-false,
  82. I restore it from remembered state and interpret all contained
  83. data-trap attributes inside.!
  84. !TrappedProcessorGuardContents methodsFor: 'data transformation'!
  85. toView: aDataCarrier
  86. | frozen contents |
  87. frozen := aDataCarrier copy.
  88. contents := frozen target asJQuery contents detach.
  89. frozen target trapGuard: guardPath contents: [ :html |
  90. html root asJQuery append: contents.
  91. Trapped current injectToJQuery: html root asJQuery ]
  92. ! !
  93. TrappedProcessorGuardBase subclass: #TrappedProcessorGuardProc
  94. instanceVariableNames: ''
  95. package: 'Trapped-Processors'!
  96. !TrappedProcessorGuardProc commentStamp!
  97. I am used to guard contents filling process of the brush I am installed on.
  98. I observe guard expression in the model,
  99. and when it changes to nil or false, I delete the brush contents;
  100. on the other hand, when it changes to non-nil and non-false,
  101. I run the rest on the chain, which should be one-time
  102. that sets up the contents,!
  103. !TrappedProcessorGuardProc methodsFor: 'data transformation'!
  104. toView: aDataCarrier
  105. | frozen |
  106. frozen := aDataCarrier copy.
  107. frozen target trapGuard: guardPath contents: [ frozen copy proceed ]
  108. ! !
  109. TrappedDataExpectingProcessor subclass: #TrappedProcessorInputChecked
  110. instanceVariableNames: ''
  111. package: 'Trapped-Processors'!
  112. !TrappedProcessorInputChecked commentStamp!
  113. I bind to checkbox checked attribute.!
  114. !TrappedProcessorInputChecked methodsFor: 'data transformation'!
  115. toView: aDataCarrier
  116. aDataCarrier toTargetAttr: 'checked'
  117. ! !
  118. !TrappedProcessorInputChecked methodsFor: 'installation'!
  119. installToView: aDataCarrier toModel: anotherDataCarrier
  120. | brush |
  121. brush := aDataCarrier target.
  122. brush onChange: [ anotherDataCarrier copy value: (brush asJQuery attr: 'checked') notNil; proceed ]
  123. ! !
  124. TrappedDataExpectingProcessor subclass: #TrappedProcessorInputValue
  125. instanceVariableNames: ''
  126. package: 'Trapped-Processors'!
  127. !TrappedProcessorInputValue commentStamp!
  128. I bind to input value.!
  129. !TrappedProcessorInputValue methodsFor: 'data transformation'!
  130. toView: aDataCarrier
  131. aDataCarrier toTargetValue
  132. ! !
  133. !TrappedProcessorInputValue methodsFor: 'installation'!
  134. installToView: aDataCarrier toModel: anotherDataCarrier
  135. | brush |
  136. brush := aDataCarrier target.
  137. brush onChange: [ anotherDataCarrier copy value: brush asJQuery val; proceed ]
  138. ! !
  139. TrappedProcessor subclass: #TrappedProcessorLoopBase
  140. instanceVariableNames: ''
  141. package: 'Trapped-Processors'!
  142. !TrappedProcessorLoopBase commentStamp!
  143. I serve as base class for looping processors.
  144. I cover instantiation and subclasses have to provide
  145. implementation of toVIew: that loops appropriately.!
  146. !TrappedProcessorLoopBase methodsFor: 'data transformation'!
  147. toModel: aDataCarrier
  148. "stop"
  149. !
  150. toView: aDataCarrier
  151. self subclassResponsibility
  152. ! !
  153. TrappedProcessorLoopBase subclass: #TrappedProcessorLoopContents
  154. instanceVariableNames: ''
  155. package: 'Trapped-Processors'!
  156. !TrappedProcessorLoopContents commentStamp!
  157. I am used to loop over data and repeat the contents
  158. of the brush I am installed on.
  159. I save the brush contents, then I observe the data in the model,
  160. and when it changes, I loop over it
  161. and restore the contents from remembered state
  162. and interpret all contained data-trap attributes inside
  163. for each element, putting the result _after_ my brush.
  164. My brush itself should be as least visible as possible,
  165. as it only serve as a position flag (use for example
  166. noscript, ins or del).!
  167. !TrappedProcessorLoopContents methodsFor: 'data transformation'!
  168. toView: aDataCarrier
  169. | frozen contents |
  170. frozen := aDataCarrier copy.
  171. contents := frozen target asJQuery contents detach.
  172. frozen target trapIter: #() after: [ :html |
  173. html root asJQuery append: contents clone.
  174. Trapped current injectToJQuery: html root asJQuery ]
  175. ! !
  176. TrappedProcessorLoopBase subclass: #TrappedProcessorLoopProc
  177. instanceVariableNames: ''
  178. package: 'Trapped-Processors'!
  179. !TrappedProcessorLoopProc commentStamp!
  180. I am used to loop over data and repeat the contents filling process
  181. of the brush I am installed on.
  182. I observe the data in the model,
  183. and when it changes, I loop over it
  184. and run the rest of the processing chain
  185. for each element, putting the result _after_ my brush.
  186. My brush itself should be as least visible as possible,
  187. as it only serve as a position flag (use for example
  188. noscript, ins or del).!
  189. !TrappedProcessorLoopProc methodsFor: 'data transformation'!
  190. toView: aDataCarrier
  191. | frozen |
  192. frozen := aDataCarrier copy.
  193. frozen target trapIter: #() after: [ :html | frozen copy target: html root; proceed ]
  194. ! !
  195. TrappedProcessor subclass: #TrappedProcessorReplace
  196. instanceVariableNames: 'left right'
  197. package: 'Trapped-Processors'!
  198. !TrappedProcessorReplace commentStamp!
  199. I convert data to string representation and do a regex replace.
  200. I get two parameters, in toView:, first is replaced with second,
  201. and in toModel:, the second is replaced with first.
  202. I remove leading '^' and ending '$' from the string used as replacement,
  203. so it safe to replace ^to with ^To, for example.!
  204. !TrappedProcessorReplace methodsFor: 'accessing'!
  205. left: aString
  206. left := aString
  207. !
  208. right: aString
  209. right := aString
  210. ! !
  211. !TrappedProcessorReplace methodsFor: 'data transformation'!
  212. toModel: aDataCarrier
  213. | replacement old |
  214. replacement := (left replace: '^\^' with: '') replace: '\$$' with: ''.
  215. old := aDataCarrier value asString.
  216. aDataCarrier
  217. value: (old replace: right with: replacement) whenDifferentFrom: old;
  218. proceed
  219. !
  220. toView: aDataCarrier
  221. | replacement old |
  222. replacement := (right replace: '^\^' with: '') replace: '\$$' with: ''.
  223. old := aDataCarrier value asString.
  224. aDataCarrier
  225. value: (old replace: left with: replacement) whenDifferentFrom: old;
  226. proceed
  227. ! !
  228. !TrappedProcessorReplace class methodsFor: 'instance creation'!
  229. new: aString with: anotherString
  230. ^ self new
  231. left: aString asString;
  232. right: anotherString asString;
  233. yourself
  234. ! !
  235. TrappedProcessor subclass: #TrappedProcessorSignal
  236. instanceVariableNames: 'selector'
  237. package: 'Trapped-Processors'!
  238. !TrappedProcessorSignal commentStamp!
  239. Instead of writing data directly to model,
  240. I instead modify it by sending a message specified when instantiating me.!
  241. !TrappedProcessorSignal methodsFor: 'accessing'!
  242. selector: aString
  243. selector := aString
  244. ! !
  245. !TrappedProcessorSignal methodsFor: 'data transformation'!
  246. toModel: aDataCarrier
  247. aDataCarrier modifyTargetByPerforming: selector
  248. !
  249. toView: aDataCarrier
  250. "stop"
  251. ! !
  252. !TrappedProcessorSignal class methodsFor: 'instance creation'!
  253. new: aString
  254. ^self new
  255. selector: aString;
  256. yourself
  257. ! !
  258. TrappedDataExpectingProcessor subclass: #TrappedProcessorToBlackboard
  259. instanceVariableNames: ''
  260. package: 'Trapped-Processors'!
  261. !TrappedProcessorToBlackboard commentStamp!
  262. I save the data to blackboard in toModel:, to position specified by path.!
  263. !TrappedProcessorToBlackboard methodsFor: 'data transformation'!
  264. toModel: aDataCarrier
  265. aDataCarrier target modify: [ aDataCarrier value ]
  266. ! !
  267. TrappedProcessor subclass: #TrappedProcessorWhenClicked
  268. instanceVariableNames: ''
  269. package: 'Trapped-Processors'!
  270. !TrappedProcessorWhenClicked commentStamp!
  271. I bind to an element and send true to blackboard when clicked.!
  272. !TrappedProcessorWhenClicked methodsFor: 'installation'!
  273. installToView: aDataCarrier toModel: anotherDataCarrier
  274. aDataCarrier target onClick: [ anotherDataCarrier copy proceed. false ]
  275. ! !
  276. TrappedProcessor subclass: #TrappedProcessorWhenSubmitted
  277. instanceVariableNames: ''
  278. package: 'Trapped-Processors'!
  279. !TrappedProcessorWhenSubmitted commentStamp!
  280. I bind to a form and send true to blackboard when submitted.!
  281. !TrappedProcessorWhenSubmitted methodsFor: 'installation'!
  282. installToView: aDataCarrier toModel: anotherDataCarrier
  283. aDataCarrier target onSubmit: [ anotherDataCarrier copy proceed. false ]
  284. ! !
  285. TrappedProcessor subclass: #TrappedProcessorWidget
  286. instanceVariableNames: 'viewName'
  287. package: 'Trapped-Processors'!
  288. !TrappedProcessorWidget commentStamp!
  289. I insert a widget instance of the class specified when creating me.!
  290. !TrappedProcessorWidget methodsFor: 'accessing'!
  291. viewName: aString
  292. viewName := aString
  293. ! !
  294. !TrappedProcessorWidget methodsFor: 'data transformation'!
  295. toView: aDataCarrier
  296. aDataCarrier target with: (Smalltalk current at: viewName) new
  297. ! !
  298. !TrappedProcessorWidget class methodsFor: 'instance creation'!
  299. new: aString
  300. ^self new
  301. viewName: aString;
  302. yourself
  303. ! !
  304. !TrappedDataCarrier methodsFor: '*Trapped-Processors'!
  305. modifyTarget
  306. self target modify: [ self value ]
  307. !
  308. modifyTargetByPerforming: aString
  309. self target modify: [ :m | m perform: aString ]
  310. !
  311. toTargetAttr: aString
  312. self falseAsNilValue
  313. ifNil: [ self target removeAt: aString ]
  314. ifNotNil: [ :value | self target at: aString put: (value = true ifTrue: [ aString ] ifFalse: [ value ]) ]
  315. !
  316. toTargetContents
  317. self target contents: self value
  318. !
  319. toTargetValue
  320. self target asJQuery val: (self value ifNotNil: [ :o | o value ] ifNil: [[]])
  321. ! !
  322. !TrappedProcessor class methodsFor: '*Trapped-Processors'!
  323. attr: aString
  324. ^TrappedProcessorAttribute new: aString
  325. !
  326. dataToView: aBlock
  327. ^TrappedProcessorDataAdhoc newToView: aBlock
  328. !
  329. guardContents: anArray
  330. ^TrappedProcessorGuardContents new: anArray
  331. !
  332. guardProc: anArray
  333. ^TrappedProcessorGuardProc new: anArray
  334. !
  335. inputChecked
  336. ^TrappedProcessorInputChecked new
  337. !
  338. inputValue
  339. ^TrappedProcessorInputValue new
  340. !
  341. loopContents
  342. ^TrappedProcessorLoopContents new
  343. !
  344. loopProc
  345. ^TrappedProcessorLoopProc new
  346. !
  347. path
  348. ^TrappedProcessorDescend new
  349. !
  350. replace: aString with: anotherString
  351. ^TrappedProcessorReplace new: aString with: anotherString
  352. !
  353. signal: aString
  354. ^TrappedProcessorSignal new: aString
  355. !
  356. toBlackboard
  357. ^TrappedProcessorToBlackboard new
  358. !
  359. whenClicked
  360. ^TrappedProcessorWhenClicked new
  361. !
  362. whenSubmitted
  363. ^TrappedProcessorWhenSubmitted new
  364. !
  365. widget: aString
  366. ^TrappedProcessorWidget new: aString
  367. ! !