Silk.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. define("silk/Silk", ["amber/boot", "domite/DOMite", "amber_core/Kernel-Objects", "amber_core/Kernel-Methods", "amber_core/Kernel-Collections", "amber_core/Kernel-Infrastructure"], function($boot){
  2. var $core=$boot.api,nil=$boot.nil,$recv=$boot.asReceiver,$globals=$boot.globals;
  3. $core.addPackage('Silk');
  4. $core.packages["Silk"].innerEval = function (expr) { return eval(expr); };
  5. $core.packages["Silk"].transport = {"type":"amd","amdNamespace":"silk"};
  6. $core.addClass('Silk', $globals.Domite, [], 'Silk');
  7. //>>excludeStart("ide", pragmas.excludeIdeData);
  8. $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'`.";
  9. //>>excludeEnd("ide");
  10. $core.addMethod(
  11. $core.method({
  12. selector: "nextPut:",
  13. protocol: 'writing',
  14. fn: function (anObject){
  15. var self=this;
  16. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  17. return $core.withContext(function($ctx1) {
  18. //>>excludeEnd("ctx");
  19. var $1,$receiver;
  20. $1=$recv(anObject)._renderOnSilk_(self);
  21. if(($receiver = $1) == null || $receiver.isNil){
  22. (
  23. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  24. $ctx1.supercall = true,
  25. //>>excludeEnd("ctx");
  26. $globals.Silk.superclass.fn.prototype._nextPut_.apply($recv(self), [anObject]));
  27. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  28. $ctx1.supercall = false;
  29. //>>excludeEnd("ctx");;
  30. } else {
  31. $1;
  32. };
  33. return self;
  34. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  35. }, function($ctx1) {$ctx1.fill(self,"nextPut:",{anObject:anObject},$globals.Silk)});
  36. //>>excludeEnd("ctx");
  37. },
  38. //>>excludeStart("ide", pragmas.excludeIdeData);
  39. args: ["anObject"],
  40. source: "nextPut: anObject\x0a\x09\x22Double-dispatches anObject via renderOnSilk: message.\x0a\x09If a message returns nil, this fallbacks to superclass.\x0a\x09Otherwise, it is assumed renderOnSilk: did its job.\x22\x0a\x0a\x09(anObject renderOnSilk: self)\x0a\x09\x09ifNil: [ super nextPut: anObject ]",
  41. referencedClasses: [],
  42. //>>excludeEnd("ide");
  43. messageSends: ["ifNil:", "renderOnSilk:", "nextPut:"]
  44. }),
  45. $globals.Silk);
  46. $core.addMethod(
  47. $core.method({
  48. selector: "doesNotUnderstand:",
  49. protocol: 'message handling',
  50. fn: function (aMessage){
  51. var self=this;
  52. var selector;
  53. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  54. return $core.withContext(function($ctx1) {
  55. //>>excludeEnd("ctx");
  56. var $1,$2,$3,$4,$5;
  57. selector=$recv(aMessage)._selector();
  58. $1=$recv($recv(selector)._asUppercase()).__eq(selector);
  59. if(!$core.assert($1)){
  60. $2=(
  61. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  62. $ctx1.supercall = true,
  63. //>>excludeEnd("ctx");
  64. $globals.Silk.klass.superclass.fn.prototype._doesNotUnderstand_.apply($recv(self), [aMessage]));
  65. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  66. $ctx1.supercall = false;
  67. //>>excludeEnd("ctx");;
  68. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  69. $ctx1.sendIdx["doesNotUnderstand:"]=1;
  70. //>>excludeEnd("ctx");
  71. return $2;
  72. };
  73. $3=$recv(selector)._includes_(":");
  74. if($core.assert($3)){
  75. $4=(
  76. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  77. $ctx1.supercall = true,
  78. //>>excludeEnd("ctx");
  79. $globals.Silk.klass.superclass.fn.prototype._doesNotUnderstand_.apply($recv(self), [aMessage]));
  80. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  81. $ctx1.supercall = false;
  82. //>>excludeEnd("ctx");;
  83. return $4;
  84. };
  85. $5=self._newElement_($recv(selector)._asLowercase());
  86. return $5;
  87. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  88. }, function($ctx1) {$ctx1.fill(self,"doesNotUnderstand:",{aMessage:aMessage,selector:selector},$globals.Silk.klass)});
  89. //>>excludeEnd("ctx");
  90. },
  91. //>>excludeStart("ide", pragmas.excludeIdeData);
  92. args: ["aMessage"],
  93. source: "doesNotUnderstand: aMessage\x0a\x09\x22`Silk DIV` creates a div element\x22\x0a\x09| selector |\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\x09^ self newElement: selector asLowercase",
  94. referencedClasses: [],
  95. //>>excludeEnd("ide");
  96. messageSends: ["selector", "ifFalse:", "=", "asUppercase", "doesNotUnderstand:", "ifTrue:", "includes:", "newElement:", "asLowercase"]
  97. }),
  98. $globals.Silk.klass);
  99. $core.addMethod(
  100. $core.method({
  101. selector: "renderOnSilk:",
  102. protocol: '*Silk',
  103. fn: function (aSilk){
  104. var self=this;
  105. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  106. return $core.withContext(function($ctx1) {
  107. //>>excludeEnd("ctx");
  108. self._value_(aSilk);
  109. return self;
  110. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  111. }, function($ctx1) {$ctx1.fill(self,"renderOnSilk:",{aSilk:aSilk},$globals.BlockClosure)});
  112. //>>excludeEnd("ctx");
  113. },
  114. //>>excludeStart("ide", pragmas.excludeIdeData);
  115. args: ["aSilk"],
  116. source: "renderOnSilk: aSilk\x0a\x09self value: aSilk",
  117. referencedClasses: [],
  118. //>>excludeEnd("ide");
  119. messageSends: ["value:"]
  120. }),
  121. $globals.BlockClosure);
  122. $core.addMethod(
  123. $core.method({
  124. selector: "asSilk",
  125. protocol: '*Silk',
  126. fn: function (){
  127. var self=this;
  128. function $Silk(){return $globals.Silk||(typeof Silk=="undefined"?nil:Silk)}
  129. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  130. return $core.withContext(function($ctx1) {
  131. //>>excludeEnd("ctx");
  132. var $1;
  133. $1=$recv($Silk())._at_(self._asString());
  134. return $1;
  135. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  136. }, function($ctx1) {$ctx1.fill(self,"asSilk",{},$globals.CharacterArray)});
  137. //>>excludeEnd("ctx");
  138. },
  139. //>>excludeStart("ide", pragmas.excludeIdeData);
  140. args: [],
  141. source: "asSilk\x0a\x09^ Silk at: self asString",
  142. referencedClasses: ["Silk"],
  143. //>>excludeEnd("ide");
  144. messageSends: ["at:", "asString"]
  145. }),
  146. $globals.CharacterArray);
  147. $core.addMethod(
  148. $core.method({
  149. selector: "inSilk",
  150. protocol: '*Silk',
  151. fn: function (){
  152. var self=this;
  153. function $Silk(){return $globals.Silk||(typeof Silk=="undefined"?nil:Silk)}
  154. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  155. return $core.withContext(function($ctx1) {
  156. //>>excludeEnd("ctx");
  157. var $2,$3,$1;
  158. $2=$recv($Silk())._newStream();
  159. $recv($2).__lt_lt(self);
  160. $3=$recv($2)._yourself();
  161. $1=$3;
  162. return $1;
  163. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  164. }, function($ctx1) {$ctx1.fill(self,"inSilk",{},$globals.JSObjectProxy)});
  165. //>>excludeEnd("ctx");
  166. },
  167. //>>excludeStart("ide", pragmas.excludeIdeData);
  168. args: [],
  169. source: "inSilk\x0a\x09^ Silk newStream << self; yourself",
  170. referencedClasses: ["Silk"],
  171. //>>excludeEnd("ide");
  172. messageSends: ["<<", "newStream", "yourself"]
  173. }),
  174. $globals.JSObjectProxy);
  175. $core.addMethod(
  176. $core.method({
  177. selector: "inSilk",
  178. protocol: '*Silk',
  179. fn: function (){
  180. var self=this;
  181. function $Silk(){return $globals.Silk||(typeof Silk=="undefined"?nil:Silk)}
  182. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  183. return $core.withContext(function($ctx1) {
  184. //>>excludeEnd("ctx");
  185. var $2,$3,$1;
  186. $2=$recv($Silk())._newStream();
  187. $recv($2).__lt_lt(self);
  188. $3=$recv($2)._yourself();
  189. $1=$3;
  190. return $1;
  191. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  192. }, function($ctx1) {$ctx1.fill(self,"inSilk",{},$globals.Object)});
  193. //>>excludeEnd("ctx");
  194. },
  195. //>>excludeStart("ide", pragmas.excludeIdeData);
  196. args: [],
  197. source: "inSilk\x0a\x09^ Silk newStream << self; yourself",
  198. referencedClasses: ["Silk"],
  199. //>>excludeEnd("ide");
  200. messageSends: ["<<", "newStream", "yourself"]
  201. }),
  202. $globals.Object);
  203. $core.addMethod(
  204. $core.method({
  205. selector: "renderOnSilk:",
  206. protocol: '*Silk',
  207. fn: function (aSilk){
  208. var self=this;
  209. return nil;
  210. },
  211. //>>excludeStart("ide", pragmas.excludeIdeData);
  212. args: ["aSilk"],
  213. source: "renderOnSilk: aSilk\x0a\x09^ nil",
  214. referencedClasses: [],
  215. //>>excludeEnd("ide");
  216. messageSends: []
  217. }),
  218. $globals.Object);
  219. });