<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Ali Farhadi]]></title><description><![CDATA[Yet Another Web Developer]]></description><link>http://farhadi.ir/</link><generator>NodeJS RSS Module</generator><lastBuildDate>Fri, 27 Jul 2012 19:41:16 GMT</lastBuildDate><atom:link href="http://farhadi.ir/feed.xml" rel="self" type="application/rss+xml"/><item><title><![CDATA[The Story Behind HTML5 Sortable]]></title><description><![CDATA[<p>
Recently I published a jQuery plugin named <a href="/projects/html5sortable">HTML5 Sortable</a> and despit its small size,
it was one of the most challenging and interesting works that I did recently.
</p>
<p>
But you might be wondering why I needed another sortable plugin in the first place?
</p>
<p>
In a recent project I was working on, I needed to create a sortable menu and
I was going to use jQuery UI. But when I saw it adds 34KB of JavaScript
to the project only to create a sortable list, I decided to write it myself.
And now that almost all recent browsers support HTML5 drag and drop, why not to use it?
Even better, IE supports this feature since version 5.5.
Actually HTML5 DnD API standardized based on IE's implementation.
</p>
<p id="more">
Well, I didn't want to reinvent the wheel if it was already invented. So I googled and found
this <a rel="nofollow" href="http://code.google.com/p/jquery-html5sortable/">jquery-html5sortable</a> plugin and gave it a try.
But there was a problem. It didn't work in IE. "What?! IE was supposed to support DnD since version 5.5." I said to myself.
Then I looked at its code and I thought I could do it better. So I decided to reinvent a better wheel.
</p>
<p>
After spending a few minutes googling around, it turns out, IE supports drag and drop only with <code>img</code> and <code>a</code> elements.
So I started to look for a workaround and thanks to StackOverflow the question
<a href="http://stackoverflow.com/questions/5500615/internet-explorer-9-drag-and-drop-dnd/8986075#answer-8986075">was already answered</a>.
Every HTML element in Internet Explorer have a method named
<code><a href="http://msdn.microsoft.com/en-us/library/ie/ms536415(v=vs.85).aspx">dragDrop</a></code>
which we can use to fire the <code>ondragstart</code> event. But where should we call it?
The solution proposed on StackOverflow was to use <code>mousemove</code> event and to check
the state of the left mouse button to find out if we are in a dragging state. But It wasn't straightforward.
Even it has some performance penalties because of the frequent execution of the mousemove event.
</p>
<p>
I found a better solution, IE has an event named
<code><a href="http://msdn.microsoft.com/en-us/library/ie/ms536969(v=vs.85).aspx">selectstart</a></code>
which is fired when the user starts dragging the mouse on the element.
And calling <code>dragDrop</code> inside this event is exactly what we wanted.
</p>
<P>
So the IE problem was solved. But we are not done yet. I wanted something with the same look and feel
of the jQuery UI sortable plugin. Using HTML5 API, when we drag an element, a copy of the element
moves along with the mouse and the original one remains in its own place. But in terms of user experience,
in a sortable list it's better to replace the original one with an empty placeholder.
</p>
<p>
At first inside <code>dragstart</code> handler I tried to set the visibility of the dragged element to hidden,
but it caused the dragging clone also becomes invisible. Actually every change in the style and
visibility of the dragged element was also applied to the dragging clone.
</p>
<p>
Then I found something interesting. Changes in the style of a dragged item doesn't affect its clone
once the clone is created. So the first thing that came to my mind was to hide dragged item with a delay using <code>setTimeout</code>.
It worked but it was a little laggy.
</p>
<p>
Finally I found a better approach. Hiding dragged item in a <code>dragenter</code> handler attached to draggable items was pretty seamless.
Actually the <code>dragenter</code> handler being attached to the dragged item itself caused an immediate triggering of <code>dragenter</code> after <code>dragstart</code>.
</p>
<p>
Well, the result was pretty similar to jQuery UI sortable.
I cleaned up the code and made a jquery plugin out of it in less than 50 lines of code.
</p>
<p>
See the usage docs and examples in <a href="/projects/html5sortable">here</a> and report any bugs or suggestions on <a href="http://github.com/farhadi/html5sortable">its github page</a>.
</p>
]]></description><link>http://farhadi.ir/posts/the-story-behind-html5-sortable/</link><guid isPermaLink="true">http://farhadi.ir/posts/the-story-behind-html5-sortable/</guid><dc:creator><![CDATA[Ali Farhadi]]></dc:creator><pubDate>Fri, 23 Mar 2012 00:00:00 GMT</pubDate></item><item><title><![CDATA[New Year, New Website!]]></title><description><![CDATA[<p>
After around 4 years of not updating my website, finally I've got some free time in our
<a href="http://en.wikipedia.org/wiki/Nowruz">Nowruz</a> holidays to make myself a gift, a new website!

<p>
I love static site generators, static sites are fast, there is no need to deal with administration panels, no need to databases,
no risk of being hacked, and no bullshit. There are plenty of them out there, but I couldn't pick any.
I'm a minimalist and I wanted something as simple as possible for my own needs.
So I decided to write my own from scratch, using node.js of course.

<p id="more">
The structure is really simple. There are 3 types of pages: 
<ul>
<li>Simple pages like about page.
<li>Blog posts like this page.
<li>And project pages.
</ul>

<p>
I store them in separate folders: pages, posts, and projects. Also every page has its own folder,
so related stuff like images can be stored in its own folder.

<p>
Pages can be written in markdown or html. Also each page has a json configuration which holds data like
page title, publication date, and etc.

<p>
Then I needed a template engine. Templates should be plain html files without any special markup.
So how to put data in them? I didn't say templates doesn't need logic but logics should be separated.
Every template should have a logic script to inject data to the template.
There are <a href="https://github.com/hij1nx/weld">weld</a> and <a href="https://github.com/flatiron/plates">plates</a>,
but for me <a href="https://github.com/tmpvar/jsdom">jsdom</a> was enough.

<p>
Also I needed a syntax highlighter. I used <a href="https://github.com/isagalaev/highlight.js">highlight.js</a>
with <a href="http://ethanschoonover.com/solarized">solarized</a> theme.
My generator automatically adds this script to every page containing code blocks.

<p>
And one more thing, posts should have an RSS feed. No problem, <a href="https://github.com/dylang/node-rss">node's rss module</a> is great and pretty easy to use.

<p>
And finally disqus for the comments.

<p>
That's it. Thanks to Open Source, I wrote it in less than 200 lines.

<p>
Then I started to design a theme for it. A simple, light, clean, and responsive design using HTML5 and CSS3 features.

<p>
Well, I'm done. This is exactly what I wanted and I'm really satisfied. 
]]></description><link>http://farhadi.ir/posts/new-year-new-website/</link><guid isPermaLink="true">http://farhadi.ir/posts/new-year-new-website/</guid><dc:creator><![CDATA[Ali Farhadi]]></dc:creator><pubDate>Wed, 21 Mar 2012 00:00:00 GMT</pubDate></item></channel></rss>