Trapped-Frontend.st 10 KB

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