<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jacob Wright - Flex, AIR, PHP, etc. &#187; Javascript</title>
	<atom:link href="http://jacwright.com/blog/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://jacwright.com/blog</link>
	<description>Flex, AIR, PHP, etc.</description>
	<lastBuildDate>Thu, 29 Jul 2010 15:05:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Creating a Hover Menu with HTML5 and Simpli5</title>
		<link>http://jacwright.com/blog/457/creating-a-hover-menu-with-html5-and-simpli5/</link>
		<comments>http://jacwright.com/blog/457/creating-a-hover-menu-with-html5-and-simpli5/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 19:46:11 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Simpli5]]></category>

		<guid isPermaLink="false">http://jacwright.com/blog/?p=457</guid>
		<description><![CDATA[A More Usable Application I decided to build my own version of a contextual hover menu to make my applications more usable. It is meant to appear when you select a piece of data and give you quick access to all the actions you might perform on it. Forget long toolbars and hidden right-click menus. [...]]]></description>
			<content:encoded><![CDATA[<h3>A More Usable Application</h3>
<p>I decided to build my own version of a contextual hover menu to make my applications more usable. It is meant to appear when you select a piece of data and give you quick access to all the actions you might perform on it. Forget long toolbars and hidden right-click menus. I wanted something that a user didn't have to dig around to find, that wouldn't be hard to navigate, and that wasn't hidden (a right-click on the web is not common enough for users to rely on).</p>
<p>I'll walk you through the beginning process I took to create the HoverMenu component using Simpli5 and then I'll cover at a higher level the UX considerations that went into making it even better. You can check out the <a href="http://jacwright.github.com/simpli5/demos/hover-menu.html">component in action</a> first (using Safari, Chrome, or Firefox).</p>
<h3>Base Simpli5 Component</h3>
<p>To start, <a href="http://github.com/jacwright/simpli5">Simpli5</a> encourages using tags that provide good semantics for the application, ignoring whether or not they are valid HTML tags. It makes your application easier to read and understand, and since Simpli5 was made for creating full web applications, SEO and other content-based concerns are thrown out the window. I will start with the component definition in <a href="http://github.com/jacwright/simpli5/blob/master/components/HoverMenu.js" title="HoverMenu source code">HoverMenu.js</a>. I have the base component, menu containers, and menu items that I'll need. There will also be separators, but that has no code or logic and so can just be added in the stylesheet.</p>
<div class="syntax_hilite">
<div id="javascript-5">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> HoverMenu = <span style="color: #003366; font-weight: bold;">new</span> Component<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; extend: Component, <span style="color: #009900; font-style: italic;">// the base when using custom tags</span><br />
&nbsp; &nbsp; template: <span style="color: #003366; font-weight: bold;">new</span> Template<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;hover-menu&gt;&lt;/hover-menu&gt;'</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #009900; font-style: italic;">// component template used when creating from code</span><br />
&nbsp; &nbsp; register: <span style="color: #3366CC;">'hover-menu'</span>, <span style="color: #009900; font-style: italic;">// css selector to convert matching elements into a HoverMenu</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; constructor: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// the constructor</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">submenu</span> = <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span>simpli5.<span style="color: #006600;">selector</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">submenu</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">submenu</span>.<span style="color: #006600;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #003366; font-weight: bold;">var</span> HoverMenuSubmenu = <span style="color: #003366; font-weight: bold;">new</span> Component<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; extend: Component,<br />
&nbsp; &nbsp; template: <span style="color: #003366; font-weight: bold;">new</span> Template<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;menu&gt;&lt;/menu&gt;'</span><span style="color: #66cc66;">&#41;</span>,<br />
&nbsp; &nbsp; register: <span style="color: #3366CC;">'hover-menu menu'</span><br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #003366; font-weight: bold;">var</span> HoverMenuItem = <span style="color: #003366; font-weight: bold;">new</span> Component<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; extend: Component,<br />
&nbsp; &nbsp; template: <span style="color: #003366; font-weight: bold;">new</span> Template<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;menu-item&gt;&lt;/menu-item&gt;'</span><span style="color: #66cc66;">&#41;</span>,<br />
&nbsp; &nbsp; register: <span style="color: #3366CC;">'hover-menu menu-item'</span>,<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; constructor: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>I'll give it a stylesheet <a href="http://github.com/jacwright/simpli5/blob/master/css/HoverMenu.css">HoverMenu.css</a> to make it look good.</p>
<p>What I want is when the user hovers over the button, the menu pops up.</p>
<div class="syntax_hilite">
<div id="javascript-6">
<div class="javascript">constructor: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; ...<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'rollover'</span>, <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">open</span>.<span style="color: #006600;">boundTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'rollout'</span>, <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">close</span>.<span style="color: #006600;">boundTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span>,</p>
<p><span style="color: #000066;">open</span>: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> rect = <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">submenu</span>.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'open'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">submenu</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>left: rect.<span style="color: #006600;">right</span>, top: rect.<span style="color: #006600;">top</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span>,</p>
<p><span style="color: #000066;">close</span>: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">submenu</span>.<span style="color: #000066;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>Of course, sometimes I might want to have the user click to popup the menu, for ones that are used less often or in the case that there are many on the screen (don't want to have popups all over the place by moving your mouse around).</p>
<div class="syntax_hilite">
<div id="javascript-7">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> HoverMenu = <span style="color: #003366; font-weight: bold;">new</span> Component<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; extend: Component,<br />
&nbsp; &nbsp; template: <span style="color: #003366; font-weight: bold;">new</span> Template<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;hover-menu&gt;&lt;/hover-menu&gt;'</span><span style="color: #66cc66;">&#41;</span>,<br />
&nbsp; &nbsp; register: <span style="color: #3366CC;">'hover-menu'</span>,<br />
&nbsp; &nbsp; properties: <span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">'click-only'</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #009900; font-style: italic;">// add attributes that translate to properties in this array</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; constructor: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'click'</span>, <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">open</span>.<span style="color: #006600;">boundTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// click will always open it</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">clickOnly</span> = <span style="color: #003366; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; get clickOnly<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._clickOnly;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,<br />
&nbsp; &nbsp; set clickOnly<span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._clickOnly == value<span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>._clickOnly = value;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">submenu</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value ? <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">un</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'rollover'</span>, <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">open</span>.<span style="color: #006600;">boundTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'rollover'</span>, <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">open</span>.<span style="color: #006600;">boundTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,<br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>Here I added an implicit getter/setter that by default is false so hovering will open the menu. But if hoverMenu.clickOnly = false or &lt;hover-menu click-only="false"&gt;...&lt;/hover-menu&gt; then you'll have to click the button to open the menu.</p>
<p>I've also added other settings for customization: autoClose to close the menu automatically when the mouse moves off of it for a few milliseconds, menuDelay to turn off the delay menus take to close (I talk about this later), and openBelow to cause the menu to open up beneath the button instead of to the side of it.</p>
<p>Next we need to allow menu items to hold submenus and to dispatch events when the user selects them. It would be nice if these can be triggered by code too.</p>
<div class="syntax_hilite">
<div id="javascript-8">
<div class="javascript"><span style="color: #009900; font-style: italic;">// HoverMenuItem</span><br />
events: <span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">'select'</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #009900; font-style: italic;">// add custom events that can be listened to via onevent attributes</span></p>
<p>constructor: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'click'</span>, <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">select</span>.<span style="color: #006600;">boundTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'rollover'</span>, <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">hovered</span>.<span style="color: #006600;">boundTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">submenu</span> = <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">submenu</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">submenu</span>.<span style="color: #006600;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'submenu'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span>,</p>
<p><span style="color: #000066;">open</span>: HoverMenu.<span style="color: #006600;">prototype</span>.<span style="color: #000066;">open</span>, <span style="color: #009900; font-style: italic;">// use the same function from HoverMenu</span></p>
<p><span style="color: #000066;">close</span>: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">submenu</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">submenu</span>.<span style="color: #000066;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">removeClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'open'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">hoveredItem</span> = <span style="color: #003366; font-weight: bold;">null</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span>,</p>
<p>select: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">disabled</span> || <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">submenu</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span>;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">dispatchEvent</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> CustomEvent<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'select'</span>, <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// this is our own event and we will bubble it</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">menu</span>.<span style="color: #000066;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// once selected, close the whole menu</span><br />
<span style="color: #66cc66;">&#125;</span>,</p>
<p>hovered: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">disabled</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span>;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">hoveredItem</span> &amp;&amp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">hoveredItem</span> != <span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">hoveredItem</span>.<span style="color: #000066;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">submenu</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">open</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>Hopefully you can understand the logic by reading through the code, but there are a couple of things I want to point out. The "events" property holds a list of custom events for the component to look for when initializing its attributes. Because I specified the select event there you can add onselect="alert('item selected')" to the tag and it will work. Also, our first usability tidbit for the menu, don't close submenus until the user moves their mouse to a sibling menu item. That wraps up our basic-component-building-101-in-Simpli5 overview and brings us to our user experience in using this component. Now I realize that UX encompasses so much more than a component, but the usability and experience the user has with this component is what I am referring to when I say UX.</p>
<h3>Making it Shine with Usability</h3>
<p>Most of these things were added because I tried using the menu and noticed spots of frustration. Some were added from suggestions of others.</p>
<p>The first thing I did to enhance the usability of the menu was to keep the entire menu and submenus from closing immediately. The less accurate a user has to be with their mouse, the quicker they can get things done and the easier it is to use an application. If the menu closes because a user accidentally moves the mouse a little beyond the menu then they have to start all over again opening the menu up from the beginning. When the hover menu's autoClose is true, the menu waits 600 milliseconds before closing. This allows a user to make mouse mistakes and recover from them before having to reopen the menu.</p>
<p>The next usability piece came from testing with longer submenus. I noticed that if I wanted to click the last item in a submenu and I moved my mouse strait to it, the mouse path went over the edge of the next sibling menu item, closing the previous item's submenu. In order to select that last submenu item I had to alter my mouse path to a 7 shape, moving across to the submenu first, then down to the desired item. In order to allow some forgiveness in the mouse movement while trying not to hamper the speed at opening the next submenu if that is the real desired action, I added a 150ms delay in opening and closing submenus. This seemed to be enough time for a quick mouse movement down across sibling menu items into the submenu, while not being too much time if you wanted to open the sibling submenu. I also added the menuDelay option that defaults to true, but can be set to false if you want to get rid of this 150ms delay.</p>
<p>I added an alternate element style in the stylesheet for elements called &lt;menu-content&gt; which is an alternative to holding menu items in a submenu and allows robust components like color pickers or lists of images to be used, adding to the overall UX of the UI.</p>
<p>I added positioning support for them menus to popup above or to the left of their parent if they are near the edge of the screen. I made the menus append to the body of the document so that they wouldn't get cut off by any overflow auto/hidden elements. There were also other small things I added too, such as a style on items with open submenus so they can be seen easier and allowing the menu to be closed by clicking elsewhere or pressing Esc.</p>
<p>Overall the component turned out quite well. Here is a demo page of it in action (view source to see the markup): <a href="http://jacwright.github.com/simpli5/demos/hover-menu.html">http://jacwright.github.com/simpli5/demos/hover-menu.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/457/creating-a-hover-menu-with-html5-and-simpli5/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Javascript Rollover Rollout Events</title>
		<link>http://jacwright.com/blog/443/javascript-rollover-rollout-events/</link>
		<comments>http://jacwright.com/blog/443/javascript-rollover-rollout-events/#comments</comments>
		<pubDate>Fri, 28 May 2010 00:01:07 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Simpli5]]></category>

		<guid isPermaLink="false">http://jacwright.com/blog/?p=443</guid>
		<description><![CDATA[Javascript has mouseover and mouseout events. Flash has these, but they also have rollover and rollout events. There is a difference, and it can be painful developing Javascript components without the rollover and rollout events. So I put together a little script that provides them for us. Problem You want to perform some action when [...]]]></description>
			<content:encoded><![CDATA[<p>Javascript has mouseover and mouseout events. Flash has these, but they also have rollover and rollout events. There is a difference, and it can be painful developing Javascript components without the rollover and rollout events. So I put together a little script that provides them for us.</p>
<h3>Problem</h3>
<p>You want to perform some action when the cursor rolls onto and off of an HTML element. When you use the mouseover/mouseout events, you get a mouseout and immediately another mouseover when the cursor is over a child element. Technically the mouse is still over the parent element, why does the mouseout happen? What the... you want it to work like CSS :hover. The way you'd think it SHOULD work. You figure, maybe it's just you're catching because bubbling child events, so you return out of your listener if the target does not equal your element. This removes the second mouseover, but you still get a mouseout when hovering over a child element.</p>
<h3>Solution</h3>
<p>Flash's solution was to build in a non-bubbling rollover/rollout event that works like you'd expect. I've never used a mouseover/mouseout in Flash since these new events came about. So, assuming you've got a modern standards-compliant browser (tested on Safari/Chrome &#038; Firefox), this little bit of code will provide rollover/rollout events for your HTML and is non-intrusive. It saves a lot of headache and works great.</p>
<p><b>Updated:</b> The code did not previously take into account siblings, only ancestors. Fixed to find the common ancestor.</p>
<div class="syntax_hilite">
<div id="javascript-10">
<div class="javascript"><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> listener<span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> child = event.<span style="color: #006600;">relatedTarget</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> ancestor = event.<span style="color: #006600;">target</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// cancel if the relatedTarget is a child of the target</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #66cc66;">&#40;</span>child<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>child.<span style="color: #006600;">parentNode</span> == ancestor<span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; child = child.<span style="color: #006600;">parentNode</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// dispatch for the child and each parentNode except the common ancestor</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ancestor = event.<span style="color: #006600;">target</span>.<span style="color: #006600;">parentNode</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> ancestors = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #66cc66;">&#40;</span>ancestor<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ancestors.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span>ancestor<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ancestor = ancestor.<span style="color: #006600;">parentNode</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ancestor = event.<span style="color: #006600;">relatedTarget</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #66cc66;">&#40;</span>ancestor<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>ancestors.<span style="color: #006600;">indexOf</span><span style="color: #66cc66;">&#40;</span>ancestor<span style="color: #66cc66;">&#41;</span> != -<span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ancestor = ancestor.<span style="color: #006600;">parentNode</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; child = event.<span style="color: #006600;">target</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #66cc66;">&#40;</span>child<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> mouseEvent = document.<span style="color: #006600;">createEvent</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'MouseEvents'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mouseEvent.<span style="color: #006600;">initEvent</span><span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">type</span>.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'mouse'</span>, <span style="color: #3366CC;">'roll'</span><span style="color: #66cc66;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">false</span>, <span style="color: #009900; font-style: italic;">// does not bubble</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.<span style="color: #006600;">cancelable</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.<span style="color: #006600;">view</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.<span style="color: #006600;">detail</span>, event.<span style="color: #006600;">screenX</span>, event.<span style="color: #006600;">screenY</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.<span style="color: #006600;">ctrlKey</span>, event.<span style="color: #006600;">altKey</span>, event.<span style="color: #006600;">metaKey</span>, event.<span style="color: #006600;">button</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.<span style="color: #006600;">relatedTarget</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; child.<span style="color: #006600;">dispatchEvent</span><span style="color: #66cc66;">&#40;</span>mouseEvent<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; child = child.<span style="color: #006600;">parentNode</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>child == ancestor<span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// setup the rollover/out events for components to use</span><br />
&nbsp; &nbsp; document.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'mouseover'</span>, listener, <span style="color: #003366; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; document.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'mouseout'</span>, listener, <span style="color: #003366; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>I hope this helps someone having a rough time with Javascript mouseovers.</p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/443/javascript-rollover-rollout-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript Data Binding</title>
		<link>http://jacwright.com/blog/438/javascript-data-binding/</link>
		<comments>http://jacwright.com/blog/438/javascript-data-binding/#comments</comments>
		<pubDate>Tue, 25 May 2010 18:20:16 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Simpli5]]></category>

		<guid isPermaLink="false">http://jacwright.com/blog/?p=438</guid>
		<description><![CDATA[Simpli5 has been coming along nicely as I've been able to put time into it. I'm very excited to announce data-binding. Data Binding Data binding is a technique we have and use in Flash quite a bit that allows one property to stay in sync with another. If obj.x is bound to obj.y then whenever [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://jacwright.com/blog/428/simpli5-an-html5-javascript-framework/">Simpli5</a> has been coming along nicely as I've been able to put time into it. I'm very excited to announce data-binding.</p>
<h3>Data Binding</h3>
<p>Data binding is a technique we have and use in Flash quite a bit that allows one property to stay in sync with another. If obj.x is bound to obj.y then whenever obj.x is changed, obj.y will automatically update to the same value. I've built my own data binding frameworks in ActionScript 3 and I wanted it for my Javascript work as well.</p>
<p>Since Simpli5 has a base-line of HTML5, I was easily able to create data binding in Javascript using implicit getters and setters. Any property on an object can be bound. My binding supports one-way, two-way, and bind-setters, and it allows deep binding.</p>
<p>One-way binding propagates the changes of the <strong>source</strong> object's property to the <strong>target</strong> object's property. Whenever the source's property changes, the target's property updates. If the target's property is changed however, nothing will happen to the source.</p>
<p>Two-way binding keeps the properties of the source and target in sync. When first bound, the source's property will be the value used, but if either the source's property is changed or the target's property is changed the other will update to stay in sync.</p>
<p>Deep binding is being able to bind the property of a property of the source. As an example, obj.x would be shallow binding where obj.x.y.z would be deep binding.</p>
<h3>Binding Usage</h3>
<p>The following shows how binding can be used.</p>
<div class="syntax_hilite">
<div id="javascript-13">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> obj1 = <span style="color: #66cc66;">&#123;</span> <span style="color: #000066;">name</span>: <span style="color: #3366CC;">'Bob'</span> <span style="color: #66cc66;">&#125;</span>;<br />
<span style="color: #003366; font-weight: bold;">var</span> obj2 = <span style="color: #66cc66;">&#123;</span> <span style="color: #000066;">name</span>: <span style="color: #3366CC;">'Henry'</span> <span style="color: #66cc66;">&#125;</span>;</p>
<p>Bind.<span style="color: #006600;">property</span><span style="color: #66cc66;">&#40;</span>obj1, <span style="color: #3366CC;">'name'</span>, obj2, <span style="color: #3366CC;">'name'</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>obj2.<span style="color: #000066;">name</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// will be Bob</span><br />
obj1.<span style="color: #000066;">name</span> = <span style="color: #3366CC;">'Patricia'</span>; <span style="color: #009900; font-style: italic;">// the binding makes obj2.name update as well</span><br />
<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>obj2.<span style="color: #000066;">name</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// will be Patricia</span></p>
<p><span style="color: #003366; font-weight: bold;">var</span> obj1 = <span style="color: #66cc66;">&#123;</span> <span style="color: #000066;">name</span>: <span style="color: #3366CC;">'Bob'</span>, address: <span style="color: #66cc66;">&#123;</span> zip: <span style="color: #CC0000;">12345</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#125;</span>;<br />
<span style="color: #003366; font-weight: bold;">var</span> obj2 = <span style="color: #66cc66;">&#123;</span> <span style="color: #000066;">name</span>: <span style="color: #3366CC;">'Henry'</span>, address: <span style="color: #66cc66;">&#123;</span> zip: <span style="color: #CC0000;">56789</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#125;</span>;</p>
<p>Bind.<span style="color: #006600;">property</span><span style="color: #66cc66;">&#40;</span>obj1, <span style="color: #3366CC;">'address.zip'</span>, obj2, <span style="color: #3366CC;">'address.zip'</span>, <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// the last param (true) means a two way bind</span></p>
<p><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>obj2.<span style="color: #006600;">address</span>.<span style="color: #006600;">zip</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// 12345</span></p>
<p>obj2.<span style="color: #006600;">address</span>.<span style="color: #006600;">zip</span> = <span style="color: #3366CC;">'unknown'</span>;<br />
<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>obj1.<span style="color: #006600;">address</span>.<span style="color: #006600;">zip</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// will be 'unknown' because we are 2-way binding</span><br />
obj1.<span style="color: #006600;">address</span>.<span style="color: #006600;">zip</span> = <span style="color: #CC0000;">12345</span>;<br />
<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>obj2.<span style="color: #006600;">address</span>.<span style="color: #006600;">zip</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// will be 12345 again, same as obj1 </span></div>
</div>
</div>
<p></p>
<h3>Binding added to Template</h3>
<p>I've added a new method to Template called createBound(). The Template.create() method will give you back an HTML element created from the template's HTML, replacing anything in curly braces with the evaluated javascript equivalent. With Template.createBound() you get back an HTML element which continues to update its attributes or text content as properties in the data or element change. Perhaps the best way to explain is to show some code.</p>
<div class="syntax_hilite">
<div id="javascript-14">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> template = <span style="color: #003366; font-weight: bold;">new</span> Template<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;button&gt;&lt;img src=&quot;{this.src}&quot; alt=&quot;{this.label}&quot;/&gt; Say {this.label}&lt;/button&gt;'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #003366; font-weight: bold;">var</span> btn = template.<span style="color: #006600;">createBound</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>btn.<span style="color: #006600;">outerHTML</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// &lt;button&gt;&lt;img src=&quot;&quot; alt=&quot;&quot;/&gt; Say &lt;/button&gt;</span></p>
<p>btn.<span style="color: #006600;">label</span> = <span style="color: #3366CC;">'Hello World'</span>;<br />
btn.<span style="color: #006600;">src</span> = <span style="color: #3366CC;">'images/hello_world.png'</span>;</p>
<p><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>btn.<span style="color: #006600;">outerHTML</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// &lt;button&gt;&lt;img src=&quot;images/hello_world.png&quot; alt=&quot;Hello World&quot;/&gt; Say Hello World&lt;/button&gt; </span></div>
</div>
</div>
<p></p>
<p>I've made it the component creation method of course, so now the properties on your components can update the HTML directly. And it isn't setting the innerHTML of the component, which would replace all the sub-elements and destroy any listeners that were on the old ones. It's binding smartly so that attributes and text get updated carefully.</p>
<h3>Limitations</h3>
<p>There are properties on elements that binding will not work the way you'd expect. Things like element.id, element.firstChild, img.src, et cetera, which the browser treats in a special way but are not getters and setters (as far as the javascript can tell anyway), these cannot be bound to reliably. They cannot be the source of a bind, but they could be the target of a one-way (non-deep) bind just fine.</p>
<h3>Testing</h3>
<p>I've started adding unit tests to the framework, especially since data binding can be such a tricky thing. I've got tests around the binding and the templates right now and feel confident that they are working pretty well. I'm using <a href="http://code.google.com/p/js-test-driver/">js-test-driver</a>. I've also added a maven build file for the tests and for compressing the library into a single file and a compressed file.</p>
<p>The Simpli5 project is being hosted on Github: <a href="http://github.com/jacwright/simpli5">http://github.com/jacwright/simpli5</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/438/javascript-data-binding/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Simpli5, an HTML5 Javascript Framework</title>
		<link>http://jacwright.com/blog/428/simpli5-an-html5-javascript-framework/</link>
		<comments>http://jacwright.com/blog/428/simpli5-an-html5-javascript-framework/#comments</comments>
		<pubDate>Fri, 07 May 2010 16:13:49 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://jacwright.com/blog/?p=428</guid>
		<description><![CDATA[I've started putting together a new HTML5 Javascript framework that is only compatible with HTML5 browsers. I'm able to have a much smaller library that way, and I'm able to do stuff I couldn't otherwise. Like... Optimization Because I'm using built-in Javascript methods (written in C) for many things I do, I am taking advantage [...]]]></description>
			<content:encoded><![CDATA[<p>I've started putting together a new <a href="http://github.com/jacwright/simpli5" title="Simpli5, HTML5 Javascript Framework">HTML5 Javascript framework</a> that is only compatible with HTML5 browsers. I'm able to have a much smaller library that way, and I'm able to do stuff I couldn't otherwise. Like...</p>
<h3>Optimization</h3>
<p>Because I'm using built-in Javascript methods (written in C) for many things I do, I am taking advantage of the speed of modern HTML5 browsers, without the bloat of code that makes up for missing features. Array.prototype.slice is used to convert NodeLists and Argument objects into arrays with $.toArray(); querySelector and querySelectorAll are used to quickly find the first or all matches for a given selector string. The built-in Array.prototype.forEach is used for iterating over arrays.</p>
<h3>HTMLElement.prototype</h3>
<p>I can add methods to the prototype of Node and HTMLElement, giving all elements in my document functionality that I feel are common enough to be needed.</p>
<div class="syntax_hilite">
<div id="javascript-19">
<div class="javascript">element.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'click'</span>, callback, <span style="color: #003366; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> <span style="color: #009900; font-style: italic;">// same as element.addEventListener(...) but shorter</span><br />
element.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'my-class-name'</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// hasClass, removeClass, toggleClass</span><br />
element.<span style="color: #006600;">findFirst</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div .red'</span><span style="color: #66cc66;">&#41;</span>; element.<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #66cc66;">&#41;</span>;<br />
element.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; element.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;b&gt;Hi!&lt;/b&gt;'</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>And much much more.</p>
<h3><code>simpl5</code> Element Wrapper</h3>
<p>I've also create a jQuery-like class (called simpli5) which is basically an Array (uses all the <em>native</em> Array methods like concat, splice, forEach, map, etc.) and forwards all my calls I specify onto the elements in the array.</p>
<div class="syntax_hilite">
<div id="javascript-20">
<div class="javascript">simpli5<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#my-div'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toggleClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'blue'</span><span style="color: #66cc66;">&#41;</span>;<br />
$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'click'</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; event.<span style="color: #006600;">preventDefault</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// real event object, don't need to fake it with a compatible browser</span><br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<h3>Class Model and EventDispatcher</h3>
<p>I like the class model a lot. It is actually compatible with earlier browsers as well. And I do use getters/setters now that I can. I also created an EventDispatcher class that provides the addEventListener/removeEventListener/dispatchEvent, and the shortcut methods on/un.</p>
<div class="syntax_hilite">
<div id="javascript-21">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> Foo = <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Class</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; extend: EventDispatcher,<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; init: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066;">name</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>._name = <span style="color: #000066;">name</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; get <span style="color: #000066;">name</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._name;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,<br />
&nbsp; &nbsp; set <span style="color: #000066;">name</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._name == value<span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> oldName = <span style="color: #000066; font-weight: bold;">this</span>._name;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>._name = value;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">dispatchEvent</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> PropertyChangeEvent<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'name'</span>, oldName, value<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #003366; font-weight: bold;">var</span> Bar = <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Class</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; extend: Foo,<br />
&nbsp; &nbsp; init: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// I've found this to be the simplest most optimized (least hacky) way for overrides</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// even though the syntax with a string isn't ideal. And I've looked around.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">callSuper</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'init'</span>, <span style="color: #3366CC;">'Bar'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// override only the getter to make it read-only</span><br />
&nbsp; &nbsp; get <span style="color: #000066;">name</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._name;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<h3>Component Model</h3>
<p>The part I'm most excited about is my component model. It allows you to make DOM elements <strong>become</strong> new classes. I can take a common &lt;button id="myButton"&gt;&lt;/button&gt; (HTMLButtonElement) element and make it become my ToggleButton component. You will even get a "true" when doing: document.getElementById('myButton') instanceof ToggleButton. The following is a simple Button component.</p>
<div class="syntax_hilite">
<div id="javascript-22">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> Button = <span style="color: #003366; font-weight: bold;">new</span> Component<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; extend: HTMLButtonElement, <span style="color: #009900; font-style: italic;">// be sure to extend the type you are becoming</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// the default template, true == cached (thanks ExtJS)</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// this component will become the top-level element in the template</span><br />
&nbsp; &nbsp; template: <span style="color: #003366; font-weight: bold;">new</span> Template<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;button&gt;&lt;/button&gt;'</span>, <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>,<br />
&nbsp; &nbsp; init: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>label<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>label<span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">label</span> = label;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,<br />
&nbsp; &nbsp; get label<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">text</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,<br />
&nbsp; &nbsp; set label<span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">text</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #009900; font-style: italic;">// create a new button and add it into the document</span><br />
<span style="color: #003366; font-weight: bold;">var</span> button = <span style="color: #003366; font-weight: bold;">new</span> Button<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
button.<span style="color: #006600;">label</span> = <span style="color: #3366CC;">"Hello World"</span>;<br />
document.<span style="color: #006600;">body</span>.<span style="color: #006600;">prepend</span><span style="color: #66cc66;">&#40;</span>button<span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #009900; font-style: italic;">// turn all existing &lt;button&gt;&lt;/button&gt; elements into Button components</span><br />
document.<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'button'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">make</span><span style="color: #66cc66;">&#40;</span>Button<span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<h3>Just the Beginning</h3>
<p>This is just the start. I've been borrowing ideas and code from libraries such as jQuery, mootools, prototype, and Extjs. The library is still only partial with many missing features, but it will mature, and the beginning I've got has been quite exciting.</p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/428/simpli5-an-html5-javascript-framework/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Displaying Flash generated images in HTML &lt;img/&gt;, realtime</title>
		<link>http://jacwright.com/blog/254/displaying-flash-generated-images-in-html-img-realtime/</link>
		<comments>http://jacwright.com/blog/254/displaying-flash-generated-images-in-html-img-realtime/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 13:00:45 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://jacwright.com/blog/?p=254</guid>
		<description><![CDATA[So there may not be an exorbitant amount of use-cases for displaying an image in HTML that was generated real-time in Flash, but I thought's cool, so I'm sharing anyway. It makes the inner-geek in me smile. Short background: I was looking into creating a Flash drawing app that consisted only of the canvas portion, [...]]]></description>
			<content:encoded><![CDATA[<p>So there may not be an exorbitant amount of use-cases for displaying an image in HTML that was generated real-time in Flash, but I thought's cool, so I'm sharing anyway. It makes the inner-geek in me smile.</p>
<p>Short background: I was looking into creating a Flash drawing app that consisted only of the canvas portion, with the rest of the UI in HTML. This is for many reasons including the size of Flex, the native OS components in the browser, speed, and hey, it forces separation of core app logic from the presentation logic, right? ;)</p>
<p>So I wanted a way to display thumbnails of the pages of my drawing. This was all proof of concept and I only have one page and one thumbnail. To do so, I used the <a href="http://en.wikipedia.org/wiki/Data_URI_scheme">URI Data scheme</a> to set the data of a HTML <code>&lt;IMG&gt;</code> element. The only modern browser (if you can call it that) which this doesn't work with is IE6, an acceptable loss for proof-of-concept. Here is how it works in my proof of concept:</p>
<p>See the <strong><a href="http://withincode.com/studio/">Studio proof of concept</a></strong></p>
<p>First, I take a resized bitmap snapshot of my canvas anytime something on it changes, in this example, when it scrolls (I may post about the scrolling in another post, also cool geek stuff). This is done using the BitmapData.copy() method. Actually, I'm using my <a href="http://code.google.com/p/jacwright/source/browse/trunk/flash/jac/src/jac/image/ImageUtils.as">ImageUtils</a> class which ensures it looks good when resized (more about that in <a title="High quality, high performance thumbnails in Flash" href="http://jacwright.com/blog/221/high-quality-high-performance-thumbnails-in-flash/">another post</a>). Then I turn the BitmapData object into a PNG with <a href="http://code.google.com/p/as3corelib/source/browse/trunk/src/com/adobe/images/PNGEncoder.as">AS3CoreLib's PNGEncoder</a>. I now have the bytes for a PNG, so how do I get it to HTML without sending it to the server first? Well, I'll tell you!!</p>
<p>I take the ByteArray that the PNG encoder class gives me and I encode it with Base64Encoder, prepend "data:image/png;base64," to it, then send it out to javascript using ExternalInterface to call a JS function already on the page. That function receives the base 64 encoded string and sets it to the "src" property of the little thumb image down at the bottom of the page. You can see every time you scroll it updates. No biggie right, but if you right-click on it you'll see that it is just an HTML image, right-click on the canvas and see that it is a Flash movie. No server round-trips, 100% Flash/Javascript realtime interaction. Tada!</p>
<p>I did a bit more playing around with it, giving the bitmap rounded corners in Flash before sending it to the browser, tried shadows too, but decided they didn't look great in this interface, so left them out.</p>
<p>Really fun stuff. I hope this is inspiring, even if it isn't useful in your situation. Next time I'll talk about how the native browser scrollbar is scrolling the Flash content, though you may be able to guess easy enough.</p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/254/displaying-flash-generated-images-in-html-img-realtime/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Mint, Stat&#8217;s and Design</title>
		<link>http://jacwright.com/blog/39/mint-stats-and-design/</link>
		<comments>http://jacwright.com/blog/39/mint-stats-and-design/#comments</comments>
		<pubDate>Wed, 14 Sep 2005 20:01:22 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[Applications]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.jacwright.com/blog/39/mint-stats-and-design/</guid>
		<description><![CDATA[<a href="http://www.haveamint.com/">Mint</a> is a new product recently released to track the statistics on your website.  It has gotten much attention from several areas, and I thought it would be worth it to check it out.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.haveamint.com/">Mint</a> is a new product recently released to track the statistics on your website.  It has gotten much attention from several areas, and I thought it would be worth it to check it out.</p>
<p>I am currently using <a href="http://www.statcounter.com/">StatCounter.com</a> to track the traffic to my site.  It is really useful when you use their paid service which keeps track of more than the last 100 visits to your site (the free version cap).  It lets you view all the regular stats as well as "drill down" to see the actual path a single visitor took through your site.  But, it is a monthly subscription service, which means potenially hundreds of dollars a year, even though it's not much each month.</p>
<p>Mint is a one-time site liscence fee of $30.  So I felt it would be a good investment over the monthly fees of StatCounter.  What sums up my thoughts upon using Mint is this: a regular stat tracker was given a <em>great</em> design and some cool javascript effects to make things easier to use.  It looks wonderful, but it lacks much.  Things such as tracking return visitors, displaying charts and graphs of the data, and being able to drill down to see individual users and their paths are things that really make a web stat tracker useful.  A good user interface is important as well.</p>
<p>One bonus to Mint is it can be extendable with plugins or "pepper" as it's called for marketing.  Although, I'm not sure if extras will be sold by other developers, release for free even though the original is a paid product, or both.  I should make it easier for the maker of mint to deploy new features anyway.</p>
<p>Overall, I enjoy Mint, and it's price is much more favorable than the recurring price of StatCounter.  I will stick with it and create some nice graphs of the data for my personal use (whenever I get spare time).  Perhaps sell it as a plugin or something.</p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/39/mint-stats-and-design/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>From Text to Node</title>
		<link>http://jacwright.com/blog/32/from-text-to-node/</link>
		<comments>http://jacwright.com/blog/32/from-text-to-node/#comments</comments>
		<pubDate>Thu, 11 Aug 2005 16:01:55 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.jacwright.com/blog/32/from-text-to-node/</guid>
		<description><![CDATA[I have created a method that assists in creating your HTML elements in Javascript. <code>textToNode</code> takes a string of text and converts it into a DOM element which you may then use for insertion.  It's much easier to use than creating each node seperately, adding attributes to it, then appending it to the parent.  It also works for table elements (tr, td, etc.) and the option element in a drop-down box.]]></description>
			<content:encoded><![CDATA[<p>I have created a method that assists in creating your HTML elements in Javascript. <code>textToNode</code> takes a string of text and converts it into a DOM element which you may then use for insertion.  It's much easier to use than creating each node seperately, adding attributes to it, then appending it to the parent.  It also works for table elements (tr, td, etc.) and the option element in a drop-down box.</p>
<p>This was created for DOMClass objects but has been very nice for creating elements on the fly.  It is as easy to use as the innerHTML property of an element, but it works with table elements etc. and gives you the node reference which you may then attach code to as needed.</p>
<p>Javascript is beautiful.</p>
<div class="code">
<div class="syntax_hilite">
<div id="javascript-23">
<div class="javascript"><span style="color: #003366; font-weight: bold;">function</span> textToNode<span style="color: #66cc66;">&#40;</span>text<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> type = text.<span style="color: #006600;">match</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/&lt;<span style="color: #66cc66;">&#40;</span>\w+<span style="color: #66cc66;">&#41;</span>/</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> container = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> elem;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #66cc66;">&#40;</span>type<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'thead'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'tfoot'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'tbody'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; container.<span style="color: #006600;">innerHTML</span> = <span style="color: #3366CC;">'&lt;table&gt;'</span> + text + <span style="color: #3366CC;">'&lt;/table&gt;'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elem = container.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">firstChild</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'tr'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'th'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; container.<span style="color: #006600;">innerHTML</span> = <span style="color: #3366CC;">'&lt;table&gt;&lt;tbody&gt;'</span> + text + <span style="color: #3366CC;">'&lt;/tbody&gt;&lt;/table&gt;'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elem = container.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">firstChild</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'td'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; container.<span style="color: #006600;">innerHTML</span> = <span style="color: #3366CC;">'&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;'</span> + text + <span style="color: #3366CC;">'&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elem = container.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">firstChild</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'option'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; container.<span style="color: #006600;">innerHTML</span> = <span style="color: #3366CC;">'&lt;select&gt;'</span> + text + <span style="color: #3366CC;">'&lt;/select&gt;'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elem = container.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">firstChild</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">default</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; container.<span style="color: #006600;">innerHTML</span> = text;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elem = container.<span style="color: #006600;">firstChild</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> elem;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p>
</div>
<p>First I check what type of element this is using a regular expression.  Then I make a container to put my innerHTML in.  Following is a switch statement to handle the special types that can't be created using innerHTML unless inside of their native element.  I assign the target node to the elem variable and then return it after the switch statement.  Pretty simple, really nice to use.</p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/32/from-text-to-node/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Class for the DOM</title>
		<link>http://jacwright.com/blog/29/class-for-the-dom/</link>
		<comments>http://jacwright.com/blog/29/class-for-the-dom/#comments</comments>
		<pubDate>Thu, 04 Aug 2005 04:37:47 +0000</pubDate>
		<dc:creator>Jacob Wright</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Object Oriented Programming]]></category>

		<guid isPermaLink="false">http://www.jacwright.com/blog/?p=29</guid>
		<description><![CDATA[It's alive!!  I have created a very cool, very simple, function to create new javascript objects that are (not just act upon) HTML DOM elements.  Let me explain.  Say I have a cool new tree menu I'm creating (true story), and I want others to be able to just start creating the menu items and adding them in via javascript if they need to.  The ideal way would be to just say <code>myItem = new MenuItem('Cool Item');</code> and have it return to me the actual menu item (the TR tag or LI tag) which can be inserted into the menu.]]></description>
			<content:encoded><![CDATA[<p>It's alive!!  I have created a very cool, very simple, function to create new javascript objects that are (not just act upon) HTML DOM elements. Let me explain. Say I have a cool new tree menu I'm creating (true story), and I want others to be able to just start creating the menu items and adding them in via javascript if they need to.  The ideal way would be to just say myItem = new MenuItem('Cool Item'); and have it return to me the actual menu item (the TR tag or LI tag) which can be inserted into the menu.</p>
<p>No more of this element wrapper stuff.  I don't want to code my classes to <strong>act</strong> upon a DOM element.  I want to write them as if they actually <strong>are</strong>the DOM element, just a specialized one.  This makes much more sense to me as I am working with the objects of the DOM.</p>
<p>Here is the code:</p>
<div class="code">
<div class="syntax_hilite">
<div id="javascript-24">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> DOMClass = <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; create: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>element<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> div = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; div.<span style="color: #006600;">innerHTML</span> = element;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> elem = div.<span style="color: #006600;">firstChild</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">delete</span> div;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> property <span style="color: #000066; font-weight: bold;">in</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elem<span style="color: #66cc66;">&#91;</span>property<span style="color: #66cc66;">&#93;</span> = <span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#91;</span>property<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">initialize</span><span style="color: #66cc66;">&#41;</span> elem.<span style="color: #006600;">initialize</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span>elem, arguments<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> elem;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span>;</div>
</div>
</div>
<p>
</div>
<p>You can just add this to your <a href="http://prototype.conio.net/" target="_blank" title="Prototype JavaScript Framework">Prototype</a> library or something.  Then start making your components.</p>
<p>The way to use this is pretty straightforward.  First, you create a new "class" like so:</p>
<div class="code">
<div class="syntax_hilite">
<div id="javascript-25">
<div class="javascript">MySentence = DOMClass.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;p&gt;This is my &lt;strong&gt;great&lt;/strong&gt; text&lt;/p&gt;'</span><span style="color: #66cc66;">&#41;</span>;<br />
MySentence.<span style="color: #006600;">prototype</span> = <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; initialize: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i = <span style="color: #CC0000;">0</span>; i &lt;this.<span style="color: #006600;">childNodes</span>.<span style="color: #006600;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeName</span> == <span style="color: #3366CC;">'STRONG'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">word</span> = <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,<br />
&nbsp; &nbsp; rewrite: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>word<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">word</span>.<span style="color: #006600;">innerHTML</span> = word;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span>;</div>
</div>
</div>
<p></div>
<p>Let's walk through this step by step.  First, I create a new class called MySentence. It will be a paragraph element and it will have some text inside of it (the contents are not required). Simple enough. I can then declare my prototype for the class which will contain all the methods of my new class, including an initialize method which will be called if it exists. In the initialize method I find the STRONG tag and assign it to the property "word" for use in my other method, rewrite. That method just changes the word inside the STRONG tag. I now have a class that when created, will be a paragraph with some <strong>great</strong> text inside of it and a method to rewrite the word "great." You may even try it out and see it working in action. Here is a fun test:</p>
<div class="code">
<div class="syntax_hilite">
<div id="javascript-26">
<div class="javascript">window.<span style="color: #000066;">onload</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> s = <span style="color: #003366; font-weight: bold;">new</span> MySentence<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; document.<span style="color: #006600;">body</span>.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; s.<span style="color: #006600;">onclick</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">rewrite</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'pretty good'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span>;</div>
</div>
</div>
<p>
</div>
<p>Putting all of the above code in a HTML page will give you some text that when clicked on, will change the wording. Pretty useful huh! Ok, so maybe this particular example isn't uber useful, but coding new objects, widgets, and components might be easier.  At least a little bit.  You may see the above <a href="/blog/DOMClass.html">example in action</a>.</p>
<p>I'll see what I can add onto this to make it better.  Perhaps something where you can access the tags inside by saying this.word = this.strong[0]; instead of having to loop through all the children.</p>
<p>This class works for both IE 6 and Firefox.  I would be very interested to know how many other browsers it worked on.</p>
<p>I have another javascript goody that my friend <a href="http://www.dexlo.com/wordpress/" target="_blank" title="Dexlo">Derek</a> and I worked on together which is pretty cool.  I'll share that soon too.</p>
]]></content:encoded>
			<wfw:commentRss>http://jacwright.com/blog/29/class-for-the-dom/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
