Silk.st 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. Smalltalk createPackage: 'Silk'!
  2. Domite subclass: #Silk
  3. slots: {}
  4. package: 'Silk'!
  5. !Silk commentStamp!
  6. I am subclass of `Domite` with more convenient high-level API.
  7. ##Rendering
  8. - `aSilk << anObject` uses double-dispatch via `renderOnSilk:`.
  9. This allows to create widgets
  10. (no formal superclass, anything with `renderOnSilk:` is a widget),
  11. as well as incorporating magic on certain types of objects:
  12. - blocks: `aSilk << aBlock` runs the block, passing aSilk as a parameter.
  13. - associations: `aSilk << (key -> value)` set attribute key to value.
  14. Worthful to note is, rendering a collection has its magic
  15. already built-in (via `putOn:`) -- if you `stream << aCollection`,
  16. its items are `<<`'d in sequence.
  17. So, de facto, arrays are deeply flattened when put on a stream via `<<`.
  18. ##Convenience
  19. - `aCssSelectorString asSilk` returns Silk wrapping an element at a selector.
  20. - `anObject inSilk` returns anObject rendered in a document fragment.
  21. ##Element creation
  22. These messages use DNU to dynamically create
  23. elements with any (letters-and-numbers) tag name,
  24. Next samples show this on an example of `<div>`.
  25. - `Silk DIV` is shortcut for `Silk newElement: 'div'`.
  26. - `aSilk DIV` is shortcut for
  27. `[ |tmp| tmp := Silk DIV. aSilk << tmp. tmp] value`.
  28. IOW, it not just creates the element and returns it,
  29. but also puts in on aSilk.
  30. - `aSilk DIV: anObject` is shortcut for
  31. `aSilk DIV << anObject; yourself`.
  32. IOW, it not just creates and inserts the element,
  33. but puts a content into it.
  34. ##Conclusions
  35. Taken all this together, one can do pretty neat constructs:
  36. ```
  37. aSilk P: { 'id'->'mission'. 'We are the champions.' }
  38. ```
  39. adds `<p id="mission">We are the champions.</p>` into `aSilk`
  40. and returns the Silk-wrapped `<p>` with insertion cursor at the end.!
  41. !Silk methodsFor: 'accessing'!
  42. namespace
  43. "<String>
  44. XML namespace for elements: html.
  45. The default for all virtual Silk tag messages"
  46. ^ self element namespaceURI
  47. ! !
  48. !Silk methodsFor: 'writing'!
  49. newElement: aString xmlns: anotherString
  50. | el |
  51. el := self class newElement: aString xmlns: anotherString.
  52. self << el.
  53. ^ el
  54. !
  55. nextPut: anObject
  56. "Double-dispatches anObject via renderOnSilk: message.
  57. If a message returns nil, this fallbacks to superclass.
  58. Otherwise, it is assumed renderOnSilk: did its job."
  59. (anObject renderOnSilk: self)
  60. ifNil: [ super nextPut: anObject ]
  61. ! !
  62. !Silk class methodsFor: 'accessing'!
  63. htmlNamespace
  64. "<String>
  65. XML namespace for HTML elements.
  66. The default for all virtual Silk tag messages"
  67. ^ 'http://www.w3.org/1999/xhtml'
  68. !
  69. namespace
  70. "<String>
  71. XML namespace for elements: html.
  72. The default for all virtual Silk tag messages"
  73. ^ self htmlNamespace
  74. ! !
  75. Trait named: #TSilkBuilder
  76. package: 'Silk'!
  77. !TSilkBuilder commentStamp!
  78. I contain Silk's "build element via DNU" behaviour.
  79. I expect #namespace and #newElement:xmlns: to be implemented.!
  80. !TSilkBuilder methodsFor: 'convenience'!
  81. newSvgElement
  82. ^ self newElement: 'svg' xmlns: 'http://www.w3.org/2000/svg'
  83. ! !
  84. !TSilkBuilder methodsFor: 'instance creation'!
  85. tryMakeDnuElement: aMessage
  86. "`DIV` creates a div element.
  87. `DIV: anObject` creates a div element and puts contents in it.
  88. An element can be optionally inserted by #newElement:xmlns:.
  89. When self is an instance and not the class Silk,
  90. then the instance's namespace is used for the new element.
  91. You can do:
  92. svg := Silk newElement: 'svg' xmlns: 'http://www.w3.org/2000/svg'.
  93. svg CIRCLE: {'cx' -> 60. 'cy' -> 25. 'r' -> 10}.
  94. This creates a svg circle, not a html circle."
  95. | selector newElement useArg |
  96. selector := aMessage selector.
  97. selector asUppercase = selector
  98. ifFalse: [ ^ nil ].
  99. selector last = ':'
  100. ifTrue: [ useArg := true. selector := selector allButLast ]
  101. ifFalse: [ useArg := false ].
  102. (selector includes: ':')
  103. ifTrue: [ ^ nil ].
  104. newElement := self newElement: selector asLowercase xmlns: self namespace.
  105. useArg ifTrue: [ newElement << aMessage arguments first ].
  106. ^ newElement
  107. ! !
  108. !TSilkBuilder methodsFor: 'message handling'!
  109. doesNotUnderstand: aMessage
  110. "`self DIV` creates (and optionally inserts) a div element.
  111. `aSilk DIV: anObject` creates (and optionally inserts)
  112. a div element, and puts contents in it"
  113. ^ (self tryMakeDnuElement: aMessage)
  114. ifNil: [ super doesNotUnderstand: aMessage ]
  115. ! !
  116. Silk setTraitComposition: {TSilkBuilder} asTraitComposition!
  117. Silk class setTraitComposition: {TSilkBuilder} asTraitComposition!
  118. ! !
  119. !Association methodsFor: '*Silk'!
  120. renderOnSilk: aSilk
  121. key attrPut: value on: aSilk
  122. ! !
  123. !BlockClosure methodsFor: '*Silk'!
  124. renderOnSilk: aSilk
  125. self value: aSilk
  126. ! !
  127. !JSObjectProxy methodsFor: '*Silk'!
  128. inSilk
  129. ^ Silk newStream << self; yourself
  130. !
  131. renderOnSilk: aSilk
  132. ^ nil
  133. ! !
  134. !Object methodsFor: '*Silk'!
  135. inSilk
  136. ^ Silk newStream << self; yourself
  137. !
  138. renderOnSilk: aSilk
  139. ^ nil
  140. ! !
  141. !String methodsFor: '*Silk'!
  142. asSilk
  143. ^ Silk at: self asString
  144. !
  145. attrPut: anObject on: aSilk
  146. aSilk attrAt: self put: anObject
  147. ! !