Smalltalk createPackage: 'Silk'! Domite subclass: #Silk slots: {} package: 'Silk'! !Silk commentStamp! I am subclass of `Domite` with more convenient high-level API. ##Rendering - `aSilk << anObject` uses double-dispatch via `renderOnSilk:`. This allows to create widgets (no formal superclass, anything with `renderOnSilk:` is a widget), as well as incorporating magic on certain types of objects: - blocks: `aSilk << aBlock` runs the block, passing aSilk as a parameter. - associations: `aSilk << (key -> value)` set attribute key to value. Worthful to note is, rendering a collection has its magic already built-in (via `putOn:`) -- if you `stream << aCollection`, its items are `<<`'d in sequence. So, de facto, arrays are deeply flattened when put on a stream via `<<`. ##Convenience - `aCssSelectorString asSilk` returns Silk wrapping an element at a selector. - `anObject inSilk` returns anObject rendered in a document fragment. ##Element creation These messages use DNU to dynamically create elements with any (letters-and-numbers) tag name, Next samples show this on an example of `
We are the champions.
` into `aSilk` and returns the Silk-wrapped `` with insertion cursor at the end.! !Silk methodsFor: 'writing'! doesNotUnderstand: aMessage "`aSilk DIV` creates a div element and inserts it. `aSilk DIV: anObject` creates a div element, inserts it and puts contents in it" (self class tryMakeDnuElement: aMessage) ifNil: [ ^ super doesNotUnderstand: aMessage ] ifNotNil: [ :newElement | self << newElement. ^ newElement ] ! nextPut: anObject "Double-dispatches anObject via renderOnSilk: message. If a message returns nil, this fallbacks to superclass. Otherwise, it is assumed renderOnSilk: did its job." (anObject renderOnSilk: self) ifNil: [ super nextPut: anObject ] ! ! !Silk class methodsFor: 'instance creation'! tryMakeDnuElement: aMessage "`DIV` creates a div element. `DIV: anObject` creates a div element and puts contents in it" | selector newElement useArg | selector := aMessage selector. selector asUppercase = selector ifFalse: [ ^ nil ]. selector last = ':' ifTrue: [ useArg := true. selector := selector allButLast ] ifFalse: [ useArg := false ]. (selector includes: ':') ifTrue: [ ^ nil ]. newElement := self newElement: selector asLowercase. useArg ifTrue: [ newElement << aMessage arguments first ]. ^ newElement ! ! !Silk class methodsFor: 'message handling'! doesNotUnderstand: aMessage "`Silk DIV` creates a div element. `Silk DIV: anObject` creates a div element and puts contents in it" ^ (self tryMakeDnuElement: aMessage) ifNil: [ super doesNotUnderstand: aMessage ] ! ! !Association methodsFor: '*Silk'! renderOnSilk: aSilk key attrPut: value on: aSilk ! ! !BlockClosure methodsFor: '*Silk'! renderOnSilk: aSilk self value: aSilk ! ! !JSObjectProxy methodsFor: '*Silk'! inSilk ^ Silk newStream << self; yourself ! renderOnSilk: aSilk ^ nil ! ! !Object methodsFor: '*Silk'! inSilk ^ Silk newStream << self; yourself ! renderOnSilk: aSilk ^ nil ! ! !String methodsFor: '*Silk'! asSilk ^ Silk at: self asString ! attrPut: anObject on: aSilk aSilk attrAt: self put: anObject ! !