Trapped-Frontend.st 10 KB

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