1
0
Просмотр исходного кода

DNU creation-and-insertion of elements

Herbert Vojčík 9 лет назад
Родитель
Сommit
ab329b9970
2 измененных файлов с 72 добавлено и 2 удалено
  1. 57 1
      src/Silk.js
  2. 15 1
      src/Silk.st

+ 57 - 1
src/Silk.js

@@ -6,8 +6,64 @@ $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'`.";
+$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.";
 //>>excludeEnd("ide");
+$core.addMethod(
+$core.method({
+selector: "doesNotUnderstand:",
+protocol: 'writing',
+fn: function (aMessage){
+var self=this;
+var selector,newElement;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $1,$2,$3,$4,$5;
+selector=$recv(aMessage)._selector();
+$1=$recv($recv(selector)._asUppercase()).__eq(selector);
+if(!$core.assert($1)){
+$2=(
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.supercall = true, 
+//>>excludeEnd("ctx");
+$globals.Silk.superclass.fn.prototype._doesNotUnderstand_.apply($recv(self), [aMessage]));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.supercall = false;
+//>>excludeEnd("ctx");;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["doesNotUnderstand:"]=1;
+//>>excludeEnd("ctx");
+return $2;
+};
+$3=$recv(selector)._includes_(":");
+if($core.assert($3)){
+$4=(
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.supercall = true, 
+//>>excludeEnd("ctx");
+$globals.Silk.superclass.fn.prototype._doesNotUnderstand_.apply($recv(self), [aMessage]));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.supercall = false;
+//>>excludeEnd("ctx");;
+return $4;
+};
+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)});
+//>>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",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: ["selector", "ifFalse:", "=", "asUppercase", "doesNotUnderstand:", "ifTrue:", "includes:", "newElement:", "class", "asLowercase", "<<"]
+}),
+$globals.Silk);
+
 $core.addMethod(
 $core.method({
 selector: "nextPut:",

+ 15 - 1
src/Silk.st

@@ -20,10 +20,24 @@ These messages use DNU to dynamically create
 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'`.!
+ - `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.!
 
 !Silk methodsFor: 'writing'!
 
+doesNotUnderstand: aMessage
+	"`aSilk DIV` creates a div element and inserts it"
+	| selector newElement |
+	selector := aMessage selector.
+	selector asUppercase = selector
+		ifFalse: [ ^ super doesNotUnderstand: aMessage ].
+	(selector includes: ':')
+		ifTrue: [ ^ super doesNotUnderstand: aMessage ].
+	newElement := self class newElement: selector asLowercase.
+	self << newElement.
+	^ newElement
+!
+
 nextPut: anObject
 	"Double-dispatches anObject via renderOnSilk: message.
 	If a message returns nil, this fallbacks to superclass.