<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:series="http://unfoldingneurons.com/"
><channel><title>David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞. &#187; MooTools</title> <atom:link href="http://davidwalsh.name/tutorials/mootools/feed" rel="self" type="application/rss+xml" /><link>http://davidwalsh.name</link> <description>Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</description> <lastBuildDate>Sat, 04 Feb 2012 19:28:58 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3</generator> <item><title>Custom Getters and Setters with&#160;MooTools</title><link>http://davidwalsh.name/get-set</link> <comments>http://davidwalsh.name/get-set#comments</comments> <pubDate>Wed, 01 Feb 2012 15:27:08 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[MooTools]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5379</guid> <description><![CDATA[Working with Dojo all day and scoping out MooTools at night gives me a unique perspective; I get to constantly evaluate the two frameworks and mentally move functionalities from framework to framework. One small but handy feature within the Dojo Toolkit&#8217;s Dijit UI Framework is its set/get system. Dijit allows developers to add custom methods [...]<p><a
href="http://davidwalsh.name/get-set">Custom Getters and Setters with&nbsp;MooTools</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>Working with Dojo all day and scoping out MooTools at night gives me a unique perspective;  I get to constantly evaluate the two frameworks and mentally move functionalities from framework to framework.  One small but handy feature within the Dojo Toolkit&#8217;s Dijit UI Framework is its set/get system.  Dijit allows developers to add custom methods tied into simple <code>get</code> and <code>set</code> methods to allow manipulation properties into and on the way out of a class.  I took a few moments to implement this system in MooTools.</p><p>The idea is that a Class instance has properties:</p><pre class="js">
var MyClass = new Class({
	value: 10/*, more... */
});
</pre><p>Instead of simply setting and getting object properties directly, sometimes they need to be treated before coming in or going out.  For setting, it can be a sort of internal formatting or validation.  For getting, it&#8217;s mostly formatting.  The method formats are <code>_get[SomeAttrName]Attr</code> and <code>_set[SomeAttrName]Attr</code>.  With that in mind, it&#8217;s time to create the mixin class.</p><h2>JavaScript GetSet for&nbsp;MooTools</h2><p>This new functionality will be independently coded as a class meant as a <code>Class</code> mixin using the Implements property:</p><pre class="js">
(function() {
	
	// Turns "thisPropertyName" into "ThisPropertyName"
	function getFunctionName(key, getSet) {
		return "_" + getSet + key.charAt(0).toUpperCase() + key.slice(1) + "Attr";
	}
	
	// Implement the getter / setter
	this.GetSet = new Class({
		// A custom getter that looks for _get
		get: function(key) {
			var fn = this[getFunctionName(key, "get")];
			return (fn &#038;&#038; fn.call(this, key)) || this[key];
		},
		set: function(key, value) {
			var fn = this[getFunctionName(key, "set")];
			if(fn) {
				fn.call(this, value);
			}
			else {
				this[key] = value;
			}
			// Returning "this" to allow chaining
			return this;
		}
	});
	
})();
</pre><p>The GetSet will feature two method, get and set, and will check for the presence of custom <code>set</code> and <code>get</code> methods;  if so, those methods are called, and if not, the property is simply set or returned.  Now let&#8217;s look at a sample usage:</p><pre class="js">
// Create a test class
var TestClass = new Class({
	// Implement the new class
	Implements: [GetSet],
	// The custom getter
	_getValueAttr: function() {
		return this.value / 10;
	},
	// The custom setter
	_setValueAttr: function(value) {
		this.value = value * 10;
	}
});

// Create a test class instance
var inst = new TestClass({
	value: 8
});

/*
	inst.set("value", 20);  // inst.value = 200
	inst.get("value");  // inst.value = 20
*/
</pre><p>In this case, we&#8217;ve set custom get and set methods which will handle the class&#8217; value property.  On the way in, the value is multiple by 10 and then set on the object.  On the way out, the value is divided by 10.  Of course, not the most realistic of scenarios, but this example provides a very simple illustration of how these methods can be used.</p><p>So what would be a more realistic scenario?  As I said above, custom setters can be very useful as an internal validator.  Both the getters and setters are valuable, however, because they also provide a way to &#8220;spy&#8221; on object properties and have multiple reactions to their changes.</p><p><a
href="http://davidwalsh.name/get-set">Custom Getters and Setters with&nbsp;MooTools</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/get-set/feed</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>MooTools Mobile: It&#8217;s&#160;Touching!</title><link>http://davidwalsh.name/mootools-mobile-touching</link> <comments>http://davidwalsh.name/mootools-mobile-touching#comments</comments> <pubDate>Thu, 19 Jan 2012 15:33:42 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[Mobile]]></category> <category><![CDATA[MooTools]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5372</guid> <description><![CDATA[When the community asked the MooTools team to add basic mobile event listening to MooTools&#8217; Event class, we listened; today MooTools supports all touch and gesture events. What if we want more detailed mobile event listeners though, like swipe with direction, pinch, or touchhold events? That&#8217;s where Christoph Pojer&#8217;s excellent MooTools Mobile comes in. MooTools [...]<p><a
href="http://davidwalsh.name/mootools-mobile-touching">MooTools Mobile: It&#8217;s&nbsp;Touching!</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<a
href="https://github.com/cpojer/mootools-mobile"><img
src="http://davidwalsh.name/dw-content/ipads.png" alt="iPad, iPhone, iOS " class="image" /></a><p>When the community asked the MooTools team to add basic mobile event listening to MooTools&#8217; Event class, we listened;  today <a
href="http://davidwalsh.name/mootools-touch">MooTools supports all touch and gesture events</a>.  What if we want more detailed mobile event listeners though, like swipe with direction, pinch, or touchhold events?  That&#8217;s where Christoph Pojer&#8217;s excellent <a
href="https://github.com/cpojer/mootools-mobile" rel="nofollow">MooTools Mobile</a> comes in.  MooTools Mobile isn&#8217;t a full mobile framework, but rather a set of utilities to make catering to mobile a bit more&#8230;touching.  Let&#8217;s have a look at the resources provided by MooTools Mobile!</p><div
class="actions"><a
href="https://github.com/cpojer/mootools-mobile" class="demo">View Demo</a><div
class="clear"></div></div><p>As you&#8217;ve come to expect from members of the MooTools team, Christoph&#8217;s new Touch-based event classes are compact and modular.  Each class will be found within the Touch path.</p><h2>Swipe</h2><p>By using the <code>Touch/Swipe</code> resource, you can attain the direction of the swipe, as well as the start and end coordinates:</p><pre class="js">
// Add the swipe event listener
anyElement.addEvent("swipe", function(event){
    event.direction // "left" or "right"
    event.start // {x: Number, y: Number} Swipe start position
    event.end // {x: Number, y: Number} Swipe end position
});

// Configure minimal swipe distance and vertical swipe preferences
anyElement.store("swipe:distance", 30); // Default: 50
anyElement.store("swipe:cancelVertical", true); // Default: false
</pre><p>As you can see above, you may also configure minimum swipe distances and cancellation of vertical swipes.</p><h2>Pinch</h2><p>The <code>Touch/Pinch</code> resource allows you to listen for pinches, as well as configure the pinch threshold:</p><pre class="js">
// Add a pinch event to anyElement
anyElement.addEvent("pinch", function(event){
    event.pinch // "in" or "out"
});

// Store a custom threshold
anyElement.store("pinch:threshold", 0.3); // Default: 0.5; Amount of scaling to be done to fire the pinch event
</pre><p>The pinch threshold is stored within elements themselves so you may customize to fit any element.</p><h2>Touchhold</h2><p>The <code>touchhold</code> event is used throughout all mobile operating systems, so why not implement touchhold within the browser?</p><pre class="js">
//  Listen for touchhold on anyElement
anyElement.addEvent("touchhold", function(event){
    // Event fires
});
</pre><p>Any element may store an optional delay before the touchhold event fires:</p><pre class="js">
anyElement.store("touchhold:delay", 1000); // Default: 750
</pre><p>Maximum versatility with minimal code!</p><h2>Browser&nbsp;Enhancements!</h2><p>Besides providing these custom touch events, MooTools Mobile provides additional information within the <code>Browser</code> object via a <code>Device</code> property:</p><pre class="js">
Browser.Device // Object added to Browser
	Browser.Device.name // ipad / iphone / ipod OR other
	Browser.Device.ipad // true if on ipad
	Browser.Device.iphone // true if on iphone
	Browser.Device.ipod // true if on ipod
	Browser.hasHighResolution // true on iPhone 4
	Browser.isMobile // True on any platform that is not Windows / Linux / Mac
</pre><p>These properties give you further insight as to the device capabilities!</p><div
class="actions"><a
href="https://github.com/cpojer/mootools-mobile" class="demo">View Demo</a><div
class="clear"></div></div><p>With mobile development booming for the foreseeable future, it&#8217;s important to make sure your site is as mobile-friendly as possible.  Christoph&#8217;s mobile contribution is just the tiny helper developers may need to make their MooTools-powered web apps more mobile device-friendly. <strong>MooTools (Mobile) FTW</strong>!</p><p><a
href="http://davidwalsh.name/mootools-mobile-touching">MooTools Mobile: It&#8217;s&nbsp;Touching!</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/mootools-mobile-touching/feed</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Page Visibility&#160;API</title><link>http://davidwalsh.name/page-visibility</link> <comments>http://davidwalsh.name/page-visibility#comments</comments> <pubDate>Mon, 21 Nov 2011 15:26:36 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[MooTools]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5335</guid> <description><![CDATA[One event that&#8217;s always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back? The Page Visibility API allows developers to react to changes in page [...]<p><a
href="http://davidwalsh.name/page-visibility">Page Visibility&nbsp;API</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>One event that&#8217;s always been lacking within the document is a signal for when the user is looking at a given tab, or another tab.  When does the user switch off our site to look at something else?  When do they come back?  The Page Visibility API allows developers to react to changes in page visibility via the <code>visibilitychange document</code> event.  New <code>document.hidden</code> and <code>document.visibilityChange</code> properties are also available.</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/page-visibility.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>document.hidden</h2><p>This new document property, <code>document.hidden</code>, returns true the page is currently not visible.</p><h2>document.visibilityState</h2><p>The <code>visibilityState</code> will either be <code>visibile</code> (the page is the foreground tab of a non-minimized window), <code>hidden</code> (document is either a background tab or part of a minimized window), or <code>prerender</code> (the page content is being prerendered and is not visible to the user).</p><h2>visibilitychange&nbsp;Event</h2><p>Listening for changes in window visibility is as easy as:</p><pre class="js">
// Adapted slightly from Sam Dutton
// Set name of hidden property and visibility change event
// since some browsers only offer vendor-prefixed support
var hidden, state, visibilityChange; 
if (typeof document.hidden !== "undefined") {
	hidden = "hidden";
	visibilityChange = "visibilitychange";
	state = "visibilityState";
} else if (typeof document.mozHidden !== "undefined") {
	hidden = "mozHidden";
	visibilityChange = "mozvisibilitychange";
	state = "mozVisibilityState";
} else if (typeof document.msHidden !== "undefined") {
	hidden = "msHidden";
	visibilityChange = "msvisibilitychange";
	state = "msVisibilityState";
} else if (typeof document.webkitHidden !== "undefined") {
	hidden = "webkitHidden";
	visibilityChange = "webkitvisibilitychange";
	state = "webkitVisibilityState";
}

// Add a listener that constantly changes the title
document.addEventListener(visibilityChange, function() {
	document.title = document[state];
}, false);

// Set the initial value
document.title = document[state];
</pre><p>The example above changes the <code>document.title</code> property during every visibility change!</p><h2>Supporting visibilityChange in&nbsp;MooTools</h2><p>MooTools doesn&#8217;t support visibilityChange out of the box, so you&#8217;ll need to add this bit of JavaScript:</p><pre class="js">
// Set it up!
Element.NativeEvents[visibilityChange] = 2;
Element.Events[visibilityChange] = {
	base: visibilityChange,
	condition: function(event) {
		event[state] = document[state];
		event.visibilityState = document[state];
		return true;
	}
};

// Now use it!
document.addEvent(visibilityChange, function(e) {
	document.title = document[state];
});
</pre><p>Don&#8217;t you love it when it&#8217;s that easy?!  This mirror the code needed to add <code>onMessage</code> events to the list of supported events.</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/page-visibility.php" class="demo">View Demo</a><div
class="clear"></div></div><p>So what could <code>visibilitychange</code> be used for?  You could stop periodically refreshing content when the page is no longer visible, then pull new content when the page becomes visible again.  You could <a
href="http://www.samdutton.com/pageVisibility/" rel="nofollow">pause and resume a video during visibility changes</a>.  Audio too.  You could adjust your site statistics to count only time spent on site while the page is visible.  There&#8217;s loads you can do!  So&#8230;the question is&#8230;what would you do with this?</p><p><a
href="http://davidwalsh.name/page-visibility">Page Visibility&nbsp;API</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/page-visibility/feed</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>Introducing MooTools&#160;Templated</title><link>http://davidwalsh.name/templated</link> <comments>http://davidwalsh.name/templated#comments</comments> <pubDate>Mon, 19 Sep 2011 13:52:22 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[MooTools]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5294</guid> <description><![CDATA[One major problem with creating UI components with the MooTools JavaScript framework is that there isn&#8217;t a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: View Demo Running the following JavaScript snippet would parse the page, find the DIV, and create a [...]<p><a
href="http://davidwalsh.name/templated">Introducing MooTools&nbsp;Templated</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>One major problem with creating UI components with the MooTools JavaScript framework is that there isn&#8217;t a great way of allowing customization of template and ease of node creation.  As of today, there are two ways of creating:</p><div
class="actions"> <a
href="http://davidwalsh.name/dw-content/templated.php" class="demo">View Demo</a><div
id="qHolder2" data-widget-type="UIWidget" data-widget-props="message:'This is the second widget!'" class="UIWidgetInstance2"></div> </pre><p>Running the following JavaScript snippet would parse the page, find the DIV, and create a widget from it!</p><pre class="js">
document.body.parse();
</pre><p>The document.widget method accepts a DOM node and returns the widget object which it represents:</p><pre class="js">
var myWidget = document.widget("qHolder2");
</pre><p>Being able to retrieve a widget based on a node is very helpful in debugging your application.</p><h2>Realistic&nbsp;Usage</h2><p>One of the popular UI plugins I've created is LightFace, the Facebook-like lightbox.  Unfortunately LightFace falls victim to the "new Element Madness" because of the complexity of the widget structure.  Many people were unhappy about it's table-based structure (which accommodated for IE6) and wanted a DIV-based structure...and with Templated, you can make that happen.</p><p>Here's what LightFace would look like with Templated:</p><pre class="js">
<table class="lightface" data-widget-attach-point="box">
	<tbody>
		<tr>
			<td class="topLeft"></td>
			<td class="topCenter"></td>
			<td class="topRight"></td>
		</tr>
		<tr>
			<td class="centerLeft"></td>
			<td class="centerCenter">
				<div class="lightfaceContent" data-widget-attach-point="contentBox">
					<h2 class="lightfaceTitle lightfaceDraggable" data-widget-attach-point="title">{title}</h2>
					<div class="lightfaceMessageBox" data-widget-attach-point="messageBox">{message}</div>
					<div class="lightfaceFooter" data-widget-attach-point="footer"></div>
					<div class="lightfaceOverlay" data-widget-attach-point="overlay"></div>
				</div>
			</td>
			<td class="centerRight"></td>
		</tr>
		<tr>
			<td class="bottomLeft"></td>
			<td class="bottomCenter"></td>
			<td class="bottomRight"></td>
		</tr>
	</tbody>
</table>
</pre><p>And if you restructured the LightFace CSS file to accommodate for DIV-based structure, the template could look like:</p><pre class="js">
<div class="lightface" data-widget-attach-point="box">
	<div class="lightfaceContent" data-widget-attach-point="contentBox">
		<h2 class="lightfaceTitle lightfaceDraggable" data-widget-attach-point="title">{title}</h2>
		<div class="lightfaceMessageBox" data-widget-attach-point="messageBox">{message}</div>
		<div class="lightfaceFooter" data-widget-attach-point="footer"></div>
		<div class="lightfaceOverlay" data-widget-attach-point="overlay"></div>
	</div>
</div>
</pre><p>The presentation state of widget would be completely in the developer's hands with Templated!</p><h2>BETA!</h2><p>Templated is currently in BETA state, so please report any bugs you find along the way.  A stable UI solution for MooTools will do everyone a favor.</p><div
class="actions"> <a
href="http://davidwalsh.name/dw-content/templated.php" class="demo">View Demo</a> <a
href="http://github.com/darkwing/Templated" class="demo">GitHub Repo</a><div
class="clear"></div></div><h2>Get&nbsp;Templated!</h2><p>The ability to apply this level of control to widget templating is amazing.  Dijit's use of _Templated help makes the UI framework the best available for any JavaScript framework.  Please consider using Templated for your UI work, especially if your components are open source or maintained by a team.  Have fun with your UIs!</p><p><a
href="http://davidwalsh.name/templated">Introducing MooTools&nbsp;Templated</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/templated/feed</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Determining Object Type with MooTools&#8217;&#160;typeOf</title><link>http://davidwalsh.name/mootools-typeof</link> <comments>http://davidwalsh.name/mootools-typeof#comments</comments> <pubDate>Sun, 18 Sep 2011 18:28:28 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[MooTools]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5297</guid> <description><![CDATA[One thing about JavaScript I dislike is the vagueness of what the typeof operator returns. Pass typeof an array? You get &#8220;object&#8221; back (which it is, but a more concise answer would be helpful). Pass typeof a Date object? You get &#8220;object&#8221; again. What if there was a better way of determining an object&#8217;s descriptive [...]<p><a
href="http://davidwalsh.name/mootools-typeof">Determining Object Type with MooTools&#8217;&nbsp;typeOf</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>One thing about JavaScript I dislike is the vagueness of what the <code>typeof</code> operator returns.  Pass <code>typeof</code> an array?  You get &#8220;object&#8221; back (which it is, but a more concise answer would be helpful).  Pass <code>typeof</code> a Date object?  You get &#8220;object&#8221; again.  What if there was a better way of determining an object&#8217;s descriptive type?  That&#8217;s where the <code>typeOf</code> function within MooTools Core comes into play.</p><h2>typeOf Source and&nbsp;Usage</h2><p>The <code>typeOf</code> function is actually quite short:</p><pre class="js">
var typeOf = this.typeOf = function(item){
	if (item == null) return 'null';
	if (item.$family) return item.$family();

	if (item.nodeName){
		if (item.nodeType == 1) return 'element';
		if (item.nodeType == 3) return (/\S/).test(item.nodeValue) ? 'textnode' : 'whitespace';
	} else if (typeof item.length == 'number'){
		if (item.callee) return 'arguments';
		if ('item' in item) return 'collection';
	}

	return typeof item;
};
</pre><p><code>typeOf</code> checks for specific properties on the object in question to determine its descriptive type.  Simple, right?  Note the <code>$family()</code> check within <code>typeOf</code>;  each Type (Array, Function, Date, etc.) instance is given a <code>$family</code> method which returns its type.  Let&#8217;s try a few <code>typeOf</code> calls:</p><pre class="js">
typeof document.body;  // returns "object"
typeOf(document.body);  // returns "element"

typeof new Date();  // returns "object"
typeOf(new Date());  // returns "date"

typeof [];  // returns "object"
typeOf([]);  // returns "array"
</pre><p><code>typeOf</code> is an awesome utility function, right?  Getting a more detailed object type than simply &#8220;object&#8221; can be hugely help in validating the object before using it. <code>typeOf</code> is just another awesome utility within the MooTools JavaScript framework.</p><p><a
href="http://davidwalsh.name/mootools-typeof">Determining Object Type with MooTools&#8217;&nbsp;typeOf</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/mootools-typeof/feed</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>JavaScript Coding with&#160;Class</title><link>http://davidwalsh.name/javascript-coding-class</link> <comments>http://davidwalsh.name/javascript-coding-class#comments</comments> <pubDate>Thu, 15 Sep 2011 15:08:16 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[AJAX]]></category> <category><![CDATA[Dojo]]></category> <category><![CDATA[JavaScript]]></category> <category><![CDATA[MooTools]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5291</guid> <description><![CDATA[I&#8217;ve spent the last two weeks in London, eating fish&#8217;n'chips, drinking cup&#8217;o'tea, and being a hooligan at the Arsenal. Oh yeah, there was a MooTools hackathon too. The MooTools hackathon was hugely successful and I&#8217;ll be providing more detail about what was accomplished and where MooTools is going over the coming weeks. It was also [...]<p><a
href="http://davidwalsh.name/javascript-coding-class">JavaScript Coding with&nbsp;Class</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<a
href="http://skillsmatter.com/podcast/ajax-ria/ajax-mootools-dojo" rel="nofollow"><img
src="http://davidwalsh.name/dw-content/london_ajax_logo.gif" class="image" alt="London Ajax" /></a><p>I&#8217;ve spent the last two weeks in London, eating fish&#8217;n'chips, drinking cup&#8217;o'tea, and being a hooligan at the Arsenal.  Oh yeah, there was a MooTools hackathon too.  The MooTools hackathon was hugely successful and I&#8217;ll be providing more detail about what was accomplished and where MooTools is going over the coming weeks.  It was also great to meet some of the development team in person instead of simple IRC.  MooTools FTW!</p><p>Another exciting part of my time in London was presenting at London Ajax.  My presentation was called &#8220;JavaScript Coding with Class&#8221;, preaching the values of class-based JavaScript frameworks like MooTools and Dojo.  I kept the talk high-level but I&#8217;m confident I got my point across, showing the value of class structures.</p><div
class="actions"><a
href="http://skillsmatter.com/podcast/ajax-ria/ajax-mootools-dojo" class="demo" rel="nofollow">Watch Presentation</a><div
class="clear"></div></div><p>This was my first time presenting this deck, so let me know if you see room for improvement (outside of the billion &#8220;um&#8217;s&#8221; I used.)</p><div
style="width:425px" id="__ss_9275189"> <strong
style="display:block;margin:12px 0 4px"><a
href="http://www.slideshare.net/davidwalsh83/javascript-coding-with-class" title="JavaScript Coding with Class" target="_blank">JavaScript Coding with Class</a></strong> <iframe
src="http://www.slideshare.net/slideshow/embed_code/9275189" width="425" height="355" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe><div
style="padding:5px 0 12px"> View more <a
href="http://www.slideshare.net/" target="_blank">presentations</a> from <a
href="http://www.slideshare.net/davidwalsh83" target="_blank">davidwalsh83</a></div></div><p><em>Due to popular request, my slides have been embedded above.</em></p><p><a
href="http://davidwalsh.name/javascript-coding-class">JavaScript Coding with&nbsp;Class</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/javascript-coding-class/feed</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Create Namespaced Classes with&#160;MooTools</title><link>http://davidwalsh.name/namespace-mootools</link> <comments>http://davidwalsh.name/namespace-mootools#comments</comments> <pubDate>Mon, 29 Aug 2011 14:22:15 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[MooTools]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5290</guid> <description><![CDATA[MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event, namespaces are technically just nested objects and I recently [...]<p><a
href="http://davidwalsh.name/namespace-mootools">Create Namespaced Classes with&nbsp;MooTools</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event, namespaces are technically just nested objects and I recently shared with you how to <a
href="http://davidwalsh.name/mootools-namespace">create and retrieve nested objects with MooTools</a>.  With those utility functions available, I&#8217;ve modified Class so that you can create namespaced classes easily!</p><h2>The MooTools&nbsp;JavaScript</h2><p>The technique below is very much like <a
href="http://davidwalsh.name/dojo-poupup-menu-hover">monkey patching</a>:</p><pre class="js">
(function() {
	
	// Keep reference to the original Class
	var klass = Class;
	
	// Redefine class ("that's deep")
	Class = function(name, params, context) {
		// Find out if this is namespaced or the original method
		var namespaced = (arguments.length > 1);
		
		// Add the class name to the param list
		if(namespaced) params.$name = name;
		
		// Create and get the original class
		var original = new klass(namespaced ? params : name);
		
		// If namespaced, set class into namespace
		if(namespaced) Object.place(name, original, context);
		
		// Return this newly created class!
		return original;
	};
	
})();
</pre><p>I keep a reference to the original <code>Class</code>, as I&#8217;ll be using it within my new <code>Class</code> function.  The new signature allows for a namespaced class name, the usual class properties, and the context which may be passed to the Object.place method.  The first step is analyzing the arguments provided to Class;  if one argument, it&#8217;s a traditional class;  if more than one argument is provided, we know we&#8217;re trying to create a namespaced class and will act accordingly.  You&#8217;ll also notice that I add a <code>$name</code> property to the class prototype, providing the given class name to the class for later use.  I&#8217;ve found the class&#8217; declaredClass property incredibly helpful within Dojo.</p><p>Here&#8217;s how you&#8217;d use the new Class function:</p><pre class="js">
// Create a namespaced class
var myClass = new Class("davidwalsh.ui.Slider", {
	initialize: function() { 
		// Do stuff!
	},
	doSomething: function(){}
});
// myClass === davidwalsh.ui.Slider!

// Create an instance of the class
var myInstance = new davidwalsh.ui.Slider({
	// props
});

// Get the name of the declared class
var instanceClassName = myInstance.$name; // equals "davidwalsh.ui.Slider"
</pre><p>The first argument becomes the namespace and class name as a string.  The second argument is the traditional parameters argument.  If only one argument is provided to the Class function, the class is created and returned per usual;  in fact, both the namespaced method and traditional method return the newly created class.  The method provided above is completely backward compatible so you wont have rewrite your existing code.</p><p>Namespaced classes are probably a good idea, as many would argue that &#8220;polluting&#8221; the global namespace with numerous objects can lead to problems.  In any event, this namespacing method should help you make the most of <a
href="http://davidwalsh.name/mootools-namespace">my set/get nested object methods</a> and allow you to keep the global namespace as clean as you&#8217;d like!</p><p><a
href="http://davidwalsh.name/namespace-mootools">Create Namespaced Classes with&nbsp;MooTools</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/namespace-mootools/feed</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Create and Retrieve Nested Object with&#160;MooTools</title><link>http://davidwalsh.name/mootools-namespace</link> <comments>http://davidwalsh.name/mootools-namespace#comments</comments> <pubDate>Wed, 24 Aug 2011 22:33:37 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[MooTools]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5288</guid> <description><![CDATA[One argument that&#8217;s been lodged by many is that MooTools doesn&#8217;t encourage the use of namespaces for classes.  The Dojo Toolkit does employ namespaces and provides two helpful functions for working with them:  dojo.getObject and dojo.setObject.  These methods allow for setting and getting of nested objects via strings.  I&#8217;ve ported this functionality MooTools so you [...]<p><a
href="http://davidwalsh.name/mootools-namespace">Create and Retrieve Nested Object with&nbsp;MooTools</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>One argument that&#8217;s been lodged by many is that MooTools doesn&#8217;t encourage the use of namespaces for classes.  The Dojo Toolkit does employ namespaces and provides two helpful functions for working with them:  dojo.getObject and dojo.setObject.  These methods allow for setting and getting of nested objects via strings.  I&#8217;ve ported this functionality MooTools so you can create and retrieve nested objects quickly!</p><h2>The MooTools&nbsp;JavaScript</h2><p>Here goes the magic, slightly modified from Dojo:</p><pre class="js">
(function() {

	// Utility method to get and set objects that may or may not exist
	var objectifier = function(splits, create, context) {
		var result = context || window;
		for(var i = 0, s; result &#038;&#038; (s = splits[i]); i++) {
			result = (s in result ? result[s] : (create ? result[s] = {} : undefined));
		}
		return result;
	};

	// Creates an object if it doesn't already exist
	Object.extend("place", function(name, value, context) {
		var splits = name.split("."), s = splits.pop(), result = objectifier(splits, true, context);
		return result &#038;&#038; s ? (result[s] = value) : undefined;
	});

	// Retrieves an object if not already present
	Object.extend("retrieve", function(name, create, context) {
		return objectifier(name.split("."), create, context);
	});

	// Checks to see if the object exists
	Object.extend("exists", function(name, context) {
		return Object.retrieve(name, false, context) !== undefined;
	});

})();
</pre><p>Both methods use a root method for parsing the string and finding the object, if it exists.  The place method creates the object, accepting the object name, value, and context (base object), creating the object as desired:</p><pre class="js">
// Creates my.namespace.MyClass
Object.place("my.namespace.MyClass", {
	name: "David"
});
// my.namespace.MyClass.name = "David"

// Creates some.existing.objecto.my.namespace.MyClass
Object.place("my.namespace.MyClass", {
	name: "David"
}, some.existing.objecto); // Has to be an existing object
</pre><p>Likewise, the retrieve method searches for the object, and optionally may create it:</p><pre class="js">
// Get an object
Object.retrieve("my.namespace.MyClassToo");

// Try to find an object, create it if it doesn't exist
Object.retrieve("my.namespace.MyClassThree", true);
</pre><p>As a bonus, I&#8217;ve also thrown in an exists method to check for the existence of an object:</p><pre class="js">
Object.exists("my.namespace.MyClassToo"); // returns TRUE or FALSE
</pre><p>Woohoo!  These are some really nice utility methods to have around if you&#8217;re looking to work with dynamic objects and namespaces!</p><p><a
href="http://davidwalsh.name/mootools-namespace">Create and Retrieve Nested Object with&nbsp;MooTools</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/mootools-namespace/feed</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Drag and Drop MooTools File&#160;Uploads</title><link>http://davidwalsh.name/mootools-upload</link> <comments>http://davidwalsh.name/mootools-upload#comments</comments> <pubDate>Wed, 10 Aug 2011 14:05:24 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[AJAX]]></category> <category><![CDATA[CSS]]></category> <category><![CDATA[HTML5]]></category> <category><![CDATA[MooTools]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5279</guid> <description><![CDATA[Honesty hour confession:  file uploading within the web browser sucks.  It just does.  Like the ugly SELECT element, the file input is almost unstylable and looks different on different platforms.  Add to those criticism the fact that we&#8217;re all used to drag and drop operations, yet up until recently, you couldn&#8217;t drag files into a [...]<p><a
href="http://davidwalsh.name/mootools-upload">Drag and Drop MooTools File&nbsp;Uploads</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<p>Honesty hour confession:  file uploading within the web browser sucks.  It just does.  Like the <a
href="http://davidwalsh.name/jquery-chosen">ugly SELECT element</a>, the file input is almost unstylable and looks different on different platforms.  Add to those criticism the fact that we&#8217;re all used to drag and drop operations, yet up until recently, you couldn&#8217;t drag files into a browser to upload them, making file uploading within the browser unintuitive.  With recent advancements in browser technology, the drag and drop method is now supported, but it doesn&#8217;t look good without a bit of work.  Luckily MooTools Core Developer Arian Stolwijk has created a <a
href="https://github.com/arian/mootools-form-upload" rel="nofollow">set of classes</a> to accommodate styling drag and drop file uploading within the browser.  Let&#8217;s have a look at how it works!</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/mootools-upload.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>The&nbsp;HTML</h2><p>The basic setup is the same as a traditional form upload within the browser;  a <code>FORM</code> element with an <code>INPUT</code> of file type:</p><pre class="html">
&lt;form method="post" action="mootools-upload.php" enctype="multipart/form-data" id="uploadForm"&gt;
&lt;div&gt;
	&lt;div class="formRow"&gt;
		&lt;label for="file" class="floated"&gt;File: &lt;/label&gt;
		&lt;input type="file" id="file" name="file[]" multiple&gt;&lt;br&gt;
	&lt;/div&gt;

	&lt;div class="formRow"&gt;
		&lt;input type="submit" name="upload" value="Upload"&gt;
	&lt;/div&gt;
&lt;/div&gt;
&lt;/form&gt;
</pre><p>This setup allows file uploading even if JavaScript is not enabled.  <em>(Note to IT snobs:  get over yourselves and turn JavaScript back on)</em></p><h2>The&nbsp;CSS</h2><p>The &#8220;drop zone&#8221; and &#8220;progress bar&#8221; areas can be easily styled in any fashion you&#8217;d like.  My sample CSS looks like this:</p><pre class="css">
.droppable {
	border: #ccc 1px solid;
	border-radius: 8px;
	background: #eee;
	color: #666;
	padding: 20px;
	margin: 10px;
	clear: both;
	text-align: center;
}

.droppable.hover {
	background: #ddd;
}

.uploadList {
	margin: 0;
	padding: 0;
	list-style: none;
}

.uploadItem {
	overflow: hidden;
	border-bottom: #BCBCBC 1px solid;
	margin: 0 20px;
	padding: 3px;
}

.uploadItem span {
	overflow: hidden;
	width: 150px;
	float: left;
	display: block;
}

a.addInputRow,
a.delInputRow,
.uploadItem a {
	display: inline-block;
	background: url(add.png) no-repeat;
	height: 16px;
	width: 16px;
	text-indent: -999px;
}

.uploadItem a {
	float: left;
	display: block;
	padding-left: 20px;
	background-image: url(delete.png);
}

a.delInputRow {
	background-image: url(delete.png);
}

.progress {
	margin: 5px 0;
	height: 15px;
	border-radius: 3px;
	background: #545A74;
}
</pre><p>Since we have (unfortunately) become accustomed to the ugly <code>INPUT type=file</code>, my only CSS advice is to make sure you make not only your &#8220;drop zone&#8221; apparent but explain that the user should drag and drop.</p><h2>The MooTools&nbsp;JavaScript</h2><p>Arian has provided 3 JavaScript classes within his mootools-form-upload repository:</p><ul><li><code>Form.Upload</code>:  The main worker class, detecting the browser capabilities and building a file uploader based on those features</li><li><code>Form.MultiFileInput</code>:  A class which builds and manages the list of files to be uploaded.</li><li><code>Request.File</code>:  Manages the FormData object, sends files, and reports progress.</li></ul><p>Another resource, iFrameFormRequest, can be included in case the user is rocking a legacy browser.  With the resources above added to the page, let&#8217;s set up our drag and drop file uploader:</p><pre class="js">
window.addEvent('domready', function(){
	
	// Create the file uploader
	var upload = new Form.Upload('file', {
		dropMsg: "Drop files here",
		onComplete: function(){
			alert('Files uploaded!');
		}
	});

	// Use iFrameFormRequest, which posts to iFrame 
	if (!upload.isModern()) {
		new iFrameFormRequest('uploadForm', {
			onComplete: function(response){
				alert('Files uploaded!');
			}
		});
	}

});
</pre><p>We start by creating an instance of <code>Form.Upload</code>, passing it the <code>INPUT</code> node and the class options.  The <code>onComplete</code> option is most important, as it represents the event that fires when all uploads have completed, allowing you to notify the user.</p><p>For more customizable uploads, like notifications for progress and success, you can pair <code>Form.MultipleFileInput</code> and <code>Request.File</code> directly:</p><pre class="js">
// From ReadMe.md

// the input element, the list (ul) and the drop zone element.
var input, list, drop;
// Form.MultipleFileInput instance
var inputFiles = new Form.MultipleFileInput(input, list, drop, {
    onDragenter: drop.addClass.pass('hover', drop),
    onDragleave: drop.removeClass.pass('hover', drop),
    onDrop: drop.removeClass.pass('hover', drop)
});

// Request instance;
var request = new Request.File({
    url: 'files.php'
    // onSuccess
    // onProgress
});

myForm.addEvent('submit', function(event){
    event.preventDefault();
    inputFiles.getFiles().each(function(file){
        request.append('url[]' , file);
    });
    request.send();
});

</pre><p>This solution would be good for using detailed progress bars.</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/mootools-upload.php" class="demo">View Demo</a><div
class="clear"></div></div><p>Outstanding work once again by Arian.  His contribution to the MooTools JavaScript framework has been priceless, and he continues that effort with his drag and drop file upload system.  These classes prove the power of MooTools and the advancement of browsers today.  Give your users the elegant option of drag and drop uploads!</p><p><a
href="http://davidwalsh.name/mootools-upload">Drag and Drop MooTools File&nbsp;Uploads</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/mootools-upload/feed</wfw:commentRss> <slash:comments>16</slash:comments> </item> <item><title>MooTools Wall&#160;Plugin</title><link>http://davidwalsh.name/mootools-wall</link> <comments>http://davidwalsh.name/mootools-wall#comments</comments> <pubDate>Thu, 04 Aug 2011 16:35:45 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[MooTools]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=5275</guid> <description><![CDATA[One of the more impressive MooTools plugins to hit the Forge recently was The Wall by Marco Dell&#8217;Anna.  The Wall creates an endless grid of elements which can be grabbed and dragged, fading in elements as they are encountered.  Let me show you a quick usage of the Wall! View Demo The&#160;HTML There are two elements [...]<p><a
href="http://davidwalsh.name/mootools-wall">MooTools Wall&nbsp;Plugin</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></description> <content:encoded><![CDATA[<a
href="http://davidwalsh.name/dw-content/wall.php"><img
src="http://davidwalsh.name/dw-content/wall.png" alt="MooTools Wall" class="image" /></a><p>One of the more impressive MooTools plugins to hit the Forge recently was <a
href="http://mootools.net/forge/p/the_wall" rel="nofollow">The Wall</a> by Marco Dell&#8217;Anna.  <a
href="http://wall.plasm.it/" rel="nofollow">The Wall</a> creates an endless grid of elements which can be grabbed and dragged, fading in elements as they are encountered.  Let me show you a quick usage of the Wall!</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/wall.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>The&nbsp;HTML</h2><p>There are two elements in the setup: a viewport and a wall holder:</p><pre class="html">
&lt;div id="viewport"&gt;
	&lt;div id="wall"&gt;&lt;/div&gt;
&lt;/div&gt;
</pre><p>Easy enough.</p><h2>The&nbsp;CSS</h2><p>The CSS must be configured to accommodate your wall.  Your viewport&#8217;s <code>height</code> and <code>width</code> properties should be multiples of your block heights.  You don&#8217;t <em>have</em> to configure the viewport that way, but it you wall will be more visually appealing.</p><pre class="css">
#viewport		{ border:1px solid #ccc; width:450px; height:450px; position:relative; overflow:hidden; }
#wall			{ z-index:1; }
#wall img		{ width:150px; height:150px; display:block; float:left; }
</pre><p>My viewport will be 450 x 450, with each element being 150 x 150.</p><h2>The MooTools&nbsp;JavaScript</h2><p>It all starts by creating a Wall instance, providing <code>width</code>, <code>height</code>, <code>rangex</code>, and <code>rangey</code> attributes, providing them as the size of the elements (or not):</p><pre class="js">
// When the DOM is ready
window.addEvent("domready", function() {
	
	// Define a few variables, including the number of images and the "current index"
	var numImages = 23, // 24 images, 0 index
		counter = 0;
		
	// Create the Wall
	new Wall("wall", {
		width: 150,
		height: 150,
		rangex: [-150,150],
		rangey: [-150,150],
		callOnUpdate: function(items) {
			// For every item returned...
			items.each(function(item) {
				// Inject a new image, fade it in
				new Element("img", {
					src: (++counter) + ".jpg",
					styles: {
						opacity: 0
					}
				}).inject(item.node).fade(1);
				// Reset to image one if the maxlength is greater than the counter
				if(counter &gt; numImages) counter = 1;
			});
		}
	}).initWall();	
});
</pre><p>The callOnUpdate method is most important, providing an index by which you can inject your new element (in our case, an image).  Once the counter reaches its maximum length, the index is reset, and the wall continues!</p><div
class="actions"><a
href="http://davidwalsh.name/dw-content/wall.php" class="demo">View Demo</a><div
class="clear"></div></div><p>Awesome work by Marco.  My example is just the most basic of usages &#8212; visit <a
href="http://wall.plasm.it/">Marco&#8217;s website</a> to see the additional options and examples he has provided.  I can see someone using The Wall to generate a very creative, all-encompassing website!</p><p><a
href="http://davidwalsh.name/mootools-wall">MooTools Wall&nbsp;Plugin</a> is a post from: <a
href="http://davidwalsh.name">David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</a></p> ]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/mootools-wall/feed</wfw:commentRss> <slash:comments>9</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 1/58 queries in 0.029 seconds using disk: basic
Object Caching 1416/1518 objects using disk: basic

Served from: davidwalsh.name @ 2012-02-08 13:49:00 -->
