Bläddra i källkod

toggle: family out, guard:trap:contents: in

Herbert Vojčík 11 år sedan
förälder
incheckning
e30aa2d0cc
4 ändrade filer med 100 tillägg och 122 borttagningar
  1. 40 45
      lib/js/Trapped-Demo.js
  2. 43 59
      lib/js/Trapped-Frontend.js
  3. 4 2
      lib/st/Trapped-Demo.st
  4. 13 16
      lib/st/Trapped-Frontend.st

+ 40 - 45
lib/js/Trapped-Demo.js

@@ -245,79 +245,77 @@ var $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$12,$13,$14,$15,$11;
 return smalltalk.withContext(function($ctx2) {
 _st(_st(html)._h2())._trap_([["title"]]);
 $ctx2.sendIdx["trap:"]=1;
-return _st(_st(html)._div())._trap_toggle_ifNotPresent_([["todos"]],(function(){
+_st(_st(html)._div())._guard_trap_contents_([["todos"], ["notNil"]],[],(function(){
 return smalltalk.withContext(function($ctx3) {
-return _st(snap)._do_((function(){
-return smalltalk.withContext(function($ctx4) {
 $1=_st(html)._span();
-$ctx4.sendIdx["span"]=1;
+$ctx3.sendIdx["span"]=1;
 _st($1)._trap_([["remaining"]]);
-$ctx4.sendIdx["trap:"]=2;
+$ctx3.sendIdx["trap:"]=2;
 _st(html)._with_(" of ");
-$ctx4.sendIdx["with:"]=1;
+$ctx3.sendIdx["with:"]=1;
 $2=_st(html)._span();
-$ctx4.sendIdx["span"]=2;
+$ctx3.sendIdx["span"]=2;
 _st($2)._trap_([["todos"], ["size"]]);
-$ctx4.sendIdx["trap:"]=3;
+$ctx3.sendIdx["trap:"]=3;
 _st(html)._with_(" remaining [ ");
-$ctx4.sendIdx["with:"]=2;
+$ctx3.sendIdx["with:"]=2;
 $3=_st(html)._a();
 _st($3)._href_("");
 _st($3)._onClick_((function(){
-return smalltalk.withContext(function($ctx5) {
+return smalltalk.withContext(function($ctx4) {
 _st(snap)._modify_((function(model){
-return smalltalk.withContext(function($ctx6) {
+return smalltalk.withContext(function($ctx5) {
 return _st(model)._archive();
-}, function($ctx6) {$ctx6.fillBlock({model:model},$ctx5,5)})}));
-$ctx5.sendIdx["modify:"]=1;
+}, function($ctx5) {$ctx5.fillBlock({model:model},$ctx4,4)})}));
+$ctx4.sendIdx["modify:"]=1;
 return false;
-}, function($ctx5) {$ctx5.fillBlock({},$ctx4,4)})}));
+}, function($ctx4) {$ctx4.fillBlock({},$ctx3,3)})}));
 $4=_st($3)._with_("archive");
-$ctx4.sendIdx["with:"]=3;
+$ctx3.sendIdx["with:"]=3;
 $4;
 _st(html)._with_(" ]");
-$ctx4.sendIdx["with:"]=4;
+$ctx3.sendIdx["with:"]=4;
 _st(_st(html)._ul())._with_((function(){
-return smalltalk.withContext(function($ctx5) {
+return smalltalk.withContext(function($ctx4) {
 return _st(html)._trapIter_tag_do_([["todos"]],"li",(function(each){
-return smalltalk.withContext(function($ctx6) {
+return smalltalk.withContext(function($ctx5) {
 $5=_st(html)._root();
-$ctx6.sendIdx["root"]=1;
+$ctx5.sendIdx["root"]=1;
 _st($5)._empty();
 $6=_st(html)._input();
-$ctx6.sendIdx["input"]=1;
+$ctx5.sendIdx["input"]=1;
 _st($6)._type_("checkbox");
-$ctx6.sendIdx["type:"]=1;
+$ctx5.sendIdx["type:"]=1;
 $7=_st($6)._trap_(["done"]);
-$ctx6.sendIdx["trap:"]=4;
+$ctx5.sendIdx["trap:"]=4;
 $7;
 $8=_st(html)._span();
 _st($8)._trap_read_(["done"],(function(model){
-return smalltalk.withContext(function($ctx7) {
+return smalltalk.withContext(function($ctx6) {
 return _st(_st(html)._root())._class_("done-".__comma(model));
-$ctx7.sendIdx["class:"]=1;
-}, function($ctx7) {$ctx7.fillBlock({model:model},$ctx6,8)})}));
+$ctx6.sendIdx["class:"]=1;
+}, function($ctx6) {$ctx6.fillBlock({model:model},$ctx5,7)})}));
 $9=_st($8)._trap_(["text"]);
-$ctx6.sendIdx["trap:"]=5;
+$ctx5.sendIdx["trap:"]=5;
 return $9;
-}, function($ctx6) {$ctx6.fillBlock({each:each},$ctx5,7)})}));
-}, function($ctx5) {$ctx5.fillBlock({},$ctx4,6)})}));
-$ctx4.sendIdx["with:"]=5;
+}, function($ctx5) {$ctx5.fillBlock({each:each},$ctx4,6)})}));
+}, function($ctx4) {$ctx4.fillBlock({},$ctx3,5)})}));
+$ctx3.sendIdx["with:"]=5;
 $10=_st(html)._form();
 _st($10)._onSubmit_((function(){
-return smalltalk.withContext(function($ctx5) {
+return smalltalk.withContext(function($ctx4) {
 _st(snap)._modify_((function(model){
-return smalltalk.withContext(function($ctx6) {
+return smalltalk.withContext(function($ctx5) {
 return _st(model)._addTodo();
-}, function($ctx6) {$ctx6.fillBlock({model:model},$ctx5,10)})}));
+}, function($ctx5) {$ctx5.fillBlock({model:model},$ctx4,9)})}));
 return false;
-}, function($ctx5) {$ctx5.fillBlock({},$ctx4,9)})}));
+}, function($ctx4) {$ctx4.fillBlock({},$ctx3,8)})}));
 $11=_st($10)._with_((function(){
-return smalltalk.withContext(function($ctx5) {
+return smalltalk.withContext(function($ctx4) {
 $12=_st(html)._input();
-$ctx5.sendIdx["input"]=2;
+$ctx4.sendIdx["input"]=2;
 _st($12)._type_("text");
-$ctx5.sendIdx["type:"]=2;
+$ctx4.sendIdx["type:"]=2;
 _st($12)._trap_([["todoText"]]);
 _st($12)._at_put_("size",(30));
 $13=_st($12)._placeholder_("add new todo here");
@@ -327,19 +325,16 @@ _st($14)._class_("btn-primary");
 _st($14)._type_("submit");
 $15=_st($14)._value_("add");
 return $15;
-}, function($ctx5) {$ctx5.fillBlock({},$ctx4,11)})}));
-$ctx4.sendIdx["with:"]=6;
+}, function($ctx4) {$ctx4.fillBlock({},$ctx3,10)})}));
 return $11;
-}, function($ctx4) {$ctx4.fillBlock({},$ctx3,3)})}));
-}, function($ctx3) {$ctx3.fillBlock({},$ctx2,2)})}),(function(){
-return smalltalk.withContext(function($ctx3) {
-return _st(html)._with_("Loading ...");
-}, function($ctx3) {$ctx3.fillBlock({},$ctx2,12)})}));
+}, function($ctx3) {$ctx3.fillBlock({},$ctx2,2)})}));
+$ctx2.sendIdx["guard:trap:contents:"]=1;
+return _st(_st(html)._p())._guard_trap_contents_([["todos"], ["isNil"]],[],"Loading ...");
 }, function($ctx2) {$ctx2.fillBlock({snap:snap},$ctx1,1)})}));
 return self}, function($ctx1) {$ctx1.fill(self,"renderOn:",{html:html},smalltalk.AppView)})},
 args: ["html"],
-source: "renderOn: html\x0a    #() trapDescend: [ :snap |\x0a\x09html h2 trap: #((title)).\x0a    html div trap: #((todos)) toggle: [ snap do: [\x0a        html span trap:#((remaining)).\x0a        html with: ' of '.\x0a        html span trap: #((todos) (size)).\x0a        html with: ' remaining [ '.\x0a        html a href:''; onClick: [\x0a            snap modify: [ :model | model archive ].\x0a            false\x0a        ]; with: 'archive'.\x0a        html with: ' ]'.\x0a        html ul with: [ html trapIter: #((todos)) tag: #li do: [ :each |\x0a            html root empty.\x0a            html input type: 'checkbox'; trap: #('done').\x0a            html span trap: #('done') read: [ :model | html root class: 'done-', model ]; trap: #('text').\x0a        ]].\x0a        html form onSubmit: [\x0a            snap modify: [ :model | model addTodo ].\x0a            false\x0a        ]; with: [\x0a            html input type: 'text'; trap: #((todoText)); at: 'size' put: 30; placeholder: 'add new todo here'.\x0a            html input class: 'btn-primary'; type: 'submit'; value: 'add'.\x0a        ].\x0a    ]] ifNotPresent: [ html with: 'Loading ...' ]]",
-messageSends: ["trapDescend:", "trap:", "h2", "trap:toggle:ifNotPresent:", "div", "do:", "span", "with:", "href:", "a", "onClick:", "modify:", "archive", "ul", "trapIter:tag:do:", "empty", "root", "type:", "input", "trap:read:", "class:", ",", "onSubmit:", "form", "addTodo", "at:put:", "placeholder:", "value:"],
+source: "renderOn: html\x0a    #() trapDescend: [ :snap |\x0a\x09html h2 trap: #((title)).\x0a    html div guard: #((todos) (notNil)) trap: #() contents: [\x0a        html span trap:#((remaining)).\x0a        html with: ' of '.\x0a        html span trap: #((todos) (size)).\x0a        html with: ' remaining [ '.\x0a        html a href:''; onClick: [\x0a            snap modify: [ :model | model archive ].\x0a            false\x0a        ]; with: 'archive'.\x0a        html with: ' ]'.\x0a        html ul with: [ html trapIter: #((todos)) tag: #li do: [ :each |\x0a            html root empty.\x0a            html input type: 'checkbox'; trap: #('done').\x0a            html span trap: #('done') read: [ :model | html root class: 'done-', model ]; trap: #('text').\x0a        ]].\x0a        html form onSubmit: [\x0a            snap modify: [ :model | model addTodo ].\x0a            false\x0a        ]; with: [\x0a            html input type: 'text'; trap: #((todoText)); at: 'size' put: 30; placeholder: 'add new todo here'.\x0a            html input class: 'btn-primary'; type: 'submit'; value: 'add'.\x0a        ].\x0a    ].\x0a\x09html p guard: #((todos) (isNil)) trap: #() contents: 'Loading ...'.\x0a\x09]",
+messageSends: ["trapDescend:", "trap:", "h2", "guard:trap:contents:", "div", "span", "with:", "href:", "a", "onClick:", "modify:", "archive", "ul", "trapIter:tag:do:", "empty", "root", "type:", "input", "trap:read:", "class:", ",", "onSubmit:", "form", "addTodo", "at:put:", "placeholder:", "value:", "p"],
 referencedClasses: []
 }),
 smalltalk.AppView);

+ 43 - 59
lib/js/Trapped-Frontend.js

@@ -796,6 +796,49 @@ referencedClasses: ["Trapped"]
 }),
 smalltalk.HTMLCanvas);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "guard:trap:contents:",
+category: '*Trapped-Frontend',
+fn: function (anArray,anotherArray,aBlock){
+var self=this;
+var shown,snap;
+return smalltalk.withContext(function($ctx1) { 
+var $1,$2,$3;
+shown=nil;
+_st(anotherArray)._trapDescend_((function(aSnap){
+return smalltalk.withContext(function($ctx2) {
+snap=aSnap;
+return snap;
+}, function($ctx2) {$ctx2.fillBlock({aSnap:aSnap},$ctx1,1)})}));
+self._trap_read_(anArray,(function(gdata){
+return smalltalk.withContext(function($ctx2) {
+$1=_st(shown).__eq(gdata);
+if(! smalltalk.assert($1)){
+shown=gdata;
+shown;
+$2=shown;
+if(smalltalk.assert($2)){
+_st(snap)._do_((function(){
+return smalltalk.withContext(function($ctx3) {
+return self._contents_(aBlock);
+}, function($ctx3) {$ctx3.fillBlock({},$ctx2,5)})}));
+$3=self._asJQuery();
+$ctx2.sendIdx["asJQuery"]=1;
+return _st($3)._show();
+} else {
+return _st(self._asJQuery())._hide();
+};
+};
+}, function($ctx2) {$ctx2.fillBlock({gdata:gdata},$ctx1,2)})}));
+return self}, function($ctx1) {$ctx1.fill(self,"guard:trap:contents:",{anArray:anArray,anotherArray:anotherArray,aBlock:aBlock,shown:shown,snap:snap},smalltalk.TagBrush)})},
+args: ["anArray", "anotherArray", "aBlock"],
+source: "guard: anArray trap: anotherArray contents: aBlock\x0a    | shown snap |\x0a    shown := nil.\x0a\x09anotherArray trapDescend: [ :aSnap | snap := aSnap ].\x0a    self trap: anArray read: [ :gdata |\x0a        shown = gdata ifFalse: [\x0a            shown := gdata.\x0a            shown\x0a\x09\x09\x09\x09ifTrue: [ snap do: [ self contents: aBlock ]. self asJQuery show ]\x0a\x09\x09\x09\x09ifFalse: [ self asJQuery hide ] ]\x0a    ]",
+messageSends: ["trapDescend:", "trap:read:", "ifFalse:", "=", "ifTrue:ifFalse:", "do:", "contents:", "show", "asJQuery", "hide"],
+referencedClasses: []
+}),
+smalltalk.TagBrush);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "trap:",
@@ -847,63 +890,4 @@ referencedClasses: ["KeyedPubSubUnsubscribe"]
 }),
 smalltalk.TagBrush);
 
-smalltalk.addMethod(
-smalltalk.method({
-selector: "trap:toggle:",
-category: '*Trapped-Frontend',
-fn: function (path,aBlock){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
-self._trap_toggle_ifNotPresent_(path,aBlock,(function(){
-return smalltalk.withContext(function($ctx2) {
-return _st(self._asJQuery())._hide();
-}, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)})}));
-return self}, function($ctx1) {$ctx1.fill(self,"trap:toggle:",{path:path,aBlock:aBlock},smalltalk.TagBrush)})},
-args: ["path", "aBlock"],
-source: "trap: path toggle: aBlock\x0a    self trap: path toggle: aBlock ifNotPresent: [ self asJQuery hide ]",
-messageSends: ["trap:toggle:ifNotPresent:", "hide", "asJQuery"],
-referencedClasses: []
-}),
-smalltalk.TagBrush);
-
-smalltalk.addMethod(
-smalltalk.method({
-selector: "trap:toggle:ifNotPresent:",
-category: '*Trapped-Frontend',
-fn: function (path,aBlock,anotherBlock){
-var self=this;
-var shown;
-return smalltalk.withContext(function($ctx1) { 
-var $2,$3,$1,$4,$5,$7,$6;
-shown=nil;
-self._trap_read_(path,(function(data,html){
-return smalltalk.withContext(function($ctx2) {
-$2=shown;
-$3=_st(data)._notNil();
-$ctx2.sendIdx["notNil"]=1;
-$1=_st($2).__eq($3);
-if(! smalltalk.assert($1)){
-shown=_st(data)._notNil();
-shown;
-$4=self._asJQuery();
-_st($4)._empty();
-$5=_st($4)._show();
-$5;
-$7=shown;
-if(smalltalk.assert($7)){
-$6=aBlock;
-} else {
-$6=anotherBlock;
-};
-return _st($6)._value_value_(data,html);
-};
-}, function($ctx2) {$ctx2.fillBlock({data:data,html:html},$ctx1,1)})}));
-return self}, function($ctx1) {$ctx1.fill(self,"trap:toggle:ifNotPresent:",{path:path,aBlock:aBlock,anotherBlock:anotherBlock,shown:shown},smalltalk.TagBrush)})},
-args: ["path", "aBlock", "anotherBlock"],
-source: "trap: path toggle: aBlock ifNotPresent: anotherBlock\x0a    | shown |\x0a    shown := nil.\x0a    self trap: path read: [ :data :html |\x0a        shown = data notNil ifFalse: [\x0a            shown := data notNil.\x0a            self asJQuery empty; show.\x0a            (shown ifTrue: [aBlock] ifFalse: [anotherBlock]) value: data value: html.\x0a        ]\x0a    ]",
-messageSends: ["trap:read:", "ifFalse:", "=", "notNil", "empty", "asJQuery", "show", "value:value:", "ifTrue:ifFalse:"],
-referencedClasses: []
-}),
-smalltalk.TagBrush);
-
 });

+ 4 - 2
lib/st/Trapped-Demo.st

@@ -150,7 +150,7 @@ Widget subclass: #AppView
 renderOn: html
     #() trapDescend: [ :snap |
 	html h2 trap: #((title)).
-    html div trap: #((todos)) toggle: [ snap do: [
+    html div guard: #((todos) (notNil)) trap: #() contents: [
         html span trap:#((remaining)).
         html with: ' of '.
         html span trap: #((todos) (size)).
@@ -172,6 +172,8 @@ renderOn: html
             html input type: 'text'; trap: #((todoText)); at: 'size' put: 30; placeholder: 'add new todo here'.
             html input class: 'btn-primary'; type: 'submit'; value: 'add'.
         ].
-    ]] ifNotPresent: [ html with: 'Loading ...' ]]
+    ].
+	html p guard: #((todos) (isNil)) trap: #() contents: 'Loading ...'.
+	]
 ! !
 

+ 13 - 16
lib/st/Trapped-Frontend.st

@@ -275,6 +275,19 @@ trapIter: path tag: aSymbol do: aBlock
 
 !TagBrush methodsFor: '*Trapped-Frontend'!
 
+guard: anArray trap: anotherArray contents: aBlock
+    | shown snap |
+    shown := nil.
+	anotherArray trapDescend: [ :aSnap | snap := aSnap ].
+    self trap: anArray read: [ :gdata |
+        shown = gdata ifFalse: [
+            shown := gdata.
+            shown
+				ifTrue: [ snap do: [ self contents: aBlock ]. self asJQuery show ]
+				ifFalse: [ self asJQuery hide ] ]
+    ]
+!
+
 trap: path
 	(Trapped current binder: self) installFor: path
 !
@@ -286,21 +299,5 @@ trap: path read: aBlock
         	snap do: [ self with: [ :html | aBlock value: data value: html ] ]
     	]
     ]
-!
-
-trap: path toggle: aBlock
-    self trap: path toggle: aBlock ifNotPresent: [ self asJQuery hide ]
-!
-
-trap: path toggle: aBlock ifNotPresent: anotherBlock
-    | shown |
-    shown := nil.
-    self trap: path read: [ :data :html |
-        shown = data notNil ifFalse: [
-            shown := data notNil.
-            self asJQuery empty; show.
-            (shown ifTrue: [aBlock] ifFalse: [anotherBlock]) value: data value: html.
-        ]
-    ]
 ! !