documentation.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. ---
  2. layout: default
  3. title: Amber Smalltalk - documentation
  4. ---
  5. <div class="box last doc" id="documentation">
  6. <h1>Documentation</h1>
  7. <div class="content">
  8. <h2>Disclaimer</h2>
  9. <p class="warning">This documentation is a work in progress. If you find an error in the documentation, please <a href="https://github.com/NicolasPetton/amber/issues">file an issue</a>.</p>
  10. <p>This documentation <strong>doesn't aim to teach Smalltalk</strong>. Knowledge of Smalltalk is needed to understand the topics covered in this documentation. If you want to learn the Smalltalk language, you can read the excellent <a target="_blank" href="http://pharobyexample.org">Pharo By Example</a> book.</p>
  11. <h2>Introduction</h2>
  12. <p class="information">Amber is a young piece of code and evolves quickly. Some features are still incomplete and you may very well encounter bugs, in which case you can <a href="https://github.com/NicolasPetton/amber/issues">file an issue</a> or a pull request on the <a href="https://github.com/NicolasPetton/amber">repository</a>.</p>
  13. <p>Amber is an implementation of the Smalltalk-80 language. 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.</p>
  14. <p>Amber includes the following features:</p>
  15. <ol>
  16. <li>It is semantically and syntactically equivalent to <a href="http://www.pharo-project.org">Pharo Smalltalk</a> (the implementation considered as the reference)</li>
  17. <li>It is written in itself and compiles into efficient JavaScript</li>
  18. <li>A canvas API similar to <a href="http://www.seaside.st">Seaside</a> to generate HTML</li>
  19. <li>A <a href="http://www.jquery.com">jQuery</a> binding</li>
  20. </ol>
  21. <h2>Differences with other Smalltalk implementations</h2>
  22. Amber has some differences with other Smalltalk implementations. 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. The following list explains the main differences:
  23. <ul>
  24. <li>There is no identity in Amber. Especially, there is no <code>==</code> method, or <code>Symbol</code> class. For convenience and compatibility, the Amber parser will recognize symbol literals as strings.</li>
  25. <li>The collection class hierarchy is simpler compared to most Smalltalk implementations. There is no <code>OrderedCollection</code>, <code>Set</code> or <code>SortedCollection</code>. However, the size of arrays is dynamic, and they behave like an ordered collection. They can also be sorted with the <code>#sort*</code> methods.</li>
  26. <li>The <code>Date</code> class behaves like the <code>Date</code> <em>and</em> <code>TimeStamp</code> classes in Pharo Smalltalk. Therefore both <code>Date today</code> and <code>Date now</code> are valid in Amber.</li>
  27. <li>Amber use solely <code>=</code> to test object equality and not the <code>#hash</code> method.</li>
  28. <li><strike>Amber misses <code>thisContext</code></strike>. Amber now supports <code> thisContext</code></li>
  29. <li><strike>Amber does not support <code>#doesNotUnderstand</code></strike>. <em>Amber now <a href="http://www.nicolas-petton.fr/2011/05/03/doesnnotunderstand-in-jtalk.html">has support</a> for</em> <code>#dnu:</code>.</li>
  30. </ul>
  31. <h2>Committing changes to disk with the web-based IDE</h2>
  32. <p>The class browser is able to commit changes to disk. The <code>commit category</code> button will send a PUT request with the compiled JavaScript code of all classes in the selected class category in a file named <code>js/CATEGORY.js</code>.</p>
  33. <p>The easiest way to enable committing is probably to setup a webdav with Apache.</p>
  34. <p class="information">The following steps explain how to setup a webdav for Amber with Debian, but the setup on OSX and other Linux distros should be similar.</p>
  35. <h4>Installing Apache and enabling the dav module</h4>
  36. <p>Evaluate the following as root:</p>
  37. <pre><span class="prompt">~#</span> <span class="kbd">apt-get install</span> <span class="kbd var">apache2</span>
  38. <span class="prompt">~#</span> <span class="kbd">a2enmod</span> <span class="kbd var">dav</span>
  39. <span class="prompt">~#</span> <span class="kbd">a2enmod</span> <span class="kbd var">dav_fs</span></pre>
  40. <h4>Creating a password for the webdav</h4>
  41. <pre><span class="prompt">~#</span> <span class="kbd">htpasswd -c /etc/apache2/htpasswd-webdav <span class="kbd var">USERNAME</span></span></pre>
  42. <h4>Setting up the webdav directory</h4>
  43. <p>Add the following lines to the default vhost (in <code>/etc/apache2/sites-available/default</code>):</p>
  44. <pre>Alias /amber/ "/path/to/amber/"
  45. &lt;Directory "/path/to/amber/"&gt;
  46. Options Indexes MultiViews FollowSymLinks
  47. DirectoryIndex index.html
  48. AllowOverride None
  49. Order allow,deny
  50. allow from all
  51. Dav on
  52. AuthType Basic
  53. AuthName "amber"
  54. AuthUserFile /etc/apache2/htpasswd-webdav
  55. &lt;LimitExcept GET OPTIONS&gt;
  56. Require valid-user
  57. &lt;/LimitExcept&gt;
  58. &lt;/Directory&gt;</pre>
  59. <p>Make sure the group <code>www-data</code> has required rights to make changes to files in the webdav directory.</p>
  60. <h4>Restarting Apache</h4>
  61. <p>To restart Apache, evaluate the following: </p>
  62. <pre><span class="prompt">~#</span> <span class="kbd">/etc/init.d/apache2</span> <span class="kbd var">restart</span></pre>
  63. <p>and go to <code>http://localhost/amber/</code>.</p>
  64. <p>The class browser should now be able to commit changes to disk.</p>
  65. <h2>The counter example</h2>
  66. <p>The following example is the traditional Seaside-like multi-counter application. The buttons at the bottom of each counter increase or decrease the counter.</p>
  67. <div id="counters"></div>
  68. <script type="text/javascript">
  69. jQuery(document).ready(function() {'#counters'._asJQuery()._append_(smalltalk.Counter._new())._append_(smalltalk.Counter._new())});
  70. </script>
  71. <p>Open a <button onclick="smalltalk.Browser._openOn_(smalltalk.Counter);">browser</button> on the <code>Counter</code> class in the <code>Canvas</code> class category.
  72. <p>Each Amber widget is a subclass of <code>Widget</code>. A widget is a graphical component. The <code>#renderOn:</code> method is used to generate HTML usinng the HTML canvas.</p>
  73. <p class="information">Rendering methods should be placed in the <code>rendering</code> method protocol, and action methods in the <code>actions</code> protocol.</p>
  74. {% highlight smalltalk %}renderOn: html
  75. html h1 with: count asString.
  76. html button
  77. with: '++';
  78. onClick: [self increase].
  79. html button
  80. with: '--';
  81. onClick: [self decrease] {% endhighlight %}
  82. <h2>The HTML canvas</h2>
  83. <p>Amber allows developers to write HTML with a Canvas API similar to <a href='http://www.seaside.st'>Seaside</a>. The explanations below won't be really interesting to seasiders, there are however a few differences with the API Seaside provides.</p>
  84. <p>Each HTML tag is represented by an instance of <code>TagBrush</code>, used to paint the tag on a <code>HTMLCanvas</code>. The <code>HTMLCanvas&gt;&gt;tag:</code> method adds a tag brush to the canvas object. For convenience, the <code>tags</code> method protocol includes methods for easily adding tag brushes named after each selector name:</p>
  85. {% highlight smalltalk %}| html |
  86. html := HTMLCanvas new.
  87. html p
  88. with: 'This is a paragraph with a link to ';
  89. with: [
  90. html a
  91. href: 'http://www.google.fr';
  92. with: 'Google']
  93. {% endhighlight%}
  94. <p>You can <button onclick='smalltalk.Browser._openOn_(smalltalk.HTMLCanvas)'>browse</button> the <code>HTMLCanvas</code> class to get the list of all tag methods.</p>
  95. <p>The <code>with:</code> method will call the polymorphic <code>appendToBrush:</code> method on the argument and allows you to add blocks, strings, tags, etc. to an existing tag brush or canvas.</p>
  96. <p><code>TagBrush</code> also has methods to bind events, like <code>#onClick:</code> or <code>#onChange:</code>, in the <code>events</code> protocol.</p>
  97. <h2>Widgets</h2>
  98. <h2>jQuery</h2>
  99. <p>Amber comes with a <a href='http://www.jquery.com'>jQuery</a> binding. Each string or tag brush can be converted to a jQuery object, instance of the <code>JQuery</code> class in Amber, with <code>#asJQuery</code>.</p>
  100. {% highlight smalltalk %}'body' asJQuery.
  101. aTagBrush asJQuery.{% endhighlight %}
  102. <p>Once you get the jQuery object, you can use jQuery from Amber like you would do in JavaScript. JQuery methods in Amber follow the well documented <a href='http://api.jquery.com/'>jQuery API</a>.</p>
  103. {% highlight smalltalk %}'body' asJQuery
  104. addClass: 'foo';
  105. append: 'Hello world';
  106. aTagBrush asJQuery hide.{% endhighlight %}
  107. <p>Once again, If you're looking for some particular method or want to learn more about how to use jQuery from Amber, you can <button onclick='smalltalk.Browser._openOn_(smalltalk.JQuery)'>browse</button> the <code>JQuery</code> class.</p>
  108. </div>
  109. </div>
  110. <!-- Add anchors to doc titles and build the dropdown menu -->
  111. <script type="text/javascript">
  112. jQuery('#menu .main').append("<ul id='dropdown'></ul>");
  113. jQuery.each(jQuery('.doc h2'), function(i, heading) {
  114. jQuery(heading).attr({'id': jQuery(heading).text().replace(/ /g,"_")});
  115. jQuery(heading).html(i + ". " + jQuery(heading).html());
  116. jQuery('#dropdown').append("<li><a href='#" + jQuery(heading).attr('id') + "'>" + jQuery(heading).text() + "</a></li>");
  117. jQuery(heading).html(jQuery(heading).html() + " <a href='#" + jQuery(heading).attr('id') + "'>&para;</a>");
  118. });
  119. jQuery('#doc_link')
  120. .bind('mouseenter', function() {jQuery('#dropdown').show()})
  121. .bind('mouseleave', function() {jQuery('#dropdown').hide()});
  122. jQuery('#dropdown')
  123. .bind('mouseenter', function() {jQuery('#dropdown').show()})
  124. .bind('mouseleave', function() {jQuery('#dropdown').hide()})
  125. .bind('click', function() {jQuery('#dropdown').hide()});
  126. </script>