Silk.st 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. | selector |
  61. selector := aMessage selector.
  62. selector asUppercase = selector
  63. ifFalse: [ ^ super doesNotUnderstand: aMessage ].
  64. (selector includes: ':')
  65. ifTrue: [ ^ super doesNotUnderstand: aMessage ].
  66. ^ self newElement: selector asLowercase
  67. ! !
  68. !Association methodsFor: '*Silk'!
  69. renderOnSilk: aSilk
  70. aSilk attrAt: key put: value
  71. ! !
  72. !BlockClosure methodsFor: '*Silk'!
  73. renderOnSilk: aSilk
  74. self value: aSilk
  75. ! !
  76. !CharacterArray methodsFor: '*Silk'!
  77. asSilk
  78. ^ Silk at: self asString
  79. ! !
  80. !JSObjectProxy methodsFor: '*Silk'!
  81. inSilk
  82. ^ Silk newStream << self; yourself
  83. ! !
  84. !Object methodsFor: '*Silk'!
  85. inSilk
  86. ^ Silk newStream << self; yourself
  87. !
  88. renderOnSilk: aSilk
  89. ^ nil
  90. ! !