<?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>Chris Miller &#187; Coding</title>
	<atom:link href="http://chris-miller.org/archives/category/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://chris-miller.org</link>
	<description>Life, and how to live it!</description>
	<lastBuildDate>Tue, 24 Jan 2012 14:41:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Get link to currently playing Spotify track via AppleScript</title>
		<link>http://chris-miller.org/archives/2011/10/05/get-link-to-currently-playing-spotify-track-via-applescript/</link>
		<comments>http://chris-miller.org/archives/2011/10/05/get-link-to-currently-playing-spotify-track-via-applescript/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 12:55:01 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[applescript]]></category>
		<category><![CDATA[spotify]]></category>
		<category><![CDATA[track]]></category>

		<guid isPermaLink="false">http://chris-miller.org/?p=3152</guid>
		<description><![CDATA[I was looking around for a means to grab the URL of the currently playing track in Spotify earlier, but couldn&#8217;t find anything. I rattled together an AppleScript to do just that and copy the result, including the track and artist name, to your clipboard. &#8220;Feel Like Makin&#8217; Love &#8211; Bad Company: http://open.spotify.com/track/3HKthdb7Ejnydb74BvmQW0&#8221; I used [...]]]></description>
			<content:encoded><![CDATA[<p>I was looking around for a means to grab the URL of the currently playing track in <a href="http://spotify.com" title="Spotify">Spotify</a> earlier, but couldn&#8217;t find anything.</p>
<p>I rattled together an AppleScript to do just that and copy the result, including the track and artist name, to your clipboard.</p>
<pre class="brush: applescript; title: ; notranslate">
tell application &quot;Spotify&quot;
	set theTrack to name of the current track
	set theArtist to artist of the current track
	set theAlbum to album of the current track
	set track_id to id of current track
end tell

set AppleScript's text item delimiters to &quot;:&quot;
set track_id to third text item of track_id
set AppleScript's text item delimiters to {&quot;&quot;}
set realurl to (&quot;http://open.spotify.com/track/&quot; &amp; track_id)

set theString to theTrack &amp; &quot; - &quot; &amp; theArtist &amp; &quot;: &quot; &amp; realurl
set the clipboard to theString
</pre>
<p>&#8220;Feel Like Makin&#8217; Love &#8211; Bad Company: <a href="http://open.spotify.com/track/3HKthdb7Ejnydb74BvmQW0" title="Feel Like Makin' Love - Bad Company" target="_blank">http://open.spotify.com/track/3HKthdb7Ejnydb74BvmQW0</a>&#8221;</p>
<p>I used a few bits of <a href="http://conceitedsoftware.com/forums/topic/spotify-script" title="Conceited Software forum">this Ceonceited Software forum post</a> for pointers on accessing Spotify details via AppleScript.</p>
<p>Enjoy,<br />
- Chris</p>
]]></content:encoded>
			<wfw:commentRss>http://chris-miller.org/archives/2011/10/05/get-link-to-currently-playing-spotify-track-via-applescript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Pretty UK Vehicle registration years</title>
		<link>http://chris-miller.org/archives/2010/10/06/pretty-uk-vehicle-registration-years-2/</link>
		<comments>http://chris-miller.org/archives/2010/10/06/pretty-uk-vehicle-registration-years-2/#comments</comments>
		<pubDate>Wed, 06 Oct 2010 11:34:36 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[pretty]]></category>
		<category><![CDATA[reg]]></category>
		<category><![CDATA[registration]]></category>
		<category><![CDATA[uk]]></category>

		<guid isPermaLink="false">http://chris-miller.org/?p=521</guid>
		<description><![CDATA[If you&#8217;ve ever wanted to convert a date of a UK vehicle into a nice looking representation of the year code then you&#8217;ve came to the right place! I&#8217;ve written a small PHP function to convert a given date to one of the following formats: Dates before Feb 1963 [YEAR] i.e. &#8220;1960&#8243;, &#8220;1962&#8243; Dates from [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever wanted to convert a date of a UK vehicle into a nice looking representation of the year code then you&#8217;ve came to the right place!</p>
<p>I&#8217;ve written a small PHP function to convert a given date to one of the following formats:</p>
<ul>
<li>
		Dates before Feb 1963</p>
<ul>
<li>
				[YEAR]
			</li>
<li>
				i.e. &#8220;1960&#8243;, &#8220;1962&#8243;
			</li>
</ul>
</li>
<li>
		Dates from Feb 1963 &#8211; August 2001</p>
<ul>
<li>
				[SUFFIX/PREFIX LETTER] ([YEAR])
			</li>
<li>
				i.e. &#8220;V (1999)&#8221;, &#8220;A (1983)&#8221;
			</li>
</ul>
</li>
<li>
		Dates from September 2001 onwards</p>
<ul>
<li>
				[AGE IDENTIFIER] ([YEAR])
			</li>
<li>
				i.e. &#8220;57 (2007)&#8221;, &#8220;57 (2008)&#8221;, &#8220;60 (2010)&#8221;
			</li>
</ul>
</li>
</ul>
<p><span id="more-521"></span></p>
<pre class="php" name="code" id="pretty-uk-vehicle-registration-years">
/* getRegistrationCode *********************************************
 * Get the age identifier from a UK vehicle registration plate from
 * a given date.  Returns the identifier as a string with the year
 * of registration in brackets.
 *
 * $registrationDate    The date of registration to be converted
 *
 * return                String representation of the registration
 *                         date
 ******************************************************************/
function getRegistrationCode( $registrationDate ) {
    $reg      = $registrationDate;

    $year     = (int) date( "Y", strtotime( $registrationDate ) );
    $month    = (int) date( "n", strtotime( $registrationDate ) );
    $date     = date( "Y-m-d", strtotime( $regestrationDate ) );

    /* < FEBRUARY 1963 *********************************************
     * If the registration year is 1962 or before then just return
     * the year
     **************************************************************/
    if( $date < "1963-02-01" ) {

        $reg    = "".$year;

    /* FEBRUARY 1963 - AUGUST 2001 *********************************
     * For registrations from 1963 to August 2001 look up the
     * appropriate registration letter
     **************************************************************/
    } elseif( $date <= "2001-08-31" ) {

        $yearLetters    = array(
            "1963-02-01"    => 'A',    // 1 Feb 1963 – 31 Dec 1963
            "1964-01-01"    => 'B',    // 1 Jan 1964 – 31 Dec 1964
            "1965-01-01"    => 'C',    // 1 Jan 1965 – 31 Dec 1965
            "1966-01-01"    => 'D',    // 1 Jan 1966 – 31 Dec 1966
            "1967-01-01"    => 'E',    // 1 Jan 1967 – 31 Jul 1967
            "1967-08-01"    => 'F',    // 1 Aug 1967 – 31 Jul 1968
            "1968-08-01"    => 'G',    // 1 Aug 1968 – 31 Jul 1969
            "1969-08-01"    => 'H',    // 1 Aug 1969 – 31 Jul 1970
            "1970-08-01"    => 'J',    // 1 Aug 1970 – 31 Jul 1971
            "1971-08-01"    => 'K',    // 1 Aug 1971 – 31 Jul 1972
            "1972-08-01"    => 'L',    // 1 Aug 1972 – 31 Jul 1973
            "1973-08-01"    => 'M',    // 1 Aug 1973 – 31 Jul 1974
            "1974-08-01"    => 'N',    // 1 Aug 1974 – 31 Jul 1975
            "1975-08-01"    => 'P',    // 1 Aug 1975 – 31 Jul 1976
            "1976-08-01"    => 'R',    // 1 Aug 1976 – 31 Jul 1977
            "1977-08-01"    => 'S',    // 1 Aug 1977 – 31 Jul 1978
            "1978-08-01"    => 'T',    // 1 Aug 1978 – 31 Jul 1979
            "1979-08-01"    => 'V',    // 1 Aug 1979 – 31 Jul 1980
            "1980-08-01"    => 'W',    // 1 Aug 1980 – 31 Jul 1981
            "1981-08-01"    => 'X',    // 1 Aug 1981 – 31 Jul 1982
            "1982-08-01"    => 'Y',    // 1 Aug 1982 – 31 Jul 1983
            "1983-08-01"    => 'A',    // 1 Aug 1983 – 31 Jul 1984
            "1984-08-01"    => 'B',    // 1 Aug 1984 – 31 Jul 1985
            "1985-08-01"    => 'C',    // 1 Aug 1985 – 31 Jul 1986
            "1986-08-01"    => 'D',    // 1 Aug 1986 – 31 Jul 1987
            "1987-08-01"    => 'E',    // 1 Aug 1987 – 31 Jul 1988
            "1988-08-01"    => 'F',    // 1 Aug 1988 – 31 Jul 1989
            "1989-08-01"    => 'G',    // 1 Aug 1989 – 31 Jul 1990
            "1990-08-01"    => 'H',    // 1 Aug 1990 – 31 Jul 1991
            "1991-08-01"    => 'J',    // 1 Aug 1991 – 31 Jul 1992
            "1992-08-01"    => 'K',    // 1 Aug 1992 – 31 Jul 1993
            "1993-08-01"    => 'L',    // 1 Aug 1993 – 31 Jul 1994
            "1994-08-01"    => 'M',    // 1 Aug 1994 – 31 Jul 1995
            "1995-08-01"    => 'N',    // 1 Aug 1995 – 31 Jul 1996
            "1996-08-01"    => 'P',    // 1 Aug 1996 – 31 Jul 1997
            "1997-08-01"    => 'R',    // 1 Aug 1997 – 31 Jul 1998
            "1998-08-01"    => 'S',    // 1 Aug 1998 – 28 Feb 1999
            "1999-03-01"    => 'T',    // 1 Mar 1999 – 31 Aug 1999
            "1999-09-01"    => 'V',    // 1 Sep 1999 – 29 Feb 2000
            "2000-03-01"    => 'W',    // 1 Mar 2000 – 31 Aug 2000
            "2000-09-01"    => 'X',    // 1 Sep 2000 – 28 Feb 2001
            "2001-03-01"    => 'Y',    // 1 Mar 2001 – 31 Aug 2001
        );

        // Loop through the letters until we hit a date that's after
        // our registration date
        foreach( $yearLetters as $key => $letter ) {
            $reg    = $letter." (".$year.")";
            if( $key > $date )
                break;
        }

    /* SEPTEMBER 2001 ONWARDS **************************************
     * For registrations since September 2001 use the current
     * 00 - 99 numbering scheme based on the year.
     **************************************************************/
    } else {

        /* CALCULATE THE REG CODE **********************************
         * New style registration years run from the start of March
         * to the end of February of the next year.
         *
         * The two digit year value represents the March - August
         * identifier (i.e. 09, 10) and 50 is added to the year for
         * the September - February identifier (i.e. 59, 60).
         *
         * To calculate the registration code we must follow the
         * steps below:
         *     - YEAR % 50
         *     - if MONTH < 3 subtract 1
         *     - Add 50 if MONTH > 8 or MONTH < 3
         *
         * Giving us:
         * ( YEAR % 50 ) + ( ( MONTH < 3 ) ? 49 : ( MONTH > 8 ) ? 50 : 0 )
         */
        $reg = ( $year % 50 ) +
        		( ( $month < 3 ) ? 49 : ( ( $month > 8 ) ? 50 : 0 ) );
        $reg = str_pad( $reg, 2, "0", STR_PAD_LEFT )." (".$year.")";

    }

    return $reg;
}
</pre>
<p>The <a href="#pretty-uk-vehicle-registration-years" title="View the code">function above</a> should convert all dates since 1963 to codes.  It should also hold true for the future as the current partern is a 50 year repeating one (lest they change it of course).<br />
- Chris</p>
]]></content:encoded>
			<wfw:commentRss>http://chris-miller.org/archives/2010/10/06/pretty-uk-vehicle-registration-years-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting and Setting anchors with JavaScript</title>
		<link>http://chris-miller.org/archives/2010/04/09/getting-and-setting-anchors-with-javascript/</link>
		<comments>http://chris-miller.org/archives/2010/04/09/getting-and-setting-anchors-with-javascript/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 10:10:49 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[anchor]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JS]]></category>
		<category><![CDATA[URL]]></category>

		<guid isPermaLink="false">http://chris-miller.org/?p=504</guid>
		<description><![CDATA[Something I&#8217;ve been playing with recently that&#8217;s not very well documented is using JavaScript to retrieve and to update the current page anchor from a URL. It&#8217;s very simple to do. In order to retrieve the page anchor (i.e. get test from http://chris-miller.org/88#test: var anchor = self.document.location.hash.substring( 1 ); Setting the anchor to update, for [...]]]></description>
			<content:encoded><![CDATA[<p>Something I&#8217;ve been playing with recently that&#8217;s not very well documented is using JavaScript to retrieve and to update the current page anchor from a URL.</p>
<p>It&#8217;s very simple to do.  In order to retrieve the page anchor (i.e. get <code>test</code> from http://chris-miller.org/88#test:</p>
<pre class="javascript" name="code" id="getting-and-setting-anchors-with-javascript_get_snippit">
var anchor = self.document.location.hash.substring( 1 );
</pre>
<p>Setting the anchor to update, for example to http://chris-miller.org/88#newAnchor it can be achived using:</p>
<pre class="javascript" name="code" id="getting-and-setting-anchors-with-javascript_set_snippit">
self.document.location.hash = "newAnchor";
</pre>
<p>Very useful for creating permalinks to AJAX loaded content or JS popups.<br />
- Chris</p>
]]></content:encoded>
			<wfw:commentRss>http://chris-miller.org/archives/2010/04/09/getting-and-setting-anchors-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fade between two divs using mootools</title>
		<link>http://chris-miller.org/archives/2010/04/08/fade-between-two-divs-using-mootools/</link>
		<comments>http://chris-miller.org/archives/2010/04/08/fade-between-two-divs-using-mootools/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 14:17:05 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[fade]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[mootools]]></category>

		<guid isPermaLink="false">http://chris-miller.org/?p=488</guid>
		<description><![CDATA[I&#8217;ve been playing with mootools for a while, they&#8217;re very nice, making easy work of some nice visual tweens and effects. One thing I was struggling a bit to do was fade between two divs, having the first one fade out then the second fade in. Rather than grabbing a plugin to do it I [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://chris-miller.org/wp-content/uploads/2010/04/mootools.png" alt="MooTools" width="184" height="46" class="alignright size-full wp-image-491" />I&#8217;ve been playing with mootools for a while, they&#8217;re very nice, making easy work of some nice visual tweens and effects.</p>
<p>One thing I was struggling a bit to do was fade between two divs, having the first one fade out then the second fade in.  Rather than grabbing a plugin to do it I wanted a small bit of JavaScript I could embed in a page, I couldn&#8217;t find one <em>anywhere</em>.</p>
<p>It&#8217;s pretty simple to do, the code below should give you a starting point to set up some JS to fade between 2 or more divs.</p>
<pre class="javascript" name="code" id="fade-between-two-divs-using-mootools_snippit">
function fadeBetweenDivs( div1, div2 ) {
	$$( div1 ).fade( "out" );
	(function(){
		$$( div1 ).setStyles({
			display:	'none',
			opacity:	0
		});
	}).delay( 150 );
	(function(){
		$( div2 ).setStyles({
			display:	'block',
			opacity:	0
		});
	}).delay( 150 );
	$$( div2 ).fade( "in" );
}
</pre>
<p>That is all,<br />
- Chris</p>
]]></content:encoded>
			<wfw:commentRss>http://chris-miller.org/archives/2010/04/08/fade-between-two-divs-using-mootools/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaScript custom controls</title>
		<link>http://chris-miller.org/archives/2009/02/15/javascript-custom-controls/</link>
		<comments>http://chris-miller.org/archives/2009/02/15/javascript-custom-controls/#comments</comments>
		<pubDate>Sun, 15 Feb 2009 20:19:57 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[HCI]]></category>
		<category><![CDATA[custom controls]]></category>
		<category><![CDATA[interaction]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ui]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://chris-miller.org/?p=185</guid>
		<description><![CDATA[I spend a lot of time writing bespoke content management systems, a lot! I&#8217;m always trying to add some small, lightweight additions to make the user experience a little bit better. One means of doing this is by providing some better custom controls which give good user feedback and emulate similar offline controls. One such [...]]]></description>
			<content:encoded><![CDATA[<p>I spend a lot of time writing bespoke content management systems, a lot!  I&#8217;m always trying to add some small, lightweight additions to make the user experience a little bit better.  One means of doing this is by providing some better custom controls which give good user feedback and emulate similar offline controls.</p>
<p><div id="javascript-custom-controls_figure1" class="wp-caption alignright" style="width: 185px"><img alt="Figure 1: Ordering Control" src="http://chris-miller.org/images/ordering_field.png" title="Ordering Control" width="175" height="26" /><p class="wp-caption-text"><strong>Figure 1</strong>: Ordering Control</p></div>One such control is a linear range control, catchy I know, which allows the user to select a value within a particular range with a given default value.  An example of this may be selecting an ordering value for a blog post, <a href="#javascript-custom-controls_figure1" title="Ordering Control" onmouseover="document.getElementById('javascript-custom-controls_figure1').style.borderColor = '#2266aa';" onmouseout="document.getElementById('javascript-custom-controls_figure1').style.borderColor = '#d6d6d6';">Figure 1</a>, ranging from say -100 to 100 with a default value of 0.  Posts in our imaginary blog would be ordered by this ordering field ascending with 0 being the default value, resulting in posts being ordered automatically by date.  Rather than using a plain text box to allow the user to enter the ordering value directly, subsequently giving us little or no control over the contents of the field until the form is submitted and no means to convey the nature of the field itself, we can create controls to manipulate the value ourselves.</p>
<p>Creating a custom control allows us to do a number of things that we just cannot do with a normal HTML form element:<br />
<div id="javascript-custom-controls_figures234" class="wp-caption alignright" style="width: 185px"><img id="javascript-custom-controls_figure2" alt="Figure 2: Lower Bounds" src="http://chris-miller.org/images/ordering_field_lower.png" title="Lower Bounds" width="175" height="26" /> <img id="javascript-custom-controls_figure3" alt="Figure 3: Non-Default Value" src="http://chris-miller.org/images/ordering_field_middle.png" title="Non-Default Value" width="175" height="26" /> <img id="javascript-custom-controls_figure4" alt="Figure 4: Upper Bounds" src="http://chris-miller.org/images/ordering_field_upper.png" title="Upper Bounds" width="175" height="26" /><p class="wp-caption-text"><strong>Figures 2, 3 &amp; 4</strong>: Showing lower bounds, non-default value and upper bounds</p></div></p>
<ul>
<li>Display relevant information instead of certain values, such as showing Automatic at 0 in <a href="#javascript-custom-controls_figure1" title="Ordering Control" onmouseover="document.getElementById('javascript-custom-controls_figure1').style.borderColor = '#2266aa';" onmouseout="document.getElementById('javascript-custom-controls_figure1').style.borderColor = '#d6d6d6';">Figure 1</a></li>
<li>Show when the upper and lower bounds are reached, for example at <a href="#javascript-custom-controls_figure2" title="Lower Limit" onmouseover="document.getElementById('javascript-custom-controls_figures234').style.borderColor = '#2266aa';" onmouseout="document.getElementById('javascript-custom-controls_figures234').style.borderColor = '#d6d6d6';">-100</a> and <a href="#javascript-custom-controls_figure4" title="Upper Limit" onmouseover="document.getElementById('javascript-custom-controls_figures234').style.borderColor = '#2266aa';" onmouseout="document.getElementById('javascript-custom-controls_figures234').style.borderColor = '#d6d6d6';">100</a> as per our example</li>
<li>Allow the user to reset the value to the default or previous setting</li>
<li>Give some inkling into how the control should be used</li>
</ul>
<p><span id="more-185"></span><br />
<h3>Controls</h3>
<p><div class="wp-caption alignright" style="width: 323px"><img alt="Annotated Ordering Field" src="http://chris-miller.org/images/ordering_field_annotated.png" title="Annotated Ordering Field" width="313" height="63" /><p class="wp-caption-text"><strong>Figure 5</strong>: Annotated Ordering Field</p></div>By using JavaScript <code>onclick</code> or <code>onmousedown</code> events we can manipulate the contents of our ordering field held behind the scenes.  The addition of functions and some variables defining the bounds and the default value of our control then allows us to constrain the upper and lower limits, as we&#8217;ve seen, and to reset the field back to it&#8217;s defaults.  This gives us the following interaction:</p>
<ul>
<li>Previous/Next: update the value of the field to the previous or next value in the range</li>
<li>Reset: reset the value to the pre-defined default value</li>
<li>Display: allow us to display feedback or custom messages based on the value of the field we&#8217;re manipulating</li>
</ul>
<p>This interaction is fairly rudimentary and not very exciting to say the least.  We really want to provide more desktop-like interaction via our own custom controls such as the ability to click-and-hold a button in order to scroll through available values.  With such a control, in the majority of desktop applications, when the button is held the action that button denotes will be repeated after a short delay usually with increasing speed the longer it&#8217;s held.  We can emulate this in JavaScript using a timeout to create the pause and a modifier to decrease the length of this pause and in turn increase the speed with which the actions are executed.</p>
<p><a href="#javascript-custom-controls_snippit1" onmouseover="document.getElementById('javascript-custom-controls_snippit1').style.borderColor = '#2266aa';" onmouseout="document.getElementById('javascript-custom-controls_snippit1').style.borderColor = '#d6d6d6';" title="Click-and-hold example">Code Snippit 1</a> contains a brief outline of how to create a mechanism for firing events when a button is held.</p>
<div class="code_header">
<p>
<strong>Code Snippit 1</strong>: Rough JavaScript to deal with the click-and-hold updating mechanism
</p>
<pre class="javascript" name="code" id="javascript-custom-controls_snippit1" >
&lt;script type="text/javascript"&gt;
    var timeout          = 1000;
    var timeout_limit    = 200;
    var timeout_speedup  = 1.5;
    var default          = 0;
    var lower_bounds     = -100;
    var upper_bounds     = 100;

    function setView( str, element ) {
        // Update the ordering element setting the values and
        // icons as necessary
      ...
    }

    function updateVariable() {
        // Update variable
        order        = document.getElementById( "ordering" );
        order_disp   = document.getElementById( "order_disp" );

        // Update variable
        order.value  = order.value + 1;

        // Check Bounds
        if( order.value &gt;= upper_bounds )
            setView( "upper_bounds", order_disp );
        elseif( order.value == default )
            setView( "Default", order_disp );
        else
            setView( "" + order.value, order_disp );

        // Start firing click-and-hold events
        if( timeout &gt; timeout_limit )
            timeout = max( timeout / timeout_speedup, timeout_limit );
        t = setTimeout( updateVariable, timeout );
    }

    function buttonStop( t ) {
        timeout = 1000;
        clearTimeout( t );
    }
&lt;/script&gt;
</pre>
</div>
<p><code>timeout</code> is the time between calling button events, the <code>timeout_speedup</code> modifier is used to recalculate the timeout on each event in order to increase the speed with which the event is fired.  We set a lower limit, <code>timeout_limit</code>, to ensure that the events aren&#8217;t called too quickly to update the display and to give the user time to react once they near their desired value.  A plot of time between ticks against ticks is shown in <a href="#javascript-custom-controls_graph1" onmouseover="document.getElementById('javascript-custom-controls_graph1').style.borderColor = '#2266aa';" onmouseout="document.getElementById('javascript-custom-controls_graph1').style.borderColor = '#d6d6d6';" title="Time between ticks vs. Ticks">Graph 1</a>, as you can see it quickly ramps up so <code>timeout</code> is set to the lower limit, much like you would expect from a desktop application&#8217;s click-and-hold button interaction.</p>
<div id="javascript-custom-controls_graph1" class="wp-caption aligncenter" style="width: 551px"><img alt="Timeout vs. Ticks" src="http://chris-miller.org/images/timeout_graph.png" title="Timeout vs. Ticks" width="541" height="250" /><p class="wp-caption-text"><strong>Graph 1</strong>: Timeout vs. Ticks</p></div>
<p><a href="#javascript-custom-controls_snippit2" onmouseover="document.getElementById('javascript-custom-controls_snippit2').style.borderColor = '#2266aa';" onmouseout="document.getElementById('javascript-custom-controls_snippit2').style.borderColor = '#d6d6d6';" title="HTML button">Code Snippit 2</a> shows sample HTML used to create the next button with the appropriate <code>onmousedown</code>, <code>onmouseup</code> and <code>onmouseout</code> JavaScript calls to add the desired functionality to the button.</p>
<div id="javascript-custom-controls_snippit2" class="code_header">
<p>
<strong>Code Snippit 2</strong>: Button calling the events when clicked
</p>
<pre class="html" name="code">
&lt;img
    title="Next" src="images/icons/arrow_right.png" alt="&amp;rarr;"
    <!-- Update the variable and fire click-and-hold events -->
    onmousedown="updateVariable();"
    <!-- Stop the timer if mouse released -->
    onmouseup="buttonStop( t );"
    <!-- Stop the timer if the mouse moves off the control -->
    onmouseout="buttonStop( t );"
/&gt;
</pre>
</div>
<p>We use both <code>onmouseup</code> and <code>onmouseout</code> to ensure that the click-and-hold behavior ceases when the user either releases the mouse button or moves the cursor off the control.</p>
<p><div id="javascript-custom-controls_figure6" class="wp-caption alignright" style="width: 185px"><img alt="Textentry into ordering field" src="http://chris-miller.org/images/ordering_field_textentry.png" title="Textentry into ordering field" width="175" height="36" /><p class="wp-caption-text"><strong>Figure 6</strong>: Textentry into ordering field</p></div>We can extend the custom control even further and give the user a less stringent method of inputting a value by allowing them to type directly into the field in the same manner as the original textbox, <a href="#javascript-custom-controls_figure6" title="Textentry into ordering field" onmouseover="document.getElementById('javascript-custom-controls_figure6').style.borderColor = '#2266aa';" onmouseout="document.getElementById('javascript-custom-controls_figure6').style.borderColor = '#d6d6d6';">Figure 6</a>.</p>
<h3>Feedback</h3>
<p>Here&#8217;s where our custom controls can really begin to shine, we can give the user direct feedback in the control that they&#8217;re manipulating in real-time.  Sure we can do this with JavaScript as it stands, but extending beyond the &#8220;red border = wrong&#8221; methodology we can give visual cues to the user to show that they&#8217;re trying to do something thata&#8217;s wrong and what specifically is wrong with what they&#8217;re doing.</p>
<p>Showing the bounds of the field is easy using the visual cues we&#8217;ve detailed in <a href="#javascript-custom-controls_figures234" title="Upper Limit" onmouseover="document.getElementById('javascript-custom-controls_figures234').style.borderColor = '#2266aa';" onmouseout="document.getElementById('javascript-custom-controls_figures234').style.borderColor = '#d6d6d6';">Figures 2 &#038; 4</a>.  Other visual cues we can use are that of text/background colouring to show that an error occurred.  We can also, using the same timeout methods used for the click-and-hold interaction, display <em>useful</em> messages to the user for a brief time before correcting their mistakes telling them what they did wrong and why their entries have been changed, <a href="#javascript-custom-controls_figure7" title="Error detection and correction" onmouseover="document.getElementById('javascript-custom-controls_figure7').style.borderColor = '#2266aa';" onmouseout="document.getElementById('javascript-custom-controls_figure7').style.borderColor = '#d6d6d6';">Figure 7</a>.</p>
<div id="javascript-custom-controls_figure7" class="wp-caption alignright" style="width: 573px"><img alt="Error detection and correction" src="http://chris-miller.org/images/ordering_field_errorcorrection.png" title="Error detection and correction" width="563" height="36" /><p class="wp-caption-text"><strong>Figure 7</strong>: Error detection and correction</p></div>
<h3 style="margin-top: 10px;">It&#8217;s only an example!</h3>
<p><div class="wp-caption alignright" style="width: 319px"><img alt="Other possible controls?" src="http://chris-miller.org/images/controls_other.png" title="Other possible controls?" width="309" height="59" /><p class="wp-caption-text"><strong>Figure 8</strong>: Other possible controls?</p></div>Of course using such controls for an ordering field is only one example where we can extend upon HTML form elements with this simple control.  This type of control mechanism could be used for <em>any</em> field that has a linear progression in values.  Not only that, you could extend upon the functionality of this example to meet your specific requirements, by wrapping the bounds around to the start, having varying degrees of scale over numerous controls that are swapped in and out for fine or granular interaction or just have it toggle between two set values.</p>
<p>What&#8217;s the point?  There&#8217;s so much we can do nowadays in terms of interaction, even on the web.  Try to add a little bit of interactive zest to your HTML controls, it makes everyone&#8217;s experience that little bit easier and better.</p>
<p>Thanks for reading,<br />
- Chris</p>
]]></content:encoded>
			<wfw:commentRss>http://chris-miller.org/archives/2009/02/15/javascript-custom-controls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get multi-class elements in JavaScript</title>
		<link>http://chris-miller.org/archives/2008/10/02/get-multi-class-elements-in-javascript/</link>
		<comments>http://chris-miller.org/archives/2008/10/02/get-multi-class-elements-in-javascript/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 20:35:49 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[getElementsByClass]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[multi-class elements]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://chris-miller.org/?p=184</guid>
		<description><![CDATA[As any person who uses JavaScript for client side interaction knows, getting elements by their class is one essential thing we need to do over and over again. For the most part using a function equivalent to the one shown below will do. Code Snippit 1: Simplistic getElementsByClass() JavaScript method function getElementsByClass( className ) { [...]]]></description>
			<content:encoded><![CDATA[<p>As any person who uses JavaScript for client side interaction knows, getting elements by their class is one essential thing we need to do over and over again.  For the most part using a function equivalent to the one <a href="#get-multi-class-elements-in-javascript_snippit1" title="Simple getElementsByClass()" onmouseover="document.getElementById('get-multi-class-elements-in-javascript_snippit1).style.borderColor = '#2266aa';" onmouseout="document.getElementById('get-multi-class-elements-in-javascript_snippit1').style.borderColor = '#d6d6d6';">shown below</a> will do.</p>
<div id="get-multi-class-elements-in-javascript_snippit1" class="code_header">
<p>
<strong>Code Snippit 1</strong>: Simplistic getElementsByClass() JavaScript method
</p>
<pre class="javascript" name="code">
function getElementsByClass( className ) {
    var all =
        document.all ? document.all : document.getElementsByTagName( '*' );
    var elements = new Array();
    for( var e = 0; e < all.length; e++ ) {
        if (all[e].className == className) {
            elements[elements.length] = all[e];
        }
    }
    return elements;
}
</pre>
</div>
<p>This function basically checks the string that is entered into the class attribute for each HTML element and returns all elements where there is an exact match.</p>
<p>This method will fail however if we're trying to select elements that have multiple class associations.  We need to use something more complex when, for example, creating a list of items that are filterable by class when arranged in overlapping groups.  Splitting the class attribute on space characters will allow us to check for matches in these cases:</p>
<div id="get-multi-class-elements-in-javascript_snippit2" class="code_header">
<p>
<strong>Code Snippit 2</strong>: More advanced getElementsByClass() function
</p>
<pre class="javascript" name="code">
function getElementsByClass( className ) {
    var all =
        document.all ? document.all : document.getElementsByTagName( '*' );
    var elements = new Array();
    for( var e = 0; e < all.length; e++ ) {
        var classes = all[e].className.split(/\s/g);
        for( var c = 0; c < classes.length; c++ ) {
            if( classes == className ) {
                elements[elements.length] = all[e];
            }
        }
    }
    return elements;
}
</pre>
</div>
<p>The <a href="#get-multi-class-elements-in-javascript_snippit2" title="Advanced getElementsByClass()" onmouseover="document.getElementById('get-multi-class-elements-in-javascript_snippit2).style.borderColor = '#2266aa';" onmouseout="document.getElementById('get-multi-class-elements-in-javascript_snippit2').style.borderColor = '#d6d6d6';">function above</a> does pretty much the same as the first one, however splits the class attribute string on the spaces and loops through them, trying to match the parts individually.  This should match on elements with singular and multiple class names associated with them and solve any problems with multiple classed elements.<br />
- Chris</pre>
]]></content:encoded>
			<wfw:commentRss>http://chris-miller.org/archives/2008/10/02/get-multi-class-elements-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

