<?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>SwellJS - another big fish in the sea &#187; element</title>
	<atom:link href="http://blog.justswell.org/tag/element/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.justswell.org</link>
	<description>well-minded javascript library</description>
	<lastBuildDate>Fri, 20 Aug 2010 20:48:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Cross-browser getStyle with ease</title>
		<link>http://blog.justswell.org/almighty-element-for-all-your-dom-needs/</link>
		<comments>http://blog.justswell.org/almighty-element-for-all-your-dom-needs/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 17:16:57 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Swell]]></category>
		<category><![CDATA[element]]></category>
		<category><![CDATA[style]]></category>

		<guid isPermaLink="false">http://blog.justswell.org/?p=77</guid>
		<description><![CDATA[As Element is one of the most important components, we wanted to be able to manipulate DOM, CSS and everything related to content manipulation. Our main goal is to make the user write a minimum of code while keeping the smallest codebase possible (yes, we are DRY hard fervents). To achieve this challenging goal, we [...]]]></description>
			<content:encoded><![CDATA[<p>As Element is one of the most important components, we wanted to be able to manipulate DOM, CSS and everything related to content manipulation. Our main goal is to make the user write a minimum of code while keeping the smallest codebase possible (yes, we are DRY hard fervents).</p>
<p>To achieve this challenging goal, we made a batch system that allows us to run a specific function without modifying it several times over given arguments.</p>
<p>So, to put it in a more comprehensive manner, by simply writing this snippet of code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">foo<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> _fn <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>arg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">domEl</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">+</span> arg<span style="color: #339933;">+</span> <span style="color: #3366CC;">'bar'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> _delegate.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> _fn<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you execute <span style="color: #008000;">$(&#8216;foo&#8217;, &#8216;bar&#8217;, &#8216;baz&#8217;).foo(&#8216;baz&#8217;);</span> you&#8217;ll get<span style="color: #008000;"> ['foobazbar', 'barbazbar', 'bazbazbar']<span style="color: #000000;">.<br />
The <span style="color: #008000;">_delegate</span> method loops through the internal elements stack and executes on each element the given <span style="color: #008000;">_fn</span>.</span></span><br/><br/></p>
<h2 style="margin-bottom:20px;padding:0;">getStyle at work</h2>
<p>Another thing worth mentioning is the normalization process done backwards by getStyle, as properties may differ between browsers, which one should we choose? currentStyle or getComputedStyle? usually you would use chained if statements to address each possibility.</p>
<p>This is where our approach clearly gives better results by keeping trace of native properties  (e.g. under currentStyle &#8220;float&#8221; would be &#8220;styleFloat&#8221;, and getComputedStyle, &#8220;cssFloat&#8221;) as well as providing an abstraction layer for getting/setting values.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> _styleExceptions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">'CURRENT_STYLE'</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">'float'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'styleFloat'</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">'opacity'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'filter'</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">'border'</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'DEFAULT_VIEW'</span>  <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">'float'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'cssFloat'</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">'border'</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
getStyle <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> _fn <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>style<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> _method <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>._domEl.<span style="color: #660066;">currentStyle</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#123;</span> o <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>._domEl.<span style="color: #660066;">currentStyle</span><span style="color: #339933;">,</span> type <span style="color: #339933;">:</span> <span style="color: #3366CC;">'CURRENT_STYLE'</span> <span style="color: #009900;">&#125;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> o <span style="color: #339933;">:</span> document.<span style="color: #660066;">defaultView</span>.<span style="color: #660066;">getComputedStyle</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._domEl<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> type <span style="color: #339933;">:</span> <span style="color: #3366CC;">'DEFAULT_VIEW'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> _exceptions <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Swell.<span style="color: #660066;">Core</span>.<span style="color: #660066;">Hashtable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">inject</span><span style="color: #009900;">&#40;</span>_styleExceptions<span style="color: #009900;">&#91;</span>_method.<span style="color: #660066;">type</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        _exceptions.<span style="color: #660066;">defineGetter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> val<span style="color: #339933;">,</span> arg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> _oStyle <span style="color: #339933;">=</span> arg.<span style="color: #660066;">o</span><span style="color: #339933;">,</span>
                _isCurrentStyle <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>arg.<span style="color: #660066;">type</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'CURRENT_STYLE'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #003366; font-weight: bold;">true</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'opacity'</span><span style="color: #339933;">:</span>
                    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_isCurrentStyle<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009966; font-style: italic;">/opacity\s?=\s?([0-9]+)/</span>.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span>_oStyle<span style="color: #009900;">&#91;</span>val<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span><span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'border'</span><span style="color: #339933;">:</span>
                    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#91;</span>_oStyle<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'borderTopWidth'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> _oStyle<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'borderRightWidth'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> _oStyle<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'borderBottomWidth'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> _oStyle<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'borderLeftWidth'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span>
                    <span style="color: #000066; font-weight: bold;">return</span> _oStyle<span style="color: #009900;">&#91;</span>val<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> _method<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_exceptions.<span style="color: #660066;">exists</span><span style="color: #009900;">&#40;</span>style<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> _exceptions.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>style<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> _method.<span style="color: #660066;">o</span><span style="color: #009900;">&#91;</span>style<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> _delegate.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> _fn<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For example retrieving the opacity value without worrying about the browser.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    &lt;div id=&quot;foo&quot; style=&quot;filter:alpha(opacity=50);&quot;&gt;&lt;/div&gt;
    <span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getStyle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'opacity'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns 0.5</span>
    <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.justswell.org/almighty-element-for-all-your-dom-needs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

