Trapped-Frontend.st 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. Smalltalk current createPackage: 'Trapped-Frontend' properties: #{}!
  2. Object subclass: #TrappedFly
  3. instanceVariableNames: 'payload'
  4. package: 'Trapped-Frontend'!
  5. !TrappedFly methodsFor: 'accessing'!
  6. payload
  7. ^payload
  8. !
  9. payload: anObject
  10. payload := anObject
  11. ! !
  12. !TrappedFly methodsFor: 'action'!
  13. name
  14. ^ self class name
  15. !
  16. start
  17. Trapped current register: self name: self name
  18. ! !
  19. !TrappedFly class methodsFor: 'action'!
  20. start
  21. self new start
  22. ! !
  23. Object subclass: #TrappedSingleton
  24. instanceVariableNames: ''
  25. package: 'Trapped-Frontend'!
  26. !TrappedSingleton methodsFor: 'action'!
  27. start
  28. ^ self subclassResponsibility
  29. ! !
  30. TrappedSingleton class instanceVariableNames: 'current'!
  31. !TrappedSingleton class methodsFor: 'accessing'!
  32. current
  33. ^ current ifNil: [ current := self new ]
  34. ! !
  35. !TrappedSingleton class methodsFor: 'action'!
  36. start
  37. self current start
  38. ! !
  39. TrappedSingleton subclass: #Trapped
  40. instanceVariableNames: 'registry'
  41. package: 'Trapped-Frontend'!
  42. !Trapped methodsFor: 'accessing'!
  43. byName: aString
  44. ^ registry at: aString
  45. !
  46. read: path do: aBlock
  47. | model |
  48. model := path allButFirst
  49. inject: (self byName: path first) payload
  50. into: [ :soFar :segment | soFar at: segment ].
  51. aBlock value: model.
  52. !
  53. register: aFly name: aString
  54. registry at: aString put: aFly
  55. ! !
  56. !Trapped methodsFor: 'action'!
  57. start
  58. '[data-trap]' asJQuery each: [ :index :elem |
  59. | trap jq viewName modelName tokens |
  60. jq := elem asJQuery.
  61. trap := jq attr: 'data-trap'.
  62. tokens := trap tokenize: ':'.
  63. viewName := tokens first.
  64. modelName := tokens second.
  65. { modelName } trapDescend: [(Smalltalk current at: viewName) new appendToJQuery: jq].
  66. ]
  67. ! !
  68. !Trapped methodsFor: 'initialization'!
  69. initialize
  70. super initialize.
  71. registry := #{}.
  72. ! !
  73. !Trapped class methodsFor: 'accessing'!
  74. path
  75. ^TrappedPathStack current elements
  76. ! !
  77. TrappedSingleton subclass: #TrappedPathStack
  78. instanceVariableNames: 'elements'
  79. package: 'Trapped-Frontend'!
  80. !TrappedPathStack methodsFor: 'accessing'!
  81. elements
  82. ^elements
  83. ! !
  84. !TrappedPathStack methodsFor: 'descending'!
  85. append: anArray
  86. elements := elements, anArray
  87. !
  88. with: anArray do: aBlock
  89. | old |
  90. old := elements.
  91. [ self append: anArray.
  92. aBlock value ] ensure: [ elements := old ]
  93. ! !
  94. !TrappedPathStack methodsFor: 'initialization'!
  95. initialize
  96. elements := #().
  97. ! !
  98. !Array methodsFor: '*Trapped-Frontend'!
  99. trapDescend: aBlock
  100. TrappedPathStack current with: self do: aBlock
  101. ! !
  102. !Array methodsFor: '*Trapped-Frontend'!
  103. trapDescend: aBlock
  104. TrappedPathStack current with: self do: aBlock
  105. ! !
  106. !TagBrush methodsFor: '*Trapped-Frontend'!
  107. trap: path read: aBlock
  108. path trapDescend: [ | actual |
  109. actual := Trapped path.
  110. "TODO register for later"
  111. [ Trapped current read: actual do: [ :model |
  112. aBlock value: self value: model
  113. ]] fork
  114. ]
  115. !
  116. trapShow: path
  117. self trap: path read: [ :brush :model | brush empty; with: model ]
  118. ! !