Documentation.st 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. Smalltalk current createPackage: 'Documentation' properties: #{}!
  2. Object subclass: #DocumentationBuilder
  3. instanceVariableNames: 'chapters announcer widget'
  4. category: 'Documentation'!
  5. !DocumentationBuilder methodsFor: 'accessing'!
  6. chapters
  7. ^chapters ifNil: [chapters := self buildChapters]
  8. !
  9. announcer
  10. ^announcer ifNil: [announcer := Announcer new]
  11. !
  12. widget
  13. ^widget ifNil: [widget := DocumentationWidget on: self]
  14. ! !
  15. !DocumentationBuilder methodsFor: 'building'!
  16. buildChapters
  17. ^((self class methodDictionary values sorted: [:a :b | a selector < b selector])
  18. select: [:each | each category = 'chapters'])
  19. collect: [:each | self perform: each selector]
  20. !
  21. buildOn: aCanvas
  22. aCanvas with: self widget.
  23. self
  24. checkHashChange;
  25. checkHash
  26. !
  27. buildOnJQuery: aJQuery
  28. self buildOn: (HTMLCanvas onJQuery: aJQuery)
  29. ! !
  30. !DocumentationBuilder methodsFor: 'chapters'!
  31. ch1introduction
  32. ^DocChapter new
  33. title: 'Introduction';
  34. contents: '
  35. ##Amber Smalltalk in a nutshell.
  36. Amber is an implementation of the Smalltalk-80 language.
  37. It allows developers to write client-side heavy web applications in Smalltalk. Amber includes an integrated development environment with a class browser, workspace and transcript.
  38. Amber includes the following features:
  39. - It is semantically and syntactically equivalent to Pharo Smalltalk (the implementation considered as the reference)
  40. - It is written in itself and compiles into efficient JavaScript
  41. - A canvas API similar to Seaside to generate HTML
  42. - A jQuery binding.
  43. ## Disclaimer
  44. This documentation doesn''t aim to teach Smalltalk.
  45. Knowledge of Smalltalk is needed to understand the topics covered in this documentation.
  46. If you want to learn the Smalltalk language, you can read the excellent [Pharo By Example](http://www.pharobyexample.org) book.
  47. '
  48. !
  49. ch2differencesWithOtherSmalltalks
  50. ^DocChapter new
  51. title: 'Differences with other Smalltalks';
  52. contents: '
  53. Amber has some differences with other Smalltalk implementations.
  54. Because it maps Smalltalk constructs one-to-one with the JavaScript equivalent, including Smalltalk classes to JavaScript constructors, the core class library is simplified compared to Pharo Smalltalk.
  55. The following list explains the main differences:
  56. - The collection class hierarchy is simpler compared to most Smalltalk implementations. As of today, there is no SortedCollection. The size of arrays is dynamic, and they behave like an ordered collection.
  57. They can also be sorted with the #sort* methods.
  58. - The Date class behaves like the Date and TimeStamp classes in Pharo Smalltalk. Therefore both Date today and Date now are valid in Amber.
  59. '
  60. !
  61. ch3GettingStarted
  62. ^DocChapter new
  63. title: 'Getting started';
  64. contents: '
  65. To get started hacking in Amber you can basically take three routes, independent of your platform:
  66. 1. Just try it out at http://www.amber-lang.net (click the "Class browser" button) - but you will **not be able to save any code you write**!!
  67. Still, it works fine for looking at the IDE and playing around. Just don''t press F5/reload - it will bring you back to zero.
  68. (Well, if you still want to develop and save code online someone has set up this site seems for free use: https://www.screwtopdb.com/amberstore/topics?name=amberstore/amber.html )
  69. 2. Download an Amber zip-ball, install Nodejs, fire up the Amber server and then open Amber from localhost - then you **can save code**. Detailed instructions are below!!
  70. 3. Same as above but install git first and get a proper clone from http://github.com/NicolasPetton/amber instead of a zip/tar-ball.
  71. If you want to **contribute to Amber itself** this is really what you want to do. It requires installing git first, but it is quite simple - although we leave this bit as an "exercise to the reader" :)
  72. **PLEASE NOTE:** Amber core developers use Linux.
  73. We do not want to introduce dependencies that aren''t cross platform - but currently amberc (the command line compiler) is a bash script and we also use Makefiles
  74. (for building Amber itself and server side examples) written on Linux/Unix. So using Windows is currently a bit limited - you can''t run "make" in the .st directory to rebuild whole of Amber for example.
  75. BUT... if you only want to use Amber to build web client apps and not really get involved in hacking Amber itself - then you should be fine!!
  76. ## Downloading Amber
  77. Currently you can download in zip or tar-ball format, either cutting edge or a release. [Downloads are available here](https://github.com/NicolasPetton/amber/archives/amber).
  78. At the moment of writing you have release [0.9 as zip](https://github.com/NicolasPetton/amber/zipball/0.9) or [tar](https://github.com/NicolasPetton/amber/tarball/0.9),
  79. or [cutting edge as zip](https://github.com/NicolasPetton/amber/zipball/amber) or [tar](https://github.com/NicolasPetton/amber/tarball/amber).
  80. At the moment this is just a **1.5Mb download**, so its very small. Unpack wherever you like, but I would rename the directory that is unpacked to something slightly shorter - like say "amber-0.9" or just "amber".
  81. And yes, at this point you can double click the index.html file in the amber directory to get the IDE up, but again, **you will not be able to save code**. So please continue below :)
  82. ## Installing Node.js
  83. [Node](http://www.nodejs.org) (for short) is simply the V8 Javascript VM from Google (used in Chrome) hooked together with some hard core C-libraries for doing "evented I/O".
  84. Basically it''s Javascript for the server - on asynch steroids. Amber runs fine in Node and we use it for several Amber tools, like amberc (the command line Amber compiler) or the Amber server (see below).
  85. There are also several Amber-Node example to look at if you want to play with running Amber programs server side. **In short - you really want to install Nodejs. :)**
  86. - Installing Node on Linux can be done using your package tool of choice ("apt-get install nodejs" for example) or any other way described at [the download page](http://nodejs.org/#download).
  87. - Installing Node on MacOS seems to be easiest by getting it from [here](https://sites.google.com/site/nodejsmacosx/).
  88. - Installing Node on Windows is probably done best by using the [download from Nodejs.org](http://nodejs.org/#download). This is not an installer, it is instead simply the node executable - **node.exe**.
  89. - Put node.exe somewhere in your path. In Windows7 I can run a command prompt "As administrator" (right click the command prompt in Start menu) and then just "copy node.exe c:\windows\" or such.
  90. ## Starting Amber server
  91. Nicolas has written a minimal webDAV server that is the easiest way to get up and running Amber with the ability to save code. This little server is written in... Amber!!
  92. And it runs on top of Node. So to start it up serving your brand new directory tree of sweet Amber you do:
  93. cd amber (or whatever you called the directory you unpackaged)
  94. ./bin/server (in windows you type "node server\server.js" instead)
  95. It should say it is listening on port 4000. If it does, hooray!! That means both Node and Amber are good. In Windows you might get a question about opening that port in the local firewall - yep, do it!!
  96. ## Firing up Amber
  97. The Amber IDE is written in... Amber. It uses JQuery and runs right in your browser as a ... well, a web page.
  98. We could open it up just using a file url - but the reason we performed the previous steps is so that we can load the IDE web page from a server that can handle PUTs (webDAV) of source code.
  99. According to web security Amber can only do PUT back to the same server it was loaded from. Thus we instead want to open it through our little server now listening on port 4000:
  100. http://localhost:4000/index.html
  101. Clicking the above link should get your Amber running.
  102. To verify that you can indeed commit - just select a Package in the browser, like say "Examples" and press "Commit package" button. **If all goes well nothing happens :)**.
  103. So in order to really know if it worked we can check the modified date on the files **amber/st/Examples.st**, **amber/js/Examples.js** and **amber/js/Examples.deploy.js** - they should be brand new.
  104. NOTE: We can use any webDAV server and Apache2 has been used earlier and works fine. But the Amber server is smaller and simpler to start.
  105. '
  106. !
  107. ch4FirstApp
  108. ^DocChapter new
  109. title: 'A first application';
  110. contents: '
  111. Let''s make Hello World in Amber.
  112. First, you need a place for your new project. I made a new directory under amber:
  113. amber/projects/hello
  114. This will store your project files. To get started, add a new index.html file to this folder, as well as empty js and st folders.
  115. Your index.html can be really basic. The most important thing it does is include amber.js and run loadAmber. Here is a basic index.html you can use:
  116. <!!DOCTYPE html>
  117. <html>
  118. <head>
  119. <title>My First Amber Project</title>
  120. <script src="../../js/amber.js" type="text/javascript"></script>
  121. <script type="text/javascript">
  122. loadAmber({
  123. files: [],
  124. prefix: ''projects/hello/js'',
  125. ready: function() {
  126. }});
  127. </script>
  128. </head>
  129. <body>
  130. <article>
  131. <h1>My First Amber Project</h1>
  132. <button onclick="smalltalk.Browser._open()">class browser</button>
  133. <button id="sayHello">say hello</button>
  134. </article>
  135. </body>
  136. </html>
  137. Now start up amber with node.js and navigate to http://localhost:4000/projects/hello/index.html
  138. It''s boring so far, so lets write some code. Click the button to open the class browser. Find an existing class and change its name to Hello and its package to HelloApp.
  139. Then click save. This creates a new class and leaves the old one intact, it doesn''t overwrite it. Your class will look like this:
  140. Object subclass: #Hello
  141. instanceVariableNames: ''''
  142. package: ''HelloApp''
  143. Now click save and navigate to your new class in its new package.
  144. Then click ''commit package''. You just created a new class and saved your work.
  145. On your file system check out your js and st folders. Your new class is now saved in both JavaScript and Smalltalk.
  146. Now, refresh your browser page and reopen the class browser. Oh no, your new class is gone!! To load your new class automatically, you have to add it in index.html. Make your JavaScript look like this:
  147. loadAmber({
  148. files: [''HelloApp.js''],
  149. prefix: ''projects/hello/js'',
  150. ready: function() {
  151. }});
  152. Save and refresh again. Now your class is loaded and shows up in the class browser.
  153. Now, let''s make this class do something. Create a new message in the class browser by navigating to your class, then clicking ''not yet classified'' and fill in a simple message. Try this for example:
  154. begin
  155. "Makes me say hello to the user."
  156. | msg button |
  157. msg := ''Hello world!!''.
  158. button := ''#sayHello'' asJQuery.
  159. button click: [button after: ''<p>'' , msg , ''</p>''].
  160. Your message isn''t too helpful if it doesn''t get called. Save it, commit the package, then edit index.html again. You can write JavaScript code that sends a message to Smalltalk:
  161. loadAmber({
  162. files: [''HelloApp.js''],
  163. prefix: ''projects/hello/js'', // path for js files i think
  164. ready: function() {
  165. $(function() {
  166. smalltalk.Hello._new()._begin();
  167. });
  168. }});
  169. From there, you can create new Smalltalk classes and messages to build up your app. Enjoy!!
  170. '
  171. !
  172. ch5Index
  173. ^ClassesIndexChapter new
  174. !
  175. ch6KernelObjects
  176. ^(PackageDocChapter on: (Package named: 'Kernel-Objects'))
  177. title: 'Kernel-Objects';
  178. yourself
  179. ! !
  180. !DocumentationBuilder methodsFor: 'routing'!
  181. checkHashChange
  182. (window jQuery: window) bind: 'hashchange' do: [self checkHash]
  183. !
  184. checkHash
  185. | hash presentation |
  186. hash := document location hash replace: '^#' with: ''.
  187. self announcer announce: (ChapterSelectionAnnouncement new
  188. id: hash;
  189. yourself)
  190. ! !
  191. DocumentationBuilder class instanceVariableNames: 'current'!
  192. !DocumentationBuilder class methodsFor: 'accessing'!
  193. current
  194. ^current ifNil: [current := self new]
  195. ! !
  196. !DocumentationBuilder class methodsFor: 'initialization'!
  197. initialize
  198. self current buildOnJQuery: (window jQuery: 'body')
  199. ! !
  200. Widget subclass: #DocChapter
  201. instanceVariableNames: 'title contents parent'
  202. category: 'Documentation'!
  203. !DocChapter methodsFor: 'accessing'!
  204. title
  205. ^title ifNil: ['']
  206. !
  207. title: aString
  208. title := aString
  209. !
  210. contents
  211. ^contents ifNil: ['']
  212. !
  213. contents: aString
  214. contents := aString
  215. !
  216. htmlContents
  217. ^(Showdown at: #converter) new makeHtml: self contents
  218. !
  219. chapters
  220. "A doc chapter can contain sub chapters"
  221. ^#()
  222. !
  223. cssClass
  224. ^'doc_chapter'
  225. !
  226. level
  227. ^self parent ifNil: [1] ifNotNil: [self parent level +1]
  228. !
  229. level: anInteger
  230. level := anInteger
  231. !
  232. parent
  233. ^parent
  234. !
  235. parent: aChapter
  236. parent := aChapter
  237. !
  238. id
  239. "The id is used in url fragments.
  240. It must be unique amoung all chapters"
  241. ^self title replace: ' ' with: '-'
  242. !
  243. announcer
  244. ^DocumentationBuilder current announcer
  245. ! !
  246. !DocChapter methodsFor: 'actions'!
  247. selectClass: aClass
  248. DocumentationBuilder current announcer announce: (ClassSelectionAnnouncement on: aClass)
  249. !
  250. selectChapter: aChapter
  251. document location hash: aChapter id
  252. !
  253. displayChapter: aChapter
  254. DocumentationBuilder current widget displayChapter: aChapter
  255. ! !
  256. !DocChapter methodsFor: 'initialization'!
  257. initialize
  258. super initialize.
  259. self subscribe
  260. ! !
  261. !DocChapter methodsFor: 'rendering'!
  262. renderOn: html
  263. html div
  264. class: self cssClass;
  265. with: [
  266. self renderDocOn: html.
  267. self renderLinksOn: html]
  268. !
  269. renderDocOn: html
  270. | div |
  271. html h1 with: self title.
  272. self renderNavigationOn: html.
  273. div := html div class: 'contents'.
  274. div asJQuery html: self htmlContents
  275. !
  276. renderNavigationOn: html
  277. self parent ifNotNil: [
  278. html div
  279. class: 'navigation'; with: [
  280. html a
  281. with: '← back to ', self parent title;
  282. onClick: [self selectChapter: self parent]]]
  283. !
  284. renderLinksOn: html
  285. html ul
  286. class: 'links';
  287. with: [
  288. self chapters do: [:each |
  289. html li with: [
  290. html a
  291. with: each title;
  292. onClick: [self selectChapter: each]]]]
  293. ! !
  294. !DocChapter methodsFor: 'subscriptions'!
  295. subscribe
  296. self announcer on: ChapterSelectionAnnouncement do: [:ann |
  297. ann id = self id ifTrue: [self displayChapter: self]]
  298. ! !
  299. DocChapter subclass: #PackageDocChapter
  300. instanceVariableNames: 'package chapters'
  301. category: 'Documentation'!
  302. !PackageDocChapter methodsFor: 'accessing'!
  303. package
  304. ^package
  305. !
  306. title
  307. ^'Package ', self package name
  308. !
  309. chapters
  310. ^chapters
  311. !
  312. contents
  313. ^'Classes in package ', self package name, ':'
  314. ! !
  315. !PackageDocChapter methodsFor: 'initialization'!
  316. initializeWithPackage: aPackage
  317. package := aPackage.
  318. chapters := (aPackage classes sorted: [:a :b | a name < b name]) collect: [:each |
  319. (ClassDocChapter on: each)
  320. parent: self;
  321. yourself]
  322. ! !
  323. !PackageDocChapter class methodsFor: 'instance creation'!
  324. on: aPackage
  325. ^self basicNew
  326. initializeWithPackage: aPackage;
  327. initialize;
  328. yourself
  329. ! !
  330. DocChapter subclass: #ClassDocChapter
  331. instanceVariableNames: 'theClass'
  332. category: 'Documentation'!
  333. !ClassDocChapter methodsFor: 'accessing'!
  334. theClass
  335. ^theClass
  336. !
  337. contents
  338. ^self theClass comment isEmpty
  339. ifTrue: [self theClass name, ' is not documented yet.']
  340. ifFalse: [self theClass comment]
  341. !
  342. cssClass
  343. ^'doc_class ', super cssClass
  344. !
  345. title
  346. ^self theClass name
  347. !
  348. initializeWithClass: aClass
  349. theClass := aClass
  350. ! !
  351. !ClassDocChapter methodsFor: 'rendering'!
  352. renderLinksOn: html
  353. html ul
  354. class: 'links';
  355. with: [
  356. html li with: [html a
  357. with: 'Browse this class';
  358. onClick: [Browser openOn: self theClass]]]
  359. ! !
  360. !ClassDocChapter methodsFor: 'subscriptions'!
  361. subscribe
  362. super subscribe.
  363. self announcer
  364. on: ClassSelectionAnnouncement do: [:ann |
  365. ann theClass = self theClass ifTrue: [
  366. self selectChapter: self]]
  367. ! !
  368. !ClassDocChapter class methodsFor: 'accessing'!
  369. on: aClass
  370. ^self basicNew
  371. initializeWithClass: aClass;
  372. initialize;
  373. yourself
  374. ! !
  375. Widget subclass: #DocumentationWidget
  376. instanceVariableNames: 'builder selectedChapter chapterDiv'
  377. category: 'Documentation'!
  378. !DocumentationWidget methodsFor: 'accessing'!
  379. builder
  380. ^builder
  381. !
  382. builder: aDocumentationBuilder
  383. builder := aDocumentationBuilder
  384. !
  385. chapters
  386. ^self builder chapters
  387. !
  388. selectedChapter
  389. ^selectedChapter ifNil: [selectedChapter := self chapters first]
  390. !
  391. selectedChapter: aChapter
  392. ^selectedChapter := aChapter
  393. ! !
  394. !DocumentationWidget methodsFor: 'actions'!
  395. displayChapter: aChapter
  396. self selectedChapter: aChapter.
  397. self updateChapterDiv
  398. !
  399. selectChapter: aChapter
  400. document location hash: aChapter id
  401. ! !
  402. !DocumentationWidget methodsFor: 'rendering'!
  403. renderOn: html
  404. html div
  405. class: 'documentation';
  406. with: [
  407. self renderMenuOn: html.
  408. chapterDiv := html div.
  409. self updateChapterDiv]
  410. !
  411. renderMenuOn: html
  412. html div
  413. class: 'menu';
  414. with: [
  415. html ol with: [
  416. self chapters do: [:each |
  417. html li with: [
  418. self renderChapterMenu: each on: html]]]]
  419. !
  420. renderChapterMenu: aChapter on: html
  421. html a
  422. with: aChapter title;
  423. onClick: [
  424. self selectChapter: aChapter].
  425. html ol with: [
  426. aChapter chapters do: [:each |
  427. html li with: [
  428. self renderChapterMenu: each on: html]]]
  429. ! !
  430. !DocumentationWidget methodsFor: 'updating'!
  431. updateChapterDiv
  432. chapterDiv contents: [:html |
  433. html with: self selectedChapter]
  434. ! !
  435. !DocumentationWidget class methodsFor: 'instance creation'!
  436. on: aBuilder
  437. ^self new
  438. builder: aBuilder;
  439. yourself
  440. ! !
  441. DocChapter subclass: #ClassesIndexChapter
  442. instanceVariableNames: ''
  443. category: 'Documentation'!
  444. !ClassesIndexChapter methodsFor: 'accessing'!
  445. cssClass
  446. ^'index_doc ', super cssClass
  447. !
  448. title
  449. ^'Smalltalk classes by index'
  450. !
  451. alphabet
  452. ^'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  453. ! !
  454. !ClassesIndexChapter methodsFor: 'rendering'!
  455. renderDocOn: html
  456. html h1 with: self title.
  457. self alphabet do: [:letter || classes |
  458. classes := Smalltalk current classes select: [:each | each name first = letter].
  459. classes ifNotEmpty: [html h2 with: letter].
  460. html ul with: [
  461. (classes sorted: [:a :b | a name < b name])
  462. do: [:each |
  463. html li with: [html a
  464. with: each name;
  465. onClick: [self selectClass: each]]]]]
  466. ! !
  467. Object subclass: #ClassSelectionAnnouncement
  468. instanceVariableNames: 'theClass'
  469. category: 'Documentation'!
  470. !ClassSelectionAnnouncement methodsFor: 'accessing'!
  471. theClass
  472. ^theClass
  473. !
  474. theClass: aClass
  475. theClass := aClass
  476. ! !
  477. !ClassSelectionAnnouncement class methodsFor: 'instance creation'!
  478. on: aClass
  479. ^self new
  480. theClass: aClass;
  481. yourself
  482. ! !
  483. Object subclass: #ChapterSelectionAnnouncement
  484. instanceVariableNames: 'id'
  485. category: 'Documentation'!
  486. !ChapterSelectionAnnouncement methodsFor: 'accessing'!
  487. id
  488. ^id
  489. !
  490. id: aString
  491. id := aString
  492. ! !