Silk.st 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. Smalltalk createPackage: 'Silk'!
  2. Domite subclass: #Silk
  3. instanceVariableNames: ''
  4. package: 'Silk'!
  5. !Silk commentStamp!
  6. I am adding convenience APIs to my subclass, `Domite`.
  7. ##Rendering
  8. - `aSilk << anObject` uses double-dispatch via `renderOnSilk:`. This allows creating widgets (no formal superclass, anything with `renderOnSilk:` is a widget), as well as incorporating magic on other objects:
  9. - blocks: `aSilk << aBlock` runs the block, passing aSilk as a parameter.
  10. - associations: `aSilk << (key -> value)` set attribute key to value.
  11. It is good to note that rendering collection has magic
  12. of its own built-in in general: if you `stream << aCollection`, its items are `<<`'d in sequence.
  13. So, de facto, array are deeply flattened
  14. when putting on a stream via `<<`.
  15. ##Convenience
  16. - `aCssSelectorString asSilk` returns Silk wrapping an element at a selector.
  17. - `anObject inSilk` returns anObject rendered in a document fragment.
  18. ##Element creation
  19. These messages use DNU to dynamically create
  20. elements with any (letters-and-numbers) tag name,
  21. Next samples show this on an example of `<div>`.
  22. - `Silk DIV` is shortcut for `Silk newElement: 'div'`.
  23. - `aSilk DIV` is shortcut for `[ |tmp| tmp := Silk DIV. aSilk << tmp. tmp] value`. IOW, it not just creates the element and returns it, but also puts in on aSilk.
  24. - `aSilk DIV: anObject` is shortcut for `aSilk DIV << anObject; yourself`. IOW, it not just creates and insert the element, but puts a content into it.
  25. ##Conclusions
  26. Taken all this together, one can do pretty neat constructs:
  27. ```
  28. aSilk P: { 'id'->'mission'. 'We are the champions.' }
  29. ```
  30. adds `<p id="mission">We are the champions.</p>` into `aSilk`.!
  31. !Silk methodsFor: 'writing'!
  32. doesNotUnderstand: aMessage
  33. "`aSilk DIV` creates a div element and inserts it.
  34. `aSilk DIV: anObject` creates a div element, inserts it
  35. and puts contents in it"
  36. | selector newElement useArg |
  37. selector := aMessage selector.
  38. selector asUppercase = selector
  39. ifFalse: [ ^ super doesNotUnderstand: aMessage ].
  40. selector last = ':'
  41. ifTrue: [ useArg := true. selector := selector allButLast ]
  42. ifFalse: [ useArg := false ].
  43. (selector includes: ':')
  44. ifTrue: [ ^ super doesNotUnderstand: aMessage ].
  45. newElement := self class newElement: selector asLowercase.
  46. self << newElement.
  47. useArg ifTrue: [ newElement << aMessage arguments first ].
  48. ^ 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: 'message handling'!
  58. doesNotUnderstand: aMessage
  59. "`Silk DIV` creates a div element.
  60. `Silk 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: [ ^ super doesNotUnderstand: aMessage ].
  65. selector last = ':'
  66. ifTrue: [ useArg := true. selector := selector allButLast ]
  67. ifFalse: [ useArg := false ].
  68. (selector includes: ':')
  69. ifTrue: [ ^ super doesNotUnderstand: aMessage ].
  70. newElement := self newElement: selector asLowercase.
  71. useArg ifTrue: [ newElement << aMessage arguments first ].
  72. ^ newElement
  73. ! !
  74. !Association methodsFor: '*Silk'!
  75. renderOnSilk: aSilk
  76. key attrPut: value on: aSilk
  77. ! !
  78. !BlockClosure methodsFor: '*Silk'!
  79. renderOnSilk: aSilk
  80. self value: aSilk
  81. ! !
  82. !CharacterArray methodsFor: '*Silk'!
  83. asSilk
  84. ^ Silk at: self asString
  85. ! !
  86. !JSObjectProxy methodsFor: '*Silk'!
  87. inSilk
  88. ^ Silk newStream << self; yourself
  89. ! !
  90. !Object methodsFor: '*Silk'!
  91. inSilk
  92. ^ Silk newStream << self; yourself
  93. !
  94. renderOnSilk: aSilk
  95. ^ nil
  96. ! !
  97. !String methodsFor: '*Silk'!
  98. attrPut: anObject on: aSilk
  99. aSilk attrAt: self put: anObject
  100. ! !