Trapped-Processors.st 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 injectToChildren: aDataCarrier target element
  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: #TrappedProcessorGuard
  75. instanceVariableNames: ''
  76. package: 'Trapped-Processors'!
  77. !TrappedProcessorGuard commentStamp!
  78. I am used to guard contents filling process of the brush I am installed on.
  79. 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 run the rest on the chain, which should be one-time
  83. that sets up the contents,!
  84. !TrappedProcessorGuard methodsFor: 'data transformation'!
  85. toView: aDataCarrier
  86. | frozen xon |
  87. frozen := aDataCarrier copy.
  88. xon := frozen xontent.
  89. frozen target trapGuard: guardPath contents: [ :html | frozen copy target: html root; xontent: xon; proceed ]
  90. ! !
  91. TrappedDataExpectingProcessor subclass: #TrappedProcessorInputChecked
  92. instanceVariableNames: ''
  93. package: 'Trapped-Processors'!
  94. !TrappedProcessorInputChecked commentStamp!
  95. I bind to checkbox checked state.!
  96. !TrappedProcessorInputChecked methodsFor: 'data transformation'!
  97. toView: aDataCarrier
  98. aDataCarrier toTargetProp: 'checked'
  99. ! !
  100. !TrappedProcessorInputChecked methodsFor: 'installation'!
  101. installToView: aDataCarrier toModel: anotherDataCarrier
  102. | brush |
  103. brush := aDataCarrier target.
  104. brush onChange: [ anotherDataCarrier copy value: (brush asJQuery prop: 'checked'); proceed ]
  105. ! !
  106. TrappedDataExpectingProcessor subclass: #TrappedProcessorInputValue
  107. instanceVariableNames: ''
  108. package: 'Trapped-Processors'!
  109. !TrappedProcessorInputValue commentStamp!
  110. I bind to input value.!
  111. !TrappedProcessorInputValue methodsFor: 'data transformation'!
  112. toView: aDataCarrier
  113. aDataCarrier toTargetValue
  114. ! !
  115. !TrappedProcessorInputValue methodsFor: 'installation'!
  116. installToView: aDataCarrier toModel: anotherDataCarrier
  117. | brush |
  118. brush := aDataCarrier target.
  119. brush onChange: [ anotherDataCarrier copy value: brush asJQuery val; proceed ]
  120. ! !
  121. TrappedProcessor subclass: #TrappedProcessorLoopBase
  122. instanceVariableNames: ''
  123. package: 'Trapped-Processors'!
  124. !TrappedProcessorLoopBase commentStamp!
  125. I serve as base class for looping processors.
  126. I cover instantiation and subclasses have to provide
  127. implementation of toVIew: that loops appropriately.!
  128. !TrappedProcessorLoopBase methodsFor: 'data transformation'!
  129. toModel: aDataCarrier
  130. "stop"
  131. !
  132. toView: aDataCarrier
  133. self subclassResponsibility
  134. ! !
  135. TrappedProcessorLoopBase subclass: #TrappedProcessorLoopZ
  136. instanceVariableNames: ''
  137. package: 'Trapped-Processors'!
  138. !TrappedProcessorLoopZ commentStamp!
  139. I am used to loop over data and repeat the contents filling process
  140. of the brush I am installed on.
  141. I observe the data in the model,
  142. and when it changes, I loop over it
  143. and run the rest of the processing chain
  144. for each element, putting the result _after_ my brush.
  145. My brush itself should be as least visible as possible,
  146. as it only serve as a position flag (use for example
  147. script type=application/x-beacon, noscript, ins or del).!
  148. !TrappedProcessorLoopZ methodsFor: 'data transformation'!
  149. toView: aDataCarrier
  150. | frozen xon |
  151. frozen := aDataCarrier copy.
  152. xon := frozen xontent.
  153. frozen target trapIter: #() after: [ :html | frozen copy target: html root; xontent: xon; proceed ]
  154. ! !
  155. TrappedProcessor subclass: #TrappedProcessorReplace
  156. instanceVariableNames: 'left right'
  157. package: 'Trapped-Processors'!
  158. !TrappedProcessorReplace commentStamp!
  159. I convert data to string representation and do a regex replace.
  160. I get two parameters, in toView:, first is replaced with second,
  161. and in toModel:, the second is replaced with first.
  162. I remove leading '^' and ending '$' from the string used as replacement,
  163. so it safe to replace ^to with ^To, for example.!
  164. !TrappedProcessorReplace methodsFor: 'accessing'!
  165. left: aString
  166. left := aString
  167. !
  168. right: aString
  169. right := aString
  170. ! !
  171. !TrappedProcessorReplace methodsFor: 'data transformation'!
  172. toModel: aDataCarrier
  173. | replacement old |
  174. replacement := (left replace: '^\^' with: '') replace: '\$$' with: ''.
  175. old := aDataCarrier value asString.
  176. aDataCarrier
  177. value: (old replace: right with: replacement) whenDifferentFrom: old;
  178. proceed
  179. !
  180. toView: aDataCarrier
  181. | replacement old |
  182. replacement := (right replace: '^\^' with: '') replace: '\$$' with: ''.
  183. old := aDataCarrier value asString.
  184. aDataCarrier
  185. value: (old replace: left with: replacement) whenDifferentFrom: old;
  186. proceed
  187. ! !
  188. !TrappedProcessorReplace class methodsFor: 'instance creation'!
  189. new: aString with: anotherString
  190. ^ self new
  191. left: aString asString;
  192. right: anotherString asString;
  193. yourself
  194. ! !
  195. TrappedProcessor subclass: #TrappedProcessorSignal
  196. instanceVariableNames: 'selector'
  197. package: 'Trapped-Processors'!
  198. !TrappedProcessorSignal commentStamp!
  199. Instead of writing data directly to model,
  200. I instead modify it by sending a message specified when instantiating me.!
  201. !TrappedProcessorSignal methodsFor: 'accessing'!
  202. selector: aString
  203. selector := aString
  204. ! !
  205. !TrappedProcessorSignal methodsFor: 'data transformation'!
  206. toModel: aDataCarrier
  207. aDataCarrier modifyTargetByPerforming: selector
  208. !
  209. toView: aDataCarrier
  210. "stop"
  211. ! !
  212. !TrappedProcessorSignal class methodsFor: 'instance creation'!
  213. new: aString
  214. ^self new
  215. selector: aString;
  216. yourself
  217. ! !
  218. TrappedDataExpectingProcessor subclass: #TrappedProcessorToBlackboard
  219. instanceVariableNames: ''
  220. package: 'Trapped-Processors'!
  221. !TrappedProcessorToBlackboard commentStamp!
  222. I save the data to blackboard in toModel:, to position specified by path.!
  223. !TrappedProcessorToBlackboard methodsFor: 'data transformation'!
  224. toModel: aDataCarrier
  225. aDataCarrier target modify: [ aDataCarrier value ]
  226. ! !
  227. TrappedProcessor subclass: #TrappedProcessorUriComponentDecode
  228. instanceVariableNames: ''
  229. package: 'Trapped-Processors'!
  230. !TrappedProcessorUriComponentDecode commentStamp!
  231. I uriComponentDecode in toView:
  232. and encode in toModel:!
  233. !TrappedProcessorUriComponentDecode methodsFor: 'data transformation'!
  234. toModel: aDataCarrier
  235. aDataCarrier
  236. value: aDataCarrier value uriComponentEncoded;
  237. proceed
  238. !
  239. toView: aDataCarrier
  240. aDataCarrier
  241. value: aDataCarrier value uriComponentDecoded;
  242. proceed
  243. ! !
  244. TrappedProcessor subclass: #TrappedProcessorUriComponentEncode
  245. instanceVariableNames: ''
  246. package: 'Trapped-Processors'!
  247. !TrappedProcessorUriComponentEncode commentStamp!
  248. I uriComponentEncode in toView:
  249. and decode in toModel:!
  250. !TrappedProcessorUriComponentEncode methodsFor: 'data transformation'!
  251. toModel: aDataCarrier
  252. aDataCarrier
  253. value: aDataCarrier value uriComponentDecoded;
  254. proceed
  255. !
  256. toView: aDataCarrier
  257. aDataCarrier
  258. value: aDataCarrier value uriComponentEncoded;
  259. proceed
  260. ! !
  261. TrappedProcessor subclass: #TrappedProcessorWhenClicked
  262. instanceVariableNames: ''
  263. package: 'Trapped-Processors'!
  264. !TrappedProcessorWhenClicked commentStamp!
  265. I bind to an element and send true to blackboard when clicked.!
  266. !TrappedProcessorWhenClicked methodsFor: 'installation'!
  267. installToView: aDataCarrier toModel: anotherDataCarrier
  268. aDataCarrier target onClick: [ anotherDataCarrier copy proceed. false ]
  269. ! !
  270. TrappedProcessor subclass: #TrappedProcessorWhenSubmitted
  271. instanceVariableNames: ''
  272. package: 'Trapped-Processors'!
  273. !TrappedProcessorWhenSubmitted commentStamp!
  274. I bind to a form and send true to blackboard when submitted.!
  275. !TrappedProcessorWhenSubmitted methodsFor: 'installation'!
  276. installToView: aDataCarrier toModel: anotherDataCarrier
  277. aDataCarrier target onSubmit: [ anotherDataCarrier copy proceed. false ]
  278. ! !
  279. TrappedProcessor subclass: #TrappedProcessorWidget
  280. instanceVariableNames: 'viewName'
  281. package: 'Trapped-Processors'!
  282. !TrappedProcessorWidget commentStamp!
  283. I insert a widget instance of the class specified when creating me.!
  284. !TrappedProcessorWidget methodsFor: 'accessing'!
  285. viewName: aString
  286. viewName := aString
  287. ! !
  288. !TrappedProcessorWidget methodsFor: 'data transformation'!
  289. toView: aDataCarrier
  290. aDataCarrier target with: (Smalltalk current at: viewName) new
  291. ! !
  292. !TrappedProcessorWidget class methodsFor: 'instance creation'!
  293. new: aString
  294. ^self new
  295. viewName: aString;
  296. yourself
  297. ! !
  298. TrappedProcessor subclass: #TrappedProcessorXontent
  299. instanceVariableNames: ''
  300. package: 'Trapped-Processors'!
  301. !TrappedProcessorXontent commentStamp!
  302. I am used to show xontent of the brush I am installed on
  303. (see jQuery plugin Xontent for details).
  304. I clone xontent of the brush, put it into HTML
  305. and interpret all contained data-trap attributes.!
  306. !TrappedProcessorXontent methodsFor: 'data transformation'!
  307. toView: aDataCarrier
  308. aDataCarrier target asJQuery append: (Trapped current cloneAndInject: (aDataCarrier xontent get: 0)).
  309. aDataCarrier proceed
  310. ! !
  311. !TrappedDataCarrier methodsFor: '*Trapped-Processors'!
  312. modifyTarget
  313. self target modify: [ self value ]
  314. !
  315. modifyTargetByPerforming: aString
  316. self target modify: [ :m | m perform: aString ]
  317. !
  318. primitive: anObject
  319. <return anObject === nil ? null : anObject.valueOf()>
  320. !
  321. toTargetAttr: aString
  322. self falseAsNilValue
  323. ifNil: [ self target removeAt: aString ]
  324. ifNotNil: [ :bvalue |
  325. | value |
  326. value := self primitive: bvalue.
  327. self target at: aString put: (value = true ifTrue: [ aString ] ifFalse: [ value ]) ]
  328. !
  329. toTargetContents
  330. self target contents: (self primitive: self value)
  331. !
  332. toTargetProp: aString
  333. self target element at: aString put: (self primitive: self value)
  334. !
  335. toTargetValue
  336. self target asJQuery val: (self primitive: self value)
  337. !
  338. xontent
  339. ^self target asJQuery xontent
  340. !
  341. xontent: anObject
  342. self target asJQuery xontent: 'set' data: anObject
  343. ! !
  344. !TrappedProcessor class methodsFor: '*Trapped-Processors'!
  345. attr: aString
  346. ^TrappedProcessorAttribute new: aString
  347. !
  348. dataToView: aBlock
  349. ^TrappedProcessorDataAdhoc newToView: aBlock
  350. !
  351. deuric
  352. ^TrappedProcessorUriComponentDecode new
  353. !
  354. guard: anArray
  355. ^TrappedProcessorGuard new: anArray
  356. !
  357. inputChecked
  358. ^TrappedProcessorInputChecked new
  359. !
  360. inputValue
  361. ^TrappedProcessorInputValue new
  362. !
  363. loopZ
  364. ^TrappedProcessorLoopZ new
  365. !
  366. path
  367. ^TrappedProcessorDescend new
  368. !
  369. replace: aString with: anotherString
  370. ^TrappedProcessorReplace new: aString with: anotherString
  371. !
  372. signal: aString
  373. ^TrappedProcessorSignal new: aString
  374. !
  375. toBlackboard
  376. ^TrappedProcessorToBlackboard new
  377. !
  378. uric
  379. ^TrappedProcessorUriComponentEncode new
  380. !
  381. whenClicked
  382. ^TrappedProcessorWhenClicked new
  383. !
  384. whenSubmitted
  385. ^TrappedProcessorWhenSubmitted new
  386. !
  387. widget: aString
  388. ^TrappedProcessorWidget new: aString
  389. !
  390. xontent
  391. ^TrappedProcessorXontent new
  392. ! !