Trapped-Frontend.st 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. Smalltalk current createPackage: 'Trapped-Frontend' properties: #{}!
  2. KeyedPubSubBase subclass: #TrappedDispatcher
  3. instanceVariableNames: ''
  4. package: 'Trapped-Frontend'!
  5. !TrappedDispatcher commentStamp!
  6. I am base class for change event dispatchers.
  7. I manage changed path - action block subscriptions.
  8. These subscription are instances of TrappedSubscription
  9. My subclasses need to provide implementation for:
  10. add:
  11. do:
  12. clean
  13. (optionally) run!
  14. !TrappedDispatcher methodsFor: 'action'!
  15. subscriptionKey: key block: aBlock
  16. ^TrappedSubscription new key: key block: aBlock; yourself
  17. ! !
  18. Widget subclass: #TrappedDumbView
  19. instanceVariableNames: ''
  20. package: 'Trapped-Frontend'!
  21. !TrappedDumbView commentStamp!
  22. I just read and show an actual path.!
  23. !TrappedDumbView methodsFor: 'rendering'!
  24. renderOn: html
  25. html root trapShow: #()
  26. ! !
  27. Object subclass: #TrappedModelWrapper
  28. instanceVariableNames: 'dispatcher payload'
  29. package: 'Trapped-Frontend'!
  30. !TrappedModelWrapper commentStamp!
  31. I am base class for model wrappers.
  32. I wrap a model which can be any object.
  33. My subclasses need to provide implementation for:
  34. read:do:
  35. modify:do:
  36. (optionally) name
  37. and must issue these call when initializing:
  38. model:
  39. dispatcher: (with a subclass of TrappedDispatcher)!
  40. !TrappedModelWrapper methodsFor: 'accessing'!
  41. dispatcher
  42. ^dispatcher
  43. !
  44. dispatcher: aDispatcher
  45. dispatcher := aDispatcher
  46. !
  47. model: anObject
  48. payload := anObject.
  49. self dispatcher changed: #()
  50. !
  51. name
  52. ^ self class name
  53. ! !
  54. !TrappedModelWrapper methodsFor: 'action'!
  55. start
  56. Trapped current register: self name: self name
  57. !
  58. watch: path do: aBlock
  59. self dispatcher on: path hook: [ self read: path do: aBlock ]
  60. ! !
  61. !TrappedModelWrapper class methodsFor: 'action'!
  62. start
  63. ^self new start; yourself
  64. ! !
  65. TrappedModelWrapper subclass: #TrappedMWDirect
  66. instanceVariableNames: ''
  67. package: 'Trapped-Frontend'!
  68. !TrappedMWDirect commentStamp!
  69. I am TrappedModelWrapper that directly manipulate
  70. the object passed to model:!
  71. !TrappedMWDirect methodsFor: 'action'!
  72. modify: path do: aBlock
  73. | newValue eavModel |
  74. eavModel := path asEavModel.
  75. newValue := aBlock value: (eavModel on: payload).
  76. [ eavModel on: payload put: newValue ] ensure: [ self dispatcher changed: path ]
  77. !
  78. read: path do: aBlock
  79. | eavModel |
  80. eavModel := path asEavModel.
  81. aBlock value: (eavModel on: payload)
  82. ! !
  83. TrappedModelWrapper subclass: #TrappedMWIsolated
  84. instanceVariableNames: ''
  85. package: 'Trapped-Frontend'!
  86. !TrappedMWIsolated commentStamp!
  87. I am TrappedModelWrapper than wrap access
  88. to an object passed to model: via Isolator.!
  89. !TrappedMWIsolated methodsFor: 'accessing'!
  90. model: anObject
  91. super model: (Isolator on: anObject)
  92. ! !
  93. !TrappedMWIsolated methodsFor: 'action'!
  94. modify: path do: aBlock
  95. | eavModel |
  96. eavModel := ({#root},path) asEavModel.
  97. [ payload model: eavModel modify: aBlock ] ensure: [ self dispatcher changed: path ]
  98. !
  99. read: path do: aBlock
  100. | eavModel |
  101. eavModel := ({#root},path) asEavModel.
  102. payload model: eavModel read: aBlock
  103. ! !
  104. Object subclass: #TrappedSingleton
  105. instanceVariableNames: ''
  106. package: 'Trapped-Frontend'!
  107. !TrappedSingleton methodsFor: 'action'!
  108. start
  109. ^ self subclassResponsibility
  110. ! !
  111. TrappedSingleton class instanceVariableNames: 'current'!
  112. !TrappedSingleton class methodsFor: 'accessing'!
  113. current
  114. ^ current ifNil: [ current := self new ]
  115. ! !
  116. !TrappedSingleton class methodsFor: 'action'!
  117. start
  118. self current start
  119. ! !
  120. TrappedSingleton subclass: #Trapped
  121. instanceVariableNames: 'registry'
  122. package: 'Trapped-Frontend'!
  123. !Trapped methodsFor: 'accessing'!
  124. byName: aString
  125. ^ registry at: aString
  126. !
  127. register: aFly name: aString
  128. registry at: aString put: aFly
  129. ! !
  130. !Trapped methodsFor: 'action'!
  131. start
  132. '[data-trap]' asJQuery each: [ :index :elem |
  133. | trap jq viewName modelName tokens path |
  134. jq := elem asJQuery.
  135. trap := jq attr: 'data-trap'.
  136. tokens := trap tokenize: ':'.
  137. tokens size = 1 ifTrue: [ tokens := { 'TrappedDumbView' }, tokens ].
  138. viewName := tokens first.
  139. tokens := (tokens second tokenize: ' ') select: [ :each | each notEmpty ].
  140. modelName := tokens first.
  141. path := Trapped parse: tokens allButFirst.
  142. { modelName }, path trapDescend: [(Smalltalk current at: viewName) new appendToJQuery: jq].
  143. ]
  144. ! !
  145. !Trapped methodsFor: 'initialization'!
  146. initialize
  147. super initialize.
  148. registry := #{}.
  149. ! !
  150. !Trapped class methodsFor: 'accessing'!
  151. parse: anArray
  152. ^anArray collect: [ :each |
  153. | asNum |
  154. <asNum = parseInt(each)>.
  155. asNum = asNum ifTrue: [ asNum ] ifFalse: [
  156. each first = '#' ifTrue: [ each allButFirst asSymbol ] ifFalse: [ each ]]]
  157. !
  158. path
  159. ^TrappedPathStack current elements
  160. ! !
  161. TrappedSingleton subclass: #TrappedPathStack
  162. instanceVariableNames: 'elements'
  163. package: 'Trapped-Frontend'!
  164. !TrappedPathStack methodsFor: 'accessing'!
  165. elements
  166. ^elements
  167. ! !
  168. !TrappedPathStack methodsFor: 'descending'!
  169. append: anArray
  170. elements := elements, anArray
  171. !
  172. with: anArray do: aBlock
  173. | old |
  174. old := elements.
  175. [ self append: anArray.
  176. aBlock value ] ensure: [ elements := old ]
  177. ! !
  178. !TrappedPathStack methodsFor: 'initialization'!
  179. initialize
  180. super initialize.
  181. elements := #().
  182. ! !
  183. KeyedSubscriptionBase subclass: #TrappedSubscription
  184. instanceVariableNames: ''
  185. package: 'Trapped-Frontend'!
  186. !TrappedSubscription methodsFor: 'testing'!
  187. accepts: aKey
  188. ^aKey size <= key size and: [aKey = (key copyFrom: 1 to: aKey size)]
  189. ! !
  190. !Array methodsFor: '*Trapped-Frontend'!
  191. trapDescend: aBlock
  192. TrappedPathStack current with: self do: aBlock
  193. ! !
  194. !Array methodsFor: '*Trapped-Frontend'!
  195. trapDescend: aBlock
  196. TrappedPathStack current with: self do: aBlock
  197. ! !
  198. !TagBrush methodsFor: '*Trapped-Frontend'!
  199. trap: path read: aBlock
  200. path trapDescend: [ | actual model |
  201. actual := Trapped path.
  202. model := Trapped current byName: actual first.
  203. model watch: actual allButFirst do: [ :data |
  204. (self asJQuery closest: 'html') toArray isEmpty ifTrue: [ KeyedPubSubUnsubscribe signal ].
  205. actual trapDescend: [ self with: [ :html | aBlock value: data value: html ] ]
  206. ]
  207. ]
  208. !
  209. trap: path toggle: aBlock
  210. self trap: path toggle: aBlock ifNotPresent: [ self asJQuery hide ]
  211. !
  212. trap: path toggle: aBlock ifNotPresent: anotherBlock
  213. | shown |
  214. shown := nil.
  215. self trap: path read: [ :data : html |
  216. shown = data notNil ifFalse: [
  217. shown := data notNil.
  218. self asJQuery empty; show.
  219. (shown ifTrue: [aBlock] ifFalse: [anotherBlock]) value: data value: html.
  220. ]
  221. ]
  222. !
  223. trapIter: path tag: aSymbol do: aBlock
  224. self trap: path read: [ :model :html |
  225. html root empty.
  226. model ifNotNil: [ model withIndexDo: [ :item :i |
  227. (html perform: aSymbol) trap: {i} read: aBlock
  228. ]]
  229. ]
  230. !
  231. trapShow: path
  232. self trapShow: path default: []
  233. !
  234. trapShow: path default: anObject
  235. self trap: path read: [ :model | self empty; with: (model ifNil: [anObject]) ]
  236. ! !