Silk.st 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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: 'writing'!
  42. doesNotUnderstand: aMessage
  43. "`aSilk DIV` creates a div element and inserts it.
  44. `aSilk DIV: anObject` creates a div element, inserts it
  45. and puts contents in it"
  46. (self class tryMakeDnuElement: aMessage)
  47. ifNil: [ ^ super doesNotUnderstand: aMessage ]
  48. ifNotNil: [ :newElement | self << newElement. ^ newElement ]
  49. !
  50. nextPut: anObject
  51. "Double-dispatches anObject via renderOnSilk: message.
  52. If a message returns nil, this fallbacks to superclass.
  53. Otherwise, it is assumed renderOnSilk: did its job."
  54. (anObject renderOnSilk: self)
  55. ifNil: [ super nextPut: anObject ]
  56. ! !
  57. !Silk class methodsFor: 'instance creation'!
  58. tryMakeDnuElement: aMessage
  59. "`DIV` creates a div element.
  60. `DIV: anObject` creates a div element and puts contents in it"
  61. | selector newElement useArg |
  62. selector := aMessage selector.
  63. selector asUppercase = selector
  64. ifFalse: [ ^ nil ].
  65. selector last = ':'
  66. ifTrue: [ useArg := true. selector := selector allButLast ]
  67. ifFalse: [ useArg := false ].
  68. (selector includes: ':')
  69. ifTrue: [ ^ nil ].
  70. newElement := self newElement: selector asLowercase.
  71. useArg ifTrue: [ newElement << aMessage arguments first ].
  72. ^ newElement
  73. ! !
  74. !Silk class methodsFor: 'message handling'!
  75. doesNotUnderstand: aMessage
  76. "`Silk DIV` creates a div element.
  77. `Silk DIV: anObject` creates a div element and puts contents in it"
  78. ^ (self tryMakeDnuElement: aMessage)
  79. ifNil: [ super doesNotUnderstand: aMessage ]
  80. ! !
  81. !Association methodsFor: '*Silk'!
  82. renderOnSilk: aSilk
  83. key attrPut: value on: aSilk
  84. ! !
  85. !BlockClosure methodsFor: '*Silk'!
  86. renderOnSilk: aSilk
  87. self value: aSilk
  88. ! !
  89. !JSObjectProxy methodsFor: '*Silk'!
  90. inSilk
  91. ^ Silk newStream << self; yourself
  92. !
  93. renderOnSilk: aSilk
  94. ^ nil
  95. ! !
  96. !Object methodsFor: '*Silk'!
  97. inSilk
  98. ^ Silk newStream << self; yourself
  99. !
  100. renderOnSilk: aSilk
  101. ^ nil
  102. ! !
  103. !String methodsFor: '*Silk'!
  104. asSilk
  105. ^ Silk at: self asString
  106. !
  107. attrPut: anObject on: aSilk
  108. aSilk attrAt: self put: anObject
  109. ! !