Trapped-Processors.st 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 tag |
  170. frozen := aDataCarrier copy.
  171. tag := (frozen target element at: 'tagName') asLowercase.
  172. contents := tag = 'template'
  173. ifTrue: [ frozen target element content asJQuery ]
  174. ifFalse: [ frozen target asJQuery contents detach ].
  175. frozen target trapIter: #() after: [ :html |
  176. html root asJQuery append: contents clone.
  177. Trapped current injectToJQuery: html root asJQuery ]
  178. ! !
  179. TrappedProcessorLoopBase subclass: #TrappedProcessorLoopProc
  180. instanceVariableNames: ''
  181. package: 'Trapped-Processors'!
  182. !TrappedProcessorLoopProc commentStamp!
  183. I am used to loop over data and repeat the contents filling process
  184. of the brush I am installed on.
  185. I observe the data in the model,
  186. and when it changes, I loop over it
  187. and run the rest of the processing chain
  188. for each element, putting the result _after_ my brush.
  189. My brush itself should be as least visible as possible,
  190. as it only serve as a position flag (use for example
  191. noscript, ins or del).!
  192. !TrappedProcessorLoopProc methodsFor: 'data transformation'!
  193. toView: aDataCarrier
  194. | frozen |
  195. frozen := aDataCarrier copy.
  196. frozen target trapIter: #() after: [ :html | frozen copy target: html root; proceed ]
  197. ! !
  198. TrappedProcessor subclass: #TrappedProcessorReplace
  199. instanceVariableNames: 'left right'
  200. package: 'Trapped-Processors'!
  201. !TrappedProcessorReplace commentStamp!
  202. I convert data to string representation and do a regex replace.
  203. I get two parameters, in toView:, first is replaced with second,
  204. and in toModel:, the second is replaced with first.
  205. I remove leading '^' and ending '$' from the string used as replacement,
  206. so it safe to replace ^to with ^To, for example.!
  207. !TrappedProcessorReplace methodsFor: 'accessing'!
  208. left: aString
  209. left := aString
  210. !
  211. right: aString
  212. right := aString
  213. ! !
  214. !TrappedProcessorReplace methodsFor: 'data transformation'!
  215. toModel: aDataCarrier
  216. | replacement old |
  217. replacement := (left replace: '^\^' with: '') replace: '\$$' with: ''.
  218. old := aDataCarrier value asString.
  219. aDataCarrier
  220. value: (old replace: right with: replacement) whenDifferentFrom: old;
  221. proceed
  222. !
  223. toView: aDataCarrier
  224. | replacement old |
  225. replacement := (right replace: '^\^' with: '') replace: '\$$' with: ''.
  226. old := aDataCarrier value asString.
  227. aDataCarrier
  228. value: (old replace: left with: replacement) whenDifferentFrom: old;
  229. proceed
  230. ! !
  231. !TrappedProcessorReplace class methodsFor: 'instance creation'!
  232. new: aString with: anotherString
  233. ^ self new
  234. left: aString asString;
  235. right: anotherString asString;
  236. yourself
  237. ! !
  238. TrappedProcessor subclass: #TrappedProcessorSignal
  239. instanceVariableNames: 'selector'
  240. package: 'Trapped-Processors'!
  241. !TrappedProcessorSignal commentStamp!
  242. Instead of writing data directly to model,
  243. I instead modify it by sending a message specified when instantiating me.!
  244. !TrappedProcessorSignal methodsFor: 'accessing'!
  245. selector: aString
  246. selector := aString
  247. ! !
  248. !TrappedProcessorSignal methodsFor: 'data transformation'!
  249. toModel: aDataCarrier
  250. aDataCarrier modifyTargetByPerforming: selector
  251. !
  252. toView: aDataCarrier
  253. "stop"
  254. ! !
  255. !TrappedProcessorSignal class methodsFor: 'instance creation'!
  256. new: aString
  257. ^self new
  258. selector: aString;
  259. yourself
  260. ! !
  261. TrappedDataExpectingProcessor subclass: #TrappedProcessorToBlackboard
  262. instanceVariableNames: ''
  263. package: 'Trapped-Processors'!
  264. !TrappedProcessorToBlackboard commentStamp!
  265. I save the data to blackboard in toModel:, to position specified by path.!
  266. !TrappedProcessorToBlackboard methodsFor: 'data transformation'!
  267. toModel: aDataCarrier
  268. aDataCarrier target modify: [ aDataCarrier value ]
  269. ! !
  270. TrappedProcessor subclass: #TrappedProcessorWhenClicked
  271. instanceVariableNames: ''
  272. package: 'Trapped-Processors'!
  273. !TrappedProcessorWhenClicked commentStamp!
  274. I bind to an element and send true to blackboard when clicked.!
  275. !TrappedProcessorWhenClicked methodsFor: 'installation'!
  276. installToView: aDataCarrier toModel: anotherDataCarrier
  277. aDataCarrier target onClick: [ anotherDataCarrier copy proceed. false ]
  278. ! !
  279. TrappedProcessor subclass: #TrappedProcessorWhenSubmitted
  280. instanceVariableNames: ''
  281. package: 'Trapped-Processors'!
  282. !TrappedProcessorWhenSubmitted commentStamp!
  283. I bind to a form and send true to blackboard when submitted.!
  284. !TrappedProcessorWhenSubmitted methodsFor: 'installation'!
  285. installToView: aDataCarrier toModel: anotherDataCarrier
  286. aDataCarrier target onSubmit: [ anotherDataCarrier copy proceed. false ]
  287. ! !
  288. TrappedProcessor subclass: #TrappedProcessorWidget
  289. instanceVariableNames: 'viewName'
  290. package: 'Trapped-Processors'!
  291. !TrappedProcessorWidget commentStamp!
  292. I insert a widget instance of the class specified when creating me.!
  293. !TrappedProcessorWidget methodsFor: 'accessing'!
  294. viewName: aString
  295. viewName := aString
  296. ! !
  297. !TrappedProcessorWidget methodsFor: 'data transformation'!
  298. toView: aDataCarrier
  299. aDataCarrier target with: (Smalltalk current at: viewName) new
  300. ! !
  301. !TrappedProcessorWidget class methodsFor: 'instance creation'!
  302. new: aString
  303. ^self new
  304. viewName: aString;
  305. yourself
  306. ! !
  307. !TrappedDataCarrier methodsFor: '*Trapped-Processors'!
  308. modifyTarget
  309. self target modify: [ self value ]
  310. !
  311. modifyTargetByPerforming: aString
  312. self target modify: [ :m | m perform: aString ]
  313. !
  314. toTargetAttr: aString
  315. self falseAsNilValue
  316. ifNil: [ self target removeAt: aString ]
  317. ifNotNil: [ :value | self target at: aString put: (value = true ifTrue: [ aString ] ifFalse: [ value ]) ]
  318. !
  319. toTargetContents
  320. self target contents: self value
  321. !
  322. toTargetValue
  323. self target asJQuery val: (self value ifNotNil: [ :o | o value ] ifNil: [[]])
  324. ! !
  325. !TrappedProcessor class methodsFor: '*Trapped-Processors'!
  326. attr: aString
  327. ^TrappedProcessorAttribute new: aString
  328. !
  329. dataToView: aBlock
  330. ^TrappedProcessorDataAdhoc newToView: aBlock
  331. !
  332. guardContents: anArray
  333. ^TrappedProcessorGuardContents new: anArray
  334. !
  335. guardProc: anArray
  336. ^TrappedProcessorGuardProc new: anArray
  337. !
  338. inputChecked
  339. ^TrappedProcessorInputChecked new
  340. !
  341. inputValue
  342. ^TrappedProcessorInputValue new
  343. !
  344. loopContents
  345. ^TrappedProcessorLoopContents new
  346. !
  347. loopProc
  348. ^TrappedProcessorLoopProc new
  349. !
  350. path
  351. ^TrappedProcessorDescend new
  352. !
  353. replace: aString with: anotherString
  354. ^TrappedProcessorReplace new: aString with: anotherString
  355. !
  356. signal: aString
  357. ^TrappedProcessorSignal new: aString
  358. !
  359. toBlackboard
  360. ^TrappedProcessorToBlackboard new
  361. !
  362. whenClicked
  363. ^TrappedProcessorWhenClicked new
  364. !
  365. whenSubmitted
  366. ^TrappedProcessorWhenSubmitted new
  367. !
  368. widget: aString
  369. ^TrappedProcessorWidget new: aString
  370. ! !