<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><description></description><title>thudjs</title><generator>Tumblr (3.0; @thudjs)</generator><link>https://thudjs.tumblr.com/</link><item><title>stylesheet.onload: or lack thereof</title><description>&lt;p class="note"&gt;For a version of stylesheet.onload that works cross domain, please see: &lt;a href="http://www.phpied.com/when-is-a-stylesheet-really-loaded/"&gt;When is a stylesheet really loaded?&lt;/a&gt;by &lt;a href="http://twitter.com/stoyanstefanov"&gt;@stoyanstefanov&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Loading stylesheets into your page dynamically can be a good way of reducing your initial page download size. You may want to load a print stylesheet in &lt;strong&gt;only&lt;/strong&gt; when a user initiates a print action, you may want to allow your users to change the theme or skin of your site/ app or before displaying some content loaded in dynamically via AJAX.&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;s only one caveat, you need to know that the stylesheet has actually loaded before you start the print action, or display your content to avoid any mishaps or &lt;a href="http://en.wikipedia.org/wiki/Flash_of_unstyled_content"&gt;foucs&lt;/a&gt;. This is easy when using AJAX or inserting a script or image node into the page. Unfortunately though, most browsers have not implemented the same functionality for the humble link node.&lt;/p&gt;
&lt;p&gt;Funnily enough, Internet Explorer supports the onreadystatechange event and Opera supports the onload event for link nodes. This still leaves us with a large gap of unsupported browsers and the IE and Opera only tell us that the stylesheet has completed loading and not whether it was loaded successfully. So, what are our options then?&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;Don&amp;rsquo;t support this kind of functionality&lt;/li&gt;
	&lt;li&gt;Use AJAX to load in the stylesheet&amp;rsquo;s content, create a style node and insert the &lt;a href="http://www.w3.org/TR/XMLHttpRequest/#the-responsetext-attribute"&gt;XHR responseText&lt;/a&gt; into the style node.&lt;/li&gt;
	&lt;li&gt;Dynamically create a link node, load the stylesheet in and use &lt;a href="https://developer.mozilla.org/en/DOM/window.setTimeout"&gt;setTimeout&lt;/a&gt; to run a callback after a abitrary amount of time has elapsed (e.g. 250ms), and hope it all works out.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;None of these options really seems viable, although option 2 is probably the nicest of the 3. There is one more option though: option 4. Oooohhhh&amp;hellip;&lt;/p&gt;
&lt;h4&gt;Option 4: The bestest option so far.&lt;/h4&gt;
&lt;p&gt;Even though there is no event to listen to, we do have a way we can check a stylesheet has loaded in the remaining browsers: by checking it&amp;rsquo;s &lt;code&gt;cssRules&lt;/code&gt; length.&lt;/p&gt;
&lt;p&gt;In W3C compliant browsers the link node will have a &lt;code&gt;sheet&lt;/code&gt; property which, in itself, contains a &lt;code&gt;cssRules&lt;/code&gt; property, which, as you can probably guess, is a list of all the CSS rules in the style sheet. If the style sheet is loaded, and not empty, then:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;link_node.sheet.cssRules.length &amp;gt; 0;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can expect, Internet Explorer, has a slightly different syntax:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;link_node.styleSheet.rules.length &amp;gt; 0;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So using this little piece of knowledge and a little &lt;a href="https://developer.mozilla.org/en/DOM/window.setInterval"&gt;setInterval&lt;/a&gt;/ &lt;a href="https://developer.mozilla.org/en/DOM/window.setTimeout"&gt;setTimeout&lt;/a&gt; magic we can create a script that allows us to load style sheet into the page dynamically and then fire a callback when it has completed.&lt;/p&gt;
&lt;p&gt;With option 4, we can create a function that loads a style sheet in any browser and fires a callback when it is has loaded. The callback will tell us if it was successfull or unsuccessfull (it would only be unsuccessful if the file did not exist or the style sheet had no rules in it).&lt;/p&gt;&lt;h5&gt;The basics&lt;/h5&gt;
&lt;p&gt;The most basic implementation of this can be done in a mesely 30 lines of — framework independent — JavaScript code: &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;function loadStyleSheet( path, fn, scope ) {
   var head = document.getElementsByTagName( 'head' )[0], // reference to document.head for appending/ removing link nodes
       link = document.createElement( 'link' );           // create the link node
   link.setAttribute( 'href', path );
   link.setAttribute( 'rel', 'stylesheet' );
   link.setAttribute( 'type', 'text/css' );

   var sheet, cssRules;
// get the correct properties to check for depending on the browser
   if ( 'sheet' in link ) {
      sheet = 'sheet'; cssRules = 'cssRules';
   }
   else {
      sheet = 'styleSheet'; cssRules = 'rules';
   }

   var interval_id = setInterval( function() {                    // start checking whether the style sheet has successfully loaded
          try {
             if ( link[sheet] &amp;amp;&amp;amp; link[sheet][cssRules].length ) { // SUCCESS! our style sheet has loaded
                clearInterval( interval_id );                     // clear the counters
                clearTimeout( timeout_id );
                fn.call( scope || window, true, link );           // fire the callback with success == &lt;strong&gt;true&lt;/strong&gt;
             }
          } catch( e ) {} finally {}
       }, 10 ),                                                   // how often to check if the stylesheet is loaded
       timeout_id = setTimeout( function() {       // start counting down till fail
          clearInterval( interval_id );            // clear the counters
          clearTimeout( timeout_id );
          head.removeChild( link );                // since the style sheet didn't load, remove the link node from the DOM
          fn.call( scope || window, false, link ); // fire the callback with success == &lt;strong&gt;false&lt;/strong&gt;
       }, 15000 );                                 // how long to wait before failing

   head.appendChild( link );  // insert the link node into the DOM and start loading the style sheet

   return link; // return the link node;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This would allow you to load a style sheet with an onload callback function like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;loadStyleSheet( '/path/to/my/stylesheet.css', function( success, link ) {
   if ( success ) {
      // code to execute if the style sheet was loaded successfully
   }
   else {
      // code to execute if the style sheet failed to successfully
   }
} );&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or if you want to your callback to maintain its scope/ context, you could do something kind of like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;loadStyleSheet( '/path/to/my/stylesheet.css', this.onComplete, this );&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you want to &lt;a href="http://thudjs.com/blog/loadStyleSheet.basic.html"&gt;see it in action&lt;/a&gt; I&amp;rsquo;ve created a test page. I&amp;rsquo;ve also created a &lt;a href="http://thudjs.com/blog/loadStyleSheet.js"&gt;nicer version of loadStyleSheet&lt;/a&gt; which:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Allows you to add multiple callbacks per stylesheet&lt;/li&gt;
	&lt;li&gt;Uses onreadystatechange and onload events for browsers that support them; and&lt;/li&gt;
	&lt;li&gt;Does not try and load the same stylesheet if it is still in the DOM — if you try and load the same style sheet twice it will simply fire the callback you passed, with &lt;code&gt;success == true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It has it&amp;rsquo;s own test page &lt;a href="http://thudjs.com/blog/loadStyleSheet.html"&gt;test page&lt;/a&gt;. &lt;a href="http://thudjs.com/blog/loadStyleSheet.js"&gt;loadStyleSheet.js&lt;/a&gt; is about 1kb gzipped.&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/637855087</link><guid>https://thudjs.tumblr.com/post/637855087</guid><pubDate>Thu, 27 May 2010 16:27:00 +0100</pubDate><category>css</category><category>javascript</category></item><item><title>pseudocode + diagrams = ui interactions on a silver platter</title><description>&lt;p&gt;I mentioned in a &lt;a href="/post/501011181/the-dragging-and-droppings"&gt;previous post&lt;/a&gt; how I&amp;rsquo;d been working on the drag and drop module of thud for about 2 weeks. It was definitely challenging, though now that I&amp;rsquo;ve done it, I think I could do it again in about 2 or 3 days.&lt;/p&gt;
&lt;p&gt;Making something on a page draggable without using the new HTML5 attributes and events is the easy part. What are the tricky parts? Setting the coordinates of the drag image correctly when the drag element is in a positioned element, scrolling correctly and getting the snap to grid correct. It was a little frustrating, but I got there in the end. Thanks to &lt;a href="http://en.wikipedia.org/wiki/Pseudocode"&gt;pseudocode&lt;/a&gt;. This post is mostly to do with implementing drag functionality. However, it was pseudocode and drawing diagrams that saved me, hence the title.&lt;/p&gt;
&lt;p&gt;I first &amp;ldquo;learnt&amp;rdquo; (about) pseudocode in high school — I use the term loosely, because I didn&amp;rsquo;t really learn much in high school, except about how to be a yes man and how everyone in the class was going to fail. I failed at both failing and being a yes man — computer studies class. I didn&amp;rsquo;t really get it then and thought it was a waste of time, never looking at it again until recently.&lt;/p&gt;
&lt;p&gt;However, after failing to figure out some of the major errors I was encountering I needed to get back to basics and so I turned to pen and paper. I didn&amp;rsquo;t follow the exact pseudocode syntax, I don&amp;rsquo;t think you really need to. As long as you understand what is going on and can use what you write in your program. Mine is my own special recipe of pseudocode and JavaScript.&lt;/p&gt;
&lt;p&gt;I thought I would add post about it, in case anyone else finds it useful.&lt;/p&gt;
&lt;h4&gt;Finding and setting the drag image&amp;rsquo;s current left and top positions&lt;/h4&gt;
&lt;p&gt;The logic for finding and setting the drag image&amp;rsquo;s current left and top position also includes the logic to snap the drag image to a grid, constrain it to specific dimensions, keep it a specific amount of pixels from the mouse pointer (offset) and/ or limit it to been dragged horizontally or vertically.&lt;/p&gt;
&lt;h5&gt;Glossary&lt;/h5&gt;
&lt;p&gt;I&amp;rsquo;ve put this at the top so it&amp;rsquo;s easier to see while reading the rest. Assuming you&amp;rsquo;re lucky enough to have a big monitor.&lt;/p&gt;
&lt;table class="data" cellspacing="0" cellpadding="0" border="0"&gt;
	&lt;thead&gt;
		&lt;tr&gt;&lt;th&gt;property&lt;/th&gt;&lt;th&gt;description&lt;/th&gt;&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
		&lt;tr&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;The position the mouse cursor is at, relative to the page. i.e. the event&amp;rsquo;s pageX value.&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;coordinates&lt;/td&gt;&lt;td&gt;The bottom, height, left, offsetLeft, offsetTop, right, scrollHeight, scrollLeft, scrollTop, scrollWidth, top &amp;amp; width properties of an html element or the document.&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;orientation&lt;/td&gt;&lt;td&gt;A value, either horizontal, vertical or null, and if set is used to limit the drag_image to only moving in either direction but not both.&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;drag_element&lt;/td&gt;&lt;td&gt;The element we want to drag.&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;drag_image&lt;/td&gt;&lt;td&gt;The element that will be following the cursor while the drag operation is taking place. This could be the drag_element itself, a clone of the drag_element or a completely bespoke element.&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;scroll_element&lt;/td&gt;&lt;td&gt;The element to scroll if scrolling is allowed and the drag_image is at the boundary of the viewable_area.&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;viewable_area&lt;/td&gt;&lt;td&gt;The coordinates of the area currently visible in the scroll_element (this will generally be the document).&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;snap&lt;/td&gt;&lt;td&gt;As in snap to grid. The number of pixels to snap the drag_image to. If this is less than 2 it will be ignored.&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;grid&lt;/td&gt;&lt;td&gt;A predefined array of all the values the drag_image should snap to within the boundaries of the scroll_element. This is worked out as:&lt;br/&gt;
&lt;code&gt;( new Array( Math.ceil( scroll_element.scrollWidth / snap ) + 2 ) ).map( function( value, index ) { return index * snap; } );&lt;/code&gt;&lt;br/&gt;
e.g. If the scrollWidth was 1024px and the snap is 16px, the grid array would be: [16, 32, 48, &amp;hellip;, 1024, 1040]&lt;br/&gt;
The reason the final grid value is larger than the scroll_element&amp;rsquo;s boundary is that we want to be able to position the drag_image at the scroll_element&amp;rsquo;s boundary.&lt;br/&gt;
To figure out the vertical grid, we use scrollHeight instead of scrollWidth.&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;constrain_coordinates&lt;/td&gt;&lt;td&gt;the bottom, left, right, &amp;amp; top coordinates to limit the drag_image&amp;rsquo;s movements within.&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;offset&lt;/td&gt;&lt;td&gt;the number of pixels to keep the drag_image from the mouse pointer.&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;scrollX&lt;/td&gt;&lt;td&gt;the amount of pixels the viewable_area was scrolled left or right.&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;aggregateOffsetLeft&lt;/td&gt;&lt;td&gt;This is worked out as:&lt;br/&gt;
&lt;code&gt;var aggregateOffsetLeft = 0, el = drag_element;&lt;br/&gt;
do { aggregateOffsetLeft += el.offsetLeft; } while ( el = el.offsetParent );&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;aggregateOffsetTop&lt;/td&gt;&lt;td&gt;This is worked out as above, except instead of offsetLeft we aggregate offsetTop.&lt;/td&gt;&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;h5&gt;Finding and setting the drag image&amp;rsquo;s left (x) position&lt;/h5&gt;
&lt;pre&gt;&lt;code&gt;IF orientation != vertical // if we are only allowing vertical movement, don't worry about calculating the horizontal
   IF viewable_area WAS SCROLLED BY AMOUNT scrollX // check to see if the scrolling function has scrolled horizontally
      viewable_area.left += scrollX // if so we need to adjust the viewable_area properties to reflect this
      viewable_area.right += scrollX

   x = x - drag_image.aggregateOffsetLeft // we don't want ot use the -= operator here as it will change the original x value

   IF x &amp;gt;= viewable_area.right // this makes sure the drag_image is always visible to the user dragging it
      IF viewable_area.right &amp;lt;= scroll_element.scrollWidth
         x = viewable_area.right - drag_image.width - 20 // 20 is the standard vertical scroll bar width
      ELSE
         x = scroll_element.scrollWidth - drag_image.width - |offset| // |offset| if you don't know is the absolute value of offset so |8| == |-8| == 8
   ELSE IF x &amp;lt; 0 - drag_image.aggregateOffsetLeft
      x = 0 - drag_image.aggregateOffsetLeft

   IF SNAP TO GRID
      x = grid.find( closestGridPosition, x ) || x // closestGridPosition returns the number in the grid array that x is between. 
                                                   // e.g. if snap == 16px and x is between 16 and 32 it will return 16, 
                                                   // if x is between 32 and 48 it will return 32, etc.
   IF CONSTRAIN TO COORDINATES
      IF constrain_coordinates.left &amp;amp;&amp;amp; x &amp;lt; constrain_coordinates.left
         x = constrain_coordinates.left
      IF constrain_coordinates.right &amp;amp;&amp;amp; x &amp;gt; constrain_coordinates.right
         x = constrain_coordinates.right

   drag_image.left = x + offset&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On line 5, we have the following code &lt;code&gt;x = x - drag_image.aggregateOffsetLeft&lt;/code&gt;. This was a tricky part, it took me about 2 days to realise I was using the wrong property here.&lt;/p&gt;
&lt;p&gt;As I mentioned before making something draggable is easy. In it&amp;rsquo;s simplest form you can set the left and top position of the drag_image to the mousemove event&amp;rsquo;s pageX and pageY values. Consider the following diagram:&lt;/p&gt;
&lt;img alt="diagram: dragging within the viewport" class="nodeco" src="http://thudjs.com/blog/assets/dragging_within_the_viewport.gif"/&gt;
&lt;p&gt;If our drag_element is a child — and not a descendant of a positioned element — of the body tag and you don&amp;rsquo;t need to scroll the page, then setting the drag_image&amp;rsquo;s left and top styles to the event&amp;rsquo;s pageX and pageY values is ok. However, consider the next diagram:&lt;/p&gt;
&lt;img alt="diagram: dragging within a positioned element" class="nodeco" src="http://thudjs.com/blog/assets/dragging_within_a_positioned_element.gif"/&gt;
&lt;p&gt;Basically, the element we want to drag is a child of &lt;strong&gt;positioned_element&lt;/strong&gt; in the diagram. Its left and top values are relative to this element. So setting the left and top values to the event&amp;rsquo;s pageX and pageY values, would position the drag_image in the wrong location on the page, as its position is relative to positioned_element, &lt;strong&gt;NOT the viewport&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;If we want to drag the drag_image out of the positioned_element, then we need a way of figuring out the drag_image&amp;rsquo;s left and top values, relative to positioned_element.&lt;/p&gt;
&lt;p&gt;In the example above, positioned_element is 100px from the left and top of the document, the drag_image is currently 100px from the left and top of the positioned_element and we want to drag the drag_image to 10px from the left and top of the document, the new coordinates for the drag_image would need to be set to -90px from the left and top of positioned_element.&lt;/p&gt;
&lt;p&gt;This is worked out as, 10px from the left of the viewport minus the positioned_element&amp;rsquo;s 100px from the left of the viewport, equals -90px.&lt;/p&gt;
&lt;h5&gt;Finding and setting the drag image&amp;rsquo;s top (y) position&lt;/h5&gt;
&lt;p&gt;I was going to put the pseudocode for this in, but it&amp;rsquo;s exactly the same as the above, only we replace the left (x) properties, with the corresponding top (y) properties:&lt;/p&gt;
&lt;table class="data" cellspacing="0" cellpadding="0" border="0"&gt;
	&lt;thead&gt;
		&lt;tr&gt;&lt;th&gt;left (x) property&lt;/th&gt;&lt;th&gt;top (y) property&lt;/th&gt;&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
		&lt;tr&gt;&lt;td&gt;x&lt;/td&gt;&lt;td&gt;y&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;vertical&lt;/td&gt;&lt;td&gt;horizontal&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;scrollX&lt;/td&gt;&lt;td&gt;scrollY&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;viewable_area.left&lt;/td&gt;&lt;td&gt;viewable_area.top&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;viewable_area.right&lt;/td&gt;&lt;td&gt;viewable_area.bottom&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;drag_image.height&lt;/td&gt;&lt;td&gt;drag_image.width&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;drag_image.left&lt;/td&gt;&lt;td&gt;drag_image.top&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;drag_image.aggregateOffsetLeft&lt;/td&gt;&lt;td&gt;drag_image.aggregateOffsetTop&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;scroll_element.scrollWidth&lt;/td&gt;&lt;td&gt;scroll_element.scrollHeight&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;constrain_coordinates.left&lt;/td&gt;&lt;td&gt;constrain_coordinates.top&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;constrain_coordinates.right&lt;/td&gt;&lt;td&gt;constrain_coordinates.bottom&lt;/td&gt;&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;h4&gt;Scrolling when the drag image is at a boundary point&lt;/h4&gt;
&lt;p&gt;The tricky part here is working the scroll sensitivity into the equation. We could adjust the viewable_area boundary of the scroll_element to incorporate the scroll_sensitivity, so that when the drag_image reaches this new boundary we can start scrolling.&lt;/p&gt;
&lt;p&gt;However, as the viewable area will then change we will need to readjust the boundary each time we scroll. We need to create a boundary — I like to think of it as a force field — around the drag_image. The easiest way to envision this, I&amp;rsquo;ve found, is using the following diagram:&lt;/p&gt;
&lt;img alt="diagram: scrolling an element while the drag_image is at its boundary" class="nodeco" src="http://thudjs.com/blog/assets/dragging_and_scrolling.gif"/&gt;
&lt;p&gt;Using this method, we can create a boundary — force field — around the drag_image the size of the scroll_sensitivity amount. When this &lt;strong&gt;drag_boundary&lt;/strong&gt;&amp;rsquo;s bottom, left, right, top — or a combination of those — is greater — or less than, depending on the direction of the drag — the boundary of the viewable_area we can initiate scrolling in the specific direction. Figuring out the drag_boundary is defined in the glossary below, so I won&amp;rsquo;t repeat it here.&lt;/p&gt;
&lt;h5&gt;Glossary&lt;/h5&gt;
&lt;p&gt;Apart from what&amp;rsquo;s already above, we also have these properties:&lt;/p&gt;
&lt;table class="data" cellspacing="0" cellpadding="0" border="0"&gt;
	&lt;thead&gt;
		&lt;tr&gt;&lt;th&gt;property&lt;/th&gt;&lt;th&gt;description&lt;/th&gt;&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
		&lt;tr&gt;&lt;td&gt;scroll_sensitivity&lt;/td&gt;&lt;td&gt;The distance from the scroll_element&amp;rsquo;s boundary points to initiate scrolling.&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;drag_boundary&lt;/td&gt;&lt;td&gt;This is an object representing the drag_image dimensions plus the scroll_sensitivity. This is worked out as:
&lt;pre&gt;&lt;code&gt;var drag_boundary = {
   bottom : drag_image.height + drag_image.aggregateOffsetTop + scroll_sensitivity,
   left   : drag_image.aggregateOffsetLeft - scroll_sensitivity,
   right  : drag_image.width + drag_image.aggregateOffsetLeft + scroll_sensitivity,
   top    : drag_image.aggregateOffsetTop - scroll_sensitivity
};&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;scroll_amount&lt;/td&gt;&lt;td&gt;The amount in pixels to scroll by if scrolling is required.&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;scrollX&lt;/td&gt;&lt;td&gt;If scrolling is required, then this will be set to either positive scroll_amount (to scroll down/ right) or negative scroll_amount (to scroll up/ left).&lt;/td&gt;&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;h5&gt;Scrolling horizontally&lt;/h5&gt;
&lt;pre&gt;&lt;code&gt;IF orientation != vertical
   IF drag_boundary.right &amp;gt; viewable_area.right &amp;amp;&amp;amp; drag_boundary.right &amp;lt; scroll_element.scrollWidth &amp;amp;&amp;amp; scroll_element.scrollWidth &amp;gt; scroll_element.scrollLeft + scroll_sensitivity
      scrollX = scroll_amount
   ELSE IF drag_boundary.left &amp;lt; viewable_area.left &amp;amp;&amp;amp; scroll_element.scrollLeft &amp;gt;= 0
      scrollX = -scroll_amount

   IF scrollX IS A NUMBER
      scroll_element.scrollLeft += scrollX&lt;/code&gt;&lt;/pre&gt;
&lt;h5&gt;Scrolling vertically&lt;/h5&gt;
&lt;p&gt;Again, apart from what is already above, we swap the corresponding properties:&lt;/p&gt;
&lt;table class="data" cellspacing="0" cellpadding="0" border="0"&gt;
	&lt;thead&gt;
		&lt;tr&gt;&lt;th&gt;left (x) property&lt;/th&gt;&lt;th&gt;top (y) property&lt;/th&gt;&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
		&lt;tr&gt;&lt;td&gt;drag_boundary.left&lt;/td&gt;&lt;td&gt;drag_boundary.top&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;drag_boundary.right&lt;/td&gt;&lt;td&gt;drag_boundary.bottom&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;scroll_element.scrollLeft&lt;/td&gt;&lt;td&gt;scroll_element.scrollTop&lt;/td&gt;&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;Using pseudocode and diagrams, we can clearly visualise user interface interactions and this in turn will help us write clean and efficient code.&lt;/p&gt;
&lt;p&gt;Obviously, project time constraints are an issue and a lot of development teams are pushed to get something live rather than something — of quality — that works. This type of careful planning often takes a back seat to showing working code for your next &lt;a href="http://en.wikipedia.org/wiki/Scrum_%28development%29"&gt;sprint&lt;/a&gt; so you can rush through the next chunk of work.&lt;/p&gt;
&lt;p&gt;Having the opportunity to take time off, to focus on development with an emphasis on quality is an amazing experience. It&amp;rsquo;s going to be hard if I have to go back!&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/515940368</link><guid>https://thudjs.tumblr.com/post/515940368</guid><pubDate>Mon, 12 Apr 2010 16:54:00 +0100</pubDate></item><item><title>the dragging and droppings</title><description>&lt;p&gt;For the past two weeks I&amp;rsquo;ve been working on the drag and drop module for thud. Even though I had read &lt;a href="http://www.quirksmode.org/blog/archives/2009/09/the_html5_drag.html"&gt;PPK&amp;rsquo;s: The HTML5 drag and drop disaster&lt;/a&gt;. I thought, &amp;ldquo;Wow! I can use the new HTML5 drag and drop events to build my drag and drop module!&amp;rdquo; &lt;strong&gt;WRONG!!!&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I won&amp;rsquo;t go into everything he&amp;rsquo;s already talked about in his post, it&amp;rsquo;s brilliant, and if you haven&amp;rsquo;t read it you absolutely should. I would like to, however, extend the list of grievances:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Setting a drag image — the html snippet or image you see following the cursor — is convoluted. What if I wanted to use the item I clicked on rather than a copy of it? There isn&amp;rsquo;t a method in place for that.&lt;/li&gt;
	&lt;li&gt;Scrolling the document instead of exiting the browser window. Currently dragging allows you to drag outside of the browser window. What if i want to restrict the dragging to within the current document and scroll the document in the direction i am dragging when i hit the edge?&lt;/li&gt;
	&lt;li&gt;Constraining the drag to specific coordinates in the document (e.g. { bottom : 400, left : 10, right : 400, top : 10 }).&lt;/li&gt;
	&lt;li&gt;Limiting the drag to only horizontal or vertical directions.&lt;/li&gt;
	&lt;li&gt;Snap to grid dragging (e.g. a 16px snap would only move the drag image in increments of 16px in any direction).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These options are all available in the drag and drop implementations of various frameworks I&amp;rsquo;ve worked used — MooTools, Ext, Scriptaculous, etc&amp;hellip; — and are there for good reason, we need them to give the user a rich experience when building UI components!&lt;/p&gt;
&lt;p&gt;My feeling is I will need 2 different types of drag and drop in thud. One will be for restricting UI components to a page (non-html5 implementation); the other will be for dragging objects from external sources to the document and vice versa (html5 implementation). WAH! :&amp;rsquo;(&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/501011181</link><guid>https://thudjs.tumblr.com/post/501011181</guid><pubDate>Tue, 06 Apr 2010 17:50:00 +0100</pubDate><category>javascript</category><category>html5</category></item><item><title>wtf!? new commodore 64!</title><description>&lt;a class="fr" href="http://www.commodoreusa.net/design.html"&gt;&lt;img alt="commodore c4 revamped" src="http://www.commodoreusa.net/i//9100_2-1.jpg" style="height : 185px ; width : 320px ;"/&gt;&lt;/a&gt;
&lt;p&gt;I was looking for some design inspiration today when I happened to see this &lt;a href="http://www.arnnet.com.au/article/340148/commodore_64_awakes_from_slumber_makeover/"&gt;link&lt;/a&gt; in my search results.&lt;/p&gt;
&lt;p&gt;Seeing the Commodore 64 again made me think how it was similar to the Apple iMac, only it needs a monitor instead of a keyboard. Then I realised, the Commodore 64 was doing this from its beginnings. However, as I&amp;rsquo;m writing this, I also realised that Apple has also been doing the whole computer in the monitor thing since 1983 with the introduction of the &lt;a href="http://oldcomputers.net/lisa.html"&gt;Apple Lisa&lt;/a&gt; and then the following year with the first &lt;a href="http://oldcomputers.net/macintosh.html"&gt;Macintosh&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m wondering though if you can combine the two to get some awesome processing power and two machines using the same monitor without any loss of space. Would probably require at least &lt;a href="http://www.mnftiu.cc/2002/11/26/filing-009/"&gt;3 rolls of scotch tape though&lt;/a&gt;.&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/493857162</link><guid>https://thudjs.tumblr.com/post/493857162</guid><pubDate>Sat, 03 Apr 2010 19:58:00 +0100</pubDate><category>random</category></item><item><title>Hackers and Painters</title><description>&lt;a href="http://www.paulgraham.com/hp.html"&gt;Hackers and Painters&lt;/a&gt;: &lt;p&gt;Hacking and painting have a lot in common. In fact, of all the different types of people I’ve known, hackers and painters are among the most alike.&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/492955425</link><guid>https://thudjs.tumblr.com/post/492955425</guid><pubDate>Sat, 03 Apr 2010 09:21:00 +0100</pubDate><category>random</category></item><item><title>i see your schwartz is bigger than mine</title><description>&lt;img alt="scene from the movie spaceballs" src="http://thudjs.com/blog/assets/schwartz_sm.jpg" style="float : right ; height : 158px ; margin : 8px 8px 8px 0 ; width : 297px ;" title="scene from the movie spaceballs"/&gt;
&lt;p&gt;A week or two ago a &lt;a href="http://www.mutteringmadman.com"&gt;perl 6 enthusiast friend of mine&lt;/a&gt; mentioned how he&amp;rsquo;d, &amp;ldquo;just used a &lt;a href="http://en.wikipedia.org/wiki/Schwartzian_transform"&gt;Schwartzian transform&lt;/a&gt; to answer a guys question in #perl&amp;rdquo; during the brief passing of an otherwise routine instant message conversation.&lt;/p&gt;
&lt;p&gt;Knowing nothing about the Schwartzian transform — in fact, only hearing of its existence for the first time — and not wanting to look like a narc in front of my nerd fanboy friend. I did what any self-respecting good guy would do. I turned to &lt;a href="http://www.theonion.com/articles/google-launches-the-google-for-older-adults,5850/"&gt;the google&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I skimmed through the &lt;a href="http://en.wikipedia.org/wiki/Schwartzian_transform"&gt;wikipedia page&lt;/a&gt; felt slightly confused, read it properly and gained a better understanding. I also happened on &lt;a href="http://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Schwartzian_transform"&gt;this page&lt;/a&gt; which lists the implementation of the Schwartzian tranform in several different languages. Except one, your favourite AND mine: JavaScript.&lt;/p&gt;
&lt;p&gt;So I decided I would implement a Schwartzian transform in JavaScript and design a &lt;a href="http://thudjs.com/blog/schwartziantransform.html"&gt;crude test&lt;/a&gt; to see the performance benefits for myself.&lt;/p&gt;
&lt;p&gt;The test sorts arrays of strings — containing numbers with thousands separators — in ascending order of lengths: 20, 200, 800, 1600 &amp;amp; 2000; and uses arrays in descending order, as well as randomly ordered arrays.&lt;/p&gt;
&lt;p&gt;Before I explain the benefits of using a Schwartzian transform over a regular sort, I&amp;rsquo;ll go through the code required to implement a Schwartzian transform.&lt;/p&gt;
&lt;p&gt;As it turns out, this is hella-easy and — the actual sort — only requires one line of code, if you like squeezing code onto one line. For the purposes of explaining this technique though, I will be more verbose.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;function getValue( o ) {
    /* returns a specific value from item: o */
}
function compareFunction( a, b ) { // depending on how you want to sort, asc or desc, your sort function would look something like this
    var _a = a[1], _b = b[1];
    return _a == _b ? 0 : _a &amp;lt; _b ? -1 : 1;
}
var unsorted_array      = [/* array of stuff */], // the schwartz starts here
    paired_array        = unsorted_array.map( function( o ) { return [o, getValue( o )]; } ),
    paired_array_sorted = paired_array.sort( compareFunction ),
    sorted_array        = paired_array_sorted.map( function( o ) { return o[0]; } );
 &lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;Breaking it down&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;unsorted_array&lt;/strong&gt; — is the array of unsorted values we want to sort.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;paired_array&lt;/strong&gt; — is the result of iterating through unsorted_array to create and return, a new array, of tuples, containing the original item and the value we want to sort on. This would look something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
[[original_item_1, sort_value_1], [original_item_2, sort_value_2], ..., [original_item_N, sort_value_N]]
 &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;paired_array_sorted&lt;/strong&gt; — is the result of sorting paired_array, though as sorting happens inline, paired_array_sorted is the same as paired_array.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;sorted_array&lt;/strong&gt; — is the result of iterating through paired_array_sorted and returning the first, original, item from each tuple.&lt;/p&gt;
&lt;p&gt;Putting it all on one line we can get something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
sorted_array = unsorted_array.map( function( o ) { return [o, getValue( o )]; } ).sort( compareFunction ).map( function( o ) { return o[0]; } );
 &lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;What&amp;rsquo;s the benefit of using a Schwartzian tranform?&lt;/h4&gt;
&lt;p&gt;When I first saw this, I thought it was kind of complex and that running a map on the array we want to sort, twice, was excessive. But have a look at the code for just using a regular sort:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;function getValue( o ) {
    /* returns a specific value from item: o */
}
function compareFunction( a, b ) { // depending on how you want to sort, asc or desc, your sort function would look something like this
    var _a = &lt;strong&gt;getValue( a )&lt;/strong&gt;, _b = &lt;strong&gt;getValue( b )&lt;/strong&gt;;
    return _a == _b ? 0 : _a &amp;lt; _b ? -1 : 1;
}
var unsorted_array = [/* array of stuff */],
    sorted_array   = unsorted_array.sort( compareFunction );
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At first glance this looks much more elegant, I mean there is a lot less code. But in actual fact this sort can take a significantly longer amount of time to complete than the Schwartzian tranform sort, depending on the time it takes to retrieve each value from the &lt;code&gt;getValue&lt;/code&gt; function. &lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote cite="http://en.wikibooks.org/wiki/Optimizing_Code_for_Speed#The_Schwartzian_Transform"&gt;&lt;p&gt;&amp;hellip;the Schwartzian transform actually reduced the order of complexity of the number of times the keys are calculated from O(N*log(N)) to O(N).&lt;/p&gt;&lt;/blockquote&gt;
&lt;p class="source"&gt;&lt;cite&gt;&lt;a href="http://en.wikibooks.org/wiki/Optimizing_Code_for_Speed#The_Schwartzian_Transform"&gt;Optimizing Code for Speed - wikibooks&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;p&gt;In plain english this means:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Using the Schwartzian method, &lt;strong&gt;we calculate the values we want to sort on, for every item in the array, ONCE&lt;/strong&gt;, storing them with their original item in a new array.&lt;/li&gt;
	&lt;li&gt;Using a regular sort, &lt;strong&gt;we are calculating the values we want to sort on, EVERY TIME the sort function compares two items in the array&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;Chart time!&lt;/h5&gt;
&lt;p&gt;So with this post almost at an end, it&amp;rsquo;s time to display a chart, showing the efficiency of the Schwartzian tranform over a regular sort, for the &lt;a href="http://thudjs.com/blog/schwartziantransform.html"&gt;test I created&lt;/a&gt;.&lt;/p&gt;
&lt;img alt="graph: regular sort vs schwartzian transform" class="nodeco" src="http://thudjs.com/blog/assets/graph_regular_sort_vs_schwartzian_transform.png"/&gt;
&lt;p&gt;I ran the test in the latest stable versions of each browser on a MacBook Pro with OS X 10.6.3. Except Internet Explorer which I tested using a VM running Windows XP. Sorry the graph times aren&amp;rsquo;t incrementing nicely, Internet Explorer&amp;rsquo;s times blow the whole graph out and I wanted to show the distributions more clearly.&lt;/p&gt;
&lt;p&gt;As you can see though, in each browser the Schwartzian transform far out performs a regular sort for this test.&lt;/p&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;The Schwartzian transform is a simple method we can use to improve the performance of sorting arrays of items, where the values we wish to sort on involve expensive calculations. However, you should always consider when to use this method on a case by case basis.&lt;/p&gt;
&lt;p class="note"&gt;PS. This is &lt;strong&gt;NOT&lt;/strong&gt; an April fools&amp;rsquo; day prank. :¬P&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/489270285</link><guid>https://thudjs.tumblr.com/post/489270285</guid><pubDate>Thu, 01 Apr 2010 17:12:00 +0100</pubDate><category>javascript</category><category>performance</category></item><item><title>css organisation: putting it all together</title><description>&lt;p&gt;Ok, so no series is complete without an example. I&amp;rsquo;ve created two very simple pages, which should look the same as each other. However, they will degrade gracefully in less capable browsers.&lt;/p&gt;
&lt;p&gt;The pages all have their CSS inline, except for the reset CSS. They have been tested on FF3.6, Safari4.0.5, Opera10.5beta, Chrome4 and MSIE6+.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://thudjs.com/blog/css_organised.html"&gt;The first page&lt;/a&gt; achieves &lt;a href="/post/428000513/a-b-a-cross-browser-css"&gt;cross browser css&lt;/a&gt; using JavaScript, &lt;a href="/post/443377569/css-organisation-alphabetising-style-properties"&gt;alphabetised style properties&lt;/a&gt;, &lt;a href="/post/449622966/css-organisation-hyphenate-classes-and-ids-use-short"&gt;hyphenated classes and short consistent naming conventions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://thudjs.com/blog/css_unorganised.html"&gt;The second page&lt;/a&gt; uses conditional comments, browser hacks, style properties in any order and camelCase class names.&lt;/p&gt;
&lt;p&gt;My (biased) opinion is that &lt;a href="http://thudjs.com/blog/css_organised.html"&gt;the first page&amp;rsquo;s&lt;/a&gt; CSS is easier and quicker to read, debug and update. I can clearly see all the browser adjustments for a specific rule and if I need to make a change to a property or add a new browser or browser version override, I know immediately where to look. If a user has JavaScript turned off for whatever reason — &lt;a href="http://thudjs.com/blog/css_organised_nojs.html"&gt;try it&lt;/a&gt; — the page doesn&amp;rsquo;t explode in any of the tested browsers. If I walked into a new contract with a CSS code base similar to this, for a change, I would be one happy chappy!&lt;/p&gt;
&lt;p&gt;What I don&amp;rsquo;t like about &lt;a href="http://thudjs.com/blog/css_unorganised.html"&gt;the second page&lt;/a&gt; is all the scrolling I have to do to see all the browser and browser version overrides. What makes this worse is that generally you would put the Internet Explorer hacks in separate files, this would mean having to tab back and forth between files, it really slows you down and as I&amp;rsquo;ve already said, you have hacks everywhere. No thanks, not for me; if it has to be done, I like to be able to see it all in the one place.&lt;/p&gt;
&lt;p&gt;So, at this point I&amp;rsquo;d like to ask  — if anyone out there in internet land actually reads this blog — What do you think? Which is your preference and why? If you are generally a page two kind of developer, would you consider doing things in a page one kind of way in the future? And if not, why not?&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/456372117</link><guid>https://thudjs.tumblr.com/post/456372117</guid><pubDate>Thu, 18 Mar 2010 10:00:00 +0000</pubDate><category>css</category><category>a better approach</category></item><item><title>css organisation: hyphenate classes and ids, use short and consistent naming conventions</title><description>&lt;p&gt;In this post we will see how implementing a consistent naming convention for our CSS classes and ids, using hyphen delimiters, will allow us to easily arrange our CSS into concise modules making our style sheets more organised and can also speed up our page loading and rendering too.&lt;/p&gt;
&lt;p&gt;For the remainder of this post I will be referring mainly to CSS classes, though the same principles can and should be applied to IDs as well.&lt;/p&gt;
&lt;h5&gt;Use hyphens, not camelCase&lt;/h5&gt;
&lt;p&gt;There is nothing stopping you from using camel case. I&amp;rsquo;ve always found using camel case for defining CSS classes makes your code look messy and harder to read when skimming through an html page. It is also easier to search for hyphenated CSS classes in JavaScript rather than camel case as we generally uses camel case naming conventions in JavaScript. Consider the following code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ns.fooBar = $( '.fooBar' );
ns.fooBar.bind( 'click', function() {
   ns.fooBar.toggleClass( 'selected' );
} );&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Compared to this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ns.fooBar = $( '.foo-bar' );
ns.fooBar.bind( 'click', function() {
   ns.fooBar.toggleClass( 'selected' );
} );&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Your eye has a much easier time finding the &lt;strong&gt;foo-bar&lt;/strong&gt; CSS class amongst all the &lt;strong&gt;fooBar&lt;/strong&gt; JavaScript variables. When you have thousands of lines of code to skim through, this can become a valuable time saver. If you want to do a search and replace on &lt;code&gt;.fooBar&lt;/code&gt;, it also leaves a greater margin for creating a JavaScript error — as you can see the JavaScript property &lt;code&gt;ns.fooBar&lt;/code&gt; is also preceded by a period — whereas search and replacing on &lt;code&gt;.foo-bar&lt;/code&gt; would not only be a much quicker search, it almost eliminates the margin for error by excluding all &lt;strong&gt;fooBar&lt;/strong&gt; strings. Hyphens are not valid characters for standard JavaScript variable/ property names, it&amp;rsquo;s worthwhile having thus clear separation of presentation and behaviour.&lt;/p&gt;
&lt;p&gt;However, if that&amp;rsquo;s not enough to convince you. Using a hyphen also makes it easier to use a CSS class to hold values we want to later retrieve using JavaScript. Consider the following simple example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;div class="carousel &lt;strong&gt;horizontal3easeIn&lt;/strong&gt;"&amp;gt;...&amp;lt;/div&amp;gt;

&amp;lt;script type="text/javascript"&amp;gt;
   var optionsStr = documents.getElementsByClassName( 'carousel' )[0].className.split( ' ' )[1], 
       carouselOptions = &lt;strong&gt;optionsStr.match( /([A-Za-z]+)(d+)(.*)/ )&lt;/strong&gt;, // regex to extract carousel options
       carousel = new Carousel( {
          orientation : carouselOptions[0],
          itemsPerPage : carouselOptions[1],
          animation : carouselOptions[2]
       } );
&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To get the options to initialise the JavaScript Carousel class we need to create a regex to extract the values from the CSS class, &lt;strong&gt;horizontal3easeIn&lt;/strong&gt;. This means, if we want to add more options or change the order of these options, we would need to keep adjusting the regex as well as the instantiation code. Consider the next example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;div class="carousel &lt;strong&gt;horizontal-3-easein&lt;/strong&gt;"&amp;gt;...&amp;lt;/div&amp;gt;

&amp;lt;script type="text/javascript"&amp;gt;
   var optionsStr = documents.getElementsByClassName( 'carousel' )[0].className.split( ' ' )[1], 
       carouselOptions = &lt;strong&gt;optionsStr.split( '-' )&lt;/strong&gt;, // simple String.split
       carousel = new Carousel( {
          orientation : carouselOptions[0],
          itemsPerPage : carouselOptions[1],
          animation : carouselOptions[2]
       } );
&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this example we only need to split the CSS class, &lt;strong&gt;horizontal-3-easein&lt;/strong&gt;, at the hyphen delimeter. There&amp;rsquo;s nothing complicated and it&amp;rsquo;s a lot faster than using a regex.&lt;/p&gt;
&lt;p&gt;So to keep things simple, consistent and fast we will only use hyphens in our CSS code.&lt;/p&gt;
&lt;h4&gt;Don&amp;rsquo;t include the tag name!&lt;/h4&gt;
&lt;p&gt;The amount of times I&amp;rsquo;ve seen code like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;style type="text/css"&amp;gt;
   #containerDiv {}
   .leftDiv {}
   .middleDiv {}
   .rightDiv {}
&amp;lt;/style&amp;gt;
...
&amp;lt;div id="containerDiv"&amp;gt;
  &amp;lt;div class="leftDiv" /&amp;gt;
  &amp;lt;div class="middleDiv" /&amp;gt;
  &amp;lt;div class="rightDiv" /&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I mean seriously, at what point do you think you&amp;rsquo;re going to forget any of these are a &lt;strong&gt;div&lt;/strong&gt; tag and if you did why would that matter?&lt;/p&gt;
&lt;p&gt;There is really no need for this kind of verbose commentary in HTML/ CSS, leave it out, it&amp;rsquo;s annoying and looks amateurish. That&amp;rsquo;s all I have to say about that.&lt;/p&gt;
&lt;h4&gt;Use classes not tags&lt;/h4&gt;
&lt;p&gt;These two fantastic articles on &lt;a href="https://developer.mozilla.org/en/Writing_Efficient_CSS"&gt;Writing Efficient CSS&lt;/a&gt; and &lt;a href="http://www.stevesouders.com/blog/2009/06/18/simplifying-css-selectors/"&gt;Simplifying CSS Selectors&lt;/a&gt;  outline how browsers match a rule and the performance losses and gains from using different types of CSS selectors. They also include links to further research on the subject. In my opinion this is all mandatory reading for anyone who builds web user interfaces using HTML/ CSS.&lt;/p&gt;
&lt;h5&gt;Adaptability&lt;/h5&gt;
&lt;p&gt;The one thing I will say is, using CSS classes instead of tags will allow you to change a tag in your HTML document without having to change your style sheet.&lt;/p&gt;
&lt;p&gt;The best example I can give is the use of headings. If you are as fanatical about correctly nesting your headings as I am, then consider the following example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;style type="text/css"&amp;gt;
   .foo-ct h3 {}
&amp;lt;/style&amp;gt;
...
&amp;lt;div class="foo"&amp;gt;
   &amp;lt;div class="foo-ct"&amp;gt;
     &lt;strong&gt;&amp;lt;h3 class="foo-hd" /&amp;gt;&lt;/strong&gt;
     &amp;lt;div class="foo-bd" /&amp;gt;
     &amp;lt;div class="foo-ft" /&amp;gt;
   &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we want to reuse this module in a different section of our page/ site where the &lt;code&gt;&amp;lt;h3&amp;gt;&lt;/code&gt; would be an &lt;code&gt;&amp;lt;h4&amp;gt;&lt;/code&gt;, we would need to add more styles to our stylesheet to reflect this or our headings could potentially be different sizes and worse, lacking specific styles like backgrounds, borders, colours, etc.&lt;/p&gt;
&lt;p&gt;Using the CSS class &lt;code&gt;.foo-hd&lt;/code&gt; instead of the tag, allows us to adapt our mark up without having to, also adjust, our style sheets.&lt;/p&gt;
&lt;h4&gt;Having a system/ Keeping it simple&lt;/h4&gt;
&lt;p&gt;Having tried to implement this type of naming convention in a few jobs, I&amp;rsquo;ve seen some developers churn out CSS/ HTML code with some crazily long class names — I&amp;rsquo;ve seen some class names with over 30 characters. Consider the following (tame) example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;style type="text/css"&amp;gt;
   .foo {}
   .foo-container {} 
   .foo-container-header {} 
   .foo-container-body {} 
   .foo-container-footer {} 
&amp;lt;/style&amp;gt;
...
&amp;lt;div class="foo"&amp;gt;
  &amp;lt;div class="foo-container"&amp;gt;
	 &amp;lt;h3 class="foo-&lt;strong&gt;container-&lt;/strong&gt;header" /&amp;gt;
	 &amp;lt;div class="foo-&lt;strong&gt;container-&lt;/strong&gt;body" /&amp;gt;
	 &amp;lt;div class="foo-&lt;strong&gt;container-&lt;/strong&gt;footer /"&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Repetition like this seems ridiculous. Why do we need to repeat the word &lt;strong&gt;container&lt;/strong&gt; in each of its childrens&amp;rsquo; class names?&lt;/p&gt;
&lt;p&gt;The first thing we can do is get rid of the &lt;code&gt;container-&lt;/code&gt; in the header, body and footer div tag class names. That&amp;rsquo;s easy, but why do we need long names like &lt;strong&gt;foo-container&lt;/strong&gt;, &lt;strong&gt;foo-header&lt;/strong&gt;, etc? If we want to be more efficient we can further reduce those names down, as in the following table:&lt;/p&gt;
&lt;table border="0" cellpadding="0" cellspacing="0" class="data"&gt;
	&lt;thead&gt;
		&lt;tr&gt;&lt;th style="width : 75px ;"&gt;class name&lt;/th&gt;&lt;th&gt;description&lt;/th&gt;&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
		&lt;tr&gt;&lt;td&gt;ct&lt;/td&gt;&lt;td&gt;container&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;hd&lt;/td&gt;&lt;td&gt;header&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;bd&lt;/td&gt;&lt;td&gt;body&lt;/td&gt;&lt;/tr&gt;
		&lt;tr&gt;&lt;td&gt;ft&lt;/td&gt;&lt;td&gt;footer&lt;/td&gt;&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Let&amp;rsquo;s look at the revised code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;style type="text/css"&amp;gt;
   .foo {}
   .foo-ct {}
   .foo-hd {}
   .foo-bd {}
   .foo-ft {}
&amp;lt;/style&amp;gt;
...
&amp;lt;div class="foo"&amp;gt;
   &amp;lt;div class="foo-ct"&amp;gt;
      &amp;lt;h3 class="foo-hd" /&amp;gt;
      &amp;lt;div class="foo-bd" /&amp;gt;
      &amp;lt;div class="foo-ft" /&amp;gt;
   &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Say you&amp;rsquo;re building a site for an online retailer and they are displaying 100 products on a page using this format. The first example has an extra &lt;strong&gt;4kb&lt;/strong&gt; of CSS class names in the HTML page for the end user to download!&lt;/p&gt;
&lt;p&gt;Having a set of abbreviated class names in a table like the one above —  that you can easily print out and stick on a wall visible to all developers, or as a card developers can have on their desks — can greatly reduce the amount of time developers need to spend coming up with their own class names, improve the consistency of your naming conventions and reduce your overall page weight, without the need of a lengthy document.&lt;/p&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;Having a consistent naming convention for your CSS classes and IDs, using a hyphen delimiter, and assigning style rules to CSS classes, instead of tags, will help to make your CSS easier to read, easier to program, with JavaScript and potentially quicker to render.&lt;/p&gt;
&lt;p&gt;Abbreviating commonly used CSS class names can help to reduce your overall page weight. Printing them out, in a table and making them easily accessible to all developers, provides a very simple guideline to follow without the use of lengthy documents.&lt;/p&gt;
&lt;p class="note"&gt;&lt;strong&gt;This is part 2 of a 3 part series.&lt;/strong&gt;&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/449622966</link><guid>https://thudjs.tumblr.com/post/449622966</guid><pubDate>Mon, 15 Mar 2010 08:58:00 +0000</pubDate><category>css</category><category>a better approach</category></item><item><title>css organisation: alphabetising style properties</title><description>&lt;p&gt;I started off alphabetising my style properties very early in my web development career. &lt;a href="http://www.w3.org/TR/CSS1/"&gt;CSS1&lt;/a&gt; was still relatively new so we were all still using the font tag and spacer images inside nested tables.&lt;/p&gt;
&lt;p&gt;My main reason for this wasn&amp;rsquo;t the orderly, easy to find, easy to read and difficult to duplicate the same property in the same rule structure that was to ensue. It was purely because of a slight obsessive compulsive tendency for neatness.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.stubbornella.org/"&gt;Nicole Sullivan&lt;/a&gt; wrote an excellent &lt;a href="http://www.stubbornella.org/content/2009/11/09/css-wish-list/"&gt;CSS wish list&lt;/a&gt; a few months ago. However, this post is about what we can do to improve the here and now.&lt;/p&gt;
&lt;h4&gt;Having a system/ Keeping it simple&lt;/h4&gt;
&lt;p&gt;When trying to manage any code base, having a system is important. This is generally available in the form of coding standards and guidelines. Unfortunately, these documents can quickly become bloated, reducing their usefulness, by reducing their &amp;ldquo;readability&amp;rdquo;; lost in a sea of standards and guidelines on dated wiki or worse, a forgotten directory living in a far off network drive; or as in most cases, due to unrealistic deadlines, standards taking a back seat to an iota&amp;rsquo;s more code been churned out.&lt;/p&gt;
&lt;p&gt;Keeping a simple system, such as alphabetising your properties makes it not only easy to remember, but also easy for developers joining your existing project to become familiar with.&lt;/p&gt;
&lt;p&gt;As well as alphabetising my CSS, I also like to include similar properties on the same line — think border-*, font-*, margin-*, etc — as long as readability is maintained. Consider the following rule:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.foo {
   background : #fcc ; 
   border-color : #fee #fee #faa #faa ; border-style : solid ; border-width : 1px ; 
   color : #333 ; 
   float : left ; 
   font-size : 1.1em ; font-weight : bold ; 
   height : 50px ; 
   margin : 3px ; margin-left : 5px ; 
   padding : 2px 4px 3px ; 
   position : relative ; 
   width : 150px ; 
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If I want to know what font properties are declared in the rule, my eye scrolls down from b, c to &lt;strong&gt;f&lt;/strong&gt;, if I want to know what margin properties are declared, my eye scrolls down from b, c, f, h to &lt;strong&gt;m&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Compare this to the same rule with the properties in a random order:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.foo {
   position : relative ;
   float : left ;
   height : 50px ;
   width : 150px ;
   margin : 3px ; 
   border-color : #fee #fee #faa #faa ; 
   border-style : solid ; 
   border-width : 1px ;
   padding : 2px 4px 3px ; 
   margin-left : 5px ; 
   font-size : 1.1em ;
   background : #fcc ;
   color : #333 ; 
   font-weight : bold ;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first rule is making CSS easy to skim through, eliminating the time it takes to find specific properties in a rule. The second rule requires more time to look through to find there is &lt;strong&gt;margin-left&lt;/strong&gt; override on the previous &lt;strong&gt;margin&lt;/strong&gt; property, more time to see that there is a &lt;strong&gt;font-size&lt;/strong&gt; property as well as &lt;strong&gt;font-weight&lt;/strong&gt; property on the rule, etc.&lt;/p&gt;
&lt;p&gt;Though this &amp;ldquo;extra time&amp;rdquo; might not seem like much at first, when you start looking at the size of some CSS files — anywhere from 700-2000+ lines — it can quickly add up!&lt;/p&gt;
&lt;p&gt;Tools like &lt;a href="http://getfirebug.com/"&gt;Firebug&lt;/a&gt; alphabetise your rules for you — as well as showing you what rules belong to what files — in their CSS editors. However, this doesn&amp;rsquo;t always stop people lazily copying and pasting the code from firebug into their CSS files without first removing the existing rules. These tools also don&amp;rsquo;t show duplicate properties on a specific rule, so be careful not to duplicate properties!&lt;/p&gt;
&lt;h4&gt;Rules are meant to be broken (sometimes): Browser specific CSS3 properties&lt;/h4&gt;
&lt;p&gt;The only caveat to this very simple method of keeping your CSS tidy is the new &lt;a href="http://www.css3.info/preview/"&gt;CSS3 properties&lt;/a&gt; available in some browsers. The main reason being: browsers&amp;rsquo; implementations of these properties using their own specific namespace e.g. -moz-*, -webkit-*, etc.&lt;/p&gt;
&lt;p&gt;There are two ways we can work with these properties: by sticking to the original guideline and having all the browser specific properties at the top or bottom of the rule. Shown below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.foo {
   background : #fcc ; 
   border-color : #fee #fee #faa #faa ; border-radius : 3px ; border-style : solid ; border-width : 1px ; 
   box-shadow : 1px 1px 7px #999 ; 
   color : #333 ; 
   float : left ; 
   font-size : 1.1em ; font-weight : bold ; 
   height : 50px ; 
   margin : 3px ; margin-left : 5px ; 
   padding : 2px 4px 3px ; 
   position : relative ; 
   width : 150px ; 
   -moz-border-radius : 3px ; 
   -moz-box-shadow : 1px 1px 7px #999 ; 
   -webkit-border-radius : 3px ; 
   -webkit-box-shadow : 1px 1px 7px #999 ;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or, by separating out the CSS3 properties onto a separate line and including the browser specific versions of these properties on the same line, as in the example below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.foo {
   background : #fcc ; 
   border-color : #fee #fee #faa #faa ; border-style : solid ; border-width : 1px ; 
   border-radius : 3px ; -moz-border-radius : 3px ; -webkit-border-radius : 3px ; 
   box-shadow : 1px 1px 7px #999 ; -moz-box-shadow : 1px 1px 7px #999 ; -webkit-box-shadow : 1px 1px 7px #999 ; 
   color : #333 ; 
   float : left ; 
   font-size : 1.1em ; font-weight : bold ; 
   height : 50px ; 
   margin : 3px ; margin-left : 5px ; 
   padding : 2px 4px 3px ; 
   position : relative ; 
   width : 150px ; 
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;My preference is for the latter version. My reasons being:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;It&amp;rsquo;s easier to update the properties as they&amp;rsquo;re all on the same line; and&lt;/li&gt;
	&lt;li&gt;It&amp;rsquo;s easier to skim through the standard CSS3 properties and then see the browser specific properties next to them.&lt;/li&gt;
&lt;/ul&gt;The standards are in the files
&lt;p&gt;Simple standards like these become so easy to follow that you generally don&amp;rsquo;t need much documentation to get someone going. The documentation is in the code you are working from, it in itself becomes the standards and the examples. &lt;strong&gt;IF&lt;/strong&gt; a developer joining your project has a shred of intelligence, then they can generally get by with a only bit of mentoring.
&lt;/p&gt;&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;Alphabetising your CSS properties is an easy way to neaten up your CSS, eliminate the possibility of duplicate properties in the same rule and it generally speeds up finding properties in a rule.&lt;/p&gt;
&lt;p&gt;It will greatly improve a new developer&amp;rsquo;s familiarisation with your project&amp;rsquo;s CSS codebase, as it provides a very simple guideline to follow.&lt;/p&gt;
&lt;p class="note"&gt;&lt;strong&gt;This is part 1 of a 3 part series.&lt;/strong&gt;&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/443377569</link><guid>https://thudjs.tumblr.com/post/443377569</guid><pubDate>Fri, 12 Mar 2010 14:31:00 +0000</pubDate><category>css</category><category>a better approach</category></item><item><title>a.b.a: cross browser css</title><description>&lt;p class="note"&gt;I first saw this technique in &lt;a href="http://jackslocum.com/blog/"&gt;Jack Slockum’s&lt;/a&gt; &lt;a href="http://www.extjs.com/"&gt;Ext JS framework&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;There are a &lt;a href="http://paulirish.com/2009/browser-specific-css-hacks/"&gt;range of CSS hacks&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/ms537512%28VS.85%29.aspx"&gt;other types of work arounds&lt;/a&gt; available to target the inconsistencies in the various CSS rendering implementations of different browsers; and different browser versions.&lt;/p&gt;
&lt;p&gt;A lot of developers I’ve worked with vehemently support the use &lt;a href="http://msdn.microsoft.com/en-us/library/ms537512%28VS.85%29.aspx"&gt;conditional comments&lt;/a&gt; — this is, to insert IE specific stylesheets into a page, to target the various versions of Internet Explorer. Their reasons being:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It’s only picked up by Internet Explorer so other browsers don’t have to worry about downloading irrelevant CSS code.&lt;/li&gt;
&lt;li&gt;You can keep all your different Internet Explorer CSS hacks separate so as you stop supporting versions you can simply remove the offending file and conditional comment code.&lt;/li&gt;
&lt;li&gt;It’s neater than having all your CSS hacks in the one file.&lt;/li&gt;
&lt;li&gt;CSS hacks aren&amp;rsquo;t generally valid CSS and your stylesheet will not validate if you include them in your stylesheets.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Needless to say I tend to disagree with all four of these reasons. My reasons being:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What about versions of Firefox, Opera and Safari that need specific hacks in place to get them to display your page correctly? Now you have some hacks in your main stylesheets and others in another file. All your hacks are all over the place, it makes bug fixing a lot more time consuming!&lt;/li&gt;
&lt;li&gt;Having some styles, in a separate stylesheet, that override styles in your main stylsheet — for a specific browser/ version — makes it harder to see the overall all picture. I have to either remember, or worse, if I am not familiar with the codebase, constantly check the IE specific stylesheet to see if IE specific hacks are in place. It makes developing and debugging a lot more frustrating and time consuming.&lt;/li&gt;
&lt;li&gt;I now not only have separate stylesheets to worry about, I have to add conditional comments to my templates to make sure they pick that stylesheet up, then manage the stylesheets AND the conditional comments when new versions of a browser come out. Generally this means — for sites with badly designed templates, which, from what I have seen, is a lot — regenerating, retesting and releasing the whole site again. Very time consuming!&lt;/li&gt;
&lt;li&gt;It’s an extra HTTP request for only IE, especially IE6 which only supports 2 downloads per domain, and probably for only a small amount of extra CSS code.&lt;/li&gt;
&lt;li&gt;IE is probably your biggest market share and you’re punishing them with this extra download.&lt;/li&gt;
&lt;li&gt;It’s easier to see what’s going on when you have all your hacks for each browser grouped together rather than scattered all over the place.&lt;/li&gt;
&lt;li&gt;How many users care whether or not your CSS validates? Chances are it’s going to be less than a percent of them.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;A Better Approach&lt;/h4&gt;
&lt;p&gt;I’ve used Ext JS on a few big &amp;ldquo;single page application&amp;rdquo; projects over the past 4 years. It’s an amazing framework, In my opinion, it’s probably the best and most complete — inlcuding documentation and samples — JavaScript UI framework on the market, at the moment.&lt;/p&gt;
&lt;p&gt;One of the first things I wanted to know was: how does it get it’s UI components to look the same in all supported browsers?&lt;/p&gt;
&lt;p&gt;It definitely doesn’t use conditional comments because you only need to include one stylesheet and one JavaScript file if you want to use everything Ext has to offer. I started looking at its stylesheets to see if I could find any hacks, none.&lt;/p&gt;
&lt;p&gt;What I did find were styles like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.ext-ie6 .x-form-text, .ext-ie7 .x-form-text {...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In case you can&amp;rsquo;t tell, this specific rule is targetting IE&amp;rsquo;s versions 6 &amp;amp; 7.&lt;/p&gt;
&lt;p&gt;So I looked at the &lt;a href="http://www.extjs.com/deploy/dev/docs/"&gt;Ext JS API doc&amp;rsquo;s&lt;/a&gt; generated markup in Firebug and saw this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;body class="ext-gecko ext-gecko3 ext-mac"/&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And in Safari’s debugger, this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;body class="ext-safari ext-mac"/&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In case you can’t guess, these classes were added to the body tag via JavaScript.&lt;/p&gt;
&lt;p&gt;To me this is a brilliant idea! So brilliant I have used it on other projects to make it easy for all developers to write cross browser CSS. I will also be using this approach in the thudjs framework for the same reasons.&lt;/p&gt;
&lt;h4&gt;But this will only work with JavaScript turned on…&lt;/h4&gt;
&lt;p&gt;All I can say to that is, &amp;ldquo;So?&amp;rdquo;. How many of your users are actually using your site WITHOUT JavaScript turned on? Do you actually test your site(s) without JavaScript turned on, in every browser? Chances are you’re either living in a fantasy world or your site is so simple you don’t require any hacks or cross browser CSS.&lt;/p&gt;
&lt;p&gt;This technique still conforms to the principles of &lt;a href="http://www.w3.org/TR/html-design-principles/#degrade-gracefully"&gt;graceful degredation&lt;/a&gt;/ &lt;a href="http://en.wikipedia.org/wiki/Progressive_enhancement"&gt;progressive enhancement&lt;/a&gt;. You&amp;rsquo;re site may look a little b0rked to 0.001% of your users, if that. It does not mean that this miniscule percentage of users will not be able to navigate your site.&lt;/p&gt;
&lt;p&gt;This is the technique you want to use if you want to make your CSS easier to maintain and reduce your overall development/ bug fixing time.&lt;/p&gt;
&lt;p&gt;Imagine opening a CSS file and seeing code similar to this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#container { background-position : left top ; background-repeat : no-repeat ; width : 100px ; }
.gecko  #container { background-image : url(firefox.png) ; height : 97px ; }
.ie     #container { height : 94px ; }
.ie6    #container { background-image : url(msie6.png) ; }
.ie7    #container { background-image : url(msie7.png) ; }
.ie8    #container { background-image : url(msie8.png) ; }
.safari #container { background-image : url(safari.png) ; height : 88px ; }
.chrome #container { background-image : url(chrome) ; height : 104px ; }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now to me, that is an easy to read snippet of CSS. Rather than having to waste time looking for hacks or sifting through different files looking for the correct rule(s) to change. I can see the main rule and what browsers/ versions required adjustments. If there are any bugs raised with specific browsers/ versions I can make quick adjustments. If a new browser version comes out that fixes a previous CSS rendering bug or updates its CSS3 support, I can take care of it in a matter of a few minutes.&lt;/p&gt;
&lt;h4&gt;Your choice…&lt;/h4&gt;
&lt;p&gt;If your interested in this approach to cross browser CSS, I have created a &lt;a href="http://thudjs.com/blog/ua.js"&gt;standalone script available for download&lt;/a&gt; (less than 1kb gzipped). It creates a global object called UserAgent which you could very easily change to sit under your own namespace. There’s also a &lt;a href="http://thudjs.com/blog/ua.html"&gt;test page&lt;/a&gt; to display all the values contained in the UserAgent object, the classes assigned to the page’s documentElement (HTML tag) and an approximation of how long it took to complete the operation, for anyone interested in how this script will affect page speed — it&amp;rsquo;s generally less than 5ms.&lt;/p&gt;
&lt;p&gt;However, if you get your kicks from making things difficult for yourself, well then there are, as we’ve seen, a few choices at your disposal. :^)&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/428000513</link><guid>https://thudjs.tumblr.com/post/428000513</guid><pubDate>Fri, 05 Mar 2010 09:17:50 +0000</pubDate><category>css</category><category>cross browser</category><category>javascript</category><category>A.B.A</category><category>a better approach</category></item><item><title>a.b.a: a better approach</title><description>&lt;p&gt;So I had a bright idea — pilfered from my girlfriend&amp;rsquo;s blog sub-section entitled, &lt;strong&gt;microtrend&lt;/strong&gt; —  for a sort of sub-section entitled, &lt;strong&gt;a better approach&lt;/strong&gt; or &lt;strong&gt;a.b.a&lt;/strong&gt; — think Abba — for my blog.&lt;/p&gt;

&lt;p&gt;In this sub-section I&amp;rsquo;ll be talking about the various approaches I have discovered either on my own or from using existing frameworks — credits will be given — that make life a lot easier and your code a lot more efficient, readable, maintainable, etc..&lt;/p&gt;

&lt;p&gt;In doing this I not only want to impart this knowledge, of what I&amp;rsquo;ve found to work better, to others — who for some reason want to read my blog — but, also to keep an archive for my own reference.&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/421880740</link><guid>https://thudjs.tumblr.com/post/421880740</guid><pubDate>Tue, 02 Mar 2010 11:37:00 +0000</pubDate><category>a.b.a</category><category>a better approach</category></item><item><title>localStorage/ userData</title><description>&lt;p&gt;So I’ve been working on the thud.state package. The package has one class, thud.state.Storage and creates two instances of this class. One is thud.state.cookie (also available as thud.cookie), which as you can guess provides an easy to use interface for dealing with cookies. The other is thud.state.localStorage (also available as thud.localStorage) which provides cross browser interface for &lt;a href="http://dev.w3.org/html5/webstorage/"&gt;HTML5 localStorage&lt;/a&gt; which is available in all next generation browsers like firefox, safari, chrome, opera and internet explorer 8. If &lt;code&gt;localStorage&lt;/code&gt; is not present, as is the case in internet explorer 6 and 7, it will use the &lt;a href="http://msdn.microsoft.com/en-us/library/ms531424%28VS.85%29.aspx"&gt;userData behaviour&lt;/a&gt; in its place.&lt;/p&gt;
&lt;p&gt;As well as providing a cross-browser interface for getting, setting and removing items from the store, clearing the  whole store, I also wanted to be able to return an object representation of the store to make it easy to iterate over every item in the store.&lt;/p&gt;
&lt;p&gt;HTML5 &lt;code&gt;localStorage&lt;/code&gt; doesn’t give you an easy way to iterate over all the data contained in it. Trying to use a &lt;code&gt;for... in&lt;/code&gt; loop in firefox throws a &lt;code&gt;TypeError&lt;/code&gt;. There is however a way you can retrieve all the values: using the &lt;code&gt;length&lt;/code&gt; property of the &lt;code&gt;Storage&lt;/code&gt; interface I can retrieve the specific key at each index and then retrieve the value using the &lt;code&gt;getItem&lt;/code&gt; method. You can see this in the example below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;function getLocaLStorageAsObject() {
   var i = -1, 
       key, 
       len = localStorage.length, // the length property tells us 
                                  // how many items are in the storage
       res = {};
   while ( ++i &amp;lt; len ) { 
       key = localStorage.key( i ); // retrieve the value of each &lt;b&gt;key&lt;/b&gt; at each index
       res[key] = localStorage.getItem( key ); // retrieve the value using the &lt;b&gt;getItem&lt;/b&gt; method
   }
   return res;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With Internet Explorer 6 and 7’s userData behaviour you’re dealing with an instance of an &lt;a href="http://msdn.microsoft.com/en-us/library/ms531328%28VS.85%29.aspx"&gt;XMLDocument&lt;/a&gt; which is accessible via the storage element’s &lt;code&gt;xmlDocument&lt;/code&gt; — in some of the docs on MSDN’s site it also appears as &lt;code&gt;XMLDocument&lt;/code&gt;, it doesn’t seem to be case sensitive — property. Any item of data you save to the element’s userData store is saved as attributes to the root element of this XMLDocument.&lt;/p&gt;
&lt;p&gt;So if I want to retrieve the userData as an object, from an element I can do so by iterating over the attributes in the XMLDocument’s root node. You can see this in the example below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;function getUserDataAsObject( storage_element /* the element used to store userData */, store_id /* the id of the element’s store */ ) {
   storage_element.load( store_id ); // make sure the element’s store is loaded
   var attr, 
       doc = storage_element.xmlDocument, // the reference to the XMLDocument
       attributes = doc.firstChild.attributes, // the root element will always be the firstChild of the XMLDocument
       i = -1, 
       len = attributes.length, 
       res = {};
   while ( ++i &amp;lt; len ) { 
       attr = attributes[i]; 
       res[attr.nodeName] = attr.nodeValue; // use the standard DOM properties to retrieve the key and value
   }
   return res;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The last piece of functionality missing from the userData behaviour is a function which clears the element’s userData store. Using a similar approach to the &lt;code&gt;getUserDataAsObject&lt;/code&gt; function I can now write a function to clear userData store. The only important thing to remember to start iterating from the end of the element’s &lt;code&gt;attributes&lt;/code&gt; collection. As I will be removing items from the collection, it will affect the length of the &lt;code&gt;attributes&lt;/code&gt; collection. You can see this in the example below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;function clearUserData( storage_element /* the element used to store userData */, store_id /* the id of the element’s store */ ) {
   storage_element.load( store_id ); // make sure the element’s store is loaded
   var doc = storage_element.xmlDocument, // the reference to the XMLDocument
       attributes = doc.firstChild.attributes, // the root element will always be the firstChild of the XMLDocument
       attr, 
       len = attributes.length, 
       res = {};
   while ( 0 &amp;lt;= --len ) { 
       attr = attributes[len]; 
       storage_element.removeAttribute( attr.nodeName ); // use the standard DOM properties to remove the item
   }
   storage_element.save( store_id ); // don’t forget to save the element’s store, otherwise the data won’t be cleared
   return storage_element; // return the storage element
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So along with the existing localStorage and userData APIs, I can now build a cross-browser API for storing user data locally and without the &lt;a href="http://developer.yahoo.com/performance/rules.html#cookie_size"&gt;performance degradation of using cookies&lt;/a&gt; to store my UI specific preferences.&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/419577524</link><guid>https://thudjs.tumblr.com/post/419577524</guid><pubDate>Mon, 01 Mar 2010 09:16:00 +0000</pubDate><category>javascript</category><category>cross browser</category><category>html5</category><category>storage</category></item><item><title>modelling javascript</title><description>&lt;p&gt;One of the things I like to do before embarking on any large scale development work, is to draw out all the packages, classes, singletons/ objects I will be creating: i.e. modelling my code using &lt;a href="http://en.wikipedia.org/wiki/Unified_Modeling_Language"&gt;UML&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;From most of what I&amp;rsquo;ve seen in the development world, it&amp;rsquo;s becoming somewhat of a lost art. I feel this is a shame as I have found modelling applications or frameworks I&amp;rsquo;ve worked on not only gives me a great overall picture of what it is I am building, it also helps me speed up my overall development time by cutting down the time I spend creating most of the common skeleton code, very early on in the development cycle.&lt;/p&gt;
&lt;h4&gt;where/ how to start?&lt;/h4&gt;
&lt;p&gt;My main aim is not to list every single property and method — be they private, protected or public — that will be present in the API up front. I simply want to see a basic outline of what it is I will be building.&lt;/p&gt;
&lt;p&gt;The benefits of this allow me to see relationships between components and which components should be grouped into packages together.&lt;/p&gt;
&lt;p&gt;Whats important to remember is that &lt;b&gt;these relationships are not set in stone&lt;/b&gt;. Things can and will change. We want a general outline to be able to start visualising what we are building. For example, this is a sample from some of my first run through designing the thud framework:&lt;/p&gt;
&lt;p&gt;&lt;img alt="a sample diagram of the thud framework" src="http://thudjs.com/blog/assets/thud.sample.png"/&gt;&lt;/p&gt;
&lt;p&gt;From this point I can start creating the directories and files that match whats present in my diagram. Packages become directories, the rest become files. I&amp;rsquo;m not concerned about how many files are present at this stage as I will be merging most of these into singular files later. For now, I want to keep things as modular as I can, to make it easy to find functionality and fix errors, etc.&lt;/p&gt;
&lt;h4&gt;Getting more specific&lt;/h4&gt;
&lt;p&gt;Once I&amp;rsquo;ve done this, I&amp;rsquo;m able to get started on the particulars of each each package and its components. Here is my first run through the &lt;code&gt;thud.base&lt;/code&gt; and &lt;code&gt;thud.lang&lt;/code&gt; packages:&lt;/p&gt;
&lt;p&gt;&lt;img alt="thud.base" src="http://thudjs.com/blog/assets/thud.base.png"/&gt;&lt;img alt="thud.lang" src="http://thudjs.com/blog/assets/thud.lang.png"/&gt;&lt;/p&gt;
&lt;p&gt;From this point I can start creating the skeleton of each component, for each file, in each package/ directory.&lt;/p&gt;
&lt;p&gt;A good sizeable portion of my development work is now done. It&amp;rsquo;s simply a matter of filling in the empty functions with code and assigning the correct values to each property. A great part of this is that I can cut out a lot of the laborious work very early on and save time by copying and pasting a lot of the common skeleton code!&lt;/p&gt;
&lt;p&gt;Another benefit is, as I am writing unit tests for a specific component which relies on incomplete functionality, I can simply have that empty function return a valid value to satisfy the test for now.&lt;/p&gt;
&lt;p&gt;Of course I made changes to the code, some components got moved to different packages as I realised I needed specific functionality to be available earlier on; I changed the names of some functions to make them easier to understand; and, I&amp;rsquo;ve added in private functions that are only required by specific components and not exposed in the public API.&lt;/p&gt;
&lt;p&gt;However, I&amp;rsquo;m not hung up on keeping my diagrams up to date, they were simply a starting point to get an overall picture of what it is I am building. :^)&lt;/p&gt;
&lt;h4&gt;so, what UML tools are available?&lt;/h4&gt;
&lt;p&gt;There are a &lt;a href="http://en.wikipedia.org/wiki/List_of_Unified_Modeling_Language_tools"&gt;range of UML tools&lt;/a&gt; available to developers. Most of the &lt;a href="http://www.altova.com/download/umodel/uml_tool_enterprise.html"&gt;halfway&lt;/a&gt; &lt;a href="http://www.modeliosoft.com/index.php"&gt;descent&lt;/a&gt; &lt;a href="http://www.smartdraw.com/specials/ppc/umldesign.htm?id=48383&amp;amp;gclid=CM7pxNqIkKACFZBb4wod0V2Adg"&gt;looking&lt;/a&gt; ones are only available for the Windows operating system, and most of the Java based cross-platform ones, frankly, suck.&lt;/p&gt;
&lt;h5&gt;paid tools&lt;/h5&gt;
&lt;p&gt;As far as paid UML tools go, I&amp;rsquo;ve used &lt;a href="http://www.altova.com/download/umodel/uml_tool_enterprise.html"&gt;Altova UModel&lt;/a&gt; before and I found it to be fairly good for modelling JavaScript. There re only two problems. The first is, I bought a license and used it one company on the computer they provided for me. I uninstalled it from that computer when my contract ended, when I tried to install it on my home computer, I was informed that the license was already in use, £90 down the drain. The second problem is it&amp;rsquo;s only available on windows and I made the mistake of buying a mac two years ago.&lt;/p&gt;
&lt;h5&gt;free tools&lt;/h5&gt;
&lt;p&gt;As far as free tools go, I&amp;rsquo;ve haven&amp;rsquo;t found any that are easy to use and have enough features/ functionality to allow me to model JavaScript instead of Java.&lt;/p&gt;
&lt;h5&gt;online tools&lt;/h5&gt;
&lt;p&gt;There are also two online tools that I&amp;rsquo;ve found, both are flash based. The first is &lt;a href="http://www.gskinner.com/gmodeler/app/run.html"&gt;gModeler&lt;/a&gt;. gModeler is a free, very simple tool, you can create packages, classes and notes. You to save your diagrams for later, however, it can become somewhat frustrating to use the bigger your diagram gets.&lt;/p&gt;
&lt;p&gt;The second is a called &lt;a href="https://www.gliffy.com/"&gt;gliffy&lt;/a&gt;. Gliffy is a more complete diagraming tool, it comes with a free version which allows you to save 3 diagrams and a premium account, which I couldn&amp;rsquo;t find a price for.&lt;/p&gt;
&lt;p&gt;I used gliffy to create my diagrams for thud, it can also become a bit frustrating, I lost one diagram and had to recreate it from scratch, but on the whole isn&amp;rsquo;t a bad tool to get you started.&lt;/p&gt;
&lt;p&gt;In summary, I would recommend modelling your application/ framework to any developer. It not only allows you to gain an over all picture of what it is you&amp;rsquo;re building, it&amp;rsquo;s a great way to communicate the scope of your work to other team mates and your project manager, and in the long run can speed up your development time, by doing a lot of your planning and laborious development up front.&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/413484388</link><guid>https://thudjs.tumblr.com/post/413484388</guid><pubDate>Fri, 26 Feb 2010 16:31:00 +0000</pubDate><category>modelling</category><category>architecture</category><category>javascript</category></item><item><title>why another javascript framework?</title><description>&lt;p&gt;I know, I know. Why!? But in truth, why not? I’m always complaining about something in each and every framework I’ve used. They all have good, bad and incomplete (at least to me) parts. I want to merge everything I love about every framework I’ve used and then some to see what the outcome is.&lt;/p&gt;
&lt;p&gt;The main reasons though, are as a learning exercise and because I want to work on a project where quality comes before profit. I am a pretty good at writing HTML, CSS and JavaScript, as well as UI application architecture and design. I have written mini-frameworks and UI frameworks that sit on top of existing frameworks, like prototype and MooTools for various projects I have worked on in the last 5 years. There’s always more to learn and there’s no better way of doing it that diving into the deep end.&lt;/p&gt;
&lt;p&gt;As a work of quality: I’ve worked on one project after another in the almost 5 years since I moved to London which involved me being used as a paddle to get whoever out of shit creek. Research and development, unit testing, functional testing, retrospectives and basically doing anything except coding like a madman and the like have all been seen as massive wastes of resource, time and money by past employers, with the exception of the BBC, where it can and has been taken to the other extreme.&lt;br/&gt;You can only do that for so long before you burn out, so I want to do something that I AM proud of for once.&lt;/p&gt;
&lt;p&gt;It’s either that, or have a nervous breakdown. :¬P&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/406757727</link><guid>https://thudjs.tumblr.com/post/406757727</guid><pubDate>Tue, 23 Feb 2010 10:00:00 +0000</pubDate></item><item><title>intro part 2: who the what, where?</title><description>&lt;p&gt;As I mentioned previously, &amp;ldquo;Until the 7th November 2009, I had been working as a user interface developer…&amp;rdquo;. A few months ago my girlfriend and I decided that we should get out of working for corporate and do more meaningful work for ourselves.&lt;/p&gt;
&lt;p&gt;We will be attempting to build 2 web applications over the next 6-8 months — this actually started circa Nov 2009 — one of which is (hopefully) for profit and one which is a non-profit, help the world and feel good doing it, app. We want to give something back to the world, but we also don&amp;rsquo;t want to have to go back to working for big companies on big projects with small deadlines.&lt;/p&gt;
&lt;p&gt;This blog will be dedicated (mostly) to the JavaScript framework I will be developing over the next few months. However, as well as blogging about the highs and lows of building a JavaScript framework and the highs and lows of the various JavaScript frameworks I have worked with over the past 4 years, I will also be using this blog to rant about various gripes I have with the world at large.&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/404667187</link><guid>https://thudjs.tumblr.com/post/404667187</guid><pubDate>Mon, 22 Feb 2010 10:00:00 +0000</pubDate></item><item><title>intro part 1: salutations</title><description>&lt;p&gt;My name is Christos Constandinou. Until the 7th November 2009, I had been working as a user interface developer for various companies in both Sydney, Australia (7 years) and London, England (5 years).&lt;br/&gt;I don’t consider myself to be a “guru” to the likes of Thomas Fuchs, Sam Stephenson, Dean Edwards, Jack Slocum, Alex Russell, John Resig, Doug Crockford, Nicholas Zakas, Valerio Proiett, Aaron Newton, etc, etc, etc&amp;hellip; But that’s ok, I know things of varying significance and I’ve only worked with an actual handful of people I can say the same about; I&amp;rsquo;m feeling it’s time to do something with that knowledge, hence this blog.&lt;/p&gt;
&lt;p&gt;For more detail on my employment exploits you can check out my &lt;a title="linked in profile" href="http://www.linkedin.com/in/christosconstandinou"&gt;linkedin profile&lt;/a&gt;.&lt;/p&gt;</description><link>https://thudjs.tumblr.com/post/401022969</link><guid>https://thudjs.tumblr.com/post/401022969</guid><pubDate>Sat, 20 Feb 2010 19:57:00 +0000</pubDate></item></channel></rss>
