Trapped-Frontend.st 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. Smalltalk current createPackage: 'Trapped-Frontend' properties: #{}!
  2. Object subclass: #TrappedBinder
  3. instanceVariableNames: 'brush'
  4. package: 'Trapped-Frontend'!
  5. !TrappedBinder methodsFor: 'accessing'!
  6. brush: aTagBrush
  7. brush := aTagBrush
  8. ! !
  9. !TrappedBinder methodsFor: 'action'!
  10. installFor: path
  11. brush trap: path read: self showBlock
  12. !
  13. showBlock
  14. ^[ :model | brush empty; with: (model ifNil: [[]]) ]
  15. ! !
  16. !TrappedBinder methodsFor: 'converting'!
  17. prim: anObject
  18. <return anObject.valueOf()>
  19. ! !
  20. TrappedBinder subclass: #TrappedCheckedBinder
  21. instanceVariableNames: ''
  22. package: 'Trapped-Frontend'!
  23. !TrappedCheckedBinder methodsFor: 'action'!
  24. installFor: path
  25. super installFor: path.
  26. path trapDescend: [ :snap |
  27. brush onChange: [ snap modify: [
  28. (brush asJQuery attr: 'checked') notNil
  29. ]]
  30. ]
  31. !
  32. showBlock
  33. ^[ :model | brush asJQuery attr: 'checked' put: (model ifNotNil: [ self prim: model ] ifNil: [ false ]) ]
  34. ! !
  35. TrappedBinder subclass: #TrappedValBinder
  36. instanceVariableNames: ''
  37. package: 'Trapped-Frontend'!
  38. !TrappedValBinder methodsFor: 'action'!
  39. installFor: path
  40. super installFor: path.
  41. path trapDescend: [ :snap |
  42. brush onChange: [ snap modify: [
  43. brush asJQuery val
  44. ]]
  45. ]
  46. !
  47. showBlock
  48. ^[ :model | brush asJQuery val: (model ifNotNil: [self prim: model] ifNil: [[]]) ]
  49. ! !
  50. Widget subclass: #TrappedDumbView
  51. instanceVariableNames: ''
  52. package: 'Trapped-Frontend'!
  53. !TrappedDumbView commentStamp!
  54. I just read and show an actual path.!
  55. !TrappedDumbView methodsFor: 'rendering'!
  56. renderOn: html
  57. html root trap: #()
  58. ! !
  59. Object subclass: #TrappedSingleton
  60. instanceVariableNames: ''
  61. package: 'Trapped-Frontend'!
  62. !TrappedSingleton methodsFor: 'action'!
  63. start: args
  64. ^ self subclassResponsibility
  65. ! !
  66. TrappedSingleton class instanceVariableNames: 'current'!
  67. !TrappedSingleton class methodsFor: 'accessing'!
  68. current
  69. ^ current ifNil: [ current := self new ]
  70. ! !
  71. !TrappedSingleton class methodsFor: 'action'!
  72. start: zzz
  73. self current perform: #start: withArguments: arguments
  74. ! !
  75. TrappedSingleton subclass: #Trapped
  76. instanceVariableNames: 'registry'
  77. package: 'Trapped-Frontend'!
  78. !Trapped methodsFor: 'accessing'!
  79. byName: aString
  80. ^ registry at: aString
  81. !
  82. register: aListKeyedEntity
  83. self register: aListKeyedEntity name: aListKeyedEntity class name
  84. !
  85. register: aListKeyedEntity name: aString
  86. registry at: aString put: aListKeyedEntity
  87. ! !
  88. !Trapped methodsFor: 'action'!
  89. descend: anArray snapshotDo: aBlock
  90. | tpsc |
  91. tpsc := TrappedPathStack current.
  92. tpsc append: anArray do: [
  93. | path model |
  94. path := tpsc elements copy.
  95. model := self byName: path first.
  96. aBlock value: (TrappedSnapshot new path: path model: model)
  97. ]
  98. !
  99. start: zzz
  100. | args |
  101. <args = [].slice.call(arguments)>.
  102. args do: [ :each | self register: each ].
  103. '[data-trap]' asJQuery each: [ :index :elem |
  104. | trap jq viewName modelName tokens path |
  105. jq := elem asJQuery.
  106. trap := jq attr: 'data-trap'.
  107. tokens := trap tokenize: ':'.
  108. tokens size = 1 ifTrue: [ tokens := { 'TrappedDumbView' }, tokens ].
  109. viewName := tokens first.
  110. tokens := (tokens second tokenize: ' ') select: [ :each | each notEmpty ].
  111. modelName := tokens first.
  112. path := Trapped parse: tokens allButFirst.
  113. { modelName }, path trapDescend: [(Smalltalk current at: viewName) new appendToJQuery: jq].
  114. ]
  115. ! !
  116. !Trapped methodsFor: 'binders'!
  117. binder: aTagBrush
  118. "Prototype; will select based on tag etc."
  119. | binder tag |
  120. tag := aTagBrush element nodeName.
  121. tag = 'INPUT' ifTrue: [
  122. | type |
  123. type := aTagBrush asJQuery attr: 'type'.
  124. type = 'checkbox' ifTrue: [ binder := TrappedCheckedBinder new ].
  125. type = 'text' ifTrue: [ binder := TrappedValBinder new ]
  126. ].
  127. binder ifNil: [ binder := TrappedBinder new ].
  128. ^ binder brush: aTagBrush; yourself
  129. ! !
  130. !Trapped methodsFor: 'initialization'!
  131. initialize
  132. super initialize.
  133. registry := #{}.
  134. ! !
  135. !Trapped class methodsFor: 'accessing'!
  136. parse: anArray
  137. ^anArray collect: [ :each |
  138. | asNum |
  139. <asNum = parseInt(each)>.
  140. asNum = asNum ifTrue: [ asNum ] ifFalse: [
  141. each first = '#' ifTrue: [ each allButFirst asSymbol ] ifFalse: [ each ]]]
  142. ! !
  143. TrappedSingleton subclass: #TrappedPathStack
  144. instanceVariableNames: 'elements'
  145. package: 'Trapped-Frontend'!
  146. !TrappedPathStack methodsFor: 'accessing'!
  147. elements
  148. ^elements
  149. ! !
  150. !TrappedPathStack methodsFor: 'descending'!
  151. append: anArray do: aBlock
  152. self with: elements, anArray do: aBlock
  153. !
  154. with: anArray do: aBlock
  155. | old |
  156. old := elements.
  157. [ elements := anArray.
  158. aBlock value ] ensure: [ elements := old ]
  159. ! !
  160. !TrappedPathStack methodsFor: 'initialization'!
  161. initialize
  162. super initialize.
  163. elements := #().
  164. ! !
  165. Object subclass: #TrappedSnapshot
  166. instanceVariableNames: 'path model'
  167. package: 'Trapped-Frontend'!
  168. !TrappedSnapshot methodsFor: 'accessing'!
  169. model
  170. ^model
  171. !
  172. path
  173. ^path
  174. !
  175. path: anArray model: aTrappedMW
  176. path := anArray.
  177. model := aTrappedMW
  178. ! !
  179. !TrappedSnapshot methodsFor: 'action'!
  180. do: aBlock
  181. TrappedPathStack current with: path do: [ aBlock value: model ]
  182. !
  183. modify: aBlock
  184. self model modify: self path allButFirst do: aBlock
  185. ! !
  186. !Array methodsFor: '*Trapped-Frontend'!
  187. trapDescend: aBlock
  188. Trapped current descend: self snapshotDo: aBlock
  189. ! !
  190. !Array methodsFor: '*Trapped-Frontend'!
  191. trapDescend: aBlock
  192. Trapped current descend: self snapshotDo: aBlock
  193. ! !
  194. !TagBrush methodsFor: '*Trapped-Frontend'!
  195. trap: path
  196. (Trapped current binder: self) installFor: path
  197. !
  198. trap: path read: aBlock
  199. path trapDescend: [ :snap |
  200. snap model watch: snap path allButFirst do: [ :data |
  201. (self asJQuery closest: 'html') toArray isEmpty ifTrue: [ KeyedPubSubUnsubscribe signal ].
  202. snap do: [ self with: [ :html | aBlock value: data value: html ] ]
  203. ]
  204. ]
  205. !
  206. trap: path toggle: aBlock
  207. self trap: path toggle: aBlock ifNotPresent: [ self asJQuery hide ]
  208. !
  209. trap: path toggle: aBlock ifNotPresent: anotherBlock
  210. | shown |
  211. shown := nil.
  212. self trap: path read: [ :data : html |
  213. shown = data notNil ifFalse: [
  214. shown := data notNil.
  215. self asJQuery empty; show.
  216. (shown ifTrue: [aBlock] ifFalse: [anotherBlock]) value: data value: html.
  217. ]
  218. ]
  219. !
  220. trapIter: path tag: aSymbol do: aBlock
  221. self trap: path read: [ :model :html |
  222. html root empty.
  223. model ifNotNil: [ model withIndexDo: [ :item :i |
  224. (html perform: aSymbol) trap: {i} read: aBlock
  225. ]]
  226. ]
  227. ! !