<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><!-- generator="wordpress/2.2.2" --><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>perfection kills</title>
	<link>http://thinkweb2.com/projects/prototype</link>
	<description>Exploring prototype by example</description>
	<pubDate>Fri, 15 Aug 2008 21:50:04 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/PerfectionKills" type="application/rss+xml" /><item>
		<title>Semantic constructors</title>
		<link>http://feeds.feedburner.com/~r/PerfectionKills/~3/337591570/</link>
		<comments>http://thinkweb2.com/projects/prototype/semantic-constructors/#comments</comments>
		<pubDate>Thu, 17 Jul 2008 01:17:14 +0000</pubDate>
		<dc:creator>kangax</dc:creator>
		
		<category><![CDATA[Class.create]]></category>

		<guid isPermaLink="false">http://thinkweb2.com/projects/prototype/semantic-constructors/</guid>
		<description><![CDATA[One of the easiest ways to inspect an object is to type convert it to a string:

function foo&#40;&#41; &#123; 
  return 'foo'; 
&#125; 
foo + ''; // &#34;function foo(){ return 'foo'; }&#34; 
// or 
foo.toString&#40;&#41;; 
// or 
String&#40;foo&#41;;

When using Class.create from prototype.js, I often get annoyed by a meaningless result of constructor&#8217;s toString:

var Person [...]]]></description>
			<content:encoded><![CDATA[<p>One of the easiest ways to inspect an object is to type convert it to a string:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> foo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'foo'</span>; 
<span style="color: #66cc66;">&#125;</span> 
foo + <span style="color: #3366CC;">''</span>; <span style="color: #009900; font-style: italic;">// &quot;function foo(){ return 'foo'; }&quot; </span>
<span style="color: #009900; font-style: italic;">// or </span>
foo.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 
<span style="color: #009900; font-style: italic;">// or </span>
String<span style="color: #66cc66;">&#40;</span>foo<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>When using Class.create from prototype.js, I often get annoyed by a meaningless result of constructor&#8217;s toString:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> Person = <span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>
  initialize: <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>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> = <span style="color: #000066;">name</span>;
  <span style="color: #66cc66;">&#125;</span>,
  speak: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>msg<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> + <span style="color: #3366CC;">' says: '</span> + msg;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
Person + <span style="color: #3366CC;">''</span>; <span style="color: #009900; font-style: italic;">// &quot;function klass() { this.initialize.apply(this, arguments); }&quot;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> Employee = <span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span>Person, <span style="color: #66cc66;">&#123;</span>
  initialize: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>$super, dept<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    $super<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">dept</span> = dept;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
Employee + <span style="color: #3366CC;">''</span>; <span style="color: #009900; font-style: italic;">// &quot;function klass() { this.initialize.apply(this, arguments); }&quot;</span></pre></div></div>

<p>As you can see, constructor&#8217;s default toString tells little about what&#8217;s going on under the hood. In fact, Class.create returns a generic constructor-function which only calls initialize method of a prototype. In other words it acts as a proxy.  One of the ways to see actual constructor&#8217;s code is to inspect &#8220;initialize&#8221; method directly:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #009900; font-style: italic;">// nothing new here</span>
Person + <span style="color: #3366CC;">''</span>; <span style="color: #009900; font-style: italic;">// &quot;function klass() { this.initialize.apply(this, arguments); }&quot;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// and the actual code</span>
Person.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">initialize</span> + <span style="color: #3366CC;">''</span>; <span style="color: #009900; font-style: italic;">// &quot;function (name) { this.name = name; }&quot;</span></pre></div></div>

<p>This is quite verbose, don&#8217;t you think? Let&#8217;s monkey patch Class.create a little:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #006600;">create</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>original<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> fn = <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>
    <span style="color: #003366; font-weight: bold;">var</span> result = original.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span>, arguments<span style="color: #66cc66;">&#41;</span>;
    result.<span style="color: #006600;">toString</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> <span style="color: #000066; font-weight: bold;">return</span> result.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">initialize</span>.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>;
    <span style="color: #000066; font-weight: bold;">return</span> result;
  <span style="color: #66cc66;">&#125;</span>;
  fn.<span style="color: #006600;">toString</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> <span style="color: #000066; font-weight: bold;">return</span> original.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>;
  <span style="color: #000066; font-weight: bold;">return</span> fn;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>We simply redefine original Class.create with an &#8220;enhanced&#8221; version. New version &#8220;binds&#8221; <code>toString</code> of prototype.initialize as <code>toString</code> of constructor-function. As a bonus, it also takes care of a &#8220;wrapped&#8221; Class.create.toString itself - trying to be as unobtrusive as possible:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #009900; font-style: italic;">// monkey-patch Class.create first...</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// Person.toString now yields much more informative result</span>
Person + <span style="color: #3366CC;">''</span>; <span style="color: #009900; font-style: italic;">// &quot;function (name) { this.name = name; }&quot;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// Class.create code seems to be unchanged</span>
<span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #006600;">create</span> + <span style="color: #3366CC;">''</span>; <span style="color: #009900; font-style: italic;">// outputs actual Class.create code</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/PerfectionKills/~4/337591570" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thinkweb2.com/projects/prototype/semantic-constructors/feed/</wfw:commentRss>
		<feedburner:origLink>http://thinkweb2.com/projects/prototype/semantic-constructors/</feedburner:origLink></item>
		<item>
		<title>Protolicious</title>
		<link>http://feeds.feedburner.com/~r/PerfectionKills/~3/299544738/</link>
		<comments>http://thinkweb2.com/projects/prototype/protolicious/#comments</comments>
		<pubDate>Wed, 28 May 2008 03:51:27 +0000</pubDate>
		<dc:creator>kangax</dc:creator>
		
		<category><![CDATA[Protolicious]]></category>

		<guid isPermaLink="false">http://thinkweb2.com/projects/prototype/protolicious/</guid>
		<description />
			<content:encoded><![CDATA[<img src="http://feeds.feedburner.com/~r/PerfectionKills/~4/299544738" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thinkweb2.com/projects/prototype/protolicious/feed/</wfw:commentRss>
		<feedburner:origLink>http://thinkweb2.com/projects/prototype/protolicious/</feedburner:origLink></item>
		<item>
		<title>Updated Scripteka</title>
		<link>http://feeds.feedburner.com/~r/PerfectionKills/~3/280165539/</link>
		<comments>http://thinkweb2.com/projects/prototype/updated-scripteka/#comments</comments>
		<pubDate>Tue, 29 Apr 2008 15:48:48 +0000</pubDate>
		<dc:creator>kangax</dc:creator>
		
		<category><![CDATA[scripteka]]></category>

		<guid isPermaLink="false">http://thinkweb2.com/projects/prototype/updated-scripteka/</guid>
		<description><![CDATA[Max and I have just updated Scripteka to version 1.1
In case you haven&#8217;t been following things around here, Scripteka is the prototype extensions library - a convenient place to find, rate and submit prototype.js based goodness.
The library still looks and feels the same, since our main goal was to improve overall usability and functionality.
Some of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://mediumexposure.com">Max</a> and I have just updated <a href="http://scripteka.com">Scripteka</a> to version 1.1<br/><br />
In case you haven&#8217;t been following things around here, Scripteka is the prototype extensions library - a convenient place to find, rate and submit prototype.js based goodness.<br/><br />
The library still looks and feels the same, since our main goal was to improve overall usability and functionality.<br/></p>
<p>Some of the new features include:</p>
<ul>
<li>User Dashboard, where one can see all their scripts, and edit them.</li>
<li>Image upload for scripts. Finally, you can replace that default &#8220;empty page&#8221; image with your own.</li>
<li>Improved RSS feed with more useful information, and better formatting.</li>
<li>Improved communication and response to various actions. This includes:
<ul>
<li>Automated, token-based &#8220;claim script&#8221; feature.  No more claiming script and not knowing what to do next.</li>
<li>More robust and informative feedback on script approval/rejection.</li>
</ul>
</li>
<li>Small usability improvements.</li>
<li>Many bug fixes, polishing.</li>
</ul>
<img src="http://feeds.feedburner.com/~r/PerfectionKills/~4/280165539" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thinkweb2.com/projects/prototype/updated-scripteka/feed/</wfw:commentRss>
		<feedburner:origLink>http://thinkweb2.com/projects/prototype/updated-scripteka/</feedburner:origLink></item>
		<item>
		<title>Namespacing made easy</title>
		<link>http://feeds.feedburner.com/~r/PerfectionKills/~3/226085561/</link>
		<comments>http://thinkweb2.com/projects/prototype/namespacing-made-easy/#comments</comments>
		<pubDate>Wed, 30 Jan 2008 20:14:24 +0000</pubDate>
		<dc:creator>kangax</dc:creator>
		
		<category><![CDATA[inject]]></category>

		<guid isPermaLink="false">http://thinkweb2.com/projects/prototype/namespacing-made-easy/</guid>
		<description><![CDATA[Enumerable#inject is one scary method. It took me a while to understand the beauty of this functional programming paradigm.  Just in case you were ever wondering about its real life usage - here&#8217;s one of them.
We all know that using namespaces is generally a wise thing to do. You don&#8217;t have to create a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.prototypejs.org/api/enumerable/inject" title="Enumerable#inject documentation">Enumerable#inject</a> is one scary method. It took me a while to understand the beauty of this <a href="http://en.wikipedia.org/wiki/Reduce_%28higher-order_function%29" title="Fold (higher-order function)">functional programming paradigm</a>.  Just in case you were ever wondering about its real life usage - here&#8217;s one of them.</p>
<p>We all know that using namespaces is generally a wise thing to do. You don&#8217;t have to create a multi level object structure, but nesting your code under a pseudo-namespace is what makes maintenance, upgrades and migrations somewhat easier and error-prone.</p>
<p>Few popular javascript libraries implement some sort of a utility function that automatically creates nested objects. What it means is that instead of writing it manually, you could let the helper handle it for you:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// creating nested structure manually is painful</span>
<span style="color: #003366; font-weight: bold;">var</span> com = <span style="color: #66cc66;">&#123;</span>
  thinkweb2: <span style="color: #66cc66;">&#123;</span>
    projects: <span style="color: #66cc66;">&#123;</span>
      _prototype: <span style="color: #3366CC;">'a deeply nested property...'</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
com.<span style="color: #006600;">thinkweb2</span>.<span style="color: #006600;">projects</span>._prototype <span style="color: #009900; font-style: italic;">// =&gt; 'a deeply nested property...'</span></pre></div></div>

<p>Well, it appears that inject can handle such task in only <strong>3 lines of code</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">&nbsp;
<span style="color: #3366CC;">'com.thinkweb2.projects.prototype'</span>.<span style="color: #006600;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">inject</span><span style="color: #66cc66;">&#40;</span>window, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>parent, child<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> o = parent<span style="color: #66cc66;">&#91;</span>child<span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">&#125;</span>; <span style="color: #000066; font-weight: bold;">return</span> o;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
Object.<span style="color: #006600;">inspect</span><span style="color: #66cc66;">&#40;</span>com.<span style="color: #006600;">thinkweb2</span>.<span style="color: #006600;">projects</span>.<span style="color: #006600;">prototype</span><span style="color: #66cc66;">&#41;</span> <span style="color: #009900; font-style: italic;">// =&gt; &quot;[object Object]&quot;</span></pre></div></div>

<p>Not bad - a completely generic namespacing snippet.</p>
<p>What&#8217;s happening here is not a black magic. Inject accepts initial object as a first argument (in this case - window object) and iterator function as the second argument. It then calls iterator function on each item from the collection (in our case it&#8217;s simply a string split into an array). Iterator function receives <strong>accumulated result</strong> as a first argument and current value as a second. Iterator performs an action (creates a nested object) and returns accumulated result (the newly created parent object) to be used in following iterations.  </p>
<p>Alternatively, we could wrap this all nicely and define as a String.prototype method:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">&nbsp;
String.<span style="color: #006600;">prototype</span>.<span style="color: #003366; font-weight: bold;">namespace</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>separator<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">split</span><span style="color: #66cc66;">&#40;</span>separator || <span style="color: #3366CC;">'.'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">inject</span><span style="color: #66cc66;">&#40;</span>window, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>parent, child<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> o = parent<span style="color: #66cc66;">&#91;</span>child<span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">&#125;</span>; <span style="color: #000066; font-weight: bold;">return</span> o;
  <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
foo.<span style="color: #006600;">bar</span>.<span style="color: #006600;">baz</span>.<span style="color: #006600;">quux</span> = <span style="color: #3366CC;">'Nothing special...'</span>; <span style="color: #009900; font-style: italic;">// =&gt; ERROR: foo is not defined</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// Default separator is '.'</span>
<span style="color: #3366CC;">'foo.bar.baz.quux'</span>.<span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
foo.<span style="color: #006600;">bar</span>.<span style="color: #006600;">baz</span>.<span style="color: #006600;">quux</span> = <span style="color: #3366CC;">'Nothing special...'</span>; <span style="color: #009900; font-style: italic;">// =&gt; &quot;Nothing special...&quot;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// Or using a custom one</span>
<span style="color: #3366CC;">'MY_AWESOME_APPLICATION::util::DOM::dimensions'</span>.<span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'::'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #3366CC;">'dimensions'</span> <span style="color: #000066; font-weight: bold;">in</span> MY_AWESOME_APPLICATION.<span style="color: #006600;">util</span>.<span style="color: #006600;">DOM</span>; <span style="color: #009900; font-style: italic;">// =&gt; true</span></pre></div></div>

<p>Note how we use &#8220;separator&#8221; argument (or defaulting to &#8216;.&#8217;) making the function somewhat more flexible.</p>
<p>P.S.<br />
Heavy namespacing could lead to unnecessary complexity.<br />
Lack of it - to buggy behavior.<br />
In the end, the only thing that matters is what works best for you.</p>
<p>Enjoy!</p>
<img src="http://feeds.feedburner.com/~r/PerfectionKills/~4/226085561" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thinkweb2.com/projects/prototype/namespacing-made-easy/feed/</wfw:commentRss>
		<feedburner:origLink>http://thinkweb2.com/projects/prototype/namespacing-made-easy/</feedburner:origLink></item>
		<item>
		<title>Prototype 1.6.0.2 Cheat Sheet</title>
		<link>http://feeds.feedburner.com/~r/PerfectionKills/~3/222258445/</link>
		<comments>http://thinkweb2.com/projects/prototype/prototype-1602-cheat-sheet/#comments</comments>
		<pubDate>Tue, 22 Jan 2008 23:59:09 +0000</pubDate>
		<dc:creator>kangax</dc:creator>
		
		<category><![CDATA[cheat sheet]]></category>

		<guid isPermaLink="false">http://thinkweb2.com/projects/prototype/2008/01/22/prototype-1602-cheat-sheet/</guid>
		<description><![CDATA[
A long awaited Prototype cheat sheet  - a full reference to a bleeding edge 1.6.0.2 is finally here. I had no experience creating something like this before, so any bugs or suggestions are very much appreciated.  Couple of notes about notations:

Modules are sorted in a somewhat logical order - those commonly used are [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://attic.scripteka.com/cheatsheet_sneakpeak.png" alt="Prototype 1.6+ cheat sheet" /></p>
<p style="margin-top: 40px">A long awaited <strong>Prototype cheat sheet</strong>  - a full reference to a <strong>bleeding edge 1.6.0.2</strong> is finally <a href="http://thinkweb2.com/projects/prototype/downloads/Prototype Cheat Sheet 1.6.0.2" title="prototype (v1.6.0.2) cheat sheet ">here</a>. I had no experience creating something like this before, so any bugs or suggestions are very much appreciated.  Couple of notes about notations:</p>
<ul>
<li>Modules are sorted in a somewhat logical order - those commonly used are mostly in the left/center area, while deprecated/utility methods are all the way to the right</li>
<li>Method can be recognized by parentheses following it (anything that doesn&#8217;t have ones is a property)</li>
<li>Deprecated items are marked red and have NO parentheses/arguments specified</li>
<li>Prototype extends quite few native objects&#8217; prototypes with a set of convenient methods. In such cases there&#8217;s an explicit note about it next to a module name - i.g. <code>stripScripts()</code> method from &#8220;String (String.prototype)&#8221; can be called as <code>'foo'.stripScripts()</code></li>
<li>When a module is also a class, there&#8217;s a &#8220;(constructor)&#8221; note next to it -  i.g. &#8220;Hash (constructor)&#8221; means that it should be called as <code>new Hash()</code></li>
<li>There are few bonus items (such as those from Prototype.Browser) which are not yet included in documentation</li>
</ul>
<p><a href="http://thinkweb2.com/projects/prototype/downloads/Prototype Cheat Sheet 1.6.0.2" title="prototype (v1.6.0.2) cheat sheet ">Download</a> (<span style="color: #ff5555">18378</span> downloads) and Enjoy!</p>
<p><strong>Update:</strong><br />
I have managed to choose the most retarded format for the cheat sheet - almost squared - which was impossible to print or navigate. Sincere apologies.  There is an updated version at the same address which also fixes few other annoyances:</p>
<ul>
<li>Ajax.Responders is now a separate section</li>
<li>Added missing Event.fire</li>
<li>Added Prototype.BrowserFeatures.XPath</li>
<li>Added simple &#8220;Dimensions/Offsets&#8221; diagram</li>
<li>Minor rearrangements</li>
</ul>
<p><strong>Update 2:</strong><br />
Uploaded a <a href="http://thinkweb2.com/projects/prototype/downloads/Prototype Cheat Sheet 1.6.0.2 b/w">Higher Contrast version</a> (<span style="color: #ff5555">4349</span> downloads)<br />
Seems to look much better when printed.</p>
<img src="http://feeds.feedburner.com/~r/PerfectionKills/~4/222258445" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thinkweb2.com/projects/prototype/prototype-1602-cheat-sheet/feed/</wfw:commentRss>
		<feedburner:origLink>http://thinkweb2.com/projects/prototype/prototype-1602-cheat-sheet/</feedburner:origLink></item>
		<item>
		<title>Proto.IPS - In place select widget</title>
		<link>http://feeds.feedburner.com/~r/PerfectionKills/~3/222258447/</link>
		<comments>http://thinkweb2.com/projects/prototype/protoips-in-place-select-widget/#comments</comments>
		<pubDate>Sat, 22 Dec 2007 20:40:01 +0000</pubDate>
		<dc:creator>kangax</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://thinkweb2.com/projects/prototype/2007/12/22/protoips-in-place-select-widget/</guid>
		<description><![CDATA[
In place editing is a great way to enhance user interface when dynamically changing data on a page. Scriptaculous&#8217; In Place Editor is a nice extension with extensive set of features and options. I recently needed such functionality in one of the projects, but IPE wasn&#8217;t exactly the best tool for the job. The idea [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://thinkweb2.com/ips.png" alt="Proto.IPS" /></p>
<p><a href="http://wiki.script.aculo.us/scriptaculous/show/InPlaceEditing">In place editing</a> is a great way to enhance user interface when dynamically changing data on a page. Scriptaculous&#8217; In Place Editor is a nice extension with extensive set of features and options. I recently needed such functionality in one of the projects, but IPE wasn&#8217;t exactly the best tool for the job. The idea was to let users select one of the predefined options as well as add their own. GMail chat widget and the way it allows to change availability status is a great example of such user interface addition. Another concern was to make control fit in a limited space - google simply gets rid of &#8220;Save&#8221;/&#8221;Cancel&#8221; buttons and saves value of the input field when clicking outside of control.</p>
<p>I tried duplicating this behavior into a simple prototype based script which can be simply plugged into any project.<br />
This work is experimental (there are couple of known issues) and I would appreciate any feedback.</p>
<p>Head over to a <a href="http://yura.thinkweb2.com/playground/in-place-select/">demo page</a> and let me know what you think.</p>
<img src="http://feeds.feedburner.com/~r/PerfectionKills/~4/222258447" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thinkweb2.com/projects/prototype/protoips-in-place-select-widget/feed/</wfw:commentRss>
		<feedburner:origLink>http://thinkweb2.com/projects/prototype/protoips-in-place-select-widget/</feedburner:origLink></item>
		<item>
		<title>Proto.Menu gets facelift</title>
		<link>http://feeds.feedburner.com/~r/PerfectionKills/~3/222258450/</link>
		<comments>http://thinkweb2.com/projects/prototype/protomenu-gets-facelift/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 05:09:35 +0000</pubDate>
		<dc:creator>kangax</dc:creator>
		
		<category><![CDATA[extension]]></category>

		<category><![CDATA[Proto.Menu]]></category>

		<guid isPermaLink="false">http://thinkweb2.com/projects/prototype/2007/12/03/protomenu-gets-facelift/</guid>
		<description><![CDATA[ 
It&#8217;s been over 2 months since the last release of Proto.Menu - highly lightweight and easy-to-use prototype based extension aimed to make context menu creation as simple as possible. Version 0.5 happened to be quite useful (considering all the positive feedback). There were many requests for all kinds of features, but the goal was [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://attic.scripteka.com/proto.menu.png" alt="Proto.Menu" /> </p>
<p>It&#8217;s been over 2 months since the last release of <a href="http://yura.thinkweb2.com/scripting/contextMenu/">Proto.Menu</a> - highly lightweight and easy-to-use prototype based extension aimed to make context menu creation as simple as possible. Version 0.5 happened to be quite useful (considering all the positive feedback). There were many requests for all kinds of features, but the goal was to keep things small and concise. After analyzing the source, I found few obvious drawbacks which were supposed to be taken care of long time ago. Trying not to bloat the code base, the script was rewritten with speed, size and accessibility in mind. The result is a new shiny version - still small and fast but is much more robust and accessible. Without further ado, here is what the new version is all about:</p>
<h2>Prototype 1.6</h2>
<p>Prototype 1.6 is finally out. Upgrading Proto.Menu to the latest revision was definitely a reasonable thing to do. Besides tons of great enhancements and speed improvements, RC1 fixed a <strong>&#8220;conextmenu&#8221;</strong> bug (which was taken into account in a previous version) - that fix itself made the script even more compact.</p>
<h2>Iframe shim</h2>
<p>We all know the notoriously famous <a href="http://blogs.msdn.com/ie/archive/2006/01/17/514076.aspx">IE6 bug</a> - select elements don&#8217;t play nice, popping through any elements positioned on top (even when <strong>z-index</strong> is set to be higher). Unfortunately, IE6 is still the most used browser out there. I have no idea how this fix didn&#8217;t make its way into previous versions, but I believe that any self respectable extension should be taking care of this annoyance.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">shim</span> = <span style="color: #003366; font-weight: bold;">new</span> Element<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'iframe'</span>, <span style="color: #66cc66;">&#123;</span>
   style: <span style="color: #3366CC;">'position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);display:none'</span>,
   src: <span style="color: #3366CC;">'javascript:false;'</span>,
   frameborder: <span style="color: #CC0000;">0</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
...
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>Prototype.<span style="color: #006600;">Browser</span>.<span style="color: #006600;">IE</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">container</span>.<span style="color: #006600;">insert</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>
      after: <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">shim</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span>Object.<span style="color: #006600;">extend</span><span style="color: #66cc66;">&#40;</span>Object.<span style="color: #006600;">extend</span><span style="color: #66cc66;">&#40;</span>elOffsets, elDimension<span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#123;</span>
         zIndex: <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">options</span>.<span style="color: #006600;">zIndex</span> - <span style="color: #CC0000;">1</span>
      <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>zIndex option</h2>
<p>When fixing the z-index bug, I realized that it might be convenient to explicitly specify <strong>z-index</strong> of a menu container. As web applications become more complicated, the number of various elements on a page increases. Modal windows, tooltips and other floating elements should interact nicely with each other. Proto.menu now has a zIndex option (which is internally used to set iframe z-index properly)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">new</span> Proto.<span style="color: #006600;">Menu</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>
   ...
   <span style="color: #006600;">zIndex</span>: <span style="color: #CC0000;">500</span>,
   ...
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h2>&#8220;beforeShow&#8221;, &#8220;beforeHide&#8221; and &#8220;beforeSelect&#8221; callbacks</h2>
<p>Callbacks is something every extensions developer should consider adding to their scripts. They allow users to intercept script execution at certain moments - add, change or stop certain behaviour. Proto.Menu now supports 3 callbacks: <strong>beforeShow</strong> is called every time the right click occurs but before the menu was positioned and rendered. If you wish to intercept the left click on a document which &#8220;cancels&#8221; the menu, you can now use <strong>beforeHide</strong> callback. Note that it&#8217;s not executed on menu item selection, we have another callback for that - <strong>beforeSelect</strong> is executed after the menu item was clicked but before the menu container was hidden and item callback invoked. Note that all callbacks get an event object as a first argument.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">document.<span style="color: #006600;">observe</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>e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> 
   <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;">container</span>.<span style="color: #006600;">visible</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> &amp;amp;&amp;amp; !e.<span style="color: #006600;">isRightClick</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">options</span>.<span style="color: #006600;">beforeHide</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">container</span>.<span style="color: #006600;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>.<span style="color: #006600;">bind</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>;</pre></div></div>

<h2>Better callbacks</h2>
<p>Menu item callbacks sucked a lot in previous version. There was no way to get neither reference to a clicked element, nor anything else. This was probably one of the most requested features. To make everyone happy, callbacks now get <strong>event</strong> object as a first argument. You can now easily access any event&#8217;s properties/methods such as target (clicked element), mouse position, and others.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> menuItems = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#123;</span>
   <span style="color: #000066;">name</span>: <span style="color: #3366CC;">'Select all'</span>,
   callback: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>e.<span style="color: #006600;">target</span>.<span style="color: #006600;">match</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'input[type=checkbox]'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         e.<span style="color: #006600;">target</span>.<span style="color: #006600;">up</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'form'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">select</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'input[type=checkbox]'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
            el.<span style="color: #006600;">checked</span> = <span style="color: #003366; font-weight: bold;">true</span>;
         <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#125;</span>
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>, <span style="color: #66cc66;">&#123;</span>
   ...
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span>
...</pre></div></div>

<h2>&#8220;className&#8221; option</h2>
<p>Another highly requested feature (and one of the big drawbacks of previous version) was a lack of descriptive images in menu items. Images definitely take user interface on a next level. Not only visually pleasing, they let us make decision faster and be more productive. Any menu item, besides name callback and other options, now supports a <strong>className</strong> which is then assigned to an anchor element. Image based menus have never been easier!</p>
<h2>Semantic markup</h2>
<p>The last but not least of the new features is a <strong>proper markup</strong> of a menu element. Instead of just set of links (I can&#8217;t believe I made it this way), the structure now actually represents its true essence - unordered list of anchor elements. Making the web a somewhat meaningful environment is definitely the right way to go - interface elements should represent their purpose and try to be more user-friendly.   As usual, I am always looking forward to any bug reports/suggestions or just any feedback about this extension.  Check out a</p>
<h3><a href="http://yura.thinkweb2.com/scripting/contextMenu">demo page</a></h3>
<p>and have fun!   </p>
<p><strong>P.S.</strong><br />
Just to make things a little tidier, the extension now has an extensive set of <a href="http://yura.thinkweb2.com/scripting/contextMenu/tests/test/testProtoMenu.html">unit tests</a> (<a href="http://proto-menu.googlecode.com/svn/trunk/test/">svn</a>). Unit tests are a wonderful way to maintain proper cross-browser functionality and behavior. Please let me know if you find any of them failing so that the bugs can be take care of quickly.  </p>
<p>Version 0.6 is most likely the last one to be released under this name. Proto.Menu has been ported to a <a href="http://prototype-ui.com/">Prototype-UI</a> framework and will continue to be developed as one of its components.  UI.ContextMenu supports shadow, submenus, verious themes (including Mac OSX and Leopard ones) and is deeply integrated into the framework&#8217;s core. For a sneak preview of what to expect check out <a href="http://yura.thinkweb2.com/playground/Prototype-UI/test/functional/contextmenu/test_context_menu.html">latest</a> <a href="http://yura.thinkweb2.com/playground/Prototype-UI/test/functional/contextmenu/test_leopard_theme.html">examples</a></p>
<img src="http://feeds.feedburner.com/~r/PerfectionKills/~4/222258450" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thinkweb2.com/projects/prototype/protomenu-gets-facelift/feed/</wfw:commentRss>
		<feedburner:origLink>http://thinkweb2.com/projects/prototype/protomenu-gets-facelift/</feedburner:origLink></item>
		<item>
		<title>Cross-browser mouse events simulation</title>
		<link>http://feeds.feedburner.com/~r/PerfectionKills/~3/222258451/</link>
		<comments>http://thinkweb2.com/projects/prototype/cross-browser-mouse-events-simulation/#comments</comments>
		<pubDate>Sat, 17 Nov 2007 08:30:34 +0000</pubDate>
		<dc:creator>kangax</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://thinkweb2.com/projects/prototype/2007/11/17/cross-browser-mouse-events-simulation/</guid>
		<description><![CDATA[It&#8217;s been quite a while since my last post. Non-stop work on scripteka as well as few other projects is fun but takes all the free time.
I promise to write few useful tutorials in a near future, but for now, let&#8217;s talk about simulating mouse events and how this brings UI testing on a completely [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been quite a while since my last post. Non-stop work on <a href="http://scripteka.com">scripteka</a> as well as few other projects is fun but takes all the free time.<br />
I promise to write few useful tutorials in a near future, but for now, let&#8217;s talk about <strong>simulating mouse events</strong> and how this brings <strong>UI testing</strong> on a completely new level.   </p>
<p>In case you haven&#8217;t heard about <a href="http://prototype-ui.com">Prototype UI</a>, now is a good time to check it out. Originally started by Sebastian Gruhier (creator of very popular <a href="http://prototype-window.xilinus.com/">Prototype Window Class</a>) and Ruby/JS guru <a href="http://i.gotfresh.info/">Samuel Lebeau</a>, Prototype UI aims to provide a set of high quality, skinnable UI components which can ease development of complex User Interfaces in web browsers. </p>
<p>I have recently joined the team and am currently working on <a href="http://dev.prototype-ui.com/browser/trunk/src/context_menu/context_menu.js">UI.ContextMenu</a> and <a href="http://dev.prototype-ui.com/browser/trunk/src/util/iframe_shim.js">UI.IframeShim</a> modules. Besides these, the library is featuring full-blown Window and WindowManager classes (completely reworked PWC), Carousel, Dock, Shadow and others.</p>
<p>UI.ContextMenu is nothing more than a port of <a href="http://yura.thinkweb2.com/scripting/contextMenu">Proto.Menu</a> with few changes to take advantage of library&#8217;s core methods. Increased complexity and deep integration with other components is the reason why we do <a href="http://wiki.script.aculo.us/scriptaculous/show/UnitTesting">Unit Testing</a>. Unit testing is what makes development a summer breeze - knowing that next commit into a trunk plays nice with the rest of the code sure feels good.</p>
<p>One of the lesser known features of scriptaculous unit testing framework is <code>Event.simulateMouse</code> method. The name is pretty self-descriptive - it simulates mouse event, allowing us to pass some optional parameters along - <code>pointerX</code>, <code>pointerY</code>, <code>shiftKey</code>, etc. Everything would be just fine if this nifty helper was a little more cross-browser. The latest revision of Scriptaculous&#8217;s <a href="http://dev.rubyonrails.org/browser/spinoffs/scriptaculous/src/unittest.js">unittest.js</a> states that this feature is <strong>experimental and is Firefox-only</strong>. Bah! Shouldn&#8217;t we account for at least the most popular browser out there - IE6 and 7?</p>
<p>Ok, so it doesn&#8217;t work cross-browser, but do we really care? Why is it such a big deal anyway? Why simulate mouse event which triggers certain methods in our code, if we can &#8220;shorten the road&#8221; and call those methods directly? I asked myself same questions, and the answer seems to be - &#8220;Yes, we should be using it&#8221; and here&#8217;s why:</p>
<h2 class="floated">1</h2>
<p> Mouse simulation (MS) based tests are much &#8220;closer&#8221; to the real life use cases. We sort of draw a straight line from <strong>action</strong> to the <strong>result</strong> without delving into actual implementation - &#8220;Clicking toggle button should collapse container. Clicking it again should expand it.&#8221;</p>
<h2 class="floated">2</h2>
<p> The MS based tests can <strong>effectively catch UI related problems</strong> which would otherwise be really painful to find. As an example, let&#8217;s think of a window widget. This widget has minimize and maximize buttons. Clicking those buttons invokes appropriate actions - window changes its size and position. We decide to write assertions based on direct method invocation:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">&nbsp;
<span style="color: #003366; font-weight: bold;">new</span> Test.<span style="color: #006600;">Unit</span>.<span style="color: #006600;">Runner</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>
   testMaximize: <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>
&nbsp;
      <span style="color: #009900; font-style: italic;">// create window object</span>
      <span style="color: #003366; font-weight: bold;">var</span> myWindow = <span style="color: #003366; font-weight: bold;">new</span> UI.<span style="color: #006600;">Window</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
      <span style="color: #009900; font-style: italic;">// call our method</span>
      myWindow.<span style="color: #006600;">maximize</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
      <span style="color: #009900; font-style: italic;">// wait till effect is completed</span>
      wait<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">1000</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>
&nbsp;
         <span style="color: #009900; font-style: italic;">// assert that window has proper dimensions </span>
         asserEqual<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">800</span>, myWindow.<span style="color: #006600;">element</span>.<span style="color: #006600;">getWidth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
         assertEqual<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">600</span>, myWindow.<span style="color: #006600;">element</span>.<span style="color: #006600;">getHeight</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Ok, all seems fine. We run the code, window resizes, tests pass, we are happy and feeling safe. Let&#8217;s see what happens next. Few days go by and one of the users reports a problem - &#8220;clicking on maximize button does NOT maximize the window&#8221;. Feeling pretty clueless, we fire up the test suit and realize that it still passes all the assertions. Moreover, the window appears to be resizing as assertion is executed! It takes us 2 hours of tedious debugging, just to find out that the problem is not in maximize method at all. The problem is that invisible iframe, which should be positioned under the menu (to prevent IE6 &lt;select&gt; overlapping) somehow has z-index higher than window element and is effectively blocking any clicks on a button. No need to mention that MS based tests would make it much easier to catch the nasty bug. </p>
<p>I have to say that this is not a real life example - it has never happened to me when testing any of the window functionality. My point was to show one of the many UI related problems - problems which would be not so easy to find and debug. As complexity of web applications increases, the dynamic elements on a page and their behavior can quickly get out of order. Having more precise tools for the job could no doubt save a lot of time.     </p>
<p>Turning Event.simulateMouse into a somewhat robust solution was quite trivial. The result - cross-browser mouse event simulation that works in FF2+, IE6+, Safari2+ and Opera9+. Instead of calling maximize as in previous example we could now simply do:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">&nbsp;
<span style="color: #003366; font-weight: bold;">new</span> Test.<span style="color: #006600;">Unit</span>.<span style="color: #006600;">Runner</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>
   testMaximize: <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>
     ...
&nbsp;
     <span style="color: #003366; font-weight: bold;">var</span> button = myWindow.<span style="color: #006600;">getButtonElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'maximize'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
     <span style="color: #009900; font-style: italic;">// simulate mouse click on a button</span>
     Event.<span style="color: #006600;">simulateMouse</span><span style="color: #66cc66;">&#40;</span>button, <span style="color: #3366CC;">'click'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
     <span style="color: #009900; font-style: italic;">// or using element's method</span>
     button._click<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
     ...
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Pretty simple, don&#8217;t you think?</p>
<p>Last thing to mention is that all of this is very much work in progress.<br />
I will see what can be done to simulate drag&#038;drop or mouseover/out functionality, so stay tuned.</p>
<p>The latest version of Event.simulateMouse <a href="http://dev.prototype-ui.com/browser/trunk/test/lib/event_simulate_mouse.js">can be found in a trunk</a> (and <a href="http://dev.prototype-ui.com/browser/trunk/test/functional/unittest/test_event_simulate_mouse.html">unit tests</a> for the curious ones). All you need to do is inculde event_simulate_mouse.js right after unittest.js.</p>
<p>Bug reports, or any comments are as always very welcome.</p>
<p>Start experimenting and have fun!</p>
<img src="http://feeds.feedburner.com/~r/PerfectionKills/~4/222258451" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thinkweb2.com/projects/prototype/cross-browser-mouse-events-simulation/feed/</wfw:commentRss>
		<feedburner:origLink>http://thinkweb2.com/projects/prototype/cross-browser-mouse-events-simulation/</feedburner:origLink></item>
		<item>
		<title>It’s coming…</title>
		<link>http://feeds.feedburner.com/~r/PerfectionKills/~3/222258452/</link>
		<comments>http://thinkweb2.com/projects/prototype/its-coming/#comments</comments>
		<pubDate>Mon, 05 Nov 2007 00:49:24 +0000</pubDate>
		<dc:creator>kangax</dc:creator>
		
		<category><![CDATA[scripteka]]></category>

		<category><![CDATA[teaser]]></category>

		<guid isPermaLink="false">http://thinkweb2.com/projects/prototype/2007/11/04/its-coming/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p><img src="http://attic.scripteka.com/scripteka.png" height="604" width="632" /></p>
<img src="http://feeds.feedburner.com/~r/PerfectionKills/~4/222258452" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thinkweb2.com/projects/prototype/its-coming/feed/</wfw:commentRss>
		<feedburner:origLink>http://thinkweb2.com/projects/prototype/its-coming/</feedburner:origLink></item>
		<item>
		<title>Detect idle state with custom events</title>
		<link>http://feeds.feedburner.com/~r/PerfectionKills/~3/222258453/</link>
		<comments>http://thinkweb2.com/projects/prototype/detect-idle-state-with-custom-events/#comments</comments>
		<pubDate>Wed, 17 Oct 2007 17:20:52 +0000</pubDate>
		<dc:creator>kangax</dc:creator>
		
		<category><![CDATA[custom events]]></category>

		<category><![CDATA[Event.observe]]></category>

		<category><![CDATA[Event.fire]]></category>

		<category><![CDATA[Event]]></category>

		<guid isPermaLink="false">http://thinkweb2.com/projects/prototype/2007/10/17/detect-idle-state-with-custom-events/</guid>
		<description><![CDATA[Some time ago, Andrew Sellick posted his idle state snippet based on prototype. The idea is to capture the moment when user has been idle for certain period of time. Idle means that there was no interaction with document or viewport.  You might be wondering why in the world would we need to capture [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago, Andrew Sellick <a href="http://www.andrewsellick.com/67/simple-javascript-idle-state-using-prototype" title="idle state with prototype">posted his idle state snippet</a> based on prototype. The idea is to capture the moment when user has been idle for certain period of time. Idle means that there was no interaction with <strong>document</strong> or <strong>viewport</strong>.  You might be wondering why in the world would we need to capture user idle state. Well, I can definitely think of 2 good reasons:</p>
<ul>
<li>We could pause periodical ajax requests happening on a background to prevent server overload</li>
<li>We could stop some resource intensive operations happening on a client side (i.e. animation or data processing)</li>
</ul>
<p>Ok, so detecting idle state could be useful, but how do we go about implementing it?</p>
<p>Before diving into all the dirty details, I&#8217;d like to mention that Andrew&#8217;s code encaplsulates &#8220;onIdle&#8221; logic into a <strong>one single function</strong> - this is where we would have to stop our requests/calculations. Such approach is absolutely fine but to decouple things a little we could abstract them into <strong>custom events</strong>. This will allow us to subscribe anyone to <strong>independently</strong> observe idle/active state of the user and act accordingly. There are quite few <a href="http://www.dustindiaz.com/custom-events/">very</a>, <a href="http://joshdavis.wordpress.com/2007/04/10/custom-event-listeners/">good</a>, <a href="http://developer.yahoo.com/yui/event/#customevent">explanations</a> of custom events, just in case you&#8217;re not sure what they are, as well as <a href="http://www.someelement.com/2007/03/eventpublisher-custom-events-la-pubsub.html">prototype</a>, <a href="http://livepipe.net/projects/event_behavior/">based</a>, <a href="http://positionabsolute.net/blog/2007/06/event-dispatcher.php">extensions</a>. Prototype natively supports custom events since <a href="http://www.prototypejs.org/2007/8/15/prototype-1-6-0-release-candidate">version 1.6</a> (which is not yet officially released). The implementation still varies (i.e. there were <a href="http://groups.google.com/group/prototype-core/browse_thread/thread/55f4b798e36694fe">some drastic changes</a> in one of the last revisions) but overall works flawlessly. In the following example I am going to use a <strong>new way</strong> of defining events - prefixing them with pseudo namespace (which I think is more descriptive).</p>
<p>Ok, so let&#8217;s see what these weird things are all about.</p>
<h2 class="floated">1</h2>
<p>First, we store events (and corresponding elements) that represent any kind of interaction in a two dimensional array. I think these should pretty much cover any user action.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> events = <span style="color: #66cc66;">&#91;</span>
	<span style="color: #66cc66;">&#91;</span>window, <span style="color: #3366CC;">'scroll'</span><span style="color: #66cc66;">&#93;</span>,
	<span style="color: #66cc66;">&#91;</span>window, <span style="color: #3366CC;">'resize'</span><span style="color: #66cc66;">&#93;</span>,
	<span style="color: #66cc66;">&#91;</span>document, <span style="color: #3366CC;">'mousemove'</span><span style="color: #66cc66;">&#93;</span>,
	<span style="color: #66cc66;">&#91;</span>document, <span style="color: #3366CC;">'keydown'</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#93;</span>;</pre></div></div>

<h2 class="floated">2</h2>
<p>Next, we are interating over this array, invoking <code>Event.observe</code> for each element/event pair. The observe handler is simply an anonymous function. This function will fire our custom <strong>&#8217;state:active&#8217;</strong> event passing <strong>idleTime</strong> parameter. We basically encapsulate few native DOM events into one custom. To make things clear, the event is essentially just an object. It has methods such as <code>.stop()</code> and <code>.findElement()</code> as well as properties such as <code>.target</code> and <code>.pageX</code>. When firing custom event we can pass an optional object along with it - storing any additional data that might be useful later on. This data is then accessible through <strong>event.memo</strong> and in our case quite conveniently stores idle time.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">events.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	Event.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>, e<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</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>
		document.<span style="color: #006600;">fire</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'state:active'</span>, <span style="color: #66cc66;">&#123;</span>idleTime: <span style="color: #003366; font-weight: bold;">new</span> Time<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - document._idleTime<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
		clearTimeout<span style="color: #66cc66;">&#40;</span>document._idleTime<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h2 class="floated">3</h2>
<p>Now that we have taken care of &#8217;state:active&#8217; all that&#8217;s left to do is set a timer to fire the opposite event - <strong>&#8217;state:idle&#8217;</strong>. You can obviously change the time to anything you like. The trick here is that &#8217;state:idle&#8217; event will only get fired if timer doesn&#8217;t get cleared within 5 seconds (by any of the above specified user actions)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">document._timer = setTimeout<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>
	document.<span style="color: #006600;">fire</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'state:idle'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span>, <span style="color: #CC0000;">5000</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<h2 class="floated">4</h2>
<p>What&#8217;s really cool is that we can now <strong>observe</strong> these custom events just like we would observe the regular ones! Note the use of memo.idleTime to retrieve data passed along with the object.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">document.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'state:idle'</span>, onStateIdle<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'state:active'</span>, onStateActive<span style="color: #66cc66;">&#41;</span>;
&nbsp;
onIdle = <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>
	console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Oh no... where is everybody?'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
onIdleEnd = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Looks like user is still alive but have been idle for '</span>, e.<span style="color: #006600;">memo</span>.<span style="color: #006600;">idleTime</span>, <span style="color: #3366CC;">' ms'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>We can now wrap this code into a self containing class to keep things clean and unobtrusive:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> Notifier = <span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
	_events: <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>window, <span style="color: #3366CC;">'scroll'</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #66cc66;">&#91;</span>window, <span style="color: #3366CC;">'resize'</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #66cc66;">&#91;</span>document, <span style="color: #3366CC;">'mousemove'</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #66cc66;">&#91;</span>document, <span style="color: #3366CC;">'keydown'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>,
	_timer: <span style="color: #003366; font-weight: bold;">null</span>,
	_idleTime: <span style="color: #003366; font-weight: bold;">null</span>,
&nbsp;
	initialize: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>time<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">time</span> = time;
&nbsp;
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">initObservers</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">setTimer</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>,
&nbsp;
	initObservers: <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>
		<span style="color: #000066; font-weight: bold;">this</span>._events.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			Event.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>, e<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">onInterrupt</span>.<span style="color: #006600;">bind</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: #66cc66;">&#125;</span>.<span style="color: #006600;">bind</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: #66cc66;">&#125;</span>,
&nbsp;
	onInterrupt: <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>
		document.<span style="color: #006600;">fire</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'state:active'</span>, <span style="color: #66cc66;">&#123;</span> idleTime: <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #000066; font-weight: bold;">this</span>._idleTime <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">setTimer</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>,
&nbsp;
	setTimer: <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>
		clearTimeout<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._timer<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000066; font-weight: bold;">this</span>._idleTime = <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000066; font-weight: bold;">this</span>._timer = setTimeout<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>
			document.<span style="color: #006600;">fire</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'state:idle'</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>, <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">time</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Here&#8217;s a little <a href="http://yura.thinkweb2.com/playground/state-notifier/" title="state notifier demo">demo page</a> to see this in action.</p>
<p>Enjoy your ride.</p>
<img src="http://feeds.feedburner.com/~r/PerfectionKills/~4/222258453" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thinkweb2.com/projects/prototype/detect-idle-state-with-custom-events/feed/</wfw:commentRss>
		<feedburner:origLink>http://thinkweb2.com/projects/prototype/detect-idle-state-with-custom-events/</feedburner:origLink></item>
	</channel>
</rss>
