<?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>DaveOnCode &#187; javascript</title>
	<atom:link href="http://www.daveoncode.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>Objective C, iOS and more programming stuff</description>
	<lastBuildDate>Mon, 19 Dec 2011 12:11:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Debugging PhoneGap applications using Xcode console</title>
		<link>http://www.daveoncode.com/2010/01/12/debugging-phonegap-applications-using-xcode-console/</link>
		<comments>http://www.daveoncode.com/2010/01/12/debugging-phonegap-applications-using-xcode-console/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 10:13:59 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[phonegap]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=580</guid>
		<description><![CDATA[When I started to play whit PhoneGap, my greatest issue was: &#8220;how can I debug my code?&#8221;, I use often tools such FireBug and JavaScript debugger included in Internet Explorer 8 (which is the first good thing IE has to offers!) but write and test my code on iPhone simulator is completely different. Fortunately PhoneGap [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2010/01/12/debugging-phonegap-applications-using-xcode-console/' addthis:title='Debugging PhoneGap applications using Xcode console ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>When I started to play whit <strong>PhoneGap</strong>, my greatest issue was: &#8220;how can I debug my code?&#8221;, I use often tools such <strong>FireBug</strong> and JavaScript debugger included in Internet Explorer 8 (which is the first good thing IE has to offers!) but write and test my code on iPhone simulator is completely different. Fortunately PhoneGap offers a way to access to <strong>Xcode</strong> console and print messages by choosing among three different levels: <strong>log</strong>, <strong>warn</strong> and <strong>error</strong>. In order to print a message, we have to use the <strong>debug</strong> object, which has scope <strong>window</strong> (it is a global object), in this way:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">debug.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;my log message&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// or</span><br />
debug.<span style="color: #660066;">warn</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;my warning message&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// or</span><br />
debug.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;my error message&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>To open Xcode console you have to choose &#8220;Run -> Console&#8221; from the toolbar (or CMD+SHIFT+R) and after a &#8220;Build and Run&#8221; (CMD+Enter), you will see your message appear in the console.<br />
Testing applications while developing using PhoneGap and Xcode, is an intense activity, because errors are not automatically notified (like in FireBug or similar), so it&#8217;s really important to make use of <strong>try/catch/finally</strong> blocks and logging calls:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; mycommand.<span style="color: #660066;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; debug.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Error using mycommand: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// do something smart here :^)</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Finally, in order to avoid problems related to Xcode cache, I suggest to always clean the cache by running &#8220;Build -> Clean&#8221; from the toolbar (or CMD+SHIFT+K) before doing a new build and eventually remove the application folder under &#8220;/Users/{your-name}/Library/Application Support/iPhone Simulator/User/Applications/{app-number}&#8221;.</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2010/01/12/debugging-phonegap-applications-using-xcode-console/' addthis:title='Debugging PhoneGap applications using Xcode console ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2010/01/12/debugging-phonegap-applications-using-xcode-console/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to solve CSS conflicts using jQTouch</title>
		<link>http://www.daveoncode.com/2010/01/02/jqtouch-how-to-solve-css-conflicts-using-jqtouch/</link>
		<comments>http://www.daveoncode.com/2010/01/02/jqtouch-how-to-solve-css-conflicts-using-jqtouch/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 16:55:40 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jqtouch]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=569</guid>
		<description><![CDATA[In these days I&#8217;m developing my first iPhone application, using frameworks like PhoneGap and jQTouch. This aims to be a powerful app, not a mere widget, so I&#8217;m writing a lot of code and I&#8217;m using several libraries and components in order to create a really native-like application. Unfortunately, due to a bad CSS approach, [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2010/01/02/jqtouch-how-to-solve-css-conflicts-using-jqtouch/' addthis:title='How to solve CSS conflicts using jQTouch ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>In these days I&#8217;m developing my first iPhone application, using frameworks like <a href="http://phonegap.com/">PhoneGap</a> and <a href="http://www.jqtouch.com/">jQTouch</a>. This aims to be a powerful app, not a mere widget, so I&#8217;m writing a lot of code and I&#8217;m using several libraries and components in order to create a really native-like application.<br />
Unfortunately, due to a bad CSS approach, I faced an issue with the excellent <strong><a href="http://cubiq.org/spinning-wheel-on-webkit-for-iphone-ipod-touch/11">SpinningWheel</a></strong> component which won&#8217;t never be displayed unless you open and modify jQTouch CSS. The fact is that, jQTouch takes the body and use it as main container to hold all application&#8217;s pages and components and by default it hides all nodes inside the body except the current displayed content (active page/section) and this is a problem, because SpinningWheel create the ui object into the body and it won&#8217;t be displayed.<br />
Fortunately the solution is simple, we just need a wrapper, an html element which will holds all jQTouch stuff in place of body, so in my html I wrapped all inside this:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jqtouch-wrapper&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- content --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span></div></td></tr></tbody></table></div>
<p>Then I changed all jQTouch CSS, to reflect this approach (you can see the wrapper as a sandbox):</p>
<div class="codecolorer-container css mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">div<span style="color: #cc00cc;">#jqtouch-wrapper</span> <span style="color: #00AA00;">&gt;</span> <span style="color: #00AA00;">*</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; -webkit-backface-<span style="color: #000000; font-weight: bold;">visibility</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; -webkit-box-sizing<span style="color: #00AA00;">:</span> border-box<span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; -webkit-transform<span style="color: #00AA00;">:</span> translate3d<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span> rotate<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span> scale<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">min-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">420px</span> !important<span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<span style="color: #808080; font-style: italic;">/* ...and so on*/</span></div></td></tr></tbody></table></div>
<p>&#8230;and finally I changed jQuery selectors inside JavaScript code:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;$body <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#jqtouch-wrapper'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// ...and so on</span></div></td></tr></tbody></table></div>
<p>It&#8217;s also possible to solve the conflict by changing SpinningWheel code in order to put the dom node inside active jQTouch pages, but the problem of potential incompatibilities will remains. A CSS declaration, except reset statements and other rare cases, should NEVER refers directly to a whole set of nodes (ie: <strong>ul { color: red; })</strong>, but to a node type contained by a wrapper/sandbox (ie: <strong>#foo ul {color: red; }</strong>), thus conflicts will be avoided.</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2010/01/02/jqtouch-how-to-solve-css-conflicts-using-jqtouch/' addthis:title='How to solve CSS conflicts using jQTouch ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2010/01/02/jqtouch-how-to-solve-css-conflicts-using-jqtouch/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Google Closure&#8217;s idiocies: Ajax can&#8217;t be synchronous :(</title>
		<link>http://www.daveoncode.com/2009/12/31/google-closures-idiocies-ajax-cant-be-synchronous/</link>
		<comments>http://www.daveoncode.com/2009/12/31/google-closures-idiocies-ajax-cant-be-synchronous/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 11:02:08 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[google closure]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=564</guid>
		<description><![CDATA[While I love Closure tools and the possibility to write a better organized and OOP based JavaScript, I&#8217;m facing some weakness and inexplicable choices in the library. The last discover is that is impossible to make synchronous ajax call using the provided classes, since I didn&#8217;t find nothing about synch/asynch options by reading API reference, [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/12/31/google-closures-idiocies-ajax-cant-be-synchronous/' addthis:title='Google Closure&#8217;s idiocies: Ajax can&#8217;t be synchronous :( ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>While I love Closure tools and the possibility to write a better organized and OOP based JavaScript, I&#8217;m facing some weakness and inexplicable choices in the library. The last discover is that is impossible to make synchronous ajax call using the provided classes, since I didn&#8217;t find nothing about synch/asynch options by reading API reference, I took a look to the source (xhrio.js), and I noticed this:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">xhr_</span>.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span>method<span style="color: #339933;">,</span> url<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #006600; font-style: italic;">// Always async!</span></div></td></tr></tbody></table></div>
<p>that <strong>true</strong> should be a parametric value which developers can set to true or false. Why forcing it to true???<br />
I&#8217;m really disappointed :(</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/12/31/google-closures-idiocies-ajax-cant-be-synchronous/' addthis:title='Google Closure&#8217;s idiocies: Ajax can&#8217;t be synchronous :( ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/12/31/google-closures-idiocies-ajax-cant-be-synchronous/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>&#8220;Not authorized&#8221; error due to Safari &#8220;private browsing&#8221; mode! :P</title>
		<link>http://www.daveoncode.com/2009/12/27/not-authorized-error-due-to-safari-private-browsing-mode/</link>
		<comments>http://www.daveoncode.com/2009/12/27/not-authorized-error-due-to-safari-private-browsing-mode/#comments</comments>
		<pubDate>Sun, 27 Dec 2009 14:26:56 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[browsers]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=562</guid>
		<description><![CDATA[This is just a quick post to share my misadventure with Safari and the &#8220;private browsing&#8221; mode. I&#8217;m working on a small JavaScript library which has the goal to abstract SQLite database api and allow users to create table, insert, update and delete records easily&#8230; my code seems to work very well, but this morning [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/12/27/not-authorized-error-due-to-safari-private-browsing-mode/' addthis:title='&#8220;Not authorized&#8221; error due to Safari &#8220;private browsing&#8221; mode! :P ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>This is just a quick post to share my misadventure with Safari and the &#8220;private<br />
browsing&#8221; mode. I&#8217;m working on a small JavaScript library which has the goal to abstract  SQLite database api and allow users to create table, insert, update and delete<br />
records easily&#8230; my code seems to work very well, but this morning during some<br />
tests, I got the &#8220;not authorized&#8221; exception (error code n°1)  on every<br />
transaction. After hours of debugging I realized that my code was ok, but I forgot<br />
to disable the &#8220;private browsing&#8221; mode in Safari!<br />
So, bear in mind, if you want to play with database api and other client-related store capabilities (such cookies), remember to disable that option in Safari ;)</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/12/27/not-authorized-error-due-to-safari-private-browsing-mode/' addthis:title='&#8220;Not authorized&#8221; error due to Safari &#8220;private browsing&#8221; mode! :P ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/12/27/not-authorized-error-due-to-safari-private-browsing-mode/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Extending Eclipse using JavaScript and Monkey Script engine</title>
		<link>http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/</link>
		<comments>http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 16:20:50 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[extensions]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=555</guid>
		<description><![CDATA[I was wondering how to wrap a string with quotes in Eclipse by using a shortcut, then I realized that there is not such command, so I started thinking for a solution and initially I created an Aptana&#8217;s snippet, but I was not satisfied, because I want to have an handy shortcut to invoke my [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/' addthis:title='Extending Eclipse using JavaScript and Monkey Script engine ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I was wondering how to wrap a string with quotes in Eclipse by using a shortcut, then I realized that there is not such command, so I started thinking for a solution and initially I created an Aptana&#8217;s snippet, but I was not satisfied, because I want to have an handy shortcut to invoke my snippet. By googling, I discovered Eclipse Monkey Script engine, that is an extremely powerful tool for every JavaScript developer who wants to extend Eclipse features by writing few lines of JavaScript code. In order to use the js engine, you must have the package org.eclipse.eclipsemonkey (which is installed by default by Aptana) installed. Unfortunately I can&#8217;t find a complete and exhaustive reference for this project, which seems forgotten by authors, so I learnt what I know reading different posts.<br />
Basically, Monkey Script allow developers to access editor&#8217;s instance, get selected test, get document content, edit it and update it. Moreover is possible to print text to consol, read and create files on the filesystem&#8230; and finally the scripts created will be accessible from Eclipse menu (under &#8220;Scripts&#8221;) and invokable through user defined shortcut&#8230; really nice!<br />
So, backing to my experiment, I created this monkey script:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/* <br />
* Key: M1+M2+C<br />
* Menu: Custom Scripts &gt; Wrap with double quotes<br />
* DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript<br />
* Kudos: Davide Zanotti<br />
*/</span><br />
<span style="color: #003366; font-weight: bold;">function</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// no editor... exit</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> editors.<span style="color: #660066;">activeEditor</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;No active editor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// get a reference to the editor in use</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> editor <span style="color: #339933;">=</span> editors.<span style="color: #660066;">activeEditor</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// beginning of text selection</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> startOffset <span style="color: #339933;">=</span> editor.<span style="color: #660066;">selectionRange</span>.<span style="color: #660066;">startingOffset</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// end of text selection</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> endOffset <span style="color: #339933;">=</span> editor.<span style="color: #660066;">selectionRange</span>.<span style="color: #660066;">endingOffset</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// selected text</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> selection <span style="color: #339933;">=</span> editor.<span style="color: #660066;">source</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>startOffset<span style="color: #339933;">,</span> endOffset<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// surround selection with quotes</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;editor.<span style="color: #660066;">applyEdit</span><span style="color: #009900;">&#40;</span>startOffset<span style="color: #339933;">,</span> endOffset <span style="color: #339933;">-</span> startOffset<span style="color: #339933;">,</span> <span style="color: #3366CC;">'&quot;'</span> <span style="color: #339933;">+</span> selection <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Error &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">code</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>It&#8217;s important to notice some points, first you MUST write an &#8220;header&#8221; using the comment syntax &#8220;/* */&#8221; into which you MUST declare at least 2 parameter: the &#8220;<strong>Menu</strong>&#8221; path (you can nest several menu items by writing several &#8220;&gt;&#8221;) and the &#8220;<strong>DOM</strong>&#8221; package (which allows you to access specific objects like &#8220;<strong>editors</strong>&#8220;). You can write several js functions, but you must provide a <strong>main()</strong> function, which is called automatically by Eclipse once you launch your script, otherwise you can call the main function as you like but you must then provide the &#8220;OnLoad&#8221; param in the header, specifying which function will be called.<br />
To define a shortcut you can use the &#8220;<strong>Key</strong>&#8221; parameter and define your own keys combination by choosing among the four modifiers: M1, M2, M3, M4, that are a platform-independent way of representing keys (these stand for ALT, COMMAND, CTRL, and SHIFT). The strange (at least to me) parameter &#8220;<strong>Kudos</strong>&#8221; is used to declare the author of the monkey script.<br />
To test my little script or create your own, you have simply to create a new project into Eclipse by calling it as you like (ie: &#8220;custom-extensions&#8221;), then create a &#8220;scripts&#8221; folder into which you will save your .js files&#8230; that&#8217;s all, try yourself and enjoy :)</p>
<p>This is how it looks:</p>
<div id="attachment_558" class="wp-caption alignnone" style="width: 519px"><img src="http://www.daveoncode.com/wp-content/uploads/2009/12/Screen-shot-2009-12-16-at-17.08.56.jpg" alt="eclipse monkey script menu" title="eclipse-monkey-script-menu" width="509" height="254" class="size-full wp-image-558" /><p class="wp-caption-text">eclipse monkey script menu</p></div>
<p>ps: I&#8217;m going to explore the Monkey Script API in order to write more complex and useful extensions :P</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/' addthis:title='Extending Eclipse using JavaScript and Monkey Script engine ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

