Trapped-Frontend.st 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. dataToView: aBlock
  142. ^TrappedProcessorDataAdhoc newToView: aBlock
  143. !
  144. guardContents: anArray
  145. ^TrappedProcessorGuardContents new: anArray
  146. !
  147. guardProc: anArray
  148. ^TrappedProcessorGuardProc new: anArray
  149. !
  150. inputChecked
  151. ^TrappedProcessorInputChecked new
  152. !
  153. inputValue
  154. ^TrappedProcessorInputValue new
  155. !
  156. loopContents
  157. ^TrappedProcessorLoopContents new
  158. !
  159. loopProc
  160. ^TrappedProcessorLoopProc new
  161. !
  162. path
  163. ^TrappedProcessorDescend new
  164. !
  165. signal: aString
  166. ^TrappedProcessorSignal new: aString
  167. !
  168. whenClicked
  169. ^TrappedProcessorWhenClicked new
  170. !
  171. whenSubmitted
  172. ^TrappedProcessorWhenSubmitted new
  173. !
  174. widget: aString
  175. ^TrappedProcessorWidget new: aString
  176. ! !
  177. Object subclass: #TrappedSingleton
  178. instanceVariableNames: ''
  179. package: 'Trapped-Frontend'!
  180. !TrappedSingleton methodsFor: 'action'!
  181. start: args
  182. ^ self subclassResponsibility
  183. ! !
  184. TrappedSingleton class instanceVariableNames: 'current'!
  185. !TrappedSingleton class methodsFor: 'accessing'!
  186. current
  187. ^ current ifNil: [ current := self new ]
  188. ! !
  189. !TrappedSingleton class methodsFor: 'action'!
  190. start: args
  191. self current start: args
  192. ! !
  193. TrappedSingleton subclass: #Trapped
  194. instanceVariableNames: 'registry'
  195. package: 'Trapped-Frontend'!
  196. !Trapped methodsFor: 'accessing'!
  197. byName: aString
  198. ^ registry at: aString
  199. !
  200. register: aListKeyedEntity
  201. self register: aListKeyedEntity name: aListKeyedEntity class name
  202. !
  203. register: aListKeyedEntity name: aString
  204. registry at: aString put: aListKeyedEntity
  205. ! !
  206. !Trapped methodsFor: 'action'!
  207. descend: anArray snapshotDo: aBlock
  208. | tpsc |
  209. tpsc := TrappedPathStack current.
  210. tpsc append: anArray do: [
  211. | path model |
  212. path := tpsc elements copy.
  213. model := self byName: path first.
  214. aBlock value: (TrappedSnapshot new path: path model: model)
  215. ]
  216. !
  217. injectToJQuery: aJQuery
  218. aJQuery each: [ :index :elem |
  219. | jq |
  220. jq := elem asJQuery.
  221. (jq is: '[data-trap]')
  222. ifTrue: [
  223. | parsed |
  224. parsed := Trapped parse: (jq attr: 'data-trap').
  225. jq removeAttr: 'data-trap'.
  226. parsed do: [ :rule |
  227. (HTMLCanvas onJQuery: jq) root trap: rule first processors: (rule at: 2 ifAbsent: [#()]) ] ].
  228. self injectToJQuery: jq children ]
  229. !
  230. start: args
  231. args do: [ :each | self register: each ].
  232. self injectToJQuery: 'html' asJQuery
  233. ! !
  234. !Trapped methodsFor: 'initialization'!
  235. initialize
  236. super initialize.
  237. registry := #{}.
  238. ! !
  239. !Trapped class methodsFor: 'accessing'!
  240. parse: aString
  241. ^ (aString tokenize: '.') collect: [ :rule |
  242. (rule tokenize: ':') collect: [ :message |
  243. | result stack anArray |
  244. anArray := message tokenize: ' '.
  245. result := #().
  246. stack := { result }.
  247. anArray do: [ :each |
  248. | asNum inner close |
  249. close := 0.
  250. inner := each.
  251. [ inner notEmpty and: [ inner first = '(' ]] whileTrue: [ inner := inner allButFirst. stack add: (stack last add: #()) ].
  252. [ inner notEmpty and: [ inner last = ')' ]] whileTrue: [ inner := inner allButLast. close := close + 1 ].
  253. (inner notEmpty and: [ inner first = '#' ]) ifTrue: [ inner := { inner allButFirst } ].
  254. asNum := inner isString ifTrue: [ (inner ifEmpty: [ 'NaN' ]) asNumber ] ifFalse: [ inner ].
  255. asNum = asNum ifTrue: [ stack last add: asNum ] ifFalse: [
  256. inner ifNotEmpty: [ stack last add: inner ] ].
  257. close timesRepeat: [ stack removeLast ] ].
  258. result ] ]
  259. ! !
  260. !Trapped class methodsFor: 'private'!
  261. envelope: envelope loop: model before: endjq do: aBlock
  262. | envjq |
  263. envjq := envelope asJQuery.
  264. model withIndexDo: [ :item :i |
  265. {i} trapDescend: [ envelope with: aBlock ].
  266. envjq children detach insertBefore: endjq.
  267. ].
  268. envjq remove
  269. !
  270. loop: model between: start and: end do: aBlock
  271. (start asJQuery nextUntil: end element) remove.
  272. start with: [ :html | model ifNotNil: [
  273. self envelope: html div loop: model before: end asJQuery do: aBlock
  274. ]]
  275. ! !
  276. TrappedSingleton subclass: #TrappedPathStack
  277. instanceVariableNames: 'elements'
  278. package: 'Trapped-Frontend'!
  279. !TrappedPathStack methodsFor: 'accessing'!
  280. elements
  281. ^elements
  282. ! !
  283. !TrappedPathStack methodsFor: 'descending'!
  284. append: anArray do: aBlock
  285. self with: elements, anArray do: aBlock
  286. !
  287. with: anArray do: aBlock
  288. | old |
  289. old := elements.
  290. [ elements := anArray.
  291. aBlock value ] ensure: [ elements := old ]
  292. ! !
  293. !TrappedPathStack methodsFor: 'initialization'!
  294. initialize
  295. super initialize.
  296. elements := #().
  297. ! !
  298. Object subclass: #TrappedSnapshot
  299. instanceVariableNames: 'path model'
  300. package: 'Trapped-Frontend'!
  301. !TrappedSnapshot methodsFor: 'accessing'!
  302. model
  303. ^model
  304. !
  305. path
  306. ^path
  307. !
  308. path: anArray model: aTrappedMW
  309. path := anArray.
  310. model := aTrappedMW
  311. ! !
  312. !TrappedSnapshot methodsFor: 'action'!
  313. do: aBlock
  314. TrappedPathStack current with: path do: [ aBlock value: model ]
  315. !
  316. modify: aBlock
  317. self model modify: self path allButFirst do: aBlock
  318. !
  319. watch: aBlock
  320. self model watch: self path allButFirst do: aBlock
  321. ! !
  322. !Array methodsFor: '*Trapped-Frontend'!
  323. trapDescend: aBlock
  324. Trapped current descend: self snapshotDo: aBlock
  325. ! !
  326. !HTMLCanvas methodsFor: '*Trapped-Frontend'!
  327. trapIter: path do: aBlock
  328. self with: [ :html | html noscript trapIter: path after: aBlock ]
  329. ! !
  330. !TagBrush methodsFor: '*Trapped-Frontend'!
  331. trap: path
  332. self trap: path processors: #()
  333. !
  334. trap: path processors: anArray
  335. path trapDescend: [ :snap |
  336. (TrappedProcessingChain newFromProcessorSpecs: anArray)
  337. forSnapshot: snap andBrush: self ]
  338. !
  339. trap: path read: aBlock
  340. path trapDescend: [ :snap |
  341. snap watch: [ :data |
  342. (self asJQuery closest: 'html') toArray isEmpty ifTrue: [ KeyedPubSubUnsubscribe signal ].
  343. snap do: [ self with: [ :html | aBlock value: data value: html ] ]
  344. ]
  345. ]
  346. !
  347. trapGuard: anArray contents: aBlock
  348. #() trapDescend: [ :snap |
  349. | shown |
  350. shown := nil.
  351. self trap: anArray read: [ :gdata |
  352. | sanitized |
  353. sanitized := gdata ifNil: [ false ].
  354. shown = sanitized ifFalse: [
  355. shown := sanitized.
  356. shown
  357. ifTrue: [ snap do: [ self contents: aBlock ]. self asJQuery show ]
  358. ifFalse: [ self asJQuery hide; empty ] ] ] ]
  359. !
  360. trapIter: path after: aBlock
  361. | end |
  362. end := TagBrush fromJQuery: ('<noscript />' asJQuery insertAfter: self asJQuery) canvas: canvas.
  363. self trap: path read: [ :model |
  364. Trapped loop: model between: self and: end do: aBlock.
  365. ]
  366. ! !