Trapped-Frontend.st 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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: aProcessingChain
  7. chain := aProcessingChain
  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: 'initialization'!
  22. initialize
  23. super initialize.
  24. model := true
  25. ! !
  26. !TrappedDataCarrier class methodsFor: 'not yet classified'!
  27. on: aProcessingChain target: anObject
  28. ^self new
  29. chain: aProcessingChain;
  30. target: anObject;
  31. yourself
  32. ! !
  33. TrappedDataCarrier subclass: #TrappedDataCarrierToModel
  34. instanceVariableNames: 'index'
  35. package: 'Trapped-Frontend'!
  36. !TrappedDataCarrierToModel methodsFor: 'not yet classified'!
  37. proceed
  38. index := index ifNil: [ chain lastProcessorNo ] ifNotNil: [ index - 1 ].
  39. (chain processorNo: index) toModel: self
  40. ! !
  41. TrappedDataCarrier subclass: #TrappedDataCarrierToView
  42. instanceVariableNames: 'index'
  43. package: 'Trapped-Frontend'!
  44. !TrappedDataCarrierToView methodsFor: 'not yet classified'!
  45. proceed
  46. index := index ifNil: [ chain firstProcessorNo ] ifNotNil: [ index + 1 ].
  47. (chain processorNo: index) toView: self
  48. ! !
  49. Object subclass: #TrappedProcessingChain
  50. instanceVariableNames: 'processors'
  51. package: 'Trapped-Frontend'!
  52. !TrappedProcessingChain methodsFor: 'accessing'!
  53. firstProcessorNo
  54. ^1
  55. !
  56. lastProcessorNo
  57. ^processors size
  58. !
  59. processorNo: aNumber
  60. ^processors at: aNumber
  61. !
  62. processors: anArray
  63. processors := anArray
  64. ! !
  65. !TrappedProcessingChain methodsFor: 'action'!
  66. forSnapshot: aSnapshot andBrush: aTagBrush
  67. | toViewCarrier toModelCarrier |
  68. toViewCarrier := TrappedDataCarrierToView on: self target: aTagBrush.
  69. toModelCarrier := TrappedDataCarrierToModel on: self target: aSnapshot.
  70. processors do: [ :each | each installToView: toViewCarrier toModel: toModelCarrier ].
  71. toViewCarrier value = true ifTrue: [ toViewCarrier copy proceed ]
  72. ! !
  73. !TrappedProcessingChain class methodsFor: 'instance creation'!
  74. new: anArray
  75. (anArray anySatisfy: [ :each | each isExpectingModelData ])
  76. ifFalse: [ anArray add: self dataTerminator ]
  77. ifTrue: [ anArray addFirst: self blackboardReaderWriter ].
  78. ^self new
  79. processors: anArray;
  80. yourself
  81. !
  82. newFromProcessorSpecs: anArray
  83. ^self new: ((anArray ifEmpty: [ #(contents) ]) collect: [ :each | each isString
  84. ifTrue: [ TrappedProcessor perform: each ]
  85. ifFalse: [
  86. | selector args |
  87. selector := ''.
  88. args := #().
  89. each withIndexDo: [ :element :index | index odd
  90. ifTrue: [ selector := selector, element ]
  91. ifFalse: [ selector := selector, ':'. args add: element ] ].
  92. TrappedProcessor perform: selector withArguments: args ] ])
  93. ! !
  94. !TrappedProcessingChain class methodsFor: 'private'!
  95. blackboardReaderWriter
  96. ^TrappedProcessorBlackboard new
  97. !
  98. dataTerminator
  99. ^TrappedProcessorTerminator new
  100. ! !
  101. Object subclass: #TrappedProcessor
  102. instanceVariableNames: ''
  103. package: 'Trapped-Frontend'!
  104. !TrappedProcessor commentStamp!
  105. I am a processing step in TrappedProcessingChain.
  106. I am stateless flyweight (aka servant)
  107. and will get all necessary data as arguments in API calls.
  108. My public API is:
  109. - installToView:toModel:
  110. This gets two TrappedDataCarriers set up without actual data
  111. and at the beginning of their chains. It should do one-time
  112. installation task needed (install event handlers etc.).
  113. To start a chain, do: dataCarrier copy value: data; proceed.
  114. - toView:
  115. This performs transformation of TrappedDataCarrier on its way from model to view.
  116. Should call aDataCarrier proceed to proceed to subsequent step.
  117. - toModel:
  118. This performs transformation of TrappedDataCarrier on its way from view to model.
  119. Should call aDataCarrier proceed to proceed to subsequent step.!
  120. !TrappedProcessor methodsFor: 'data transformation'!
  121. toModel: aDataCarrier
  122. "by default, proceed"
  123. aDataCarrier proceed
  124. !
  125. toView: aDataCarrier
  126. "by default, proceed"
  127. aDataCarrier proceed
  128. ! !
  129. !TrappedProcessor methodsFor: 'installation'!
  130. installToView: aDataCarrier toModel: anotherDataCarrier
  131. "by default, do nothing"
  132. ! !
  133. !TrappedProcessor methodsFor: 'testing'!
  134. isExpectingModelData
  135. ^false
  136. ! !
  137. !TrappedProcessor class methodsFor: 'factory'!
  138. contents
  139. ^TrappedProcessorContents new
  140. ! !
  141. TrappedProcessor subclass: #TrappedDataExpectingProcessor
  142. instanceVariableNames: ''
  143. package: 'Trapped-Frontend'!
  144. !TrappedDataExpectingProcessor commentStamp!
  145. I answer true to isExpectingModelData and serve as a base class
  146. for processor that present / change model data.
  147. When at least one of my instances is present in the chain,
  148. automatic databinding processor is added at the beginning
  149. (the data-binding scenario); otherwise, the chain
  150. is run immediately with true as data (run-once scenario).!
  151. !TrappedDataExpectingProcessor methodsFor: 'testing'!
  152. isExpectingModelData
  153. ^true
  154. ! !
  155. TrappedDataExpectingProcessor subclass: #TrappedProcessorContents
  156. instanceVariableNames: ''
  157. package: 'Trapped-Frontend'!
  158. !TrappedProcessorContents commentStamp!
  159. I put data into target via contents: in toView:!
  160. !TrappedProcessorContents methodsFor: 'data transformation'!
  161. toView: aDataCarrier
  162. aDataCarrier toTargetContents
  163. ! !
  164. TrappedProcessor subclass: #TrappedProcessorBlackboard
  165. instanceVariableNames: ''
  166. package: 'Trapped-Frontend'!
  167. !TrappedProcessorBlackboard commentStamp!
  168. I am used internally to fetch data from blackboard
  169. or write it back.
  170. I am added to the beginning of the chain
  171. when the chain contains at least one element
  172. that isExpectingModelData (see TrappedDataExpectingProcessor).!
  173. !TrappedProcessorBlackboard methodsFor: 'data transformation'!
  174. toModel: aDataCarrier
  175. aDataCarrier modifyTarget
  176. ! !
  177. !TrappedProcessorBlackboard methodsFor: 'installation'!
  178. installToView: aDataCarrier toModel: anotherDataCarrier
  179. | snap |
  180. snap := anotherDataCarrier target.
  181. snap watch: [ :data |
  182. (aDataCarrier target asJQuery closest: 'html') toArray isEmpty ifTrue: [ KeyedPubSubUnsubscribe signal ].
  183. snap do: [ aDataCarrier copy value: data; proceed ] ].
  184. aDataCarrier value: false
  185. ! !
  186. TrappedProcessor subclass: #TrappedProcessorTerminator
  187. instanceVariableNames: ''
  188. package: 'Trapped-Frontend'!
  189. !TrappedProcessorTerminator commentStamp!
  190. I do not proceed in toView:.
  191. I am added automatically to end of chain when it does not contain
  192. any element that isExpectingModelData (see TrappedDataExpectingProcessor).!
  193. !TrappedProcessorTerminator methodsFor: 'data transformation'!
  194. toView: aDataCarrier
  195. "stop"
  196. ! !
  197. Object subclass: #TrappedSingleton
  198. instanceVariableNames: ''
  199. package: 'Trapped-Frontend'!
  200. !TrappedSingleton methodsFor: 'action'!
  201. start: args
  202. ^ self subclassResponsibility
  203. ! !
  204. TrappedSingleton class instanceVariableNames: 'current'!
  205. !TrappedSingleton class methodsFor: 'accessing'!
  206. current
  207. ^ current ifNil: [ current := self new ]
  208. ! !
  209. !TrappedSingleton class methodsFor: 'action'!
  210. start: args
  211. self current start: args
  212. ! !
  213. TrappedSingleton subclass: #Trapped
  214. instanceVariableNames: 'registry'
  215. package: 'Trapped-Frontend'!
  216. !Trapped methodsFor: 'accessing'!
  217. byName: aString
  218. ^ registry at: aString
  219. !
  220. register: aListKeyedEntity
  221. self register: aListKeyedEntity name: aListKeyedEntity class name
  222. !
  223. register: aListKeyedEntity name: aString
  224. registry at: aString put: aListKeyedEntity
  225. ! !
  226. !Trapped methodsFor: 'action'!
  227. descend: anArray snapshotDo: aBlock
  228. | tpsc |
  229. tpsc := TrappedPathStack current.
  230. tpsc append: anArray do: [
  231. | path model |
  232. path := tpsc elements copy.
  233. model := self byName: path first.
  234. aBlock value: (TrappedSnapshot new path: path model: model)
  235. ]
  236. !
  237. injectToJQuery: aJQuery
  238. aJQuery each: [ :index :elem |
  239. | jq |
  240. jq := elem asJQuery.
  241. (jq is: '[data-trap]')
  242. ifTrue: [
  243. | parsed |
  244. parsed := Trapped parse: (jq attr: 'data-trap').
  245. jq removeAttr: 'data-trap'.
  246. parsed do: [ :rule |
  247. (HTMLCanvas onJQuery: jq) root trap: rule first processors: (rule at: 2 ifAbsent: [#()]) ] ].
  248. self injectToJQuery: jq children ]
  249. !
  250. start: args
  251. args do: [ :each | self register: each ].
  252. self injectToJQuery: 'html' asJQuery
  253. ! !
  254. !Trapped methodsFor: 'initialization'!
  255. initialize
  256. super initialize.
  257. registry := #{}.
  258. ! !
  259. !Trapped class methodsFor: 'accessing'!
  260. parse: aString
  261. ^ (aString tokenize: '.') collect: [ :rule |
  262. (rule tokenize: ':') collect: [ :message |
  263. | result stack anArray |
  264. anArray := message tokenize: ' '.
  265. result := #().
  266. stack := { result }.
  267. anArray do: [ :each |
  268. | asNum inner close |
  269. close := 0.
  270. inner := each.
  271. [ inner notEmpty and: [ inner first = '(' ]] whileTrue: [ inner := inner allButFirst. stack add: (stack last add: #()) ].
  272. [ inner notEmpty and: [ inner last = ')' ]] whileTrue: [ inner := inner allButLast. close := close + 1 ].
  273. (inner notEmpty and: [ inner first = '#' ]) ifTrue: [ inner := { inner allButFirst } ].
  274. asNum := inner isString ifTrue: [ (inner ifEmpty: [ 'NaN' ]) asNumber ] ifFalse: [ inner ].
  275. asNum = asNum ifTrue: [ stack last add: asNum ] ifFalse: [
  276. inner ifNotEmpty: [ stack last add: inner ] ].
  277. close timesRepeat: [ stack removeLast ] ].
  278. result ] ]
  279. ! !
  280. !Trapped class methodsFor: 'private'!
  281. envelope: envelope loop: model before: endjq do: aBlock
  282. | envjq |
  283. envjq := envelope asJQuery.
  284. model withIndexDo: [ :item :i |
  285. {i} trapDescend: [ envelope with: aBlock ].
  286. envjq children detach insertBefore: endjq.
  287. ].
  288. envjq remove
  289. !
  290. loop: model between: start and: end do: aBlock
  291. (start asJQuery nextUntil: end element) remove.
  292. start with: [ :html | model ifNotNil: [
  293. self envelope: html div loop: model before: end asJQuery do: aBlock
  294. ]]
  295. ! !
  296. TrappedSingleton subclass: #TrappedPathStack
  297. instanceVariableNames: 'elements'
  298. package: 'Trapped-Frontend'!
  299. !TrappedPathStack methodsFor: 'accessing'!
  300. elements
  301. ^elements
  302. ! !
  303. !TrappedPathStack methodsFor: 'descending'!
  304. append: anArray do: aBlock
  305. self with: elements, anArray do: aBlock
  306. !
  307. with: anArray do: aBlock
  308. | old |
  309. old := elements.
  310. [ elements := anArray.
  311. aBlock value ] ensure: [ elements := old ]
  312. ! !
  313. !TrappedPathStack methodsFor: 'initialization'!
  314. initialize
  315. super initialize.
  316. elements := #().
  317. ! !
  318. Object subclass: #TrappedSnapshot
  319. instanceVariableNames: 'path model'
  320. package: 'Trapped-Frontend'!
  321. !TrappedSnapshot methodsFor: 'accessing'!
  322. model
  323. ^model
  324. !
  325. path
  326. ^path
  327. !
  328. path: anArray model: aTrappedMW
  329. path := anArray.
  330. model := aTrappedMW
  331. ! !
  332. !TrappedSnapshot methodsFor: 'action'!
  333. do: aBlock
  334. TrappedPathStack current with: path do: [ aBlock value: model ]
  335. !
  336. modify: aBlock
  337. self model modify: self path allButFirst do: aBlock
  338. !
  339. watch: aBlock
  340. self model watch: self path allButFirst do: aBlock
  341. ! !
  342. !Array methodsFor: '*Trapped-Frontend'!
  343. trapDescend: aBlock
  344. Trapped current descend: self snapshotDo: aBlock
  345. ! !
  346. !HTMLCanvas methodsFor: '*Trapped-Frontend'!
  347. trapIter: path do: aBlock
  348. self with: [ :html | html noscript trapIter: path after: aBlock ]
  349. ! !
  350. !TagBrush methodsFor: '*Trapped-Frontend'!
  351. trap: path
  352. self trap: path processors: #()
  353. !
  354. trap: path processors: anArray
  355. path trapDescend: [ :snap |
  356. (TrappedProcessingChain newFromProcessorSpecs: anArray)
  357. forSnapshot: snap andBrush: self ]
  358. !
  359. trap: path read: aBlock
  360. path trapDescend: [ :snap |
  361. snap watch: [ :data |
  362. (self asJQuery closest: 'html') toArray isEmpty ifTrue: [ KeyedPubSubUnsubscribe signal ].
  363. snap do: [ self with: [ :html | aBlock value: data value: html ] ]
  364. ]
  365. ]
  366. !
  367. trapGuard: anArray contents: aBlock
  368. #() trapDescend: [ :snap |
  369. | shown |
  370. shown := nil.
  371. self trap: anArray read: [ :gdata |
  372. | sanitized |
  373. sanitized := gdata ifNil: [ false ].
  374. shown = sanitized ifFalse: [
  375. shown := sanitized.
  376. shown
  377. ifTrue: [ snap do: [ self contents: aBlock ]. self asJQuery show ]
  378. ifFalse: [ self asJQuery hide; empty ] ] ] ]
  379. !
  380. trapIter: path after: aBlock
  381. | end |
  382. end := TagBrush fromJQuery: ('<noscript />' asJQuery insertAfter: self asJQuery) canvas: canvas.
  383. self trap: path read: [ :model |
  384. Trapped loop: model between: self and: end do: aBlock.
  385. ]
  386. ! !