UNPKG

toloframework

Version:

Javascript/HTML/CSS compiler for Firefox OS or nodewebkit apps using modules in the nodejs style.

43 lines 5.77 kB
<!DOCTYPE html><html><head><link href="https://fonts.googleapis.com/css?family=Josefin+Sans" rel="stylesheet" type="text/css"></link><title>XJS user manual 666</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"><meta name="description" content="Javascript/HTML/CSS compiler for Firefox OS or nodewebkit apps using modules in the nodejs style."></meta><script defer="null" src="js/@xjs.js"></script><link rel="stylesheet" type="text/css" href="css/@xjs.css"></head><body class="theme-webgl theme-color-bg-B5"><section class="theme-color-bg-B0 theme-elevation-4"><pre><code><h1 id="xjs-against-all-the-boilerplate-">XJS against all the boilerplate!</h1> </code></pre><p><p>The Javascript for a module is usually stored in a <code>module.js</code> file. But because writing some sort of code can be fastidious, toloframework can use a <code>module.xjs</code> file to generate Javascript before minification. XJS files are written in a syntax near the JSON one: it is just less restrictive on how you use strings and it allows you to put Javascript comments.</p> <p>XJS is extensible and you can easily add your own code generators. For now, we will focus on the builtin generator: <strong>View</strong>.</p> <h2 id="xjs-view">XJS View</h2> <p>Writing good code for visual components with data binding can result in lot of unreadable javascript code. View helps you writing such a code in a declarative way. Let&#39;s take an example:</p> <p><div id="tfw.view.checkbox0" class="" style="display:none"></div></p> <p>To create this checkbox component, all you need is a <code>jsx</code> file and a <code>css</code> file. And you can click on it: it really works! And when it has the focus, you can use the keyboard to toggle its state. And all this without a single line of javascript code.</p> <p><pre class="custom highlight xjs"><span class="comment">// tfw.view.checkbox</span> <span class="symbol">{</span>View BUTTON <span class="keyword">view.attribs:</span> <span class="symbol">{</span> <span class="function">value:</span> false, <span class="function">reversed:</span> false, <span class="function">content:</span> Checkbox <span class="symbol">}</span> <span class="function">class:</span>tfw-view-checkbox <span class="keyword">class.ok:</span><span class="symbol">{</span>Attrib value<span class="symbol">}</span> <span class="keyword">event.tap:</span><span class="symbol">{</span>Toggle value<span class="symbol">}</span> <span class="keyword">event.keyup:</span><span class="symbol">{</span>Toggle value<span class="symbol">}</span> <span class="symbol">[</span> <span class="symbol">{</span>DIV <span class="function">class:</span>pin <span class="symbol">[</span> <span class="symbol">{</span>DIV <span class="function">class:</span><span class="string">"bar thm-ele2"</span> <span class="keyword">class.thm-bgSL|thm-bg2:</span> <span class="symbol">{</span>Attrib value<span class="symbol">}</span> <span class="symbol">}</span> <span class="symbol">{</span>DIV <span class="function">class:</span><span class="string">"btn thm-ele2"</span> <span class="keyword">class.thm-bgS|thm-bg1:</span> <span class="symbol">{</span>Attrib value<span class="symbol">}</span> <span class="symbol">}</span> <span class="symbol">]}</span> <span class="symbol">{</span>DIV <span class="function">class:</span>txt <span class="symbol">{</span>Attrib content<span class="symbol">}}</span> <span class="symbol">]</span> <span class="symbol">}</span> </pre> To understand this syntax you must know that permissive JSON uses implicit attribute names. So stuff like <code>{Attrib value}</code> can be translated in pure JSON as <code>{&quot;0&quot;: &quot;Attrib&quot;, &quot;1&quot;: &quot;value&quot;}</code>.</p> <p>Basically, XJS takes an object and generate javascript code. To know which code generator to use, it looks at the attribute &quot;0&quot; of the object. In our case, this is <strong>View</strong>.</p> <p>A view is an HTML element which can contain other HTML elements or views. When the first implicit attribute is uppercase, this is an HTML element, otherwise this is a View. As you can see here, this view is a BUTTON. The module <code>tfw.view.checkbox</code> will be a class which you can instantiate and use like this: <pre class="custom highlight js"><span class="keyword">var</span> <span class="identifier">Checkbox</span> <span class="operator">=</span> <span class="keyword2">require</span><span class="symbol">(</span><span class="string">"tfw.view.checkbox"</span><span class="symbol">);</span> <span class="keyword">var</span> <span class="identifier">chk</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="function">Checkbox</span><span class="symbol">({</span> <span class="identifier">content</span><span class="symbol">:</span> <span class="string">"Hello world!"</span> <span class="symbol">});</span> <span class="identifier">document</span>.<span class="identifier">body</span>.<span class="function">appendChild</span><span class="symbol">(</span> <span class="identifier">chk</span>.<span class="identifier">$</span> <span class="symbol">);</span> </pre></p> <p>All views have the <code>$</code> attribute which maps to the root HTML element. Other attributes can be defined with their default values in the XJS object: <pre class="custom highlight xjs"><span class="keyword">view.attribs:</span> <span class="symbol">{</span> <span class="function">value:</span> false, <span class="function">reversed:</span> false, <span class="function">content:</span> Checkbox <span class="symbol">}</span> </pre></p></p> </section></body></html>