Trapped-Frontend.st 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 initialize:
  38. payload
  39. dispatcher (with a subclass of TrappedDispatcher)!
  40. !TrappedModelWrapper methodsFor: 'accessing'!
  41. dispatcher
  42. ^dispatcher
  43. !
  44. dispatcher: aDispatcher
  45. dispatcher := aDispatcher
  46. !
  47. name
  48. ^ self class name
  49. !
  50. payload
  51. ^payload
  52. !
  53. payload: anObject
  54. payload := anObject.
  55. self dispatcher changed: #()
  56. ! !
  57. !TrappedModelWrapper methodsFor: 'action'!
  58. start
  59. Trapped current register: self name: self name
  60. !
  61. watch: path do: aBlock
  62. self dispatcher on: path hook: [ self read: path do: aBlock ]
  63. ! !
  64. !TrappedModelWrapper class methodsFor: 'action'!
  65. start
  66. ^self new start; yourself
  67. ! !
  68. Object subclass: #TrappedSingleton
  69. instanceVariableNames: ''
  70. package: 'Trapped-Frontend'!
  71. !TrappedSingleton methodsFor: 'action'!
  72. start
  73. ^ self subclassResponsibility
  74. ! !
  75. TrappedSingleton class instanceVariableNames: 'current'!
  76. !TrappedSingleton class methodsFor: 'accessing'!
  77. current
  78. ^ current ifNil: [ current := self new ]
  79. ! !
  80. !TrappedSingleton class methodsFor: 'action'!
  81. start
  82. self current start
  83. ! !
  84. TrappedSingleton subclass: #Trapped
  85. instanceVariableNames: 'registry'
  86. package: 'Trapped-Frontend'!
  87. !Trapped methodsFor: 'accessing'!
  88. byName: aString
  89. ^ registry at: aString
  90. !
  91. register: aFly name: aString
  92. registry at: aString put: aFly
  93. ! !
  94. !Trapped methodsFor: 'action'!
  95. start
  96. '[data-trap]' asJQuery each: [ :index :elem |
  97. | trap jq viewName modelName tokens path |
  98. jq := elem asJQuery.
  99. trap := jq attr: 'data-trap'.
  100. tokens := trap tokenize: ':'.
  101. tokens size = 1 ifTrue: [ tokens := { 'TrappedDumbView' }, tokens ].
  102. viewName := tokens first.
  103. tokens := (tokens second tokenize: ' ') select: [ :each | each notEmpty ].
  104. modelName := tokens first.
  105. path := Trapped parse: tokens allButFirst.
  106. { modelName }, path trapDescend: [(Smalltalk current at: viewName) new appendToJQuery: jq].
  107. ]
  108. ! !
  109. !Trapped methodsFor: 'initialization'!
  110. initialize
  111. super initialize.
  112. registry := #{}.
  113. ! !
  114. !Trapped class methodsFor: 'accessing'!
  115. parse: anArray
  116. ^anArray collect: [ :each |
  117. | asNum |
  118. <asNum = parseInt(each)>.
  119. asNum = asNum ifTrue: [ asNum ] ifFalse: [
  120. each first = '#' ifTrue: [ each allButFirst asSymbol ] ifFalse: [ each ]]]
  121. !
  122. path
  123. ^TrappedPathStack current elements
  124. ! !
  125. TrappedSingleton subclass: #TrappedPathStack
  126. instanceVariableNames: 'elements'
  127. package: 'Trapped-Frontend'!
  128. !TrappedPathStack methodsFor: 'accessing'!
  129. elements
  130. ^elements
  131. ! !
  132. !TrappedPathStack methodsFor: 'descending'!
  133. append: anArray
  134. elements := elements, anArray
  135. !
  136. with: anArray do: aBlock
  137. | old |
  138. old := elements.
  139. [ self append: anArray.
  140. aBlock value ] ensure: [ elements := old ]
  141. ! !
  142. !TrappedPathStack methodsFor: 'initialization'!
  143. initialize
  144. super initialize.
  145. elements := #().
  146. ! !
  147. KeyedSubscriptionBase subclass: #TrappedSubscription
  148. instanceVariableNames: ''
  149. package: 'Trapped-Frontend'!
  150. !TrappedSubscription methodsFor: 'testing'!
  151. accepts: aKey
  152. ^aKey size <= key size and: [aKey = (key copyFrom: 1 to: aKey size)]
  153. ! !
  154. !Array methodsFor: '*Trapped-Frontend'!
  155. trapDescend: aBlock
  156. TrappedPathStack current with: self do: aBlock
  157. ! !
  158. !Array methodsFor: '*Trapped-Frontend'!
  159. trapDescend: aBlock
  160. TrappedPathStack current with: self do: aBlock
  161. ! !
  162. !TagBrush methodsFor: '*Trapped-Frontend'!
  163. trap: path read: aBlock
  164. path trapDescend: [ | actual model |
  165. actual := Trapped path.
  166. model := Trapped current byName: actual first.
  167. model watch: actual allButFirst do: [ :data |
  168. (self asJQuery closest: 'html') toArray isEmpty ifTrue: [ KeyedPubSubUnsubscribe signal ].
  169. actual trapDescend: [ self with: [ :html | aBlock value: data value: html ] ]
  170. ]
  171. ]
  172. !
  173. trap: path toggle: aBlock
  174. self trap: path toggle: aBlock ifNotPresent: [ self asJQuery hide ]
  175. !
  176. trap: path toggle: aBlock ifNotPresent: anotherBlock
  177. | shown |
  178. shown := nil.
  179. self trap: path read: [ :data : html |
  180. shown = data notNil ifFalse: [
  181. shown := data notNil.
  182. self asJQuery empty; show.
  183. (shown ifTrue: [aBlock] ifFalse: [anotherBlock]) value: data value: html.
  184. ]
  185. ]
  186. !
  187. trapIter: path tag: aSymbol do: aBlock
  188. self trap: path read: [ :model :html |
  189. html root empty.
  190. model ifNotNil: [ model withIndexDo: [ :item :i |
  191. (html perform: aSymbol) trap: {i} read: aBlock
  192. ]]
  193. ]
  194. !
  195. trapShow: path
  196. self trapShow: path default: []
  197. !
  198. trapShow: path default: anObject
  199. self trap: path read: [ :model | self empty; with: (model ifNil: [anObject]) ]
  200. ! !