Browse Source

DRY, contents via data-content for <script>

Herbert Vojčík 11 years ago
parent
commit
92fe523a7a
4 changed files with 164 additions and 117 deletions
  1. 42 18
      js/Trapped-Frontend.js
  2. 74 65
      js/Trapped-Processors.js
  3. 29 23
      st/Trapped-Frontend.st
  4. 19 11
      st/Trapped-Processors.st

+ 42 - 18
js/Trapped-Frontend.js

@@ -738,10 +738,33 @@ referencedClasses: []
 }),
 smalltalk.Trapped);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "cloneAndInject:inPlaceOf:",
+category: 'private',
+fn: function (anObject,aTagBrush){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $1,$2;
+$1=_st(aTagBrush)._asJQuery();
+$ctx1.sendIdx["asJQuery"]=1;
+_st($1)._append_(_st(anObject)._clone());
+$2=_st(aTagBrush)._asJQuery();
+$ctx1.sendIdx["asJQuery"]=2;
+self._injectToJQuery_($2);
+_st(_st(_st(aTagBrush)._asJQuery())._contents())._unwrap();
+return self}, function($ctx1) {$ctx1.fill(self,"cloneAndInject:inPlaceOf:",{anObject:anObject,aTagBrush:aTagBrush},smalltalk.Trapped)})},
+args: ["anObject", "aTagBrush"],
+source: "cloneAndInject: anObject inPlaceOf: aTagBrush\x0a\x09aTagBrush asJQuery append: anObject clone.\x0a\x09self injectToJQuery: aTagBrush asJQuery.\x0a\x09aTagBrush asJQuery contents unwrap",
+messageSends: ["append:", "asJQuery", "clone", "injectToJQuery:", "unwrap", "contents"],
+referencedClasses: []
+}),
+smalltalk.Trapped);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "descend:snapshotDo:",
-category: 'action',
+category: 'private',
 fn: function (anArray,aBlock){
 var self=this;
 var tpsc;
@@ -786,7 +809,7 @@ smalltalk.Trapped);
 smalltalk.addMethod(
 smalltalk.method({
 selector: "injectToJQuery:",
-category: 'action',
+category: 'private',
 fn: function (aJQuery){
 var self=this;
 function $Trapped(){return smalltalk.Trapped||(typeof Trapped=="undefined"?nil:Trapped)}
@@ -877,27 +900,28 @@ smalltalk.Trapped);
 
 smalltalk.addMethod(
 smalltalk.method({
-selector: "envelope:loop:before:do:",
+selector: "loop:before:do:",
 category: 'private',
-fn: function (envelope,model,endjq,aBlock){
+fn: function (model,endjq,aBlock){
 var self=this;
-var envjq;
+function $HTMLCanvas(){return smalltalk.HTMLCanvas||(typeof HTMLCanvas=="undefined"?nil:HTMLCanvas)}
 return smalltalk.withContext(function($ctx1) { 
-envjq=_st(envelope)._asJQuery();
 _st(model)._withIndexDo_((function(item,i){
+var env,envjq;
 return smalltalk.withContext(function($ctx2) {
+envjq=_st("<div/>"._asJQuery())._insertBefore_(endjq);
+envjq;
 _st([i])._trapDescend_((function(){
 return smalltalk.withContext(function($ctx3) {
-return _st(envelope)._with_(aBlock);
+return _st(_st(_st($HTMLCanvas())._onJQuery_(envjq))._root())._with_(aBlock);
 }, function($ctx3) {$ctx3.fillBlock({},$ctx2,2)})}));
-return _st(_st(_st(envjq)._children())._detach())._insertBefore_(endjq);
-}, function($ctx2) {$ctx2.fillBlock({item:item,i:i},$ctx1,1)})}));
-_st(envjq)._remove();
-return self}, function($ctx1) {$ctx1.fill(self,"envelope:loop:before:do:",{envelope:envelope,model:model,endjq:endjq,aBlock:aBlock,envjq:envjq},smalltalk.Trapped.klass)})},
-args: ["envelope", "model", "endjq", "aBlock"],
-source: "envelope: envelope loop: model before: endjq do: aBlock\x0a   \x09| envjq |\x0a    envjq := envelope asJQuery.\x0a    model withIndexDo: [ :item :i |\x0a        {i} trapDescend: [ envelope with: aBlock ].\x0a        envjq children detach insertBefore: endjq.\x0a    ].\x0a    envjq remove",
-messageSends: ["asJQuery", "withIndexDo:", "trapDescend:", "with:", "insertBefore:", "detach", "children", "remove"],
-referencedClasses: []
+return _st(_st(envjq)._contents())._unwrap();
+}, function($ctx2) {$ctx2.fillBlock({item:item,i:i,env:env,envjq:envjq},$ctx1,1)})}));
+return self}, function($ctx1) {$ctx1.fill(self,"loop:before:do:",{model:model,endjq:endjq,aBlock:aBlock},smalltalk.Trapped.klass)})},
+args: ["model", "endjq", "aBlock"],
+source: "loop: model before: endjq do: aBlock\x0a\x09model withIndexDo: [ :item :i |\x0a\x09\x09| env envjq |\x0a\x09\x09envjq := '<div/>' asJQuery insertBefore: endjq.\x0a\x09\x09{i} trapDescend: [ (HTMLCanvas onJQuery: envjq) root with: aBlock ].\x0a\x09\x09envjq contents unwrap ]",
+messageSends: ["withIndexDo:", "insertBefore:", "asJQuery", "trapDescend:", "with:", "root", "onJQuery:", "unwrap", "contents"],
+referencedClasses: ["HTMLCanvas"]
 }),
 smalltalk.Trapped.klass);
 
@@ -918,13 +942,13 @@ return smalltalk.withContext(function($ctx2) {
 if(($receiver = model) == nil || $receiver == null){
 return model;
 } else {
-return self._envelope_loop_before_do_(_st(html)._div(),model,_st(end)._asJQuery(),aBlock);
+return self._loop_before_do_(model,_st(end)._asJQuery(),aBlock);
 };
 }, function($ctx2) {$ctx2.fillBlock({html:html},$ctx1,1)})}));
 return self}, function($ctx1) {$ctx1.fill(self,"loop:between:and:do:",{model:model,start:start,end:end,aBlock:aBlock},smalltalk.Trapped.klass)})},
 args: ["model", "start", "end", "aBlock"],
-source: "loop: model between: start and: end do: aBlock\x0a    (start asJQuery nextUntil: end element) remove.\x0a    start with: [ :html | model ifNotNil: [\x0a    \x09self envelope: html div loop: model before: end asJQuery do: aBlock\x0a\x09]]",
-messageSends: ["remove", "nextUntil:", "asJQuery", "element", "with:", "ifNotNil:", "envelope:loop:before:do:", "div"],
+source: "loop: model between: start and: end do: aBlock\x0a\x09(start asJQuery nextUntil: end element) remove.\x0a\x09start with: [ :html | model ifNotNil: [\x0a\x09\x09self loop: model before: end asJQuery do: aBlock\x0a\x09]]",
+messageSends: ["remove", "nextUntil:", "asJQuery", "element", "with:", "ifNotNil:", "loop:before:do:"],
 referencedClasses: []
 }),
 smalltalk.Trapped.klass);

+ 74 - 65
js/Trapped-Processors.js

@@ -220,27 +220,16 @@ var self=this;
 var frozen,contents;
 function $Trapped(){return smalltalk.Trapped||(typeof Trapped=="undefined"?nil:Trapped)}
 return smalltalk.withContext(function($ctx1) { 
-var $3,$2,$1,$5,$4;
 frozen=_st(aDataCarrier)._copy();
-$3=_st(frozen)._target();
-$ctx1.sendIdx["target"]=1;
-$2=_st($3)._asJQuery();
-$ctx1.sendIdx["asJQuery"]=1;
-$1=_st($2)._contents();
-contents=_st($1)._detach();
+contents=_st(frozen)._contents();
 _st(_st(frozen)._target())._trapGuard_contents_(self["@guardPath"],(function(html){
 return smalltalk.withContext(function($ctx2) {
-$5=_st(html)._root();
-$ctx2.sendIdx["root"]=1;
-$4=_st($5)._asJQuery();
-$ctx2.sendIdx["asJQuery"]=2;
-_st($4)._append_(contents);
-return _st(_st($Trapped())._current())._injectToJQuery_(_st(_st(html)._root())._asJQuery());
+return _st(_st($Trapped())._current())._cloneAndInject_inPlaceOf_(contents,_st(html)._del());
 }, function($ctx2) {$ctx2.fillBlock({html:html},$ctx1,1)})}));
 return self}, function($ctx1) {$ctx1.fill(self,"toView:",{aDataCarrier:aDataCarrier,frozen:frozen,contents:contents},smalltalk.TrappedProcessorGuardContents)})},
 args: ["aDataCarrier"],
-source: "toView: aDataCarrier\x0a\x09| frozen contents |\x0a\x09frozen := aDataCarrier copy.\x0a\x09contents := frozen target asJQuery contents detach.\x0a\x09frozen target trapGuard: guardPath contents: [ :html |\x0a\x09\x09html root asJQuery append: contents.\x0a\x09\x09Trapped current injectToJQuery: html root asJQuery ]",
-messageSends: ["copy", "detach", "contents", "asJQuery", "target", "trapGuard:contents:", "append:", "root", "injectToJQuery:", "current"],
+source: "toView: aDataCarrier\x0a\x09| frozen contents |\x0a\x09frozen := aDataCarrier copy.\x0a\x09contents := frozen contents.\x0a\x09frozen target trapGuard: guardPath contents: [ :html |\x0a\x09\x09Trapped current cloneAndInject: contents inPlaceOf: html del ]",
+messageSends: ["copy", "contents", "trapGuard:contents:", "target", "cloneAndInject:inPlaceOf:", "current", "del"],
 referencedClasses: ["Trapped"]
 }),
 smalltalk.TrappedProcessorGuardContents);
@@ -406,62 +395,19 @@ selector: "toView:",
 category: 'data transformation',
 fn: function (aDataCarrier){
 var self=this;
-var frozen,contents,tag;
+var frozen,contents;
 function $Trapped(){return smalltalk.Trapped||(typeof Trapped=="undefined"?nil:Trapped)}
 return smalltalk.withContext(function($ctx1) { 
-var $3,$2,$1,$4,$7,$6,$5,$8,$11,$10,$9,$13,$12;
 frozen=_st(aDataCarrier)._copy();
-$3=_st(frozen)._target();
-$ctx1.sendIdx["target"]=1;
-$2=_st($3)._element();
-$ctx1.sendIdx["element"]=1;
-$1=_st($2)._at_("tagName");
-tag=_st($1)._asLowercase();
-contents=nil;
-$4=_st(tag).__eq("template");
-if(smalltalk.assert($4)){
-$7=_st(frozen)._target();
-$ctx1.sendIdx["target"]=2;
-$6=_st($7)._element();
-$5=_st($6)._at_ifAbsent_("content",(function(){
-return smalltalk.withContext(function($ctx2) {
-return nil;
-}, function($ctx2) {$ctx2.fillBlock({},$ctx1,2)})}));
-if(($receiver = $5) == nil || $receiver == null){
-contents=$5;
-} else {
-var content;
-content=$receiver;
-contents=_st(content)._asJQuery();
-$ctx1.sendIdx["asJQuery"]=1;
-};
-contents;
-};
-$8=contents;
-if(($receiver = $8) == nil || $receiver == null){
-$11=_st(frozen)._target();
-$ctx1.sendIdx["target"]=3;
-$10=_st($11)._asJQuery();
-$ctx1.sendIdx["asJQuery"]=2;
-$9=_st($10)._contents();
-contents=_st($9)._detach();
-contents;
-} else {
-$8;
-};
+contents=_st(frozen)._contents();
 _st(_st(frozen)._target())._trapIter_after_([],(function(html){
 return smalltalk.withContext(function($ctx2) {
-$13=_st(html)._root();
-$ctx2.sendIdx["root"]=1;
-$12=_st($13)._asJQuery();
-$ctx2.sendIdx["asJQuery"]=3;
-_st($12)._append_(_st(contents)._clone());
-return _st(_st($Trapped())._current())._injectToJQuery_(_st(_st(html)._root())._asJQuery());
-}, function($ctx2) {$ctx2.fillBlock({html:html},$ctx1,5)})}));
-return self}, function($ctx1) {$ctx1.fill(self,"toView:",{aDataCarrier:aDataCarrier,frozen:frozen,contents:contents,tag:tag},smalltalk.TrappedProcessorLoopContents)})},
+return _st(_st($Trapped())._current())._cloneAndInject_inPlaceOf_(contents,_st(html)._del());
+}, function($ctx2) {$ctx2.fillBlock({html:html},$ctx1,1)})}));
+return self}, function($ctx1) {$ctx1.fill(self,"toView:",{aDataCarrier:aDataCarrier,frozen:frozen,contents:contents},smalltalk.TrappedProcessorLoopContents)})},
 args: ["aDataCarrier"],
-source: "toView: aDataCarrier\x0a\x09| frozen contents tag |\x0a\x09frozen := aDataCarrier copy.\x0a\x09tag := (frozen target element at: 'tagName') asLowercase.\x0a\x09contents := nil.\x0a\x09tag = 'template' ifTrue: [\x0a\x09\x09contents := (frozen target element at: 'content' ifAbsent: [ nil ]) ifNotNil: [ :content | content asJQuery ] ].\x0a\x09contents ifNil: [ contents := frozen target asJQuery contents detach ].\x0a\x09frozen target trapIter: #() after: [ :html |\x0a\x09\x09html root asJQuery append: contents clone.\x0a\x09\x09Trapped current injectToJQuery: html root asJQuery ]",
-messageSends: ["copy", "asLowercase", "at:", "element", "target", "ifTrue:", "=", "ifNotNil:", "at:ifAbsent:", "asJQuery", "ifNil:", "detach", "contents", "trapIter:after:", "append:", "root", "clone", "injectToJQuery:", "current"],
+source: "toView: aDataCarrier\x0a\x09| frozen contents |\x0a\x09frozen := aDataCarrier copy.\x0a\x09contents := frozen contents.\x0a\x09frozen target trapIter: #() after: [ :html |\x0a\x09\x09Trapped current cloneAndInject: contents inPlaceOf: html del ]",
+messageSends: ["copy", "contents", "trapIter:after:", "target", "cloneAndInject:inPlaceOf:", "current", "del"],
 referencedClasses: ["Trapped"]
 }),
 smalltalk.TrappedProcessorLoopContents);
@@ -887,6 +833,69 @@ referencedClasses: []
 }),
 smalltalk.TrappedProcessorWidget.klass);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "contents",
+category: '*Trapped-Processors',
+fn: function (){
+var self=this;
+var contents,tag,holder;
+return smalltalk.withContext(function($ctx1) { 
+var $1,$2,$4,$5,$3,$6,$7,$8,$9,$10;
+contents=nil;
+holder=_st(self._target())._asJQuery();
+$ctx1.sendIdx["asJQuery"]=1;
+$1=_st(holder)._prop_("tagName");
+$ctx1.sendIdx["prop:"]=1;
+tag=_st($1)._asLowercase();
+$ctx1.sendIdx["asLowercase"]=1;
+$2=_st(tag).__eq("script");
+$ctx1.sendIdx["="]=1;
+if(smalltalk.assert($2)){
+$4=_st(holder)._attr_("data-content");
+if(($receiver = $4) == nil || $receiver == null){
+$5=self._error_("<script> need data-content");
+return $5;
+} else {
+$3=$4;
+};
+holder=_st($3)._asJQuery();
+$ctx1.sendIdx["asJQuery"]=2;
+holder;
+$6=_st(holder)._prop_("tagName");
+$ctx1.sendIdx["prop:"]=2;
+tag=_st($6)._asLowercase();
+tag;
+};
+$7=_st(tag).__eq("template");
+if(smalltalk.assert($7)){
+$8=_st(holder)._prop_("content");
+if(($receiver = $8) == nil || $receiver == null){
+contents=$8;
+} else {
+var content;
+content=$receiver;
+contents=_st(content)._asJQuery();
+};
+contents;
+};
+$9=contents;
+if(($receiver = $9) == nil || $receiver == null){
+contents=_st(_st(holder)._contents())._detach();
+contents;
+} else {
+$9;
+};
+$10=contents;
+return $10;
+}, function($ctx1) {$ctx1.fill(self,"contents",{contents:contents,tag:tag,holder:holder},smalltalk.TrappedDataCarrier)})},
+args: [],
+source: "contents\x0a\x09| contents tag holder |\x0a\x09contents := nil.\x0a\x09holder := self target asJQuery.\x0a\x09tag := (holder prop: 'tagName') asLowercase.\x0a\x09tag = 'script' ifTrue: [\x0a\x09\x09holder := ((holder attr: 'data-content') ifNil: [ ^self error: '<script> need data-content' ]) asJQuery.\x0a\x09\x09tag := (holder prop: 'tagName') asLowercase ].\x0a\x09tag = 'template' ifTrue: [\x0a\x09\x09contents := (holder prop: 'content') ifNotNil: [ :content | content asJQuery ] ].\x0a\x09contents ifNil: [ contents := holder contents detach ].\x0a\x09^contents",
+messageSends: ["asJQuery", "target", "asLowercase", "prop:", "ifTrue:", "=", "ifNil:", "attr:", "error:", "ifNotNil:", "detach", "contents"],
+referencedClasses: []
+}),
+smalltalk.TrappedDataCarrier);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "modifyTarget",

+ 29 - 23
st/Trapped-Frontend.st

@@ -310,6 +310,26 @@ register: aListKeyedEntity name: aString
 
 !Trapped methodsFor: 'action'!
 
+start: args
+	args do: [ :each | self register: each ].
+	self injectToJQuery: 'html' asJQuery
+! !
+
+!Trapped methodsFor: 'initialization'!
+
+initialize
+	super initialize.
+	registry := #{}.
+! !
+
+!Trapped methodsFor: 'private'!
+
+cloneAndInject: anObject inPlaceOf: aTagBrush
+	aTagBrush asJQuery append: anObject clone.
+	self injectToJQuery: aTagBrush asJQuery.
+	aTagBrush asJQuery contents unwrap
+!
+
 descend: anArray snapshotDo: aBlock
 	| tpsc |
     tpsc := TrappedPathStack current.
@@ -333,18 +353,6 @@ injectToJQuery: aJQuery
 				parsed do: [ :rule |
 					(HTMLCanvas onJQuery: jq) root trap: rule first processors: (rule at: 2 ifAbsent: [#()]) ] ].
 			self injectToJQuery: jq children ]
-!
-
-start: args
-	args do: [ :each | self register: each ].
-	self injectToJQuery: 'html' asJQuery
-! !
-
-!Trapped methodsFor: 'initialization'!
-
-initialize
-	super initialize.
-	registry := #{}.
 ! !
 
 !Trapped class methodsFor: 'accessing'!
@@ -372,20 +380,18 @@ parse: aString
 
 !Trapped class methodsFor: 'private'!
 
-envelope: envelope loop: model before: endjq do: aBlock
-   	| envjq |
-    envjq := envelope asJQuery.
-    model withIndexDo: [ :item :i |
-        {i} trapDescend: [ envelope with: aBlock ].
-        envjq children detach insertBefore: endjq.
-    ].
-    envjq remove
+loop: model before: endjq do: aBlock
+	model withIndexDo: [ :item :i |
+		| env envjq |
+		envjq := '<div/>' asJQuery insertBefore: endjq.
+		{i} trapDescend: [ (HTMLCanvas onJQuery: envjq) root with: aBlock ].
+		envjq contents unwrap ]
 !
 
 loop: model between: start and: end do: aBlock
-    (start asJQuery nextUntil: end element) remove.
-    start with: [ :html | model ifNotNil: [
-    	self envelope: html div loop: model before: end asJQuery do: aBlock
+	(start asJQuery nextUntil: end element) remove.
+	start with: [ :html | model ifNotNil: [
+		self loop: model before: end asJQuery do: aBlock
 	]]
 ! !
 

+ 19 - 11
st/Trapped-Processors.st

@@ -114,10 +114,9 @@ data-trap attributes inside.!
 toView: aDataCarrier
 	| frozen contents |
 	frozen := aDataCarrier copy.
-	contents := frozen target asJQuery contents detach.
+	contents := frozen contents.
 	frozen target trapGuard: guardPath contents: [ :html |
-		html root asJQuery append: contents.
-		Trapped current injectToJQuery: html root asJQuery ]
+		Trapped current cloneAndInject: contents inPlaceOf: html del ]
 ! !
 
 TrappedProcessorGuardBase subclass: #TrappedProcessorGuardProc
@@ -219,16 +218,11 @@ noscript, ins or del).!
 !TrappedProcessorLoopContents methodsFor: 'data transformation'!
 
 toView: aDataCarrier
-	| frozen contents tag |
+	| frozen contents |
 	frozen := aDataCarrier copy.
-	tag := (frozen target element at: 'tagName') asLowercase.
-	contents := nil.
-	tag = 'template' ifTrue: [
-		contents := (frozen target element at: 'content' ifAbsent: [ nil ]) ifNotNil: [ :content | content asJQuery ] ].
-	contents ifNil: [ contents := frozen target asJQuery contents detach ].
+	contents := frozen contents.
 	frozen target trapIter: #() after: [ :html |
-		html root asJQuery append: contents clone.
-		Trapped current injectToJQuery: html root asJQuery ]
+		Trapped current cloneAndInject: contents inPlaceOf: html del ]
 ! !
 
 TrappedProcessorLoopBase subclass: #TrappedProcessorLoopProc
@@ -442,6 +436,20 @@ new: aString
 
 !TrappedDataCarrier methodsFor: '*Trapped-Processors'!
 
+contents
+	| contents tag holder |
+	contents := nil.
+	holder := self target asJQuery.
+	tag := (holder prop: 'tagName') asLowercase.
+	tag = 'script' ifTrue: [
+		holder := ((holder attr: 'data-content') ifNil: [ ^self error: '<script> need data-content' ]) asJQuery.
+		tag := (holder prop: 'tagName') asLowercase ].
+	tag = 'template' ifTrue: [
+		contents := (holder prop: 'content') ifNotNil: [ :content | content asJQuery ] ].
+	contents ifNil: [ contents := holder contents detach ].
+	^contents
+!
+
 modifyTarget
 	self target modify: [ self value ]
 !