Trapped-Frontend.st 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. Smalltalk current createPackage: 'Trapped-Frontend' properties: #{}!
  2. TrappedDispatcher subclass: #TrappedDumbDispatcher
  3. instanceVariableNames: 'queue'
  4. package: 'Trapped-Frontend'!
  5. !TrappedDumbDispatcher methodsFor: 'accessing'!
  6. add: aTriplet
  7. queue add: aTriplet.
  8. self dirty: aTriplet first
  9. ! !
  10. !TrappedDumbDispatcher methodsFor: 'enumeration'!
  11. do: aBlock
  12. queue do: aBlock
  13. ! !
  14. !TrappedDumbDispatcher methodsFor: 'initialization'!
  15. initialize
  16. queue := OrderedCollection new
  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. TrappedModelWrapper subclass: #TrappedPlainModel
  28. instanceVariableNames: ''
  29. package: 'Trapped-Frontend'!
  30. !TrappedPlainModel methodsFor: 'action'!
  31. read: path do: aBlock
  32. | data |
  33. data := path inject: self payload
  34. into: [ :soFar :segment | soFar at: segment ].
  35. aBlock value: data.
  36. ! !
  37. !TrappedPlainModel methodsFor: 'initialization'!
  38. initialize
  39. super initialize.
  40. self dispatcher: TrappedDumbDispatcher new
  41. ! !
  42. !TrappedPlainModel class methodsFor: 'action'!
  43. start
  44. self new start
  45. ! !
  46. Object subclass: #TrappedSingleton
  47. instanceVariableNames: ''
  48. package: 'Trapped-Frontend'!
  49. !TrappedSingleton methodsFor: 'action'!
  50. start
  51. ^ self subclassResponsibility
  52. ! !
  53. TrappedSingleton class instanceVariableNames: 'current'!
  54. !TrappedSingleton class methodsFor: 'accessing'!
  55. current
  56. ^ current ifNil: [ current := self new ]
  57. ! !
  58. !TrappedSingleton class methodsFor: 'action'!
  59. start
  60. self current start
  61. ! !
  62. TrappedSingleton subclass: #Trapped
  63. instanceVariableNames: 'registry'
  64. package: 'Trapped-Frontend'!
  65. !Trapped methodsFor: 'accessing'!
  66. byName: aString
  67. ^ registry at: aString
  68. !
  69. register: aFly name: aString
  70. registry at: aString put: aFly
  71. ! !
  72. !Trapped methodsFor: 'action'!
  73. start
  74. '[data-trap]' asJQuery each: [ :index :elem |
  75. | trap jq viewName modelName tokens path |
  76. jq := elem asJQuery.
  77. trap := jq attr: 'data-trap'.
  78. tokens := trap tokenize: ':'.
  79. tokens size = 1 ifTrue: [ tokens := { 'TrappedDumbView' }, tokens ].
  80. viewName := tokens first.
  81. tokens := (tokens second tokenize: ' ') select: [ :each | each notEmpty ].
  82. modelName := tokens first.
  83. path := Trapped parse: tokens allButFirst.
  84. { modelName }, path trapDescend: [(Smalltalk current at: viewName) new appendToJQuery: jq].
  85. ]
  86. ! !
  87. !Trapped methodsFor: 'initialization'!
  88. initialize
  89. super initialize.
  90. registry := #{}.
  91. ! !
  92. !Trapped class methodsFor: 'accessing'!
  93. parse: anArray
  94. ^anArray collect: [ :each |
  95. | asNum |
  96. <asNum = parseInt(each)>.
  97. asNum = asNum ifTrue: [ asNum ] ifFalse: [
  98. each first = '#' ifTrue: [ each allButFirst asSymbol ] ifFalse: [ each ]]]
  99. !
  100. path
  101. ^TrappedPathStack current elements
  102. ! !
  103. TrappedSingleton subclass: #TrappedPathStack
  104. instanceVariableNames: 'elements'
  105. package: 'Trapped-Frontend'!
  106. !TrappedPathStack methodsFor: 'accessing'!
  107. elements
  108. ^elements
  109. ! !
  110. !TrappedPathStack methodsFor: 'descending'!
  111. append: anArray
  112. elements := elements, anArray
  113. !
  114. with: anArray do: aBlock
  115. | old |
  116. old := elements.
  117. [ self append: anArray.
  118. aBlock value ] ensure: [ elements := old ]
  119. ! !
  120. !TrappedPathStack methodsFor: 'initialization'!
  121. initialize
  122. elements := #().
  123. ! !
  124. !Array methodsFor: '*Trapped-Frontend'!
  125. trapDescend: aBlock
  126. TrappedPathStack current with: self do: aBlock
  127. ! !
  128. !Array methodsFor: '*Trapped-Frontend'!
  129. trapDescend: aBlock
  130. TrappedPathStack current with: self do: aBlock
  131. ! !
  132. !TagBrush methodsFor: '*Trapped-Frontend'!
  133. trap: path read: aBlock
  134. path trapDescend: [ | actual model |
  135. actual := Trapped path.
  136. model := Trapped current byName: actual first.
  137. model watch: actual allButFirst do: [ :data |
  138. aBlock value: self value: data
  139. ]
  140. ]
  141. !
  142. trapShow: path
  143. self trap: path read: [ :brush :model | brush empty; with: model ]
  144. ! !