<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.1" -->
<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/"
	>

<channel>
	<title>Bill Krueger's TechBlog</title>
	<link>http://techblog.billkrueger.com</link>
	<description>A place to share thoughts and ideas on new or emerging web technologies</description>
	<pubDate>Fri, 10 Nov 2006 19:48:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.1</generator>
	<language>en</language>
			<item>
		<title>What is this Blog For?</title>
		<link>http://techblog.billkrueger.com/2006/10/10/about-this-blog/</link>
		<comments>http://techblog.billkrueger.com/2006/10/10/about-this-blog/#comments</comments>
		<pubDate>Wed, 11 Oct 2006 06:09:23 +0000</pubDate>
		<dc:creator>Bill Krueger</dc:creator>
		
	<category>General</category>
		<guid isPermaLink="false">http://techblog.billkrueger.com/?p=3</guid>
		<description><![CDATA[So I&#8217;ve finally become one of the millions of people that have a blog!  I&#8217;ve always been hesistant to create a blog because I don&#8217;t see why anyone would care to read what I have to say on a regular basis and I don&#8217;t usually have that much to say in the first place. [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve finally become one of the millions of people that have a blog!  I&#8217;ve always been hesistant to create a blog because I don&#8217;t see why anyone would care to read what I have to say on a regular basis and I don&#8217;t usually have that much to say in the first place.  However, recently I&#8217;ve been exploring some of the new web technologies that are out there and I decided that it might be nice to share some of my <strong>tech </strong>related thoughts and ideas.  Also, I plan to post tutorials and demos of the technologies I am currently working with.  Hopefully, these posts will be helpful to other people attempting to implement similar functionality or use similar technologies for their own projects.</p>
<p>More to come&#8230;
</p>
]]></content:encoded>
			<wfw:commentRSS>http://techblog.billkrueger.com/2006/10/10/about-this-blog/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Mootools Sortable List Example</title>
		<link>http://techblog.billkrueger.com/2006/10/11/mootools-sortable-list-example/</link>
		<comments>http://techblog.billkrueger.com/2006/10/11/mootools-sortable-list-example/#comments</comments>
		<pubDate>Wed, 11 Oct 2006 08:09:56 +0000</pubDate>
		<dc:creator>Bill Krueger</dc:creator>
		
	<category>Mootools</category>
	<category>JavaScript</category>
		<guid isPermaLink="false">http://techblog.billkrueger.com/2006/10/11/mootools-sortable-list-example/</guid>
		<description><![CDATA[Recently, I&#8217;ve been playing around with mootools - the new Javascript Framework from the Mad4Milk guys.  As you&#8217;ve probably already noticed, at the time of launch, there wasn&#8217;t much available in terms of demos and documentation.  As time passes, the documentation is improving and people are adding resources to the mootools wiki that [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Mootools" href="http://www.mootools.neet"><img align="left" title="Mootools logo" alt="Mootools logo" src="http://techblog.billkrueger.com/wp-content/uploads/2006/10/mootools_logo.png" /></a>Recently, I&#8217;ve been playing around with <a href="http://www.mootools.net">mootools</a> - the new Javascript Framework from the <a href="http://www.mad4milk.net">Mad4Milk</a> guys.  As you&#8217;ve probably already noticed, at the time of launch, there wasn&#8217;t much available in terms of demos and documentation.  As time passes, the documentation is improving and people are adding resources to the <a title="Mootools wiki" href="http://wiki.mootools.net/">mootools wiki</a> that is available now.</p>
<p>One of the first examples available on the web was a Drag and Drop example provided by Jonathan Snook on his blog (See example <a title="Drag and Drop Example" href="http://www.snook.ca/archives/javascript/mootools_drag_a/">here</a>).  After I played around with his example, I was interested in testing out the Sortable List functionality available in the effects portion of mootools.  I put together a quick example of a Sortable List implementation, so feel free to <a title="Sortable List Example" href="http://techblog.billkrueger.com/mootools-sortable-list">check it out</a> and view the source.</p>
<p>I tried to go a bit farther than simply implementing the Sortable List functionality by adding an Opacity effect and making additions to the onDrag and onComplete functions of the dragger.  For a more in-depth explanation of the code, read the full post.</p>
<p><a id="more-6"></a></p>
<h3>Creating a Sortable List</h3>
<p>Creating a Sortable List with the mootools framework is fairly straight-forward.  However, some of the code in the Sortables class has been stripped out to allow the list to sort properly.  When I first started playing with the Sortables, I was using Revision 57 of mootools.  I noticed that the sorting of the list was very buggy and not working properly at all, so I updated to Revision 64 and everything worked fine.  Well&#8230; sorta.  In <a href="http://mootools.net/WebSVN/listing.php?repname=mootools&#038;path=%2F&#038;rev=58&#038;sc=1">Revision 58</a>, the droppables logic was stripped out of the Sortables class because it was apparrently causing problems.  With this change, the basic functionality of Sortable Lists is available, but it will be nice when the droppables are added back in so we can make use of the onOver, onLeave and onDrop functions.</p>
<p>To create a Sortable List with mootools, all you need to do is create a new Sortables object and have each sortable element extend that object.  So first, I defined my list of sortable elements and gave each of them a className of &#8220;sort&#8221; so I can easily fetch them from the DOM:</p>
<pre><code>&lt;ul&gt;
    &lt;li class="sort"&gt;Item 1&lt;/li&gt;
    &lt;li class="sort"&gt;Item 2&lt;/li&gt;
    &lt;li class="sort"&gt;Item 3&lt;/li&gt;
    &lt;li class="sort"&gt;Item 4&lt;/li&gt;
&lt;/ul&gt;</code></pre>
<p>Now, we just need to create our Sortables object and loop through our array of sort items to have each of them extend that Sortables object.  I like the concept of having a single function to call to make a Sortable List, similar to the way you can call <code>makeDraggable()</code> on an element.  To give myself this ability for Sortable Lists as well, I extended Array and added a <code>makeSortable()</code> function that takes in a sortableOptions object to use when instantiating our Sortables object:</p>
<pre><code>Array.extend({
	makeSortable: function(options){
		var Sortable = new Sortables(this, options);
		this.each(function(el){
			el.style.cursor = 'move';
			el.extend(Sortable);
		})
	}
});</code></pre>
<p>The <code>makeSortable()</code> function just creates our Sortables object, then loops through the Array of sort items it has been called on to perform some additional logic.  In this case, it sets the cursor style of each sort item to &#8216;move&#8217; to indicate that a user can drag it, and then has each sort item extend the Sortables object we just created. </p>
<p>Now that we have the new <code>makeSortable()</code> function, all we have to do is use the helpful <code>$S()</code> function to grab all of our sort items and call makeSortable on them with any additional options we might want.  This is done in the <code>window.onload()</code> function so our Sortable List is ready to go as soon as the window is done loading.</p>
<pre><code>window.onload = function() {
	$S('#container li.sort').makeSortable(sortableOptions);
}</code></pre>
<h3>Using onStart, onComplete and onDrag</h3>
<p>At this point, we have a perfectly functioning Sortable List.  But that&#8217;s a little too easy, so I decided to add a status indicator below the list that informs the user of the current operation being performed on the list.  This is just an empy span element in the source with the id &#8217;status&#8217; so I can get a handle on it later. </p>
<p>I will update the contents of that span element with information while the user is sorting the list.<br />
To do this, I need to add logic to the onDrag and onComplete functions of the dragger object that is created in the Sortables class.  Unfortunately, Sortables is not setup to accept function definitions for onStart, onComplete and onDrag.  It would be nice if you could specify custom functionality for these events in the sortableOptions that are used to initialize the Sortables object and then have the base logic of those functions extend whatever was passed in with the sortableOptions.  </p>
<p>Since that doesn&#8217;t seem to be possible right now, I decided to use the <code>implement()</code> feature of mootools to create a new implementation of the initialize function in the Sortables class.  This isn&#8217;t very efficient because I want to keep most of the functionality of the Sortables class and only need to add a few lines of code for my custom behavior, but I can&#8217;t think of a better way to do it right now.  Below is my custom implementation of the initialize function in the Sortables class:</p>
<pre><code>Sortables.implement({
		initialize: function(elements, options){
			this.setOptions(options);
			this.options.handles = this.options.handles || elements;
			var trash = new Element('div').injectInside($(document.body));
			$A(elements).each(function(el, i){
				var copy = $(el).clone().setStyles({
					'position': 'absolute',
					'opacity': '0',
					'display': 'none'
				}).injectInside(trash);
				var elEffect = el.effect('opacity', {
					duration: this.options.fxDuration,
					wait: false,
					transition: this.options.fxTransition
				}).set(1);
				var copyEffects = copy.effects({
					duration: this.options.fxDuration,
					wait: false,
					transition: this.options.fxTransition,
					onComplete: function(){
						copy.setStyle('display', 'none');
					}
				});
				var dragger = new Drag.Move(copy, {
					xModifier: false,
					onStart: function(){
						copy.setHTML(el.innerHTML).setStyles({
							'display': 'block',
							'opacity': this.options.maxOpacity,
							'top': el.getTop()+'px',
							'left': el.getLeft()+'px'
						});
						elEffect.custom(elEffect.now, this.options.maxOpacity);
					}.bind(this),
					onComplete: function(){
						copyEffects.custom({
							'opacity': [this.options.maxOpacity, 0],
							'top': [copy.getTop(), el.getTop()]
						});
						elEffect.custom(elEffect.now, 1);
						<span style="color:red">$('status').setHTML('Sorting complete!');
						statusFade.custom(1,0);</span>
					}.bind(this),
					onDrag: function(){
						if (el.getPrevious() &amp;&amp; copy.getTop() &lt; (el.getPrevious().getTop()))
							el.injectBefore(el.getPrevious());
						else if (el.getNext() &amp;&amp; copy.getTop() &gt; (el.getNext().getTop()))
							el.injectAfter(el.getNext());
                                                <span style="color:red">statusFade.clearTimer();
                                                $('status').setOpacity(1).setHTML('Sorting in progress...');</span>
					}
				});
				this.options.handles[i].onmousedown = dragger.start.bind(dragger);
			}, this);
		}
});</code></pre>
<p>The lines of code highlighted in red are the only additions I made to the initialize function in my implementation.  Now, the code that initializes my Sortables object will automatically use my implementation and it will get the benefits of the custom logic I have added.</p>
<p>You&#8217;ll notice that in the onComplete and onDrag functions I am updating the contents of the status element I created earlier through the use of the <code>setHTML()</code> function which is really just a wrapper around innerHTML.</p>
<pre><code>$('status').setHTML('Sorting complete!');</code></pre>
<p>You&#8217;ll also notice that I am doing some other stuff with an Opacity effect that I created.  When a user is sorting the list, the status element will displaying the text <code>Sorting in progress...</code>.  When they are finished, it will display the text <code>Sorting complete!</code>, but I don&#8217;t want that status to just sit there forever.  Therefore, I created an Opacity effect that will cause the status text to slowly fade out after a set delay.  </p>
<h3>Creating an effect</h3>
<p>To do this, I created the effect in the window.onload event so it is available to use as soon as the page is done loading:</p>
<pre><code>statusFade = new Fx.Opacity('status', {duration: 2000, transition: Fx.cubic});</code></pre>
<p>This basically says that I want to create an Opacity effect on the element with id of &#8217;status&#8217; and then specifies some options for that effect.  Since I want my status text to fade slowly, I gave the effect a duration of 2000 milliseconds.  I also specified a cubic transition which will cause the fade to perform slowly at first and speed up toward the end.  </p>
<p>When I want to use this effect, I can call the <code>custom()</code> function on it and specify beginning and ending parameters.  In this case, I want to go from zero Opacity (fully visible) to 100% Opacity, I call my effect like so:</p>
<pre><code>onComplete: function(){
    copyEffects.custom({
	'opacity': [this.options.maxOpacity, 0],
	'top': [copy.getTop(), el.getTop()]
    });
    elEffect.custom(elEffect.now, 1);
    $('status').setHTML('Sorting complete!');
    <span style="color:red">statusFade.custom(1,0);</span>
}</code></pre>
<p>In the above code, when a user drops the item they are sorting, the onComplete function is executed.  At this time, the contents of the span element with an id of &#8217;status&#8217; are updated and then the effect is called to cause the status to fade out slowly.  Calling <code>custom(1,0)</code> will execute my Opacity effect and cause the element to go from fully visible (Opacity of 1) to invisible (Opacity of 0).</p>
<p>I also want to update the contents of the status element while a user is sorting the list.  The problem is, after I fade out the status element, any subsinquent sorting status will not be visible to the user since that element is still invisible.  Also, another problem is that once the status text starts fading, it won&#8217;t stop.  So if a user drags an item to a new location and drops it, the text &#8220;Sorting complete!&#8221; will be displayed and begin to fade.  But if the user then starts dragging an item again, the &#8220;Sorting in progress&#8230;&#8221; text will be displayed but continue to fade until it is gone.  The following code illustrates how I dealt with both of those issues:</p>
<pre><code>onDrag: function(){
    if (el.getPrevious() &amp;&amp; copy.getTop() &lt; (el.getPrevious().getTop()))
        el.injectBefore(el.getPrevious());
    else if (el.getNext() &amp;&amp; copy.getTop() &gt; (el.getNext().getTop()))
	el.injectAfter(el.getNext());
    <span style="color:red">statusFade.clearTimer();
    $('status').setOpacity(1).setHTML('Sorting in progress...');</span>
}</code></pre>
<p>In the onDrag function, I want to interrupt the fade effect if a user begins to sort the list again.  To do this, we can use the <code>clearTimer()</code> method on the effect.  By calling this method on the fade effect, it causes the effect to finish immediately.  Once that is done, I simply set the Opacity of the element back to 1 (fully visible) before updating the contents of its innerHTML.  Now the user will always be able to see the proper status of their operations.</p>
<h3>Conclusion</h3>
<p>That pretty much explains the logic behind all the pieces of this Sortable List example.  I know it&#8217;s not very flashy, but the purpose of this example was to illustrate some of the cool things that can be done with a Sortable List and when they will be executed.  I also figured it would be nice to explain a bit about using an effect as well.  In reality, instead of updating a status element with different text when a user is sorting or finishes sorting, a back-end process would probably be triggered via an Ajax request to update a datasource or something similiar in order to store the state of the list.</p>
<p>I hope this lengthy explanation is helpful to some people and feel free to leave comments or suggestions for improvements to the code.
</p>
]]></content:encoded>
			<wfw:commentRSS>http://techblog.billkrueger.com/2006/10/11/mootools-sortable-list-example/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Sortable List Example - Updated!</title>
		<link>http://techblog.billkrueger.com/2006/10/22/sortable-list-example-updated/</link>
		<comments>http://techblog.billkrueger.com/2006/10/22/sortable-list-example-updated/#comments</comments>
		<pubDate>Mon, 23 Oct 2006 05:16:31 +0000</pubDate>
		<dc:creator>Bill Krueger</dc:creator>
		
	<category>Mootools</category>
	<category>JavaScript</category>
		<guid isPermaLink="false">http://techblog.billkrueger.com/2006/10/22/sortable-list-example-updated/</guid>
		<description><![CDATA[When I first posted my Sortable List Example, I had hoped to be posting more frequently on this blog.  Unfortunately, life (and work) have been very busy as always, so I apologize for the lack of content.  I have been anticipating updates to the mootools framework for sometime now that would greatly reduce [...]]]></description>
			<content:encoded><![CDATA[<p>When I first posted my Sortable List Example, I had hoped to be posting more frequently on this blog.  Unfortunately, life (and work) have been very busy as always, so I apologize for the lack of content.  I have been anticipating updates to the <a href="http://www.mootools.net">mootools</a> framework for sometime now that would greatly reduce the amount of duplicated code needed in my example.  As of <a href="http://mootools.net/WebSVN/listing.php?repname=mootools&#038;path=%2Fsource%2F&#038;rev=76&#038;sc=1">SVN Revision 76</a>, the <code>Sortables</code> class now allows you to pass <code>onStart</code> and <code>onComplete</code> functions in the options used to initialize the <code>Sortables</code> object.  Therefore, I have updated my example to take advantage of this new functionality.</p>
<p>In my original example, I was passing an empty <code>sortableOptions</code> object into the initialization for the Sortable list because I didn&#8217;t need to override any options at the time.  However, I was forced to use the <code>implement</code> functionality in mootools to change the <code>initialize</code> function of the <code>Sortables</code> class just to add three or four lines of code to the <code>onStart</code> and <code>onComplete</code> functions.  This resulted in about 50 lines of duplicated code&#8230; yuck!</p>
<p>If you view the source of my example now, you will see a greatly simplified setup.  In the <code>sortableOptions</code> object that used to be empty, I have defined new <code>onStart</code> and <code>onComplete</code> functions as seen below:</p>
<pre><code>var sortableOptions = {
	onStart: function() {
		statusFade.clearTimer();
		$('status').setOpacity(1).setHTML('Sorting in progress...')
	},
	onComplete: function() {
		$('status').setHTML('Sorting complete!');
		statusFade.custom(1,0);
	}
};</code></pre>
<p>Now, when I pass that <code>sortableOptions</code> object into the <code>Sortables</code> object, the <code>onStart</code> and <code>onComplete</code> functions are executed in addition to the existing code for those functions in the dragger object.  Feel free to <a href="http://techblog.billkrueger.com/mootools-sortable-list/">take a look</a> at the updated example.  </p>
<p>Also of note, the transitions in mootools have been changed.  Instead of specifying my transition as <code>Fx.cubic</code>, I now have to say <code>Fx.Transitions.cubicIn</code>.  It&#8217;s not too much trouble to add the reference to the new <code>Transitions</code> class, but it is somewhat annoying that some effects that existed previously do not exist under the same name anymore.  For example, the old <code>Fx.sinoidal</code> is now <code>Fx.Transitions.sineInOut</code>.</p>
<p>I can&#8217;t really complain though, there are way more transitions now than there used to be and some of them are very cool!
</p>
]]></content:encoded>
			<wfw:commentRSS>http://techblog.billkrueger.com/2006/10/22/sortable-list-example-updated/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Serialize function for Mootools Sortables</title>
		<link>http://techblog.billkrueger.com/2006/11/09/serialize-function-for-mootools-sortables/</link>
		<comments>http://techblog.billkrueger.com/2006/11/09/serialize-function-for-mootools-sortables/#comments</comments>
		<pubDate>Fri, 10 Nov 2006 05:22:16 +0000</pubDate>
		<dc:creator>Bill Krueger</dc:creator>
		
	<category>Mootools</category>
	<category>JavaScript</category>
		<guid isPermaLink="false">http://techblog.billkrueger.com/2006/11/09/serialize-function-for-mootools-sortables/</guid>
		<description><![CDATA[Creating a sortable list using mootools is great fun and all, but what most people really need to do is save the state of that list once the sorting is complete.  In the current revision of mootools, there is no serialize function that will output the order of the list as can be found [...]]]></description>
			<content:encoded><![CDATA[<p>Creating a sortable list using mootools is great fun and all, but what most people really need to do is save the state of that list once the sorting is complete.  In the current revision of mootools, there is no serialize function that will output the order of the list as can be found in the <code>Sortable</code> class in <a href="http://script.aculo.us">Scriptaculous</a>.  </p>
<p>A couple of people have commented on my original Sortable List example expressing the need for this serialize function and Jessica from <a href="http://www.jesirose.com">jesirose.com</a> posted a function on her <a href="http://www.jesirose.com/entry.php?id=10">blog</a> that accomplishes serialization for her specific needs.</p>
<p>I checked around on the <a href="http://forum.mootools.net">mootools forums</a> and didn&#8217;t see any posts on the topic, so I went ahead and wrote a serialize function that could be added to the <code>Sortables</code> class:</p>
<pre><code>serialize: function(element, options){
    var str = [];
    var key = encodeURIComponent(options.key || element.id);
    var elements = $A(element.getElements(options.tag || ''));
    elements.each(function(el, i) {
        str.push(key+'['+i+']='+el.id);
    });
    return str.join('&amp;');
}</code></pre>
<p>This function takes in an element which is the parent element for all of the Sortable List items that you want serialized.  You can also pass in two optional params in the options object:</p>
<ul>
<li>key - the string to use as the key in the key/value pair</li>
<li>tag - the type of tag that should be serialized under the parent element</li>
</ul>
<p>If you do not specify a key or a tag, the id of the passed-in element will be used as the key and all tags under the the parent element will be serialized, regardless of their type.  For a quick example, say you have a block of HTML like the following:</p>
<pre><code>&lt;div id="container"&gt;
    &lt;div id="listitem1"&gt;ListItem 1&lt;/div&gt;
    &lt;div id="listitem2"&gt;ListItem 2&lt;/div&gt;
    &lt;div id="listitem3"&gt;ListItem 3&lt;/div&gt;
&lt;/div&gt;</code></pre>
<p>If you want to serialize that list in the onComplete function of your Sortables object, you can use the following code:</p>
<pre><code>this.serialize($('container'), {key: 'exampleKey', tag: 'div'});</code></pre>
<p>This would generate a serialized output of the format:</p>
<pre><code>exampleKey[0]=listitem1&amp;exampleKey[1]=listitem2&amp;exampleKey[2]=listitem3</code></pre>
<p>The output above can be passed through an Ajax POST request and provide the necessary information needed on the backend to update a database storing the state of the list.  I&#8217;ve posted the example on the mootools forums as well and I think it should be included in Sortables, but for now you&#8217;ll have to add this to your own implementation of the Sortables class to use the functionality.
</p>
]]></content:encoded>
			<wfw:commentRSS>http://techblog.billkrueger.com/2006/11/09/serialize-function-for-mootools-sortables/feed/</wfw:commentRSS>
		</item>
	</channel>
</rss>
