Trapped-Demo.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. smalltalk.addPackage('Trapped-Demo', {});
  2. smalltalk.addClass('App', smalltalk.TrappedMWIsolated, [], 'Trapped-Demo');
  3. smalltalk.App.comment="// Code from AngularJS Todo example, http://angularjs.org/#todo-js\x0afunction TodoCtrl($scope) {\x0a $scope.todos = [\x0a {text:'learn angular', done:true},\x0a {text:'build an angular app', done:false}];\x0a \x0a $scope.addTodo = function() {\x0a $scope.todos.push({text:$scope.todoText, done:false});\x0a $scope.todoText = '';\x0a };\x0a \x0a $scope.remaining = function() {\x0a var count = 0;\x0a angular.forEach($scope.todos, function(todo) {\x0a count += todo.done ? 0 : 1;\x0a });\x0a return count;\x0a };\x0a \x0a $scope.archive = function() {\x0a var oldTodos = $scope.todos;\x0a $scope.todos = [];\x0a angular.forEach(oldTodos, function(todo) {\x0a if (!todo.done) $scope.todos.push(todo);\x0a });\x0a };\x0a}"
  4. smalltalk.addMethod(
  5. "_initialize",
  6. smalltalk.method({
  7. selector: "initialize",
  8. category: 'initialization',
  9. fn: function (){
  10. var self=this;
  11. smalltalk.send(self,"_initialize",[],smalltalk.TrappedMWIsolated);
  12. smalltalk.send(self,"_dispatcher_",[smalltalk.send((smalltalk.TrappedDumbDispatcher || TrappedDumbDispatcher),"_new",[])]);
  13. smalltalk.send(self,"_model_",[smalltalk.send(smalltalk.send((smalltalk.AppModel || AppModel),"_new",[]),"_title_",["Todo"])]);
  14. smalltalk.send((function(){
  15. return smalltalk.send(self,"_modify_do_",[[smalltalk.symbolFor("todos")],(function(){
  16. return [smalltalk.HashedCollection._fromPairs_([smalltalk.send("text","__minus_gt",["learn trapped"]),smalltalk.send("done","__minus_gt",[true])]),smalltalk.HashedCollection._fromPairs_([smalltalk.send("text","__minus_gt",["build a trapped app"]),smalltalk.send("done","__minus_gt",[false])])];
  17. })]);
  18. }),"_valueWithTimeout_",[(2000)]);
  19. return self},
  20. args: [],
  21. source: "initialize\x0a\x09super initialize.\x0a self dispatcher: TrappedDumbDispatcher new.\x0a self model: (AppModel new title: 'Todo').\x0a [ self modify: #(#todos) do: [{\x0a #{'text'->'learn trapped'. 'done'->true}.\x0a #{'text'->'build a trapped app'. 'done'->false}\x0a }]] valueWithTimeout: 2000\x0a",
  22. messageSends: ["initialize", "dispatcher:", "new", "model:", "title:", "valueWithTimeout:", "modify:do:", "->"],
  23. referencedClasses: ["TrappedDumbDispatcher", "AppModel"]
  24. }),
  25. smalltalk.App);
  26. smalltalk.addClass('AppModel', smalltalk.Object, ['title', 'todos', 'todoText'], 'Trapped-Demo');
  27. smalltalk.AppModel.comment="// Code from AngularJS Todo example, http://angularjs.org/#todo-js\x0afunction TodoCtrl($scope) {\x0a $scope.todos = [\x0a {text:'learn angular', done:true},\x0a {text:'build an angular app', done:false}];\x0a \x0a $scope.addTodo = function() {\x0a $scope.todos.push({text:$scope.todoText, done:false});\x0a $scope.todoText = '';\x0a };\x0a \x0a $scope.remaining = function() {\x0a var count = 0;\x0a angular.forEach($scope.todos, function(todo) {\x0a count += todo.done ? 0 : 1;\x0a });\x0a return count;\x0a };\x0a \x0a $scope.archive = function() {\x0a var oldTodos = $scope.todos;\x0a $scope.todos = [];\x0a angular.forEach(oldTodos, function(todo) {\x0a if (!todo.done) $scope.todos.push(todo);\x0a });\x0a };\x0a}"
  28. smalltalk.addMethod(
  29. "_addTodo",
  30. smalltalk.method({
  31. selector: "addTodo",
  32. category: 'action',
  33. fn: function (){
  34. var self=this;
  35. smalltalk.send(smalltalk.send(self,"_todos",[]),"_add_",[smalltalk.HashedCollection._fromPairs_([smalltalk.send("text","__minus_gt",[smalltalk.send(self,"_todoText",[])]),smalltalk.send("done","__minus_gt",[false])])]);
  36. smalltalk.send(self,"_todoText_",[""]);
  37. return self},
  38. args: [],
  39. source: "addTodo\x0a self todos add: #{'text'->self todoText. 'done'->false}.\x0a self todoText: ''",
  40. messageSends: ["add:", "->", "todoText", "todos", "todoText:"],
  41. referencedClasses: []
  42. }),
  43. smalltalk.AppModel);
  44. smalltalk.addMethod(
  45. "_archive",
  46. smalltalk.method({
  47. selector: "archive",
  48. category: 'action',
  49. fn: function (){
  50. var self=this;
  51. smalltalk.send(self,"_todos_",[smalltalk.send(smalltalk.send(self,"_todos",[]),"_reject_",[(function(each){
  52. return smalltalk.send(each,"_at_",["done"]);
  53. })])]);
  54. return self},
  55. args: [],
  56. source: "archive\x0a self todos: (self todos reject: [ :each | each at: 'done' ])",
  57. messageSends: ["todos:", "reject:", "at:", "todos"],
  58. referencedClasses: []
  59. }),
  60. smalltalk.AppModel);
  61. smalltalk.addMethod(
  62. "_remaining",
  63. smalltalk.method({
  64. selector: "remaining",
  65. category: 'accessing',
  66. fn: function (){
  67. var self=this;
  68. var $1;
  69. $1=smalltalk.send(smalltalk.send(smalltalk.send(self,"_todos",[]),"_select_",[(function(each){
  70. return smalltalk.send(each,"_at_",["done"]);
  71. })]),"_size",[]);
  72. return $1;
  73. },
  74. args: [],
  75. source: "remaining\x0a ^(self todos select: [ :each | each at: 'done' ]) size",
  76. messageSends: ["size", "select:", "at:", "todos"],
  77. referencedClasses: []
  78. }),
  79. smalltalk.AppModel);
  80. smalltalk.addMethod(
  81. "_title",
  82. smalltalk.method({
  83. selector: "title",
  84. category: 'accessing',
  85. fn: function (){
  86. var self=this;
  87. return self["@title"];
  88. },
  89. args: [],
  90. source: "title\x0a\x09^title",
  91. messageSends: [],
  92. referencedClasses: []
  93. }),
  94. smalltalk.AppModel);
  95. smalltalk.addMethod(
  96. "_title_",
  97. smalltalk.method({
  98. selector: "title:",
  99. category: 'accessing',
  100. fn: function (aString){
  101. var self=this;
  102. self["@title"]=aString;
  103. return self},
  104. args: ["aString"],
  105. source: "title: aString\x0a\x09title := aString",
  106. messageSends: [],
  107. referencedClasses: []
  108. }),
  109. smalltalk.AppModel);
  110. smalltalk.addMethod(
  111. "_todoText",
  112. smalltalk.method({
  113. selector: "todoText",
  114. category: 'accessing',
  115. fn: function (){
  116. var self=this;
  117. return self["@todoText"];
  118. },
  119. args: [],
  120. source: "todoText\x0a\x09^todoText",
  121. messageSends: [],
  122. referencedClasses: []
  123. }),
  124. smalltalk.AppModel);
  125. smalltalk.addMethod(
  126. "_todoText_",
  127. smalltalk.method({
  128. selector: "todoText:",
  129. category: 'accessing',
  130. fn: function (aString){
  131. var self=this;
  132. self["@todoText"]=aString;
  133. return self},
  134. args: ["aString"],
  135. source: "todoText: aString\x0a\x09todoText := aString",
  136. messageSends: [],
  137. referencedClasses: []
  138. }),
  139. smalltalk.AppModel);
  140. smalltalk.addMethod(
  141. "_todos",
  142. smalltalk.method({
  143. selector: "todos",
  144. category: 'accessing',
  145. fn: function (){
  146. var self=this;
  147. return self["@todos"];
  148. },
  149. args: [],
  150. source: "todos\x0a\x09^todos",
  151. messageSends: [],
  152. referencedClasses: []
  153. }),
  154. smalltalk.AppModel);
  155. smalltalk.addMethod(
  156. "_todos_",
  157. smalltalk.method({
  158. selector: "todos:",
  159. category: 'accessing',
  160. fn: function (anArray){
  161. var self=this;
  162. self["@todos"]=anArray;
  163. return self},
  164. args: ["anArray"],
  165. source: "todos: anArray\x0a\x09todos := anArray",
  166. messageSends: [],
  167. referencedClasses: []
  168. }),
  169. smalltalk.AppModel);
  170. smalltalk.addClass('AppView', smalltalk.Widget, [], 'Trapped-Demo');
  171. smalltalk.AppView.comment=" <!-- Code from AngularJS Todo example, http://angularjs.org/#todo-html -->\x0a <body>\x0a <h2>Todo</h2>\x0a <div ng-controller=\x22TodoCtrl\x22>\x0a <span>{{remaining()}} of {{todos.length}} remaining</span>\x0a [ <a href=\x22\x22 ng-click=\x22archive()\x22>archive</a> ]\x0a <ul class=\x22unstyled\x22>\x0a <li ng-repeat=\x22todo in todos\x22>\x0a <input type=\x22checkbox\x22 ng-model=\x22todo.done\x22>\x0a <span class=\x22done-{{todo.done}}\x22>{{todo.text}}</span>\x0a </li>\x0a </ul>\x0a <form ng-submit=\x22addTodo()\x22>\x0a <input type=\x22text\x22 ng-model=\x22todoText\x22 size=\x2230\x22\x0a placeholder=\x22add new todo here\x22>\x0a <input class=\x22btn-primary\x22 type=\x22submit\x22 value=\x22add\x22>\x0a </form>\x0a </div>\x0a </body>\x0a"
  172. smalltalk.addMethod(
  173. "_renderOn_",
  174. smalltalk.method({
  175. selector: "renderOn:",
  176. category: 'rendering',
  177. fn: function (html){
  178. var self=this;
  179. var $1,$2,$3,$4,$5,$6,$7,$9,$10,$11,$12,$8;
  180. var snap;
  181. smalltalk.send(smalltalk.send(html,"_h2",[]),"_trap_",[[smalltalk.symbolFor("title")]]);
  182. snap=smalltalk.send(smalltalk.send((smalltalk.Trapped || Trapped),"_current",[]),"_snapshot",[]);
  183. smalltalk.send(smalltalk.send(html,"_div",[]),"_trap_toggle_ifNotPresent_",[[smalltalk.symbolFor("todos")],(function(){
  184. return smalltalk.send(snap,"_do_",[(function(){
  185. smalltalk.send(smalltalk.send(html,"_span",[]),"_trap_",[[smalltalk.symbolFor("remaining")]]);
  186. smalltalk.send(html,"_with_",[" of "]);
  187. smalltalk.send(smalltalk.send(html,"_span",[]),"_trap_",[[smalltalk.symbolFor("todos"), smalltalk.symbolFor("size")]]);
  188. smalltalk.send(html,"_with_",[" remaining [ "]);
  189. $1=smalltalk.send(html,"_a",[]);
  190. smalltalk.send($1,"_href_",[""]);
  191. smalltalk.send($1,"_onClick_",[(function(){
  192. return smalltalk.send((function(){
  193. smalltalk.send(smalltalk.send(snap,"_model",[]),"_modify_do_",[smalltalk.send(smalltalk.send(snap,"_path",[]),"_allButFirst",[]),(function(model){
  194. return smalltalk.send(model,"_archive",[]);
  195. })]);
  196. return false;
  197. }),"_value",[]);
  198. })]);
  199. $2=smalltalk.send($1,"_with_",["archive"]);
  200. $2;
  201. smalltalk.send(html,"_with_",[" ]"]);
  202. smalltalk.send(smalltalk.send(html,"_ul",[]),"_trapIter_tag_do_",[[smalltalk.symbolFor("todos")],smalltalk.symbolFor("li"),(function(each){
  203. smalltalk.send(smalltalk.send(html,"_root",[]),"_empty",[]);
  204. $3=smalltalk.send(html,"_input",[]);
  205. smalltalk.send($3,"_type_",["checkbox"]);
  206. $4=smalltalk.send($3,"_trap_",[["done"]]);
  207. $4;
  208. $5=smalltalk.send(html,"_span",[]);
  209. smalltalk.send($5,"_trap_read_",[["done"],(function(model){
  210. return smalltalk.send(smalltalk.send(html,"_root",[]),"_class_",[smalltalk.send("done-","__comma",[model])]);
  211. })]);
  212. $6=smalltalk.send($5,"_trap_",[["text"]]);
  213. return $6;
  214. })]);
  215. $7=smalltalk.send(html,"_form",[]);
  216. smalltalk.send($7,"_onSubmit_",[(function(){
  217. return smalltalk.send((function(){
  218. smalltalk.send(smalltalk.send(snap,"_model",[]),"_modify_do_",[smalltalk.send(smalltalk.send(snap,"_path",[]),"_allButFirst",[]),(function(model){
  219. return smalltalk.send(model,"_addTodo",[]);
  220. })]);
  221. return false;
  222. }),"_value",[]);
  223. })]);
  224. $8=smalltalk.send($7,"_with_",[(function(){
  225. $9=smalltalk.send(html,"_input",[]);
  226. smalltalk.send($9,"_type_",["text"]);
  227. smalltalk.send($9,"_trap_",[[smalltalk.symbolFor("todoText")]]);
  228. smalltalk.send($9,"_at_put_",["size",(30)]);
  229. $10=smalltalk.send($9,"_placeholder_",["add new todo here"]);
  230. $10;
  231. $11=smalltalk.send(html,"_input",[]);
  232. smalltalk.send($11,"_class_",["btn-primary"]);
  233. smalltalk.send($11,"_type_",["submit"]);
  234. $12=smalltalk.send($11,"_value_",["add"]);
  235. return $12;
  236. })]);
  237. return $8;
  238. })]);
  239. }),(function(){
  240. return smalltalk.send(html,"_with_",["Loading ..."]);
  241. })]);
  242. return self},
  243. args: ["html"],
  244. source: "renderOn: html\x0a | snap |\x0a\x09html h2 trap: #(#title).\x0a snap := Trapped current snapshot.\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 model modify: snap path allButFirst do: [ :model | model archive ].\x0a false\x0a ] value \x22amber GH-314 workaround\x22]; with: 'archive'.\x0a html with: ' ]'.\x0a html ul 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 model modify: snap path allButFirst do: [ :model | model addTodo ].\x0a false\x0a ] value \x22amber GH-314 workaround\x22]; 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 ...' ]",
  245. messageSends: ["trap:", "h2", "snapshot", "current", "trap:toggle:ifNotPresent:", "do:", "span", "with:", "href:", "a", "onClick:", "value", "modify:do:", "allButFirst", "path", "archive", "model", "trapIter:tag:do:", "empty", "root", "type:", "input", "trap:read:", "class:", ",", "ul", "onSubmit:", "addTodo", "form", "at:put:", "placeholder:", "value:", "div"],
  246. referencedClasses: ["Trapped"]
  247. }),
  248. smalltalk.AppView);
  249. smalltalk.addClass('TrappedDumbDispatcher', smalltalk.TrappedDispatcher, ['queue'], 'Trapped-Demo');
  250. smalltalk.addMethod(
  251. "_add_",
  252. smalltalk.method({
  253. selector: "add:",
  254. category: 'accessing',
  255. fn: function (aSubscription){
  256. var self=this;
  257. smalltalk.send(self["@queue"],"_add_",[aSubscription]);
  258. return self},
  259. args: ["aSubscription"],
  260. source: "add: aSubscription\x0a\x09queue add: aSubscription.\x0a",
  261. messageSends: ["add:"],
  262. referencedClasses: []
  263. }),
  264. smalltalk.TrappedDumbDispatcher);
  265. smalltalk.addMethod(
  266. "_clean",
  267. smalltalk.method({
  268. selector: "clean",
  269. category: 'bookkeeping',
  270. fn: function (){
  271. var self=this;
  272. self["@queue"]=smalltalk.send(self["@queue"],"_select_",[(function(each){
  273. return smalltalk.send(each,"_isEnabled",[]);
  274. })]);
  275. return self},
  276. args: [],
  277. source: "clean\x0a\x09queue := queue select: [ :each | each isEnabled ]",
  278. messageSends: ["select:", "isEnabled"],
  279. referencedClasses: []
  280. }),
  281. smalltalk.TrappedDumbDispatcher);
  282. smalltalk.addMethod(
  283. "_do_",
  284. smalltalk.method({
  285. selector: "do:",
  286. category: 'enumeration',
  287. fn: function (aBlock){
  288. var self=this;
  289. smalltalk.send(self["@queue"],"_do_",[aBlock]);
  290. return self},
  291. args: ["aBlock"],
  292. source: "do: aBlock\x0a\x09queue do: aBlock",
  293. messageSends: ["do:"],
  294. referencedClasses: []
  295. }),
  296. smalltalk.TrappedDumbDispatcher);
  297. smalltalk.addMethod(
  298. "_initialize",
  299. smalltalk.method({
  300. selector: "initialize",
  301. category: 'initialization',
  302. fn: function (){
  303. var self=this;
  304. self["@queue"]=smalltalk.send((smalltalk.OrderedCollection || OrderedCollection),"_new",[]);
  305. return self},
  306. args: [],
  307. source: "initialize\x0a\x09queue := OrderedCollection new",
  308. messageSends: ["new"],
  309. referencedClasses: ["OrderedCollection"]
  310. }),
  311. smalltalk.TrappedDumbDispatcher);