Silk.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. define("silk/Silk", ["amber/boot", "amber/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 magic on other objects:\x0a - blocks: `aSilk << aBlock` runs the block, passing aSilk as a parameter.\x0a - associations: `aSilk << (key -> value)` set attribute key to value.\x0a\x0aIt is good to note that rendering collection has magic\x0aof its own built-in in general: if you `stream << aCollection`, its items are `<<`'d in sequence.\x0aSo, de facto, array are deeply flattened\x0awhen putting on a stream via `<<`.\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.\x0a - `aSilk DIV: anObject` is shortcut for `aSilk DIV << anObject; yourself`. IOW, it not just creates and insert the element, but puts a content into it.\x0a\x0a##Conclusions\x0a\x0aTaken all this together, one can do pretty neat constructs:\x0a\x0a```\x0a aSilk P: { 'id'->'mission'. 'We are the champions.' }\x0a```\x0a\x0aadds `<p id=\x22mission\x22>We are the champions.</p>` into `aSilk`.";
  9. //>>excludeEnd("ide");
  10. $core.addMethod(
  11. $core.method({
  12. selector: "doesNotUnderstand:",
  13. protocol: 'writing',
  14. fn: function (aMessage){
  15. var self=this;
  16. var selector,newElement,useArg;
  17. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  18. return $core.withContext(function($ctx1) {
  19. //>>excludeEnd("ctx");
  20. var $1,$2,$3,$4,$5,$6,$7;
  21. selector=$recv(aMessage)._selector();
  22. $1=$recv($recv(selector)._asUppercase()).__eq(selector);
  23. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  24. $ctx1.sendIdx["="]=1;
  25. //>>excludeEnd("ctx");
  26. if(!$core.assert($1)){
  27. $2=(
  28. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  29. $ctx1.supercall = true,
  30. //>>excludeEnd("ctx");
  31. $globals.Silk.superclass.fn.prototype._doesNotUnderstand_.apply($recv(self), [aMessage]));
  32. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  33. $ctx1.supercall = false;
  34. //>>excludeEnd("ctx");;
  35. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  36. $ctx1.sendIdx["doesNotUnderstand:"]=1;
  37. //>>excludeEnd("ctx");
  38. return $2;
  39. };
  40. $3=$recv($recv(selector)._last()).__eq(":");
  41. if($core.assert($3)){
  42. useArg=true;
  43. useArg;
  44. selector=$recv(selector)._allButLast();
  45. selector;
  46. } else {
  47. useArg=false;
  48. useArg;
  49. };
  50. $4=$recv(selector)._includes_(":");
  51. if($core.assert($4)){
  52. $5=(
  53. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  54. $ctx1.supercall = true,
  55. //>>excludeEnd("ctx");
  56. $globals.Silk.superclass.fn.prototype._doesNotUnderstand_.apply($recv(self), [aMessage]));
  57. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  58. $ctx1.supercall = false;
  59. //>>excludeEnd("ctx");;
  60. return $5;
  61. };
  62. newElement=$recv(self._class())._newElement_($recv(selector)._asLowercase());
  63. self.__lt_lt(newElement);
  64. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  65. $ctx1.sendIdx["<<"]=1;
  66. //>>excludeEnd("ctx");
  67. $6=useArg;
  68. if($core.assert($6)){
  69. $recv(newElement).__lt_lt($recv($recv(aMessage)._arguments())._first());
  70. };
  71. $7=newElement;
  72. return $7;
  73. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  74. }, function($ctx1) {$ctx1.fill(self,"doesNotUnderstand:",{aMessage:aMessage,selector:selector,newElement:newElement,useArg:useArg},$globals.Silk)});
  75. //>>excludeEnd("ctx");
  76. },
  77. //>>excludeStart("ide", pragmas.excludeIdeData);
  78. args: ["aMessage"],
  79. source: "doesNotUnderstand: aMessage\x0a\x09\x22`aSilk DIV` creates a div element and inserts it.\x0a\x09`aSilk DIV: anObject` creates a div element, inserts it\x0a\x09and puts contents in it\x22\x0a\x09| selector newElement useArg |\x0a\x09selector := aMessage selector.\x0a\x09selector asUppercase = selector\x0a\x09\x09ifFalse: [ ^ super doesNotUnderstand: aMessage ].\x0a\x09selector last = ':'\x0a\x09\x09ifTrue: [ useArg := true. selector := selector allButLast ]\x0a\x09\x09ifFalse: [ useArg := false ].\x0a\x09(selector includes: ':')\x0a\x09\x09ifTrue: [ ^ super doesNotUnderstand: aMessage ].\x0a\x09newElement := self class newElement: selector asLowercase.\x0a\x09self << newElement.\x0a\x09useArg ifTrue: [ newElement << aMessage arguments first ].\x0a\x09^ newElement",
  80. referencedClasses: [],
  81. //>>excludeEnd("ide");
  82. messageSends: ["selector", "ifFalse:", "=", "asUppercase", "doesNotUnderstand:", "ifTrue:ifFalse:", "last", "allButLast", "ifTrue:", "includes:", "newElement:", "class", "asLowercase", "<<", "first", "arguments"]
  83. }),
  84. $globals.Silk);
  85. $core.addMethod(
  86. $core.method({
  87. selector: "nextPut:",
  88. protocol: 'writing',
  89. fn: function (anObject){
  90. var self=this;
  91. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  92. return $core.withContext(function($ctx1) {
  93. //>>excludeEnd("ctx");
  94. var $1,$receiver;
  95. $1=$recv(anObject)._renderOnSilk_(self);
  96. if(($receiver = $1) == null || $receiver.isNil){
  97. (
  98. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  99. $ctx1.supercall = true,
  100. //>>excludeEnd("ctx");
  101. $globals.Silk.superclass.fn.prototype._nextPut_.apply($recv(self), [anObject]));
  102. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  103. $ctx1.supercall = false;
  104. //>>excludeEnd("ctx");;
  105. } else {
  106. $1;
  107. };
  108. return self;
  109. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  110. }, function($ctx1) {$ctx1.fill(self,"nextPut:",{anObject:anObject},$globals.Silk)});
  111. //>>excludeEnd("ctx");
  112. },
  113. //>>excludeStart("ide", pragmas.excludeIdeData);
  114. args: ["anObject"],
  115. 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 ]",
  116. referencedClasses: [],
  117. //>>excludeEnd("ide");
  118. messageSends: ["ifNil:", "renderOnSilk:", "nextPut:"]
  119. }),
  120. $globals.Silk);
  121. $core.addMethod(
  122. $core.method({
  123. selector: "doesNotUnderstand:",
  124. protocol: 'message handling',
  125. fn: function (aMessage){
  126. var self=this;
  127. var selector;
  128. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  129. return $core.withContext(function($ctx1) {
  130. //>>excludeEnd("ctx");
  131. var $1,$2,$3,$4,$5;
  132. selector=$recv(aMessage)._selector();
  133. $1=$recv($recv(selector)._asUppercase()).__eq(selector);
  134. if(!$core.assert($1)){
  135. $2=(
  136. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  137. $ctx1.supercall = true,
  138. //>>excludeEnd("ctx");
  139. $globals.Silk.klass.superclass.fn.prototype._doesNotUnderstand_.apply($recv(self), [aMessage]));
  140. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  141. $ctx1.supercall = false;
  142. //>>excludeEnd("ctx");;
  143. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  144. $ctx1.sendIdx["doesNotUnderstand:"]=1;
  145. //>>excludeEnd("ctx");
  146. return $2;
  147. };
  148. $3=$recv(selector)._includes_(":");
  149. if($core.assert($3)){
  150. $4=(
  151. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  152. $ctx1.supercall = true,
  153. //>>excludeEnd("ctx");
  154. $globals.Silk.klass.superclass.fn.prototype._doesNotUnderstand_.apply($recv(self), [aMessage]));
  155. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  156. $ctx1.supercall = false;
  157. //>>excludeEnd("ctx");;
  158. return $4;
  159. };
  160. $5=self._newElement_($recv(selector)._asLowercase());
  161. return $5;
  162. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  163. }, function($ctx1) {$ctx1.fill(self,"doesNotUnderstand:",{aMessage:aMessage,selector:selector},$globals.Silk.klass)});
  164. //>>excludeEnd("ctx");
  165. },
  166. //>>excludeStart("ide", pragmas.excludeIdeData);
  167. args: ["aMessage"],
  168. 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",
  169. referencedClasses: [],
  170. //>>excludeEnd("ide");
  171. messageSends: ["selector", "ifFalse:", "=", "asUppercase", "doesNotUnderstand:", "ifTrue:", "includes:", "newElement:", "asLowercase"]
  172. }),
  173. $globals.Silk.klass);
  174. $core.addMethod(
  175. $core.method({
  176. selector: "renderOnSilk:",
  177. protocol: '*Silk',
  178. fn: function (aSilk){
  179. var self=this;
  180. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  181. return $core.withContext(function($ctx1) {
  182. //>>excludeEnd("ctx");
  183. $recv(self["@key"])._attrPut_on_(self["@value"],aSilk);
  184. return self;
  185. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  186. }, function($ctx1) {$ctx1.fill(self,"renderOnSilk:",{aSilk:aSilk},$globals.Association)});
  187. //>>excludeEnd("ctx");
  188. },
  189. //>>excludeStart("ide", pragmas.excludeIdeData);
  190. args: ["aSilk"],
  191. source: "renderOnSilk: aSilk\x0a\x09key attrPut: value on: aSilk",
  192. referencedClasses: [],
  193. //>>excludeEnd("ide");
  194. messageSends: ["attrPut:on:"]
  195. }),
  196. $globals.Association);
  197. $core.addMethod(
  198. $core.method({
  199. selector: "renderOnSilk:",
  200. protocol: '*Silk',
  201. fn: function (aSilk){
  202. var self=this;
  203. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  204. return $core.withContext(function($ctx1) {
  205. //>>excludeEnd("ctx");
  206. self._value_(aSilk);
  207. return self;
  208. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  209. }, function($ctx1) {$ctx1.fill(self,"renderOnSilk:",{aSilk:aSilk},$globals.BlockClosure)});
  210. //>>excludeEnd("ctx");
  211. },
  212. //>>excludeStart("ide", pragmas.excludeIdeData);
  213. args: ["aSilk"],
  214. source: "renderOnSilk: aSilk\x0a\x09self value: aSilk",
  215. referencedClasses: [],
  216. //>>excludeEnd("ide");
  217. messageSends: ["value:"]
  218. }),
  219. $globals.BlockClosure);
  220. $core.addMethod(
  221. $core.method({
  222. selector: "asSilk",
  223. protocol: '*Silk',
  224. fn: function (){
  225. var self=this;
  226. function $Silk(){return $globals.Silk||(typeof Silk=="undefined"?nil:Silk)}
  227. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  228. return $core.withContext(function($ctx1) {
  229. //>>excludeEnd("ctx");
  230. var $1;
  231. $1=$recv($Silk())._at_(self._asString());
  232. return $1;
  233. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  234. }, function($ctx1) {$ctx1.fill(self,"asSilk",{},$globals.CharacterArray)});
  235. //>>excludeEnd("ctx");
  236. },
  237. //>>excludeStart("ide", pragmas.excludeIdeData);
  238. args: [],
  239. source: "asSilk\x0a\x09^ Silk at: self asString",
  240. referencedClasses: ["Silk"],
  241. //>>excludeEnd("ide");
  242. messageSends: ["at:", "asString"]
  243. }),
  244. $globals.CharacterArray);
  245. $core.addMethod(
  246. $core.method({
  247. selector: "inSilk",
  248. protocol: '*Silk',
  249. fn: function (){
  250. var self=this;
  251. function $Silk(){return $globals.Silk||(typeof Silk=="undefined"?nil:Silk)}
  252. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  253. return $core.withContext(function($ctx1) {
  254. //>>excludeEnd("ctx");
  255. var $2,$3,$1;
  256. $2=$recv($Silk())._newStream();
  257. $recv($2).__lt_lt(self);
  258. $3=$recv($2)._yourself();
  259. $1=$3;
  260. return $1;
  261. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  262. }, function($ctx1) {$ctx1.fill(self,"inSilk",{},$globals.JSObjectProxy)});
  263. //>>excludeEnd("ctx");
  264. },
  265. //>>excludeStart("ide", pragmas.excludeIdeData);
  266. args: [],
  267. source: "inSilk\x0a\x09^ Silk newStream << self; yourself",
  268. referencedClasses: ["Silk"],
  269. //>>excludeEnd("ide");
  270. messageSends: ["<<", "newStream", "yourself"]
  271. }),
  272. $globals.JSObjectProxy);
  273. $core.addMethod(
  274. $core.method({
  275. selector: "inSilk",
  276. protocol: '*Silk',
  277. fn: function (){
  278. var self=this;
  279. function $Silk(){return $globals.Silk||(typeof Silk=="undefined"?nil:Silk)}
  280. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  281. return $core.withContext(function($ctx1) {
  282. //>>excludeEnd("ctx");
  283. var $2,$3,$1;
  284. $2=$recv($Silk())._newStream();
  285. $recv($2).__lt_lt(self);
  286. $3=$recv($2)._yourself();
  287. $1=$3;
  288. return $1;
  289. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  290. }, function($ctx1) {$ctx1.fill(self,"inSilk",{},$globals.Object)});
  291. //>>excludeEnd("ctx");
  292. },
  293. //>>excludeStart("ide", pragmas.excludeIdeData);
  294. args: [],
  295. source: "inSilk\x0a\x09^ Silk newStream << self; yourself",
  296. referencedClasses: ["Silk"],
  297. //>>excludeEnd("ide");
  298. messageSends: ["<<", "newStream", "yourself"]
  299. }),
  300. $globals.Object);
  301. $core.addMethod(
  302. $core.method({
  303. selector: "renderOnSilk:",
  304. protocol: '*Silk',
  305. fn: function (aSilk){
  306. var self=this;
  307. return nil;
  308. },
  309. //>>excludeStart("ide", pragmas.excludeIdeData);
  310. args: ["aSilk"],
  311. source: "renderOnSilk: aSilk\x0a\x09^ nil",
  312. referencedClasses: [],
  313. //>>excludeEnd("ide");
  314. messageSends: []
  315. }),
  316. $globals.Object);
  317. $core.addMethod(
  318. $core.method({
  319. selector: "attrPut:on:",
  320. protocol: '*Silk',
  321. fn: function (anObject,aSilk){
  322. var self=this;
  323. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  324. return $core.withContext(function($ctx1) {
  325. //>>excludeEnd("ctx");
  326. $recv(aSilk)._attrAt_put_(self,anObject);
  327. return self;
  328. //>>excludeStart("ctx", pragmas.excludeDebugContexts);
  329. }, function($ctx1) {$ctx1.fill(self,"attrPut:on:",{anObject:anObject,aSilk:aSilk},$globals.String)});
  330. //>>excludeEnd("ctx");
  331. },
  332. //>>excludeStart("ide", pragmas.excludeIdeData);
  333. args: ["anObject", "aSilk"],
  334. source: "attrPut: anObject on: aSilk\x0a\x09aSilk attrAt: self put: anObject",
  335. referencedClasses: [],
  336. //>>excludeEnd("ide");
  337. messageSends: ["attrAt:put:"]
  338. }),
  339. $globals.String);
  340. });