<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cameron McKay &#187; JavaScript</title>
	<atom:link href="http://cdmckay.org/blog/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Programming and Game Development</description>
	<lastBuildDate>Tue, 11 Oct 2011 05:05:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The jQuery animate() step callback function</title>
		<link>http://cdmckay.org/blog/2010/03/01/the-jquery-animate-step-callback-function/</link>
		<comments>http://cdmckay.org/blog/2010/03/01/the-jquery-animate-step-callback-function/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 16:32:59 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1189</guid>
		<description><![CDATA[If you&#8217;ve ever needed to do more complex animations than fades and slides, then you&#8217;ve probably encountered the jQuery animate function. The animate function allows you quite a bit more flexibility than just using fadeOut or slideDown. In fact, the oft-used fades and slides simply wrap calls to animate. If you&#8217;ve ever looked at the [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever needed to do more complex animations than fades and slides, then you&#8217;ve probably encountered the <a href="http://api.jquery.com/animate/">jQuery <code>animate</code> function</a>.  The <code>animate</code> function allows you quite a bit more flexibility than just using <code>fadeOut</code> or <code>slideDown</code>.  In fact, the oft-used fades and slides simply wrap calls to <code>animate</code>.</p>
<p>If you&#8217;ve ever looked at the jQuery <code>animate</code> docs at <a href="http://api.jquery.com">api.jquery.com</a> you might have noticed that one of the optional arguments you can define is <code>step</code> which is defined as:</p>
<blockquote><p>A function to be called after each step of the animation.</p></blockquote>
<p>&#8230;and that&#8217;s it.  If you search for &#8220;step&#8221; on the page, you won&#8217;t see another mention of it.</p>
<p><span id="more-1189"></span></p>
<p>Fortunately, a quick search on Google for &#8220;jquery animate step&#8221; will yield <a href="http://docs.jquery.com/Release:jQuery_1.2/Effects">the jQuery 1.2 release notes</a> that sheds a bit of light <a href="http://docs.jquery.com/Release:jQuery_1.2/Effects#Extensible_Animations">on the step function</a>:</p>
<blockquote><p>You can now extend jQuery animations with a function that is fired on every step of the animation that changes the style of the element being animated. It can be extended for specific css properties, or even to create a custom animation type.</p>
<p>For example, you can pass in an extra step function to .animate() to perform actions like animation synchronization.</p></blockquote>
<p>The release notes also contain a code sample:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#go&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.block:first&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
      duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span>
      step<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>now<span style="color: #339933;">,</span> fx<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.block:gt(0)&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;left&quot;</span><span style="color: #339933;">,</span> now<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So now we know the arguments <code>step</code> takes: <code>now</code> and <code>fx</code>.  We can also tell from the code that <code>now</code> contains the value being animated.  <code>fx</code>, however, is still a bit mystery.  We can solve that mystery by running the above code in <a href="http://getfirebug.com/">Firebug</a>, setting a break point when the step function is called, and then inspecting the <code>fx</code> and <code>now</code> objects.  Here are the results of that operation:</p>
<table>
<thead>
<tr>
<th colspan="3">The <code>fx</code> object</th>
</tr>
<tr>
<th>Attribute</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>elem</td>
<td>DOM Element</td>
<td>The element being animated.</td>
</tr>
<tr>
<td>end</td>
<td>Number</td>
<td>The value the animation will end at.</td>
</tr>
<tr>
<td>now</td>
<td>Number</td>
<td>The value the animation is currently at.</td>
</tr>
<tr>
<td>options</td>
<td>Object</td>
<td>Some animations options.</td>
</tr>
<tr>
<td>options.curAnim</td>
<td>Object</td>
<td>Information about the current animation.  For example, if you passed <code>{ opacity: 0, top: "+=16" }</code> as the first parameter of <code>animate</code>, then they will be attributes of this object.</td>
</tr>
<tr>
<td>options.duration</td>
<td>Number</td>
<td>The duration that you passed to <code>animate</code>.</td>
</tr>
<tr>
<td>pos</td>
<td>Number</td>
<td>Sweeps from 0.0 to 1.0 during the animation. Sort of like a ‘t’ value for interpolations.</td>
</tr>
<tr>
<td>prop</td>
<td>String</td>
<td>The CSS property being varied (e.g. &#8220;opacity&#8221; or &#8220;top&#8221;).</td>
</tr>
<tr>
<td>start</td>
<td>Number</td>
<td>The starting value of the CSS property being animated.</td>
</tr>
<tr>
<td>startTime</td>
<td>Number</td>
<td>The timestamp indicating what time the animation started.</td>
</tr>
<tr>
<td>state</td>
<td>Number</td>
<td>Which step of the animation we&#8217;re on.  For example, if you made an animation that was moving 100px to the right over 100ms, and jQuery moves elements every 10ms, then there would be 10 states.</td>
</tr>
<tr>
<td>unit</td>
<td>String</td>
<td>The unit of the CSS value being animated (e.g. &#8220;px&#8221; or &#8220;em&#8221;), if applicable.</td>
</tr>
</tbody>
</table>
<p>There were a couple more attributes (most of them functions) that I did not include because I either didn&#8217;t think they&#8217;d be useful or I didn&#8217;t really know what they did.  If you need to know what those do, I suggest Firebugging up your code and taking a look yourself <img src='http://cdmckay.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/03/01/the-jquery-animate-step-callback-function/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Fep Collections</title>
		<link>http://cdmckay.org/blog/2009/07/29/fep-collections/</link>
		<comments>http://cdmckay.org/blog/2009/07/29/fep-collections/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 20:47:57 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Fep]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[dictionaries]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[lists]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=863</guid>
		<description><![CDATA[As I was working on the PHP classes that Fep was to map to, I realized that it was rather silly for me to re-invent an API for lists since there were already many well-designed ones in existence. In particular, I&#8217;ve found that the .NET 3.5 collections API to be particularly nice to use. Moreover, [...]]]></description>
			<content:encoded><![CDATA[<p>As I was working on the PHP classes that Fep was to map to, I realized that it was rather silly for me to re-invent an API for lists since there were already many well-designed ones in existence.  In particular, I&#8217;ve found that the .NET 3.5 collections API to be particularly nice to use.  Moreover, since everyone and their dog and using .NET now, it would also be familiar to most programmers.  Thus, I&#8217;ve decided to use a subset of the .NET collections API for Fep collections.</p>
<p><span id="more-863"></span></p>
<p>Furthermore, after some thought, I&#8217;ve decided to split up Fep arrays.  In PHP arrays aren&#8217;t really arrays in the traditional sense, but more like array/associate array hybrids (JavaScript uses a similar concept).  This can be confusing, error-prone and I don&#8217;t think that the benefits of having an hybrid array system outweighs the complexity.</p>
<p>Thus, in Fep, there will be two main collection types: lists and dictionaries.  Here&#8217;s how a Fep list will look like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Fep</span>
fep <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;bar&quot;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
fep<span style="color: #339933;">.</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// == 4</span>
fep<span style="color: #339933;">.</span>add<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;baz&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
fep<span style="color: #339933;">.</span>find<span style="color: #009900;">&#40;</span> x <span style="color: #339933;">=&gt;</span> x <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// == 2</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Will compile into this PHP</span>
<span style="color: #000088;">$fep</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FepList<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;bar&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;baz&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$x</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> x <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And here&#8217;s how a Fep dictionary will look:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Fep</span>
fep <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> blah<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;blah&quot;</span><span style="color: #339933;">,</span> foo<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;bar&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;zero&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
fep<span style="color: #339933;">.</span>keys<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// == [ &quot;blah&quot;, &quot;foo&quot;, 0 ]</span>
fep<span style="color: #339933;">.</span>containsKey<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;foo&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// == true</span>
fep<span style="color: #339933;">.</span>remove<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;foo&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// == true</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// PHP</span>
<span style="color: #000088;">$fep</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FepDict<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;blah&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;blah&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;foo&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;bar&quot;</span><span style="color: #339933;">,</span> 
         <span style="color: #cc66cc;">0</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;zero&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">keys</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">containsKey</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;foo&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remove</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;foo&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/07/29/fep-collections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to (kinda) fix Firefox&#8217;s showModalDialog</title>
		<link>http://cdmckay.org/blog/2009/07/07/how-to-kinda-fix-firefoxs-showmodaldialog/</link>
		<comments>http://cdmckay.org/blog/2009/07/07/how-to-kinda-fix-firefoxs-showmodaldialog/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 18:48:38 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[showmodaldialog]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=738</guid>
		<description><![CDATA[As someone who has had to write a lot of IE-only code (against my will, I swear!), I was pleased to hear that Firefox 3.0 added support for the IE JavaScript function window.showModalDialog. Being in the middle of re-writing an IE-only web application, I thought this would simplify rewriting the modal dialogs to be compatible [...]]]></description>
			<content:encoded><![CDATA[<p>As someone who has had to write a lot of IE-only code (against my will, I swear!), I was pleased to hear that Firefox 3.0 added support for the IE JavaScript function <code>window.showModalDialog</code>.  Being in the middle of re-writing an IE-only web application, I thought this would simplify rewriting the modal dialogs to be compatible with both browsers.</p>
<p>Unfortunately, I don&#8217;t think the folks at Mozilla put their best effort into this one.  </p>
<p><span id="more-738"></span></p>
<p>First of all, the Firefox implementation only supports a subset of the IE options.  Here&#8217;s a table showing what each browsers implementation supports according to their documentation:</p>
<table>
<tr>
<th colspan="3">Documented Support</th>
</tr>
<tr>
<th>Option</th>
<th><a href="http://msdn.microsoft.com/en-us/library/ms536759%28VS.85%29.aspx">Internet Explorer</a></th>
<th><a href="https://developer.mozilla.org/En/DOM/Window.showModalDialog">Firefox</a></th>
</tr>
<tr>
<td>dialogWidth</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>dialogHeight</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>dialogLeft</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>dialogTop</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>center</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>dialogHide</td>
<td>Yes</td>
<td><strong>No</strong></td>
</tr>
<tr>
<td>edge</td>
<td>Yes</td>
<td><strong>No</strong></td>
</tr>
<tr>
<td>resizable</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>scroll</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>status</td>
<td>Yes</td>
<td><strong>No</strong></td>
</tr>
<tr>
<td>unadorned</td>
<td>Yes</td>
<td><strong>No</strong></td>
</tr>
</table>
<p>Doesn&#8217;t seem too bad, right?  We still have <code>center</code>, <code>resizable</code> and <code>scroll</code> right? Right? <em>Wrong.</em> </p>
<h3>Documentation != Implementation</h3>
<p>It seems the guys who wrote the Firefox documentation weren&#8217;t the same guys who wrote the code. Including <code>center</code>, <code>resizable</code> or <code>scroll</code> in your arguments string has no effect whatsoever.  In fact, <code>center</code> is supposed to be <code>on</code> by default, but I can tell you right now that the modal dialog sure as hell isn&#8217;t centered.</p>
<p>Here&#8217;s an updated support matrix:</p>
<table>
<tr>
<th colspan="3">Actual Support</th>
</tr>
<tr>
<th>Option</th>
<th>Internet Explorer</th>
<th>Firefox</th>
</tr>
<tr>
<td>dialogWidth</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>dialogHeight</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>dialogLeft</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>dialogTop</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>center</td>
<td>Yes</td>
<td><strong>No</strong></td>
</tr>
<tr>
<td>dialogHide</td>
<td>Yes</td>
<td><strong>No</strong></td>
</tr>
<tr>
<td>edge</td>
<td>Yes</td>
<td><strong>No</strong></td>
</tr>
<tr>
<td>resizable</td>
<td>Yes</td>
<td><strong>No</strong></td>
</tr>
<tr>
<td>scroll</td>
<td>Yes</td>
<td><strong>No</strong></td>
</tr>
<tr>
<td>status</td>
<td>Yes</td>
<td><strong>No</strong></td>
</tr>
<tr>
<td>unadorned</td>
<td>Yes</td>
<td><strong>No</strong></td>
</tr>
</table>
<p>Fortunately, they did get one part right: the dialog is modal.  Woooo!</p>
<h3>Can we fix it?</h3>
<p>Well, we can, kinda.  We can&#8217;t fix <code>resizable</code>, because that&#8217;s a property of the window, and we can&#8217;t fix <code>scroll</code> for the same reason.  We can, however, emulate the <code>center</code> option by using <code>dialogLeft</code> and <code>dialogTop</code>.  Here&#8217;s some quick-and-dirty code I wrote to do that.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> oldShowModalDialog <span style="color: #339933;">=</span> window.<span style="color: #660066;">showModalDialog</span><span style="color: #339933;">;</span>
window.<span style="color: #660066;">showModalDialog</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> args<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>           
  <span style="color: #006600; font-style: italic;">// Convert the options string into an object.</span>
  <span style="color: #003366; font-weight: bold;">var</span> pairs <span style="color: #339933;">=</span> options.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\s+/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> option <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>pairs<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pair <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>pair.<span style="color: #660066;">length</span> <span style="color: #339933;">!=</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
    option<span style="color: #009900;">&#91;</span>pair<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> pair<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Get the width and height of the document.</span>
  <span style="color: #003366; font-weight: bold;">var</span> width <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> height <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Get the width and height of the dialog.</span>
  <span style="color: #003366; font-weight: bold;">var</span> dialogWidth <span style="color: #339933;">=</span> option.<span style="color: #660066;">dialogWidth</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #003366; font-weight: bold;">var</span> dialogHeight <span style="color: #339933;">=</span> option.<span style="color: #660066;">dialogHeight</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Calculate where the dialog needs to be to be</span>
  <span style="color: #006600; font-style: italic;">// centered.</span>
  <span style="color: #003366; font-weight: bold;">var</span> dialogLeft <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>width <span style="color: #339933;">-</span> dialogWidth<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> dialogTop <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>height <span style="color: #339933;">-</span> dialogHeight<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Add those settings to the options string.</span>
  options <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;dialogLeft: &quot;</span> <span style="color: #339933;">+</span> dialogLeft <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;; &quot;</span><span style="color: #339933;">;</span>
  options <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;dialogTop: &quot;</span> <span style="color: #339933;">+</span> dialogTop <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;; &quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Call the original function.</span>
  <span style="color: #000066; font-weight: bold;">return</span> oldShowModalDialog<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> args<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Although this code uses <a href="http://jquery.com">jQuery</a>, you could easily rewrite it to use vanilla JavaScript.</p>
<h3>Will it be fixed soon?</h3>
<p>Firefox has had <code>showModalDialog</code> since 3.0.  So naturally, you&#8217;d have assumed they&#8217;d have fixed this bug in 3.5, right?  Nope!  The bug still exists in 3.5.  Thus, I wouldn&#8217;t hold my breath on this one being fixed anytime soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/07/07/how-to-kinda-fix-firefoxs-showmodaldialog/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JSS in Action</title>
		<link>http://cdmckay.org/blog/2009/06/29/jss-in-action/</link>
		<comments>http://cdmckay.org/blog/2009/06/29/jss-in-action/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 07:05:15 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JSS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[reneelung]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=598</guid>
		<description><![CDATA[The first page to use JSS in the wild has recently been launched: reneelung.ca. The page was designed by my fiancee Renee Lung and programmed by me. You&#8217;ll notice if you look at the source that there is no JavaScript in the traditional sense. The page consists of three JavaScript files: the jQuery library, the [...]]]></description>
			<content:encoded><![CDATA[<p>The first page to use JSS in the wild has recently been launched: <a href="http://reneelung.ca">reneelung.ca</a>.</p>
<p>The page was designed by my fiancee Renee Lung and programmed by me.  You&#8217;ll notice if you look at the source that there is no JavaScript in the traditional sense.  The page consists of three JavaScript files: the jQuery library, the JSS plugin, and a JSS stylesheet.</p>
<p>I hope this gives people an idea of the power of JSS.  Read on for an annotated version of the JSS stylesheet used on that site (as of June 29th 2009).</p>
<p><span id="more-598"></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Fades the background in on each page.</span>
<span style="color: #006600; font-style: italic;">// You'll notice the square bracket notation.  This is used to </span>
<span style="color: #006600; font-style: italic;">// attach multiple JSS commands to a single event property.  </span>
<span style="color: #006600; font-style: italic;">// They run in the order they are declared.</span>
<span style="color: #3366CC;">&quot;.background&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  setup<span style="color: #339933;">:</span> 
  <span style="color: #009900;">&#91;</span> 
    <span style="color: #3366CC;">&quot;set-css (.background) display none&quot;</span><span style="color: #339933;">,</span> 
    <span style="color: #3366CC;">&quot;fade-in (.background) normal&quot;</span> 
  <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>  
&nbsp;
<span style="color: #006600; font-style: italic;">// This block only runs on the front page.</span>
<span style="color: #006600; font-style: italic;">// It is used to show a different button mouse-over </span>
<span style="color: #006600; font-style: italic;">// image on the frontpage.</span>
<span style="color: #3366CC;">&quot;.front .button&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  setup<span style="color: #339933;">:</span> 
  <span style="color: #009900;">&#91;</span> 
    <span style="color: #006600; font-style: italic;">// This saves the name of mouseover image </span>
    <span style="color: #006600; font-style: italic;">// to the element so that the mouseover code</span>
    <span style="color: #006600; font-style: italic;">// (down further) can use it.</span>
    <span style="color: #006600; font-style: italic;">// The {id} is an attribute getter.</span>
    <span style="color: #006600; font-style: italic;">// Thus, if the id for one of the buttons is</span>
    <span style="color: #006600; font-style: italic;">// &quot;portfolio&quot;, then the class-over variable</span>
    <span style="color: #006600; font-style: italic;">// will be &quot;front-portfolio-over&quot;.</span>
    <span style="color: #3366CC;">&quot;set-data class-over front-{id}-over&quot;</span><span style="color: #339933;">,</span>       
    <span style="color: #3366CC;">&quot;preload-image gfx/buttons/front-{link-to}-over.png&quot;</span>
  <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// This block runs on any page except the front page.</span>
<span style="color: #3366CC;">&quot;#window:not(.front) .button&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  setup<span style="color: #339933;">:</span> 
  <span style="color: #009900;">&#91;</span> 
    <span style="color: #3366CC;">&quot;set-data class-over {id}-over&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;preload-image gfx/buttons/{link-to}-over.png&quot;</span>
  <span style="color: #009900;">&#93;</span>  
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>  
&nbsp;
<span style="color: #006600; font-style: italic;">// This the code that is used to transition</span>
<span style="color: #006600; font-style: italic;">// between pages.</span>
<span style="color: #3366CC;">&quot;.button&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>    
  <span style="color: #006600; font-style: italic;">// This is the rollover code.  Pretty simple eh?</span>
  hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;toggle-class [class-over] | toggle-class [class-over]&quot;</span><span style="color: #339933;">,</span>
  click<span style="color: #339933;">:</span> 
  <span style="color: #009900;">&#91;</span>
    <span style="color: #006600; font-style: italic;">// This make the background fade out </span>
    <span style="color: #006600; font-style: italic;">// and then links the user to the link</span>
    <span style="color: #006600; font-style: italic;">// provided in the HTML.</span>
    <span style="color: #3366CC;">&quot;fade-out (.background) normal !then-link-to {@link-to}.html&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;fade-out (#portfolio-window) normal&quot;</span>
  <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// This a custom user function.  They can</span>
<span style="color: #006600; font-style: italic;">// be tagged on to most JSS commands</span>
<span style="color: #006600; font-style: italic;">// (in cases where you'd have things like</span>
<span style="color: #006600; font-style: italic;">// a callback for an animation).</span>
<span style="color: #3366CC;">&quot;then-link-to&quot;</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>link_target<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>  
  <span style="color: #003366; font-weight: bold;">var</span> event <span style="color: #339933;">=</span> $.<span style="color: #660066;">Event</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  event.<span style="color: #660066;">data</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> arguments<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span> link_target <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;link-to&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// This is the code that handles making the </span>
<span style="color: #006600; font-style: italic;">// the images on the portfolio page &quot;glow&quot;</span>
<span style="color: #006600; font-style: italic;">// when you mouseover them.</span>
<span style="color: #3366CC;">&quot;div.artwork img&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// This &quot;setup&quot; event property is used</span>
  <span style="color: #006600; font-style: italic;">// to basically create a variable...</span>
  setup<span style="color: #339933;">:</span> 
  <span style="color: #009900;">&#91;</span> 
    <span style="color: #3366CC;">&quot;set-data opacity-off 0.3&quot;</span><span style="color: #339933;">,</span> 
    <span style="color: #3366CC;">&quot;set-css opacity [opacity-off]&quot;</span>
  <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// ... that is then used by the &quot;hover&quot;</span>
  <span style="color: #006600; font-style: italic;">// event property to set the opacity.</span>
  hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;fade-to 1 | fade-to [opacity-off]&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// The next block as well as the </span>
<span style="color: #006600; font-style: italic;">// two following #link-page-n classes are</span>
<span style="color: #006600; font-style: italic;">// an admittedly hack way of making pages,</span>
<span style="color: #006600; font-style: italic;">// but get the job done for a relatively simple</span>
<span style="color: #006600; font-style: italic;">// page.  If you were dealing with anymore</span>
<span style="color: #006600; font-style: italic;">// than a couple pages, this method would </span>
<span style="color: #006600; font-style: italic;">// quickly get unwieldly.</span>
&nbsp;
<span style="color: #3366CC;">&quot;#pager a&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  click<span style="color: #339933;">:</span> 
  <span style="color: #009900;">&#91;</span> 
    <span style="color: #3366CC;">&quot;set-css (.portfolio-window) display none&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;set-css (#pager a) font-weight normal&quot;</span>
  <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #3366CC;">&quot;#link-page-1&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  font_weight<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;bold&quot;</span><span style="color: #339933;">,</span>
  click<span style="color: #339933;">:</span> 
  <span style="color: #009900;">&#91;</span>
    <span style="color: #3366CC;">&quot;set-css (#portfolio-page-1) display block&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;set-css font-weight bold&quot;</span>
  <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #3366CC;">&quot;#link-page-2&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  click<span style="color: #339933;">:</span> 
  <span style="color: #009900;">&#91;</span>
    <span style="color: #3366CC;">&quot;set-css (#portfolio-page-2) display block&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;set-css font-weight bold&quot;</span>
  <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// This is using JSS to make an image</span>
<span style="color: #006600; font-style: italic;">// into a hyperlink.</span>
<span style="color: #3366CC;">&quot;#pdf-button&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  click<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;link-to {resume-link}&quot;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/06/29/jss-in-action/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSS 1.0.0-beta2 released!</title>
		<link>http://cdmckay.org/blog/2009/05/24/jss-100-beta-2-released/</link>
		<comments>http://cdmckay.org/blog/2009/05/24/jss-100-beta-2-released/#comments</comments>
		<pubDate>Mon, 25 May 2009 04:20:08 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JSS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[1.0.0-beta2]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=591</guid>
		<description><![CDATA[I&#8217;m proud to announce the release of JSS 1.0.0-beta2. Here are the changes: Fixed the way that {attributes} are handled when not using a selector. They should now properly grab the attribute from element that fired the event. Made it so each time a declaration block is applied, all the event handlers bound by JSS [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m proud to announce the release of <a href="http://javascript-stylesheets.googlecode.com/files/jquery.jss-1.0.0-beta2.js">JSS 1.0.0-beta2</a>.  Here are the changes:</p>
<ul>
<li>Fixed the way that {attributes} are handled when not using a selector.  They should now properly grab the attribute from element that fired the event.</li>
<li>Made it so each time a declaration block is applied, all the event handlers bound by JSS to the elements returned by that selector are unbound.</li>
<li>Fixed the semantics of the {@attr} syntax.  It now means that the eventTarget should always be used, regardless of whether or not a selector is defined.  In order to access the attributes of a selector use {@attr} now.</li>
<li>Added two new commands: set-data and remove-data.  These commands use the jQuery functions data() and removeData().  They are similar to set-attr and remove-attr except instead of storing the information in an attribute, they are stored in a table.  Data is accessed by using [data] syntax, similar to the {attr} syntax.</li>
<li>Added new event property: setup.  This event is fired when the JSS stylesheet is loaded (setup).  This is useful in certain cases where the user wants to setup some variables using set-data.</li>
<li>Made it so you can now set scroll-top and scroll-left via the set-attr command.</li>
<li>Added trigger command.  It can be used to trigger event properties on elements.</li>
<li>Fixed bug where you could put a string | in a binary command due to the<br />
simplistic implementation.  The new version requires any | to be escaped with \|.</li>
<li>Added fade-to command, based on the jQuery.fadeTo() command.</li>
<li>Added show and hide commands.</li>
<li>Fixed issue with data and attribute replacements not working correctly (i.e. the first event would replace them for all events).</li>
<li>Callbacks may now take string parameters.  For example, &#8220;fade-out (#target) normal !callback str1 str2&#8243;.</li>
</ul>
<p>I&#8217;ve also started writing documentation for the <a href="http://cdmckay.org/blog/jss/command-reference/">JSS commands</a>.  It is only partially completed but can still be helpful.  Check out the <a href="http://cdmckay.org/blog/jss">JSS homepage</a> for other documentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/05/24/jss-100-beta-2-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSS 1.0.0-beta1 released!</title>
		<link>http://cdmckay.org/blog/2009/05/05/jss-100-beta1-released/</link>
		<comments>http://cdmckay.org/blog/2009/05/05/jss-100-beta1-released/#comments</comments>
		<pubDate>Tue, 05 May 2009 22:43:37 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JSS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[beta]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=527</guid>
		<description><![CDATA[JSS 1.0.0-beta1 was released a couple minutes ago. This version of JSS should be mostly bug-free (although there are still many more tests to be written). For information on how to use JSS, see here and here. More extensive documentation will be forthcoming as I move towards a 1.0.0 release.]]></description>
			<content:encoded><![CDATA[<p><a href="http://javascript-stylesheets.googlecode.com/files/jquery.jss-1.0.0-beta1.js">JSS 1.0.0-beta1</a> was released a couple minutes ago.  This version of <a href="http://code.google.com/p/javascript-stylesheets/">JSS</a> should be mostly bug-free (although there are still many more tests to be written).  For information on how to use JSS, see <a href="http://cdmckay.org/blog/jss/">here</a> and <a href="http://cdmckay.org/blog/category/jss/">here</a>.</p>
<p>More extensive documentation will be forthcoming as I move towards a 1.0.0 release.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/05/05/jss-100-beta1-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The JSS Stylesheet, Part 1</title>
		<link>http://cdmckay.org/blog/2009/05/05/the-jss-stylesheet/</link>
		<comments>http://cdmckay.org/blog/2009/05/05/the-jss-stylesheet/#comments</comments>
		<pubDate>Tue, 05 May 2009 16:05:40 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JSS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[event properties]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[stylesheets]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=459</guid>
		<description><![CDATA[I&#8217;ve said previously that JSS is a lot like CSS. What is CSS? Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation (that is, the look and formatting) of a document written in a markup language. If that blurb were written about JSS, it&#8217;d sound a little like this: JavaScript [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve said previously that JSS is a lot like <a href="http://en.wikipedia.org/wiki/CSS">CSS</a>.  What is CSS?</p>
<blockquote><p>Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation (that is, the look and formatting) of a document written in a markup language.</p></blockquote>
<p>If that blurb were written about JSS, it&#8217;d sound a little like this:</p>
<blockquote><p>JavaScript Style Sheets (JSS) is a style sheet language used to describe the presentation (that is, the look and formatting) and functionality of a document written in HTML.</p></blockquote>
<p>In this article, we will look at the syntactic structure of a JSS stylesheet.  We will also talk briefly about including JSS stylesheets in your HTML documents.</p>
<p><span id="more-459"></span></p>
<p>At this point, some people may be wondering: <em>aren&#8217;t we mucking up CSS by combining functionality and presentation in the same place?</em>  This is a valid concern.  However, just because we can combine presentation and functionality in JSS, doesn&#8217;t necessarily mean we should.  A good JSS site will have two or more JSS stylesheets: one for JSS styles (if they are needed) and one for JSS event properties.  JSS event properties will be covered in a future article.</p>
<h3>Syntactic Structure</h3>
<p>Let&#8217;s get back to the CSS/JSS connection.  What do people do with CSS?  They write stylesheets.  More specifically, they write rules.  In JSS, we write rules too.  In fact, we write them in a very similar format to CSS, although with some important differences owing to the fact that JSS stylesheets are actually JavaScript programs.  Here&#8217;s the basic format of a CSS stylesheet:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">rule-selector-a
<span style="color: #00AA00;">&#123;</span>
  property-a<span style="color: #00AA00;">:</span> value-a<span style="color: #00AA00;">;</span>
  property-b<span style="color: #00AA00;">:</span> value-b<span style="color: #00AA00;">;</span>
  ...
<span style="color: #00AA00;">&#125;</span>
&nbsp;
rule-selector-b
<span style="color: #00AA00;">&#123;</span>
  property-a<span style="color: #00AA00;">:</span> value-a<span style="color: #00AA00;">;</span>
  property-b<span style="color: #00AA00;">:</span> value-b<span style="color: #00AA00;">;</span>
  ...
<span style="color: #00AA00;">&#125;</span>
&nbsp;
...</pre></div></div>

<p>In JSS, we use pretty much the same format, with the differences being the addition of boilerplate code (1), quoted selectors with colons (2), underscores instead of dashes in property names (3), quoted values with commas (4) and commas between rules (5).  The JSS format is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// (1)</span>
&nbsp;
<span style="color: #3366CC;">&quot;rule-selector-a&quot;</span><span style="color: #339933;">:</span> <span style="color: #006600; font-style: italic;">// (2)</span>
<span style="color: #009900;">&#123;</span>
  property_a<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-a&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// (3) and (4)</span>
  property_b<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-b&quot;</span><span style="color: #339933;">,</span>
  ...
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// (5)</span>
&nbsp;
<span style="color: #3366CC;">&quot;rule-selector-b&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  property_a<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-a&quot;</span><span style="color: #339933;">,</span>
  property_b<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-b&quot;</span><span style="color: #339933;">,</span>
  ...
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// (1)</span></pre></div></div>

<p>Let&#8217;s talk a bit about each of these difference:</p>
<ol>
<li><strong>Boilerplate code.</strong>  This is an unfortunate side effect of having to use JavaScript to extend CSS.  Fortunately, it&#8217;s the same for each JSS stylesheet, so it&#8217;s not too much of a hassle to remember.</li>
<li><strong>Quoted selectors with colons.</strong> Since the selector portion of a JSS rule is a string, it must be represented in JSS as a JavaScript string.  All strings in JavaScript must be either single-quoted (as in &#8216;Hello!&#8217;) or double-quoted (as in &#8220;w00t w00t&#8221;).  The colon following the selector is a necessity as internally, JSS selectors act as keys in a JavaScript object.  A JSS selector may be any valid jQuery selector.</li>
<li><strong>Underscores instead of dashes in property names.</strong>  This is not strictly true, although using underscores instead of dashes is the recommended way of representing JSS properties.  Consider, for example, the <em>font-family</em> property from CSS.  This can be represented in 3 ways in JSS:
<ol>
<li>font_family</li>
<li>&#8220;font_family&#8221;</li>
<li>&#8220;font-family&#8221;</li>
</ol>
</li>
<li><strong>Quoted values with commas.</strong> Unlike CSS, all values in JSS are quoted with either single or double quotes.  Furthermore, between declarations, there must be a comma.<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// This is correct.</span>
<span style="color: #3366CC;">&quot;rule-selector-a&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  property_a<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-a&quot;</span><span style="color: #339933;">,</span>
  property_b<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-b&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// This is incorrect.</span>
<span style="color: #3366CC;">&quot;rule-selector-b&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  property_a<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-a&quot;</span><span style="color: #339933;">,</span>
  property_b<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-b&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// This declaration should not </span>
                         <span style="color: #006600; font-style: italic;">// have a comma.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li><strong>Commas between rules.</strong> Like declarations, each rule must have a comma between it:

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;rule-selector-a&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #3366CC;">&quot;rule-selector-b&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// Make sure the last rule has no comma after it.</span></pre></div></div>

</li>
</ol>
<h3>Loading JSS and JSS stylesheets</h3>
<p>Now that we understand the syntactic structure of a JSS stylesheet, it&#8217;s time to take look at how we include them in an HTML file.  Every page that makes use of JSS will have the following structure:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Title<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.jss.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sheet-1.jss.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sheet-2.jss.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    ...
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sheet-n.jss.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #808080; font-style: italic;">&lt;!-- Your website here. --&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>Let&#8217;s quickly go through the components here.  The first <code>script</code> tag includes the jQuery library, which is required by JSS.  The second <code>script</code> tag includes the JSS jQuery plugin, which is the brains of JSS, and is required for any JSS stylesheet to work.  All the <code>script</code> tags after that are the JSS stylesheets that you wish to load.</p>
<p>As you can see, you can add an unlimited number of JSS stylesheets, and when the page loads, they&#8217;ll be applied in the same order they are included in the page.  Keep in mind, however, that if you&#8217;re including many <code>script</code> tags on a single webpage, it is advisable to use some sort of JavaScript loader.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/05/05/the-jss-stylesheet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing JSS</title>
		<link>http://cdmckay.org/blog/2009/04/30/introducing-jss/</link>
		<comments>http://cdmckay.org/blog/2009/04/30/introducing-jss/#comments</comments>
		<pubDate>Fri, 01 May 2009 04:43:34 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JSS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[ie7]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[selector]]></category>
		<category><![CDATA[stylesheet]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=391</guid>
		<description><![CDATA[Ever wanted to make an image rollover, but you don&#8217;t know JavaScript? In this tutorial, we show you how JSS, a CSS-like language, makes this a breeze. Consider these two images: Imagine we wanted to make them &#8220;rollover&#8221;, i.e. the zebra should be shown when nothing is happening, and the shark should be showed when [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to make an image rollover, but you don&#8217;t know JavaScript?  In this tutorial, we show you how JSS, a CSS-like language, makes this a breeze.</p>
<p><span id="more-391"></span></p>
<p>Consider these two images:</p>
<p><img src="http://couchware.ca/users/cam/example/rollover/image_off.gif" alt="Zebra" /><br />
<img src="http://couchware.ca/users/cam/example/rollover/image_on.gif" alt="Shark" /></p>
<p>Imagine we wanted to make them &#8220;rollover&#8221;, i.e. the zebra should be shown when nothing is happening, and the shark should be showed when the mouse is &#8220;rolled over&#8221; (i.e. on top of) the zebra.  Like this:</p>
<p><img src="http://couchware.ca/users/cam/example/rollover/image_off.gif" src-out="http://couchware.ca/users/cam/example/rollover/image_off.gif" src-over="http://couchware.ca/users/cam/example/rollover/image_on.gif" /><script src="http://www.google.com/jsapi" type="text/javascript"></script> <script type="text/javascript"><!--
google.load("jquery", "1.3.2");
google.setOnLoadCallback(function()
{
  $("img[src-over]").hover(
    function() { $(this).attr('src', $(this).attr('src-over')); },
    function() { $(this).attr('src', $(this).attr('src-out')); }
  );
});
// --></script></p>
<p>Before we start writing some JSS, let&#8217;s write some HTML for a page just showing the rollover at rest (i.e. just show a plain old static image):</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JSS Rollover<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_off.png&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>If we copied this code into an HTML file and ran it on our browser, we would see a static image of a zebra.  Not that exciting, right?  Now let&#8217;s see how we can use JSS to make this zebra &#8220;rollover&#8221; to the shark when the mouse is on it.  Here&#8217;s the JSS stylesheet:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #3366CC;">&quot;img[src-over]&quot;</span><span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span>
    hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;set-attr src {src-over} | set-attr src {src-out}&quot;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let&#8217;s step through this syntax, because there&#8217;s a lot going on here, even though it&#8217;s a single line.  First, let&#8217;s annotate the JSS source so we can refer to it easier:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// 1. Boilerplate that starts any JSS stylesheet.</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// 2. Rule</span>
  <span style="color: #3366CC;">&quot;img[src-over]&quot;</span><span style="color: #339933;">:</span> <span style="color: #006600; font-style: italic;">// 3. CSS Selector</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// 4. JSS Declaration</span>
    hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;set-attr src {src-over} | set-attr src {src-out}&quot;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you might have noticed, a JSS stylesheet looks very similar to a CSS stylesheet.  Let&#8217;s walk through the anatomy of a JSS stylesheet.</p>
<p>At the top of any JSS stylesheet there is some boilerplate code (1).  This is necessary because a JSS stylesheet is actually a JavaScript program.  However, beyond the fact that this boilerplate needs to be in every JSS stylesheet, you don&#8217;t really need to know what it does.</p>
<p>A JSS stylesheet is made up of an unlimited number of rules (2).  This is the same as in a CSS stylesheet.  Inside a JSS rule, there are two components, a selector (3) and an unlimited number of declarations (inside a declaration block) (4).  This is where we start to see some of the advantages of JSS over using plain CSS.</p>
<p>JSS selectors are powered by <a href="http://jquery.com">jQuery</a>.  As such, JSS supports any CSS3-compliant selector.  In addition, JSS also allows you to use any of the jQuery CSS selector extensions as well.</p>
<p>For example, in CSS, this stylesheet should stripe all the tables on the page as alternating gray and white:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">table tr<span style="color: #3333ff;">:nth-</span>child<span style="color: #00AA00;">&#40;</span>odd<span style="color: #00AA00;">&#41;</span>
<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#eee</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
&nbsp;
table tr<span style="color: #3333ff;">:nth-</span>child<span style="color: #00AA00;">&#40;</span>even<span style="color: #00AA00;">&#41;</span>
<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>However, if you tried to use that CSS stylesheet in the two most popular browsers available (IE7 and Firefox 3) you would discover that it didn&#8217;t work.  On the other hand, if you use the following JSS stylesheet&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #3366CC;">&quot;table tr:nth-child(odd)&quot;</span><span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span> background_color<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#eee&quot;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #3366CC;">&quot;table tr:nth-child(even)&quot;</span><span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span> background_color<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#fff&quot;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&#8230;you would discover that it worked in all major browsers.  By using a JavaScript selector engine, JSS offers better cross-browser styling than regular CSS.</p>
<p><em>(Aside: Before all you web developers out there go crazy, I would like to note that I realize there are several drawbacks to using JavaScript to style your page.  Stay tuned to the end of this article for a discussion of them).</em></p>
<p>Now let&#8217;s return to our task of making a rollover.  How does the JSS stylesheet make it possible?  Let&#8217;s take a look at that JSS stylesheet again:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// 1. Boilerplate that starts any JSS stylesheet.</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// 2. Rule</span>
  <span style="color: #3366CC;">&quot;img[src-over]&quot;</span><span style="color: #339933;">:</span> <span style="color: #006600; font-style: italic;">// 3. CSS Selector</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// 4. JSS Declaration</span>
    hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;set-attr src {src-over} | set-attr src {src-out}&quot;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let&#8217;s look closer at the rule.  More specifically, take a look at the CSS selector.  For anyone familiar CSS, it reads &ldquo;select all <strong>img</strong> tags that have the attribute <strong>src-over</strong>&rdquo;.  Why would we want to do that?  Well, as it happens, we need some way to describe which image to use when the user mouses over our image.  In order to do that, we&#8217;re going to use something called a <strong>custom attribute</strong>.</p>
<p>Custom attributes are user-defined attributes on HTML tags.  For example, the <strong>img</strong> tag has an attribute called <strong>src</strong>.  That&#8217;s a proper attribute that is defined in a W3C recommendation.  What we&#8217;re going to do is add two new attributes, <strong>src-over</strong> and <strong>src-out</strong>, that are not part of any W3C standard.  We just made them up because they&#8217;re convenient for our purposes.</p>
<p>Now that we&#8217;ve decided that we&#8217;re adding these two attributes, we need to add them to our HTML file:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JSS Rollover<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_off.png&quot;</span></span>
<span style="color: #009900;">        src-out<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_off.png&quot;</span> src-over<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_on.png&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>With that done, let&#8217;s take a look at the last part of the JSS stylesheet: the declaration.  A JSS declaration has a property (hover) and a value (&#8220;set-attr src {src-over} | set-attr src {src-out}&#8221;).  This is just like a CSS declaration, like &#8220;font-family: Arial, sans-serif&#8221;.  In that case, &#8220;font-family&#8221; is the property and &#8220;Arial, sans-serif&#8221; is the value.</p>
<p>In the case of the rollover JSS stylesheet, the hover property must have a value with the following structure:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;jss-expression | jss-expression&quot;</span></pre></div></div>

<p>The first JSS expression on the left is run when the mouse is over the image area, and the right JSS expression is run when the mouse leaves the image area.</p>
<p>Let&#8217;s look at what happens in the JSS rollover stylesheet when the mouse is brought over the image area:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">set<span style="color: #339933;">-</span>attr src <span style="color: #009900;">&#123;</span>src<span style="color: #339933;">-</span>over<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This JSS expression reads: &#8220;set the src attribute (of the selected element) to the contents of the src-over attribute (of the selected element)&#8221;.  This is because &#8220;set-attr&#8221; is a JSS <em>command</em>.  The &#8220;set-attr&#8221; command takes two arguments: the attribute to change, and the value to change it to.  That leaves one more question.  What&#8217;s the deal with the curly braces?</p>
<p>In JSS, curly braces are basically a &#8220;get-attr&#8221; command.  They fetch the value of an attribute.  In the context they are being used here, they fetch the value of the &#8220;src-over&#8221; attribute (which happens to be the name of the shark image &#8220;image_on.gif&#8221;).</p>
<p>With that information in hand, it can be deduced that the right hand side&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">set<span style="color: #339933;">-</span>attr src <span style="color: #009900;">&#123;</span>src<span style="color: #339933;">-</span>out<span style="color: #009900;">&#125;</span></pre></div></div>

<p>&#8230;switches the src attribute back to src-out image URL when the mouse leaves the image area.</p>
<p>Thus, if we put it all together, and include the appropriate JavaScript files, we have the following.  In the &#8220;index.html&#8221; file we have:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JSS Rollover<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text/javascript'</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'jquery.js'</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text/javascript'</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'jquery.jss.js'</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text/javascript'</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'styles.jss.js'</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_off.png&quot;</span></span>
<span style="color: #009900;">        src-off<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_off.png&quot;</span> src-over<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_on.png&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>And in the &#8220;styles.jss.js&#8221; file we have this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #3366CC;">&quot;img[src-over]&quot;</span><span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span>
    hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;set-attr src {src-over} | set-attr src {src-out}&quot;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>These files together create a page that has a rollover that changes from zebra to shark when the mouse is hovered over it.  Better yet, this JSS stylesheet can be used with any web page to enable rollover images.  All the user has to do is set the &#8220;src-over&#8221; and &#8220;src-out&#8221; custom attributes.</p>
<h3>Notes on JSS Availability &amp; Performance</h3>
<p>JSS applies its stylesheets using JavaScript.  This has several implications to web designers using it.  First of all, in order for a site to take advantage of JSS, JavaScript must be enabled.  Fortunately, something like 95% of all web surfers have JavaScript enabled, so that&#8217;s not too big of deal.  Secondly, JSS is a lot slower than CSS.  For most websites, this difference will probably be imperceptible deal, but it&#8217;s still something to be aware of.</p>
<p>My recommendations would be this: if you can do it in CSS, and it works properly across all browsers, then use CSS.  There&#8217;s no reason not to.  However, if you need to use JSS features like event properties (like hover) or cross-browser selectors, then use JSS.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/04/30/introducing-jss/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Arrayzing Command: merge()</title>
		<link>http://cdmckay.org/blog/2009/02/26/new-arrayzing-command-merge/</link>
		<comments>http://cdmckay.org/blog/2009/02/26/new-arrayzing-command-merge/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 01:53:12 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Arrayzing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[merge$]]></category>
		<category><![CDATA[mutators]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=242</guid>
		<description><![CDATA[Some nights, as I lay awake in bed, trying to sleep, I think about Arrayzing workflows.  Lately, I&#8217;ve been thinking of the different ways users might use Arrayzing. In particular, I&#8217;ve been thinking of how a user could edit a subset of elements in a zing without disturbing other elements. The result of this thoughtstream [...]]]></description>
			<content:encoded><![CDATA[<p>Some nights, as I lay awake in bed, trying to sleep, I think about Arrayzing workflows.  Lately, I&#8217;ve been thinking of the different ways users might use Arrayzing.  In particular, I&#8217;ve been thinking of how a user could edit a subset of elements in a zing without disturbing other elements. The result of this thoughtstream is a new Arrayzing command: <code>merge()</code>.</p>
<p><span id="more-242"></span></p>
<p>In short, the <code>merge()</code> command allows you to merge your current zing with the previous zing in your stack.  What&#8217;s the stack, you ask?</p>
<p>Recall that, by default, Arrayzing a functional API, meaning that (unless you&#8217;re using a mutator command) your zing is never modified.  Instead a new one is returned.  So, if we do this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">144</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">gteq</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&#8230;we have in fact created 3 zings, not one.  Although it seems wasteful to keep making new zings, it turns out to be pretty useful.  For one, it allows us to step backwards using the <code>undo()</code> command:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">144</span><span style="color: #009900;">&#41;</span>  <span style="color: #006600; font-style: italic;">// [1, 4, 9, 144]</span>
  .<span style="color: #660066;">gteq</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span>        <span style="color: #006600; font-style: italic;">// [4, 9, 144]</span>
  .<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// [2, 3, 12]</span>
  .<span style="color: #660066;">undo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>         <span style="color: #006600; font-style: italic;">// [4, 9, 144]</span>
  .<span style="color: #660066;">undo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>         <span style="color: #006600; font-style: italic;">// [1, 4, 9, 144]</span>
  .<span style="color: #660066;">undo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        <span style="color: #006600; font-style: italic;">// []</span></pre></div></div>

<p><em>(Aside: For those familiar with jQuery, the <code>undo()</code> command is the same as the end() command from that library).</em></p>
<p>Keeping a history (or stack) also allows us to combine the past with the present using the <code>andSelf()</code> command:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> double <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> x <span style="color: #339933;">*</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
_<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span>    <span style="color: #006600; font-style: italic;">// [1, 2, 3]</span>
  .<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span>double<span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// [2, 4, 6]</span>
  .<span style="color: #660066;">andSelf</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// [2, 4, 6, 1, 2, 3]</span></pre></div></div>

<p>But what else?  Well, consider this situation:  What if we wanted to modify only a subset of a zing?  For example, imagine we a list of monetary values and we wanted to make sure they all had the &#8220;$&#8221; character in front of them.  Let&#8217;s try and do that without using <code>merge()</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fn <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>e.<span style="color: #660066;">startsWith</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;$&quot;</span> <span style="color: #339933;">+</span> e<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
_<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$1.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$1.99&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;100.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$5&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That solution is OK.  It&#8217;s pretty simple but it still requires a lot of typing and it&#8217;s not really using Arrayzing for much (just the <code>map()</code> command).  Let&#8217;s see if we can reduce the number of keystrokes and make better use of Arrayzing&#8217;s capabilities by using the new <code>merge()</code> command:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$1.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$1.99&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;100.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$5&quot;</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">not</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\$/</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prepend</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So what just happened there?  What does it all mean?  Let&#8217;s look at that again, and track the contents of the zing:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$1.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$1.99&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;100.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$5&quot;</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">not</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\$/</span><span style="color: #009900;">&#41;</span>    <span style="color: #006600; font-style: italic;">// [ &quot;100.00&quot; ]</span>
  .<span style="color: #660066;">prepend</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// [ &quot;$100.00&quot; ]</span>
  .<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #006600; font-style: italic;">// [ &quot;$1.00&quot;, &quot;$1.99&quot;, &quot;$100.00&quot;, &quot;$5&quot; ]</span></pre></td></tr></table></div>

<p>In line 1, we create the Arrayzing (nothing special here).  In the following line, we filter out all strings that don&#8217;t begin with &#8220;$&#8221; using a regular expression.  Things get a little more interesting on line 3, where we use the mutator version of <code>prepend()</code> to modify the filtered array in-place.  Finally, on line 4, we use the <code>merge()</code> command which, basically puts the originally filtered monetary value back into the preceding zing.</p>
<p>So why did we use the <code>prepend$()</code> instead of <code>prepend()</code>?  The reason for that is because using <code>prepend()</code> would have created another level of history.  For example, here is what would happen if we had used <code>prepend()</code> instead of <code>prepend$()</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$1.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$1.99&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;100.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$5&quot;</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">not</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\$/</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">// [ &quot;100.00&quot; ]</span>
  .<span style="color: #660066;">prepend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// [ &quot;$100.00&quot; ]</span>
  .<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     <span style="color: #006600; font-style: italic;">// [ &quot;$100.00&quot; ]</span></pre></td></tr></table></div>

<p>Why did that happen?  The answer to that question lies in how <code>merge()</code> merges.  Let&#8217;s start by looking at line 2 of the last code snippet.  In that line, we filtered out all strings that did not start with a &#8220;$&#8221; character.  When Arrayzing returns a new zing that contains only the elements that start with a &#8220;$&#8221;, it also remembers where the new elements came from.  So in the case of the <code>not()</code> function on line 2, Arrayzing remembers that &#8220;100.00&#8243; came from index 2 of the zing in line 1 (that is, the zing _(&#8220;$1.00&#8243;, &#8220;$1.99&#8243;, &#8220;100.00&#8243;, &#8220;$5&#8243;)).  The <code>merge()</code> command only operates on the current zing and the one immediately previous to it on the stack.</p>
<p>With that in mind, let&#8217;s look at line 3.  On line 3, we run the <code>prepend()</code> command, which creates and returns a new zing that contains the value &#8220;$100.00&#8243;.  What&#8217;s more, it also remembers that the value &#8220;$100.00&#8243; was derived from &#8220;100.00&#8243; (index 0) in the zing immediately previous to it in the stack (that is, the zing _(&#8220;100.00&#8243;)).  Thus, when merged, it simply overwrites the value it derived from, which is the 0th index of the zing [ "100.00" ]&#8230; which results in the zing [&nbsp;"$100.00"&nbsp;].  You follow all that?</p>
<p>This concept is easier to understand by annotating the previous code snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> zing1 <span style="color: #339933;">=</span> _<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$1.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$1.99&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;100.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$5&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// [ &quot;$1.00&quot;, &quot;$1.99&quot;, &quot;100.00&quot;, &quot;$5&quot; ] index 0-3 come from nothing</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> zing2 <span style="color: #339933;">=</span> zing1.<span style="color: #660066;">not</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\$/</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// [ &quot;100.00&quot; ] index 0 comes from index 2 in zing1</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> zing3 <span style="color: #339933;">=</span> zing2.<span style="color: #660066;">prepend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// [ &quot;$100.00&quot; ] index 0 comes from index 0 in zing2</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> zing4 <span style="color: #339933;">=</span> zing3.<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// [ &quot;$100.00&quot; ] copy index 0 from zing3 over index 0 in zing2</span></pre></div></div>

<p>Now, with that more or less explained, let&#8217;s go on to one final item:  What happens when we try to merge a zing that we&#8217;ve added to or removed from.  For example, if we added elements like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;str&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">numbers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span>$<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What would be in the zing returned by this sequence of functions?  In this case, when <code>merge()</code> is called, Arrayzing would not know where to merge the 7 element (since it only remembers the original indices of elements that directly derived from the previous zing).  Arrayzing deals with this scenario by simply adding all elements that it does not have an index for to the end of the previous zing in the stack.  Here&#8217;s what that same snippet would look like if we documented each step:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;str&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">numbers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// [ 2, 3, 4 ]</span>
  .<span style="color: #660066;">add</span>$<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">// [ 2, 3, 4, 7 ]</span>
  .<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// [ &quot;str&quot;, 2, 3, 4, &quot;a&quot;, &quot;b&quot;, 7 ]</span></pre></div></div>

<p>How about if we removed elements?  If we were to remove elements, they would simply not be merged back into the previous zing on the stack.  For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;str&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">numbers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>    <span style="color: #006600; font-style: italic;">// [ 2, 3, 4 ]</span>
  .<span style="color: #660066;">set</span>$<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">// [ 4, 3, 4 ]</span>
  .<span style="color: #660066;">set</span>$<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">// [ 4, 3, 7 ]</span>
  .<span style="color: #660066;">removeAt</span>$<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// [ 3, 7 ]</span>
  .<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     <span style="color: #006600; font-style: italic;">// [ &quot;str&quot;, 2, 3, 7, &quot;a&quot;, &quot;b&quot; ]</span></pre></div></div>

<p>As you can see, the <code>merge()</code> command is a powerful addition to Arrayzing.  It allows us to select a subset of the elements, transform them, and then re-add them to our original zing.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/02/26/new-arrayzing-command-merge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arrayzing vs. Array: get() and set()</title>
		<link>http://cdmckay.org/blog/2009/02/15/arrayzing-vs-array-get-and-set/</link>
		<comments>http://cdmckay.org/blog/2009/02/15/arrayzing-vs-array-get-and-set/#comments</comments>
		<pubDate>Sun, 15 Feb 2009 22:49:11 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Arrayzing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[accessors]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[mutators]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=222</guid>
		<description><![CDATA[What does Arrayzing provide that a normal Array doesn&#8217;t? I hope to answer that question in my series of posts Arrayzing vs. Array. In today&#8217;s installment, we&#8217;ll take a look at what the get() and set() functions offer over an Array&#8217;s traditional [] accessor. When using plain old JavaScript Array objects, a common operation is [...]]]></description>
			<content:encoded><![CDATA[<p>What does Arrayzing provide that a normal Array doesn&#8217;t?  I hope to answer that question in my series of posts <em>Arrayzing vs. Array</em>.  In today&#8217;s installment, we&#8217;ll take a look at what the get() and set() functions offer over an Array&#8217;s traditional [] accessor.</p>
<p><span id="more-222"></span></p>
<p>When using plain old JavaScript Array objects, a common operation is to access or modify elements.  For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> sum <span style="color: #339933;">=</span> a<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> a<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> a<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// = 6</span>
a<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">;</span>
a.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;6,2,3&quot;</span></pre></div></div>

<p>Arrayzing also supports the built-in [] accessor.  However, it should be noted there are limitations to accessing an Array in this manner, most notable amongst them being that we must always index from the left.  For example, imagine we wanted to access the last element.  Using a normal Array, we would have to do this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
a<span style="color: #009900;">&#91;</span>a.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// a = [1, 2, 4];</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#91;</span>a.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// shows 4</span></pre></div></div>

<p>Although this works, it&#8217;s not the most convenient or elegant way to do it.  To address this problem, Arrayzing provides two new functions <strong>get()</strong> and <strong>set()</strong>.  These functions act essentially the same as the built-in [] accessors with an added benefit: we can index from the left <em>and</em> the right.  For example, to implement the above example using Arrayzing, we could instead do this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> _<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
a.<span style="color: #660066;">set</span>$<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// a = [1, 2, 4];</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>a.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// shows 4</span></pre></div></div>

<p><em>(Aside: If you&#8217;re not familiar with the $ after set$(), check out this <a href="http://cdmckay.org/blog/2009/02/15/arrayzing-design-choices/">article</a>.  It means that the function is a mutator).</em></p>
<p>What&#8217;s more, all Arrayzing commands that take an index as an argument allow right-indexing.  What convenience!</p>
<p>There is one further advantage of using set(): we can change an element in your zing (a zing is an Arrayzing array) without modifying the original array.  For example, if we wanted to replace an element in our zing but didn&#8217;t want to modify the original array, we would do this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> _<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> a.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// not set$()</span>
a.<span style="color: #660066;">str</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns &quot;1,2,3&quot;</span>
b.<span style="color: #660066;">str</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns &quot;10,2,3&quot;</span></pre></div></div>

<p>As you can see, Arrayzing provides a strict superset of the capabilities of a normal Array&#8217;s accessors, allowing you to write cleaner, easier code.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/02/15/arrayzing-vs-array-get-and-set/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

