Trapped-Frontend.st 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. Smalltalk current createPackage: 'Trapped-Frontend'!
  2. Object subclass: #TrappedDataCarrier
  3. instanceVariableNames: 'target model chain'
  4. package: 'Trapped-Frontend'!
  5. !TrappedDataCarrier methodsFor: 'accessing'!
  6. chain: aDataChain
  7. chain := aDataChain
  8. !
  9. target
  10. ^target
  11. !
  12. target: anObject
  13. target := anObject
  14. !
  15. value
  16. ^model
  17. !
  18. value: anObject
  19. model := anObject
  20. ! !
  21. !TrappedDataCarrier methodsFor: 'action'!
  22. modifyTarget
  23. self target modify: [ self value ]
  24. !
  25. toTargetAttr: aString
  26. self target asJQuery attr: aString put: (self value ifNotNil: [ :o | o value ] ifNil: [[]])
  27. !
  28. toTargetContents
  29. self target contents: self value
  30. !
  31. toTargetValue
  32. self target asJQuery val: (self value ifNotNil: [ :o | o value ] ifNil: [[]])
  33. ! !
  34. !TrappedDataCarrier class methodsFor: 'not yet classified'!
  35. on: aDataChain target: anObject
  36. ^self new
  37. chain: aDataChain;
  38. target: anObject;
  39. yourself
  40. ! !
  41. TrappedDataCarrier subclass: #TrappedDataCarrierToModel
  42. instanceVariableNames: 'index'
  43. package: 'Trapped-Frontend'!
  44. !TrappedDataCarrierToModel methodsFor: 'not yet classified'!
  45. proceed
  46. index := index ifNil: [ chain lastProcessorNo ] ifNotNil: [ index - 1 ].
  47. (chain processorNo: index) toModel: self
  48. ! !
  49. TrappedDataCarrier subclass: #TrappedDataCarrierToView
  50. instanceVariableNames: 'index'
  51. package: 'Trapped-Frontend'!
  52. !TrappedDataCarrierToView methodsFor: 'not yet classified'!
  53. proceed
  54. index := index ifNil: [ chain firstProcessorNo ] ifNotNil: [ index + 1 ].
  55. (chain processorNo: index) toView: self
  56. ! !
  57. Object subclass: #TrappedDataChain
  58. instanceVariableNames: 'processors'
  59. package: 'Trapped-Frontend'!
  60. !TrappedDataChain methodsFor: 'accessing'!
  61. firstProcessorNo
  62. ^1
  63. !
  64. lastProcessorNo
  65. ^processors size
  66. !
  67. processorNo: aNumber
  68. ^processors at: aNumber
  69. !
  70. processors: anArray
  71. processors := anArray
  72. ! !
  73. !TrappedDataChain methodsFor: 'action'!
  74. forSnapshot: aSnapshot andBrush: aTagBrush
  75. | toViewCarrier toModelCarrier |
  76. toViewCarrier := TrappedDataCarrierToView on: self target: aTagBrush.
  77. toModelCarrier := TrappedDataCarrierToModel on: self target: aSnapshot.
  78. processors do: [ :each | each installToView: toViewCarrier toModel: toModelCarrier ]
  79. ! !
  80. !TrappedDataChain class methodsFor: 'instance creation'!
  81. new: anArray
  82. ^self new
  83. processors: { self blackboardReaderWriter }, anArray;
  84. yourself
  85. !
  86. newFromProcessorNames: anArray
  87. ^self new: (anArray collect: [ :each | TrappedProcessor perform: each ])
  88. ! !
  89. !TrappedDataChain class methodsFor: 'private'!
  90. blackboardReaderWriter
  91. ^TrappedProcessorBlackboard new
  92. ! !
  93. Widget subclass: #TrappedDumbView
  94. instanceVariableNames: ''
  95. package: 'Trapped-Frontend'!
  96. !TrappedDumbView commentStamp!
  97. I just read and show an actual path.!
  98. !TrappedDumbView methodsFor: 'rendering'!
  99. renderOn: html
  100. html root trap: #()
  101. ! !
  102. Object subclass: #TrappedProcessor
  103. instanceVariableNames: ''
  104. package: 'Trapped-Frontend'!
  105. !TrappedProcessor commentStamp!
  106. I process data in TrappedDataChain.
  107. I am stateless flyweight (aka servant)
  108. and will get all necessary data as arguments in API calls.
  109. My public API is:
  110. - installToView:toModel:
  111. This gets two TrappedDataCarriers set up without actual data
  112. and at the beginning of their chains. It should do one-time
  113. installation task needed (install event handlers etc.).
  114. To start a chain, do: dataCarrier copy value: data; proceed.
  115. - toView:
  116. This performs transformation of TrappedDataCarrier on its way from model to view.
  117. Should call aDataCarrier proceed to proceed to subsequent step.
  118. - toModel:
  119. This performs transformation of TrappedDataToken on its way from view to model.
  120. Should call aDataCarrier proceed to proceed to subsequent step.!
  121. !TrappedProcessor methodsFor: 'data transformation'!
  122. toModel: aDataCarrier
  123. "by default, proceed"
  124. aDataCarrier proceed
  125. !
  126. toView: aDataCarrier
  127. "by default, proceed"
  128. aDataCarrier proceed
  129. ! !
  130. !TrappedProcessor methodsFor: 'installation'!
  131. installToView: aDataCarrier toModel: anotherDataCarrier
  132. "by default, do nothing"
  133. ! !
  134. !TrappedProcessor class methodsFor: 'factory'!
  135. contents
  136. ^TrappedProcessorContents new
  137. !
  138. inputChecked
  139. ^TrappedProcessorInputChecked new
  140. !
  141. inputValue
  142. ^TrappedProcessorInputValue new
  143. ! !
  144. TrappedProcessor subclass: #TrappedProcessorBlackboard
  145. instanceVariableNames: ''
  146. package: 'Trapped-Frontend'!
  147. !TrappedProcessorBlackboard commentStamp!
  148. I am used internally to fetch data from blackboard
  149. or write it back.!
  150. !TrappedProcessorBlackboard methodsFor: 'data transformation'!
  151. toModel: aDataCarrier
  152. aDataCarrier modifyTarget
  153. ! !
  154. !TrappedProcessorBlackboard methodsFor: 'installation'!
  155. installToView: aDataCarrier toModel: anotherDataCarrier
  156. | snap |
  157. snap := anotherDataCarrier target.
  158. snap watch: [ :data |
  159. (aDataCarrier target asJQuery closest: 'html') toArray isEmpty ifTrue: [ KeyedPubSubUnsubscribe signal ].
  160. snap do: [ aDataCarrier copy value: data; proceed ] ]
  161. ! !
  162. TrappedProcessor subclass: #TrappedProcessorContents
  163. instanceVariableNames: ''
  164. package: 'Trapped-Frontend'!
  165. !TrappedProcessorContents commentStamp!
  166. I put data into target via contents: in toView:!
  167. !TrappedProcessorContents methodsFor: 'data transformation'!
  168. toView: aDataCarrier
  169. aDataCarrier toTargetContents
  170. ! !
  171. TrappedProcessor subclass: #TrappedProcessorInputChecked
  172. instanceVariableNames: ''
  173. package: 'Trapped-Frontend'!
  174. !TrappedProcessorInputChecked commentStamp!
  175. I bind to checkbox checked attribute.!
  176. !TrappedProcessorInputChecked methodsFor: 'data transformation'!
  177. toView: aDataCarrier
  178. aDataCarrier toTargetAttr: 'checked'
  179. ! !
  180. !TrappedProcessorInputChecked methodsFor: 'installation'!
  181. installToView: aDataCarrier toModel: anotherDataCarrier
  182. | brush |
  183. brush := aDataCarrier target.
  184. brush onChange: [ anotherDataCarrier copy value: (brush asJQuery attr: 'checked') notNil; proceed ]
  185. ! !
  186. TrappedProcessor subclass: #TrappedProcessorInputValue
  187. instanceVariableNames: ''
  188. package: 'Trapped-Frontend'!
  189. !TrappedProcessorInputValue commentStamp!
  190. I bind to input value.!
  191. !TrappedProcessorInputValue methodsFor: 'data transformation'!
  192. toView: aDataCarrier
  193. aDataCarrier toTargetValue
  194. ! !
  195. !TrappedProcessorInputValue methodsFor: 'installation'!
  196. installToView: aDataCarrier toModel: anotherDataCarrier
  197. | brush |
  198. brush := aDataCarrier target.
  199. brush onChange: [ anotherDataCarrier copy value: brush asJQuery val; proceed ]
  200. ! !
  201. Object subclass: #TrappedSingleton
  202. instanceVariableNames: ''
  203. package: 'Trapped-Frontend'!
  204. !TrappedSingleton methodsFor: 'action'!
  205. start: args
  206. ^ self subclassResponsibility
  207. ! !
  208. TrappedSingleton class instanceVariableNames: 'current'!
  209. !TrappedSingleton class methodsFor: 'accessing'!
  210. current
  211. ^ current ifNil: [ current := self new ]
  212. ! !
  213. !TrappedSingleton class methodsFor: 'action'!
  214. start: args
  215. self current start: args
  216. ! !
  217. TrappedSingleton subclass: #Trapped
  218. instanceVariableNames: 'registry'
  219. package: 'Trapped-Frontend'!
  220. !Trapped methodsFor: 'accessing'!
  221. byName: aString
  222. ^ registry at: aString
  223. !
  224. register: aListKeyedEntity
  225. self register: aListKeyedEntity name: aListKeyedEntity class name
  226. !
  227. register: aListKeyedEntity name: aString
  228. registry at: aString put: aListKeyedEntity
  229. ! !
  230. !Trapped methodsFor: 'action'!
  231. descend: anArray snapshotDo: aBlock
  232. | tpsc |
  233. tpsc := TrappedPathStack current.
  234. tpsc append: anArray do: [
  235. | path model |
  236. path := tpsc elements copy.
  237. model := self byName: path first.
  238. aBlock value: (TrappedSnapshot new path: path model: model)
  239. ]
  240. !
  241. start: args
  242. args do: [ :each | self register: each ].
  243. '[data-trap]' asJQuery each: [ :index :elem |
  244. | trap jq viewName modelName tokens path |
  245. jq := elem asJQuery.
  246. trap := jq attr: 'data-trap'.
  247. tokens := trap tokenize: ':'.
  248. tokens size = 1 ifTrue: [ tokens := { 'TrappedDumbView' }, tokens ].
  249. viewName := tokens first.
  250. tokens := (tokens second tokenize: ' ') select: [ :each | each notEmpty ].
  251. modelName := tokens first.
  252. path := Trapped parse: tokens allButFirst.
  253. { modelName }, path trapDescend: [(Smalltalk current at: viewName) new appendToJQuery: jq].
  254. ]
  255. ! !
  256. !Trapped methodsFor: 'initialization'!
  257. initialize
  258. super initialize.
  259. registry := #{}.
  260. ! !
  261. !Trapped class methodsFor: 'accessing'!
  262. parse: anArray
  263. ^anArray collect: [ :each |
  264. | asNum |
  265. asNum := each asNumber.
  266. asNum = asNum ifTrue: [ asNum ] ifFalse: [
  267. each first = '#' ifTrue: [ { each allButFirst } ] ifFalse: [ each ]]]
  268. ! !
  269. !Trapped class methodsFor: 'private'!
  270. envelope: envelope loop: model before: endjq tag: aSymbol do: aBlock
  271. | envjq |
  272. envjq := envelope asJQuery.
  273. model withIndexDo: [ :item :i |
  274. envelope with: [ :html | (html perform: aSymbol) trap: {i} read: aBlock ].
  275. envjq children detach insertBefore: endjq.
  276. ].
  277. envjq remove
  278. !
  279. loop: model between: start and: end tag: aSymbol do: aBlock
  280. (start asJQuery nextUntil: end element) remove.
  281. start with: [ :html | model ifNotNil: [
  282. self envelope: html div loop: model before: end asJQuery tag: aSymbol do: aBlock
  283. ]]
  284. ! !
  285. TrappedSingleton subclass: #TrappedPathStack
  286. instanceVariableNames: 'elements'
  287. package: 'Trapped-Frontend'!
  288. !TrappedPathStack methodsFor: 'accessing'!
  289. elements
  290. ^elements
  291. ! !
  292. !TrappedPathStack methodsFor: 'descending'!
  293. append: anArray do: aBlock
  294. self with: elements, anArray do: aBlock
  295. !
  296. with: anArray do: aBlock
  297. | old |
  298. old := elements.
  299. [ elements := anArray.
  300. aBlock value ] ensure: [ elements := old ]
  301. ! !
  302. !TrappedPathStack methodsFor: 'initialization'!
  303. initialize
  304. super initialize.
  305. elements := #().
  306. ! !
  307. Object subclass: #TrappedSnapshot
  308. instanceVariableNames: 'path model'
  309. package: 'Trapped-Frontend'!
  310. !TrappedSnapshot methodsFor: 'accessing'!
  311. model
  312. ^model
  313. !
  314. path
  315. ^path
  316. !
  317. path: anArray model: aTrappedMW
  318. path := anArray.
  319. model := aTrappedMW
  320. ! !
  321. !TrappedSnapshot methodsFor: 'action'!
  322. do: aBlock
  323. TrappedPathStack current with: path do: [ aBlock value: model ]
  324. !
  325. modify: aBlock
  326. self model modify: self path allButFirst do: aBlock
  327. !
  328. watch: aBlock
  329. self model watch: self path allButFirst do: aBlock
  330. ! !
  331. !Array methodsFor: '*Trapped-Frontend'!
  332. trapDescend: aBlock
  333. Trapped current descend: self snapshotDo: aBlock
  334. ! !
  335. !HTMLCanvas methodsFor: '*Trapped-Frontend'!
  336. trapIter: path tag: aSymbol do: aBlock
  337. | start end |
  338. self with: [ :html | start := html script. end := html script ].
  339. start trap: path read: [ :model |
  340. Trapped loop: model between: start and: end tag: aSymbol do: aBlock.
  341. ]
  342. ! !
  343. !TagBrush methodsFor: '*Trapped-Frontend'!
  344. trap: path
  345. self trap: path processors: #(contents)
  346. !
  347. trap: path processors: anArray
  348. path trapDescend: [ :snap |
  349. (TrappedDataChain newFromProcessorNames: anArray)
  350. forSnapshot: snap andBrush: self ]
  351. !
  352. trap: path read: aBlock
  353. path trapDescend: [ :snap |
  354. snap watch: [ :data |
  355. (self asJQuery closest: 'html') toArray isEmpty ifTrue: [ KeyedPubSubUnsubscribe signal ].
  356. snap do: [ self with: [ :html | aBlock value: data value: html ] ]
  357. ]
  358. ]
  359. !
  360. trapGuard: anArray contents: aBlock
  361. #() trapDescend: [ :snap |
  362. | shown |
  363. shown := nil.
  364. self trap: anArray read: [ :gdata |
  365. | sanitized |
  366. sanitized := gdata ifNil: [ false ].
  367. shown = sanitized ifFalse: [
  368. shown := sanitized.
  369. shown
  370. ifTrue: [ snap do: [ self contents: aBlock ]. self asJQuery show ]
  371. ifFalse: [ self asJQuery hide; empty ] ] ] ]
  372. ! !