<?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>Barryvan &#187; MooTools</title>
	<atom:link href="http://www.barryvan.com.au/category/programming/javascript/mootools/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.barryvan.com.au</link>
	<description>Music, Programming, Design</description>
	<lastBuildDate>Sun, 08 Jan 2012 08:48:12 +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>TrackPerformer update</title>
		<link>http://www.barryvan.com.au/2011/12/trackperformer-update/</link>
		<comments>http://www.barryvan.com.au/2011/12/trackperformer-update/#comments</comments>
		<pubDate>Sat, 17 Dec 2011 03:05:18 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Albums]]></category>
		<category><![CDATA[Announcements]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Music]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Web design]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=660</guid>
		<description><![CDATA[I&#8217;ve added in some really exciting new features to my TrackPerformer project, as well as three new performances: We Three Kings, Carol of the Bells, and Joy to the World. Filters It&#8217;s now possible to add filters, or effects, into the processing chain. These filters can be applied before any performers (pre-filters) or after all [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve added in some really exciting new features to my <a href="https://github.com/barryvan/trackPerformer">TrackPerformer</a> project, as well as three new performances: <strong><a href="http://barryvan.github.com/trackPerformer/kings.html">We Three Kings</a></strong>, <strong><a href="http://barryvan.github.com/trackPerformer/bells.html">Carol of the Bells</a></strong>, and <strong><a href="http://barryvan.github.com/trackPerformer/joy.html">Joy to the World</a></strong>.</p>
<h2>Filters</h2>
<p>It&#8217;s now possible to add filters, or effects, into the processing chain. These filters can be applied before any performers (pre-filters) or after all performers (post-filters). At the moment, the included filters are:</p>
<ul>
<li>FPS: Calculates the average framerate across the performance, optionally displaying it in a DOM element.</li>
<li>Grid: Draws a grid to the canvas. The grid may be a simple intersection grid (points), or lines. Both the X and Y axes may be independently configured.</li>
<li>Pick: Probably the most interesting (and processor-intensive) part of TrackPerformer. The Pick filter will randomly swap a pixel with one of its neighbours. It&#8217;s used at full intensity on both <a href="http://barryvan.github.com/trackPerformer/kings.html">We three Kings</a> and <a href="http://barryvan.github.com/trackPerformer/joy.html">Joy to the World</a>, and, when toned down a little for <a href="http://barryvan.github.com/trackPerformer/bells.html">Carol of the Bells</a>, provides a softening, organic effect.</li>
</ul>
<p>I&#8217;ve got some ideas for more filters down the track&#8230; The only difficulty is keeping performance acceptable: manipulation of the canvas pixel by pixel is quite slow in current browsers.</p>
<h2>Performers</h2>
<p>There are a couple of new performers, and some minor updates to some of the existing ones. The Oscillator performer, in particular, is rapidly becoming the most flexible and useful of the performers.</p>
<ul>
<li>There&#8217;s a new ShimmerGrid performer, which is great for adding texture and movement to the entire canvas. You can see it in action particularly well on <a href="http://barryvan.github.com/trackPerformer/joy.html">Joy to the World</a>.</li>
<li>The Swarm performer can now draw its particles as knots (like the SignalTracker), as well as as dots.</li>
<li>The Oscillator now has the ability to draw sustained notes, and to increase the longevity of notes. Take a look at <a href="http://barryvan.github.com/trackPerformer/bells.html">Carol of the Bells</a> to see these new options in use.</li>
<li>Notes can now be filtered based not only on their pitch, but also their velocity (volume).</li>
</ul>
<p>There are a couple of other changes here and there, but these are the main ones.</p>
<h2>We Three Kings</h2>
<p>The three new example tracks are all taken from <a href="http://barryvan.bandcamp.com/album/we-three-kings">We Three Kings</a>, my new Christmas remix album. Why not go and have a listen?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2011/12/trackperformer-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TrackPerformer</title>
		<link>http://www.barryvan.com.au/2011/10/trackperformer/</link>
		<comments>http://www.barryvan.com.au/2011/10/trackperformer/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 13:05:15 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Free music downloads]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Music]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=645</guid>
		<description><![CDATA[TrackPerformer provides a visual stage for your music, using HTML5 canvas and audio. On that stage, performers &#8220;play&#8221; the instruments in the music visually. In other words, it&#8217;s a visualisation system for music, but based on the notation (the abstract) rather than the audio (the manifestation). Essentially, you take a piece of music, convert it [...]]]></description>
			<content:encoded><![CDATA[<p>TrackPerformer provides a visual stage for your music, using HTML5 canvas and audio. On that stage, performers &#8220;play&#8221; the instruments in the music visually. In other words, it&#8217;s a visualisation system for music, but based on the notation (the abstract) rather than the audio (the manifestation).</p>
<p>Essentially, you take a piece of music, convert it into a format that TrackPerformer understands (JSON), describe how you want it to be performed, and then watch! You can, of course, write your own performers.</p>
<div style="padding:8px;margin:0 24px;background:rgba(255,255,255,0.25);border:solid 1px;-moz-border-radius:4px;-webkit-border-radius:4px;border-radius:4px;">Before going any further, let&#8217;s <a href="http://barryvan.github.com/trackPerformer/colony.html">see it in action</a>. The music is &#8220;Colony&#8221;, a new piece that I wrote about a week ago.</div>
<p>Note: You won&#8217;t be able to view the performance linked above in Internet Explorer, due to its over-aggressive script-blocking: the scripts served from GitHub have the wrong mime-type, so IE won&#8217;t let them run.</p>
<p><a href="https://github.com/barryvan/trackPerformer">Take a look at the project on GitHub</a> to see how it all fits together. TrackPerformer itself resides in the &#8220;Source&#8221; directory; in &#8220;Examples&#8221;, you&#8217;ll find <a href="http://barryvan.github.com/trackPerformer/colony.html">the performance of Colony</a>; in &#8220;Utilities&#8221;, there&#8217;s a JavaScript macro for Komodo IDE/Edit that will help you to translate copied-and-pasted OpenMPT pattern data into TrackPerformer&#8217;s JSON format.</p>
<p>You can find more information on <a href="https://github.com/barryvan/trackPerformer/wiki">the TrackPerformer wiki</a>, including an outline of the format, and some basic instructions for getting started. I&#8217;ll be adding more information to the wiki over the next few days, and I&#8217;ll post updates here too.</p>
<p>Let me know what you think!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2011/10/trackperformer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Paired sort in JavaScript</title>
		<link>http://www.barryvan.com.au/2010/08/paired-sort-in-javascript/</link>
		<comments>http://www.barryvan.com.au/2010/08/paired-sort-in-javascript/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 01:42:06 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=467</guid>
		<description><![CDATA[Sometimes, you&#8217;ll have two arrays of associated data, and you&#8217;ll need to sort them. You can&#8217;t just call sort() on both arrays, because that will potentially break the associations between them. What you need is a way to sort one of the arrays, and shuffle the elements of the second array to match. Here&#8217;s a [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, you&#8217;ll have two arrays of associated data, and you&#8217;ll need to sort them. You can&#8217;t just call sort() on both arrays, because that will potentially break the associations between them. What you need is a way to sort one of the arrays, and shuffle the elements of the second array to match. Here&#8217;s a simple MooTools function that does just that (using quicksort):</p>
<pre class="brush: javascript">Array.implement({
	pairedSort: function(other, reverse) {
		if (this.length === other.length) {
			for (var i = 0, len = this.length; i &lt; len; i++) {
				var curr = this[i];
				var currOth = other[i];
				var j = i - 1;
				while ((j &gt;= 0) &amp;&amp; (this[j] &gt; curr)) {
					this[j + 1] = this[j];
					other[j + 1] = other[j];
					j--;
				}
				this[j + 1] = curr;
				other[j + 1] = currOth;
			}
		}
		if (reverse) {
			this.reverse();
			other.reverse();
		}
		return this;
	}
});</pre>
<p>Using this function is quite straightforward:</p>
<pre class="brush: javascript">var alpha = [3,2,1,6,5,4];
var beta = [1,2,3,4,5,6];
alpha.pairedSort(beta);
// alpha == [1,2,3,4,5,6]
// beta == [3,2,1,6,5,4]</pre>
<p>When would this actually be useful? Let&#8217;s say you&#8217;ve got an object which maps numeric assessment scores to an alphabetic grade:</p>
<pre class="brush: javascript">var mapping = {
  &#039;A&#039;: 80,
  &#039;B&#039;: 60,
  &#039;C&#039;: 40,
  &#039;D&#039;: 20,
  &#039;E&#039;: 0
}</pre>
<p>Unfortunately, objects in JavaScript have no intrinsic order on their keys &#8212; this is because they&#8217;re essentially just hashmaps. What we need to do to be able to make use of these data, then, is create an array of keys, and one of values, and then sort them. The pairedSort() function allows us to do this easily. (In fact, it&#8217;s for this exact application that I wrote the method!)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2010/08/paired-sort-in-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Generative Music</title>
		<link>http://www.barryvan.com.au/2010/03/generative-music/</link>
		<comments>http://www.barryvan.com.au/2010/03/generative-music/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 08:08:11 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Ambient]]></category>
		<category><![CDATA[Chillout]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Music]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Web design]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=443</guid>
		<description><![CDATA[Continuing my HTML5 and canvas experiments, I&#8217;ve put together a generative music system. Essentially, a series of particles move across a field, occasionally triggering sounds &#8212; the sound triggered depends on their location in the field. There is, of course, a little bit more to it than that. Under the hood, I&#8217;ve got a series [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing my HTML5 and canvas experiments, I&#8217;ve put together a generative music system. Essentially, a series of particles move across a field, occasionally triggering sounds &#8212; the sound triggered depends on their location in the field.</p>
<p>There is, of course, a little bit more to it than that. Under the hood, I&#8217;ve got a series of HTML5 Audio objects that are used to provide polyphonic audio using a simple round-robin algorithm (I encoded the audio in OGG, so you&#8217;ll need to use an OGG-friendly browser, like <a href="http://www.getfirefox.com">Firefox</a>). The particles are much simpler than those in my <a href="/2010/03/canvas-swarms/">previous canvas dalliance</a>, in that they don&#8217;t swarm, and their motion is more linear.</p>
<div class="screenshots">
<a href="/demos/generative.html"><br/><br />
<img alt="Generative Audio Screenshot" src="/wp-content/uploads/2010/03/generative.png" class="aligncenter size-full"/><br/><br />
View (and hear!) it live<br/><br />
</a>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2010/03/generative-music/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Canvas Swarms</title>
		<link>http://www.barryvan.com.au/2010/03/canvas-swarms/</link>
		<comments>http://www.barryvan.com.au/2010/03/canvas-swarms/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 00:27:18 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Processing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web design]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=440</guid>
		<description><![CDATA[I&#8217;ve been meaning to start playing with the HTML5 &#60;canvas&#62; element for a while now, and yesterday I took the opportunity. I translated a Processing sketch I made a while ago into JavaScript (with a few minor enhancements). View it live Essentially, 1 to 3 swarms of particles move around the canvas, reproducing when the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been meaning to start playing with the HTML5 &lt;canvas&gt; element for a while now, and yesterday I took the opportunity. I translated a Processing sketch I made a while ago into JavaScript (with a few minor enhancements).</p>
<div class="screenshots">
<a href="/demos/swarms/swarms.html"><br />
<img class="aligncenter size-full" src="/demos/swarms/swarms.png" alt="Swarms Screenshot"/><br />
View it live<br />
</a>
</div>
<p>Essentially, 1 to 3 swarms of particles move around the canvas, reproducing when the conditions are just right, and dying of old age. Quite simple, but the patterns produced can be really quite pretty.</p>
<p>One interesting thing that I discovered whilst doing this is that you can&#8217;t pass around a canvas&#8217; context at the instantiation of a MooTools class &#8212; it complains about wrapped natives. That&#8217;s why, if you like in the source JavaScript, you&#8217;ll see me pass the actual context around to various functions. I&#8217;d be interested in hearing if anyone has a workaround for this, because this is, well, a bit clunky.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2010/03/canvas-swarms/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Javascript: Print a single element</title>
		<link>http://www.barryvan.com.au/2010/02/javascript-print-a-single-element/</link>
		<comments>http://www.barryvan.com.au/2010/02/javascript-print-a-single-element/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 08:18:47 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web design]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=434</guid>
		<description><![CDATA[Sometimes, you&#8217;ll want to allow users the ability to print only a part of your page; for example, a table but not the various links around the page. It&#8217;s possible to use a printing stylesheet, but this can cause severe headaches when you need different parts printed at different times. Really, we want to be [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, you&#8217;ll want to allow users the ability to print only a part of your page; for example, a table but not the various links around the page. It&#8217;s possible to use a printing stylesheet, but this can cause severe headaches when you need different parts printed at different times. Really, we want to be able to just say <strong>element.printElement()</strong>, and have it just work. That&#8217;s what the MooTools function below does. It&#8217;s loosely based around the concepts outlined at <a href="http://www.bennadel.com/blog/1591-Ask-Ben-Print-Part-Of-A-Web-Page-With-jQuery.htm">this website</a>.</p>
<pre class="brush: javascript">Element.implement({
	printElement: function(docTitle) {
		var strName = &#039;printer-&#039; + (new Date()).getTime();
		var styles = [];
		$$(&#039;link[type=text/css]&#039;).each(function(style) {
			styles.push(&#039;&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;&#039; + style.get(&#039;href&#039;) + &#039;&quot;/&gt;&#039;);
		});
		var title = docTitle || document.title;
		var that = this.getParent();
		var iframe = new IFrame({
			name: strName,
			styles: {
				width: 1,
				height: 1,
				position: &#039;absolute&#039;,
				left: -9999
			},
			events: {
				load: function() {
					var doc = window.frames[strName].document,
							win = window.frames[strName],
					f = function() {
						if (!doc.head) {
							f.delay(10);
							return;
						}

						// We need to delay printing so that styles are applied.
						(function() {
							doc.title = title;
							// IE7 won&#039;t let us adopt() here for some reason, and we can&#039;t do anything to the head.
							doc.body.innerHTML = styles.join(&#039;&#039;) + that.innerHTML;
							(function() {
								win.focus(); // IE needs the window to be focused.
								win.print();
							}).delay(100);
						}).delay(200);
					};
					f();
				}
			}
		}).inject($(document.body));
		(function() {
			iframe.dispose();
		}).delay(30000);
	}
});</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2010/02/javascript-print-a-single-element/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Limiting the contents of a string via RegEx</title>
		<link>http://www.barryvan.com.au/2009/12/limiting-the-contents-of-a-string-via-regex/</link>
		<comments>http://www.barryvan.com.au/2009/12/limiting-the-contents-of-a-string-via-regex/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 08:10:49 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=332</guid>
		<description><![CDATA[Often, you will need to prevent users from entering data that doesn&#8217;t conform to a specific pattern. For example, you may want to allow users to enter only numbers or only valid email addresses. To this end, I&#8217;ve written a little utility function that returns the &#8220;standardised&#8221; version of a string, according to the regex [...]]]></description>
			<content:encoded><![CDATA[<p>Often, you will need to prevent users from entering data that doesn&#8217;t conform to a specific pattern. For example, you may want to allow users to enter only numbers or only valid email addresses. To this end, I&#8217;ve written a little utility function that returns the &#8220;standardised&#8221; version of a string, according to the regex you supply.</p>
<pre class="brush: javascript">String.implement({
	limitContent: function(allowedRegex) {
		return $splat(this.match(allowedRegex)).join(&#039;&#039;);
	}
});</pre>
<p>Basically, the function takes the result of evaluating the regular expression on the string, converts it into an array if it isn&#8217;t one, and then joins the array&#8217;s elements together with an empty string.</p>
<h2>Examples:</h2>
<pre class="brush: javascript">console.log(&quot;12345&quot;.limitContent(/.{4}/)); // Only allow four characters
console.log(&quot;joe@mail.com&quot;.limitContent(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/)); // Only allow email addresses
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/12/limiting-the-contents-of-a-string-via-regex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MooTools object messaging</title>
		<link>http://www.barryvan.com.au/2009/10/mootools-object-messaging/</link>
		<comments>http://www.barryvan.com.au/2009/10/mootools-object-messaging/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 08:19:08 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=280</guid>
		<description><![CDATA[Events In JavaScript, we often tend to use events all over the place. In MooTools, the custom &#8216;domready&#8217; event is particularly prevalent. However, events suffer from a few drawbacks: You can&#8217;t attach events to non-existent objects. Pretty self-explanatory, really. What this means in practice, though, is that you can&#8217;t easily let object A know when [...]]]></description>
			<content:encoded><![CDATA[<h2>Events</h2>
<p>In JavaScript, we often tend to use events all over the place. In MooTools, the custom &#8216;domready&#8217; event is particularly prevalent. However, events suffer from a few drawbacks:</p>
<h3>You can&#8217;t attach events to non-existent objects.</h3>
<p>Pretty self-explanatory, really. What this means in practice, though, is that you can&#8217;t easily let object A know when object B exists.</p>
<h3>If an object starts to listen for an event after it&#8217;s already fired, it&#8217;ll never hear it.</h3>
<p>Because content on the web isn&#8217;t always delivered in perfect order (especially when you&#8217;re loading scripts synchronously), it&#8217;s possible for an object to try to listen for an event after it&#8217;s already been fired. Obviously, this means that your listener object will never run the code that&#8217;s dependent on that event, which could be Bad Thing™.</p>
<h3>It&#8217;s not particularly easy to know which object is listening for which events.</h3>
<p>There are ways around this, but you can&#8217;t just dir() the listeners in Firebug.</p>
<h2>A messaging system</h2>
<p>For all of these reasons (and probably a few more that I&#8217;ve forgotten about), a messaging system can be an invaluable addition to your arsenal when writing JavaScript. How does a messaging system work? Well, interested objects &#8216;register&#8217; themselves as listeners for particular message &#8216;handles&#8217;, and other objects can send messages using those &#8216;handles&#8217;. Below is a very simple MooTools messaging system that I knocked up, which has a few cool features, including:</p>
<ul>
<li>When you register() a listener, you can have its callback immediately fire if that message has ever been sent before.
<li>
<li>You can very easily see which callbacks are associated with which messages by simply dir()-ing the &#8216;listeners&#8217; member.</li>
<li>You can unregister() a listener at any time (provided you&#8217;ve got a reference to the function and the handle).</li>
<li>Handles can be any valid JavaScript type &#8212; Strings, Numbers, even Objects.</li>
</ul>
<p>Feel free to use and extend this system &#8212; as I mentioned, this is a <strong>very</strong> simple system. If you do extend it, let me know in the comments!</p>
<p>Let me know what you think about this system in the comments.</p>
<pre class="brush: javascript">
barryvan.base.Messaging = new Class({
	listeners: $H(),
	sentMessages: [],

	initialize: function() {

	},

	/**
	 * Register a listener for a particular handle.
	 * handle [String]: The message &#039;handle&#039; to listen for.
	 * callback [Function]: The function to be called when the handle is sent a message. The contents of the messages will be included in the function call.
	 * dontCheck [Boolean]: If falsey and the handle has had a message sent to it, immediately call the callback function (without contents), and continue to add the listener as normal.
	 */
	register: function(handle, callback, dontCheck) {
		if ($type(callback) !== &#039;function&#039;) return;

		if (!dontCheck &amp;&amp; this.sentMessages[handle]) {
			callback();
		}

		if (!this.listeners.has(handle)) this.listeners[handle] = [];
		this.listeners[handle].push(callback);
	},

	/**
	 * Unregister a listener for a particular handle.
	 * handle [String]: The message &#039;handle&#039; to cease listening for.
	 * callback [Function]: The function which was earlier assigned as the callback for the messages.
	 */
	unregister: function(handle, callback) {
		if (this.listeners.has(handle)) {
			this.listeners[handle].erase(callback);
		}
	},

	/**
	 * Send a message to the given handle with the given contents -- send the contents to all the registered listeners for that handle.
	 * handle [String]: The message &#039;handle&#039; to transmit to.
	 * contents [Mixed]: The contents to be sent to the listeners.
	 */
	send: function(handle, contents) {
		this.sentMessages.include(handle);
		if (this.listeners.has(handle)) {
			this.listeners[handle].each(function(callback) {
				callback(contents);
			});
		}
	}
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/10/mootools-object-messaging/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Quicksort an array of objects</title>
		<link>http://www.barryvan.com.au/2009/08/quicksort-an-array-of-objects/</link>
		<comments>http://www.barryvan.com.au/2009/08/quicksort-an-array-of-objects/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 07:14:51 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=225</guid>
		<description><![CDATA[Often, you will need to sort an array of objects in Javascript. The inbuilt sort() function can&#8217;t do this, but here is a Quicksort implementation for doing just this. Parameters array The array to be sorted. (See below for an implementation on the Array Native itself, which makes this variable unnecessary). key The key to [...]]]></description>
			<content:encoded><![CDATA[<p>Often, you will need to sort an array of objects in Javascript. The inbuilt <em>sort()</em> function can&#8217;t do this, but here is a Quicksort implementation for doing just this.</p>
<h2>Parameters</h2>
<p><strong>array</strong> The array to be sorted. (See below for an implementation on the Array Native itself, which makes this variable unnecessary).</p>
<p><strong>key</strong> The key to sort by. Make sure every object in your array has this key.</p>
<h2>Examples</h2>
<pre class="brush: javascript">
var objs = [
	{fruit:&quot;cherry&quot;},
	{fruit:&quot;apple&quot;},
	{fruit:&quot;banana&quot;}
];

console.log(objs.sortObjects(&#039;fruit&#039;));
// Logs [{fruit:&quot;apple&quot;},{fruit:&quot;banana&quot;},{fruit:&quot;cherry&quot;}] to the console
</pre>
<h2>The code</h2>
<pre class="brush: javascript">
sortObjects: function(array, key) {
	for (var i = 0; i &lt; array.length; i++) {
		var currVal = array[i][key];
		var currElem = array[i];
		var j = i - 1;
		while ((j &gt;= 0) &amp;&amp; (array[j][key] &gt; currVal)) {
			array[j + 1] = array[j];
			j--;
		}
		array[j + 1] = currElem;
	}
}
</pre>
<h3>Implemented on the Array native:</h3>
<pre class="brush: javascript">
Array.implement({
	sortObjects: function(key) {
		for (var i = 0; i &lt; this.length; i++) {
			var currVal = this[i][key];
			var currElem = this[i];
			var j = i - 1;
			while ((j &gt;= 0) &amp;&amp; (this[j][key] &gt; currVal)) {
				this[j + 1] = this[j];
				j--;
			}
			this[j + 1] = currElem;
		}
	}
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/08/quicksort-an-array-of-objects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Javascript string ellipsising</title>
		<link>http://www.barryvan.com.au/2009/08/javascript-string-ellipsising/</link>
		<comments>http://www.barryvan.com.au/2009/08/javascript-string-ellipsising/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 07:03:17 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[string manipulation]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=217</guid>
		<description><![CDATA[Putting ellipses into strings that are too long has been around for a very long time. Unfortunately, Javascript doesn&#8217;t offer a native method of doing this, so below is a little function that&#8217;ll do it for you. This function returns a copy of the string it&#8217;s called on, ellipsised, and takes three parameters: toLength (required) [...]]]></description>
			<content:encoded><![CDATA[<p>Putting ellipses into strings that are too long has been around for a very long time. Unfortunately, Javascript doesn&#8217;t offer a native method of doing this, so below is a little function that&#8217;ll do it for you.</p>
<p>This function returns a copy of the string it&#8217;s called on, ellipsised, and takes three parameters:</p>
<p><strong>toLength</strong> (required) The number of characters to truncate the string to (or 0 to disable ellipsising)</p>
<p><strong>where</strong> (optional, default &#8216;end&#8217;) A string representing where the ellipsis should be placed &#8212; &#8216;front&#8217;, &#8216;middle&#8217;, or &#8216;end&#8217;</p>
<p><strong>ellipsis</strong> (option, default &#8216;\u2026&#8242;) A string to be used as the ellipsis.</p>
<h2>Examples</h2>
<pre class="brush: javascript">
// Our clichéd string
var s = &#039;Jackdaws love my great big sphinx of quartz&#039;;

alert(s.ellipsise(10));
// Alerts &quot;Jackdaws l…&quot;

alert(s.ellipsise(10, &#039;front&#039;));
// Alerts &quot;… of quartz&quot;

alert(s.ellipsise(10, &#039;middle&#039;, &#039;pony&#039;));
// Alerts &quot;Jackdponyuartz&quot;</pre>
<h2>The code</h2>
<pre class="brush: javascript">String.implement({
	ellipsise: function(toLength, where, ellipsis) { // Where is one of [&#039;front&#039;,&#039;middle&#039;,&#039;end&#039;] -- default is &#039;end&#039;
		if (toLength &lt; 1) return this;
		ellipsis = ellipsis || &#039;\u2026&#039;;
		if (this.length &lt; toLength) return this;
		switch (where) {
			case &#039;front&#039;:
				return ellipsis + this.substr(this.length - toLength);
				break;
			case &#039;middle&#039;:
				return this.substr(0, toLength / 2) + ellipsis + this.substr(this.length - toLength / 2)
				break;
			case &#039;end&#039;:
			default:
				return this.substr(0, toLength) + ellipsis;
				break;
		}
	}
});</pre>
<p>If you&#8217;re not using MooTools, you can use this variant instead:</p>
<pre class="brush: javascript">String.prototype.ellipsise = function(toLength, where, ellipsis) { // Where is one of [&#039;front&#039;,&#039;middle&#039;,&#039;end&#039;] -- default is &#039;end&#039;
	if (toLength &lt; 1) return this;
	ellipsis = ellipsis || &#039;\u2026&#039;;
	if (this.length &lt; toLength) return this;
	switch (where) {
		case &#039;front&#039;:
			return ellipsis + this.substr(this.length - toLength);
			break;
		case &#039;middle&#039;:
			return this.substr(0, toLength / 2) + ellipsis + this.substr(this.length - toLength / 2)
			break;
		case &#039;end&#039;:
		default:
			return this.substr(0, toLength) + ellipsis;
			break;
	}
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/08/javascript-string-ellipsising/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

