Trapped-Frontend.st 3.9 KB

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