Browse Source

DNU creation-and-insertion-and-contentfill of elements

Herbert Vojčík 9 years ago
parent
commit
45d0fa2a74
2 changed files with 41 additions and 14 deletions
  1. 31 11
      src/Silk.js
  2. 10 3
      src/Silk.st

+ 31 - 11
src/Silk.js

@@ -6,7 +6,7 @@ $core.packages["Silk"].transport = {"type":"amd","amdNamespace":"silk"};
 
 $core.addClass('Silk', $globals.Domite, [], 'Silk');
 //>>excludeStart("ide", pragmas.excludeIdeData);
-$globals.Silk.comment="I am adding convenience APIs to my subclass, `Domite`.\x0a\x0a##Rendering\x0a\x0a - `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.\x0a\x0a##Convenience\x0a\x0a - `aCssSelectorString asSilk` returns Silk wrapping an element at a selector.\x0a - `anObject inSilk` returns anObject rendered in a document fragment.\x0a\x0a##Element creation\x0a\x0aThese messages use DNU to dynamically create\x0aelements with any (letters-and-numbers) tag name,\x0aNext samples show this on an example of `<div>`.\x0a\x0a - `Silk DIV` is shortcut for `Silk newElement: 'div'`.\x0a - `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.";
+$globals.Silk.comment="I am adding convenience APIs to my subclass, `Domite`.\x0a\x0a##Rendering\x0a\x0a - `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.\x0a\x0a##Convenience\x0a\x0a - `aCssSelectorString asSilk` returns Silk wrapping an element at a selector.\x0a - `anObject inSilk` returns anObject rendered in a document fragment.\x0a\x0a##Element creation\x0a\x0aThese messages use DNU to dynamically create\x0aelements with any (letters-and-numbers) tag name,\x0aNext samples show this on an example of `<div>`.\x0a\x0a - `Silk DIV` is shortcut for `Silk newElement: 'div'`.\x0a - `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.\x0a - `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.";
 //>>excludeEnd("ide");
 $core.addMethod(
 $core.method({
@@ -14,13 +14,16 @@ selector: "doesNotUnderstand:",
 protocol: 'writing',
 fn: function (aMessage){
 var self=this;
-var selector,newElement;
+var selector,newElement,useArg;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-var $1,$2,$3,$4,$5;
+var $1,$2,$3,$4,$5,$6,$7;
 selector=$recv(aMessage)._selector();
 $1=$recv($recv(selector)._asUppercase()).__eq(selector);
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["="]=1;
+//>>excludeEnd("ctx");
 if(!$core.assert($1)){
 $2=(
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
@@ -35,9 +38,19 @@ $ctx1.sendIdx["doesNotUnderstand:"]=1;
 //>>excludeEnd("ctx");
 return $2;
 };
-$3=$recv(selector)._includes_(":");
+$3=$recv($recv(selector)._last()).__eq(":");
 if($core.assert($3)){
-$4=(
+useArg=true;
+useArg;
+selector=$recv(selector)._allButLast();
+selector;
+} else {
+useArg=false;
+useArg;
+};
+$4=$recv(selector)._includes_(":");
+if($core.assert($4)){
+$5=(
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 $ctx1.supercall = true, 
 //>>excludeEnd("ctx");
@@ -45,22 +58,29 @@ $globals.Silk.superclass.fn.prototype._doesNotUnderstand_.apply($recv(self), [aM
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 $ctx1.supercall = false;
 //>>excludeEnd("ctx");;
-return $4;
+return $5;
 };
 newElement=$recv(self._class())._newElement_($recv(selector)._asLowercase());
 self.__lt_lt(newElement);
-$5=newElement;
-return $5;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx1) {$ctx1.fill(self,"doesNotUnderstand:",{aMessage:aMessage,selector:selector,newElement:newElement},$globals.Silk)});
+$ctx1.sendIdx["<<"]=1;
+//>>excludeEnd("ctx");
+$6=useArg;
+if($core.assert($6)){
+$recv(newElement).__lt_lt($recv($recv(aMessage)._arguments())._first());
+};
+$7=newElement;
+return $7;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"doesNotUnderstand:",{aMessage:aMessage,selector:selector,newElement:newElement,useArg:useArg},$globals.Silk)});
 //>>excludeEnd("ctx");
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["aMessage"],
-source: "doesNotUnderstand: aMessage\x0a\x09\x22`aSilk DIV` creates a div element and inserts it\x22\x0a\x09| selector newElement |\x0a\x09selector := aMessage selector.\x0a\x09selector asUppercase = selector\x0a\x09\x09ifFalse: [ ^ super doesNotUnderstand: aMessage ].\x0a\x09(selector includes: ':')\x0a\x09\x09ifTrue: [ ^ super doesNotUnderstand: aMessage ].\x0a\x09newElement := self class newElement: selector asLowercase.\x0a\x09self << newElement.\x0a\x09^ newElement",
+source: "doesNotUnderstand: aMessage\x0a\x09\x22`aSilk DIV` creates a div element and inserts it.\x0a\x09`aSilk DIV: anObject` creates a div element, inserts it\x0a\x09and puts contents in it\x22\x0a\x09| selector newElement useArg |\x0a\x09selector := aMessage selector.\x0a\x09selector asUppercase = selector\x0a\x09\x09ifFalse: [ ^ super doesNotUnderstand: aMessage ].\x0a\x09selector last = ':'\x0a\x09\x09ifTrue: [ useArg := true. selector := selector allButLast ]\x0a\x09\x09ifFalse: [ useArg := false ].\x0a\x09(selector includes: ':')\x0a\x09\x09ifTrue: [ ^ super doesNotUnderstand: aMessage ].\x0a\x09newElement := self class newElement: selector asLowercase.\x0a\x09self << newElement.\x0a\x09useArg ifTrue: [ newElement << aMessage arguments first ].\x0a\x09^ newElement",
 referencedClasses: [],
 //>>excludeEnd("ide");
-messageSends: ["selector", "ifFalse:", "=", "asUppercase", "doesNotUnderstand:", "ifTrue:", "includes:", "newElement:", "class", "asLowercase", "<<"]
+messageSends: ["selector", "ifFalse:", "=", "asUppercase", "doesNotUnderstand:", "ifTrue:ifFalse:", "last", "allButLast", "ifTrue:", "includes:", "newElement:", "class", "asLowercase", "<<", "first", "arguments"]
 }),
 $globals.Silk);
 

+ 10 - 3
src/Silk.st

@@ -21,20 +21,27 @@ elements with any (letters-and-numbers) tag name,
 Next samples show this on an example of `<div>`.
 
  - `Silk DIV` is shortcut for `Silk newElement: 'div'`.
- - `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.!
+ - `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.
+ - `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.!
 
 !Silk methodsFor: 'writing'!
 
 doesNotUnderstand: aMessage
-	"`aSilk DIV` creates a div element and inserts it"
-	| selector newElement |
+	"`aSilk DIV` creates a div element and inserts it.
+	`aSilk DIV: anObject` creates a div element, inserts it
+	and puts contents in it"
+	| selector newElement useArg |
 	selector := aMessage selector.
 	selector asUppercase = selector
 		ifFalse: [ ^ super doesNotUnderstand: aMessage ].
+	selector last = ':'
+		ifTrue: [ useArg := true. selector := selector allButLast ]
+		ifFalse: [ useArg := false ].
 	(selector includes: ':')
 		ifTrue: [ ^ super doesNotUnderstand: aMessage ].
 	newElement := self class newElement: selector asLowercase.
 	self << newElement.
+	useArg ifTrue: [ newElement << aMessage arguments first ].
 	^ newElement
 !