1
0

Silk.st 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 blocks: `aSilk << aBlock` runs the block, passing aSilk as a parameter.
  9. ##Convenience
  10. - `aCssSelectorString asSilk` returns Silk wrapping an element at a selector.
  11. - `anObject inSilk` returns anObject rendered in a document fragment.
  12. ##Element creation
  13. These messages use DNU to dynamically create
  14. elements with any (letters-and-numbers) tag name,
  15. Next samples show this on an example of `<div>`.
  16. - `Silk DIV` is shortcut for `Silk newElement: 'div'`.!
  17. !Silk methodsFor: 'writing'!
  18. nextPut: anObject
  19. "Double-dispatches anObject via renderOnSilk: message.
  20. If a message returns nil, this fallbacks to superclass.
  21. Otherwise, it is assumed renderOnSilk: did its job."
  22. (anObject renderOnSilk: self)
  23. ifNil: [ super nextPut: anObject ]
  24. ! !
  25. !Silk class methodsFor: 'message handling'!
  26. doesNotUnderstand: aMessage
  27. "`Silk DIV` creates a div element"
  28. | selector |
  29. selector := aMessage selector.
  30. selector asUppercase = selector
  31. ifFalse: [ ^ super doesNotUnderstand: aMessage ].
  32. (selector includes: ':')
  33. ifTrue: [ ^ super doesNotUnderstand: aMessage ].
  34. ^ self newElement: selector asLowercase
  35. ! !
  36. !BlockClosure methodsFor: '*Silk'!
  37. renderOnSilk: aSilk
  38. self value: aSilk
  39. ! !
  40. !CharacterArray methodsFor: '*Silk'!
  41. asSilk
  42. ^ Silk at: self asString
  43. ! !
  44. !JSObjectProxy methodsFor: '*Silk'!
  45. inSilk
  46. ^ Silk newStream << self; yourself
  47. ! !
  48. !Object methodsFor: '*Silk'!
  49. inSilk
  50. ^ Silk newStream << self; yourself
  51. !
  52. renderOnSilk: aSilk
  53. ^ nil
  54. ! !