Trapped-Processors.st 13 KB

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