<?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>TECKpert &#187; jQuery</title>
	<atom:link href="http://www.teckpert.com/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.teckpert.com</link>
	<description>Your web and software development experts</description>
	<lastBuildDate>Thu, 02 Sep 2010 22:33:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>jQuery Development: Your own Plugins Part II</title>
		<link>http://www.teckpert.com/blog/jquery-development-your-own-plugins-part-2/</link>
		<comments>http://www.teckpert.com/blog/jquery-development-your-own-plugins-part-2/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 23:26:16 +0000</pubDate>
		<dc:creator>Andres</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Coding Tips]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery plugin]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[web 2.0]]></category>

		<guid isPermaLink="false">http://www.teckpert.com/?p=356</guid>
		<description><![CDATA[Extending on my previous post jQuery development: Your own plugins, we now have to take a look into a more complex plugin as well as making greater use of both JavaScript's built in Regular Expression objects and jQuery provided functionality.]]></description>
			<content:encoded><![CDATA[<p>Extending on my previous post <a href="http://www.teckpert.com/blog/jquery-development-your-own-plugins/" target="_blank">jQuery development: Your own plugins</a><a href="http://www.teckpert.com/blog/jquery-development-your-own-plugins/"></a>, we now have to take a look into a more complex plugin as well as making greater use of both JavaScript&#8217;s built in <a href="http://en.wikipedia.org/wiki/Regular_expression" target="_blank">Regular Expression</a> objects and jQuery provided functionality.</p>
<p>For the purposes of this post, I&#8217;ll be extending on the previous plugin to readily handle more video services: Dailymotion, Megavideo &amp; Vimeo as well as the already supported YouTube. Also, we&#8217;ll be adding an option to leave the link but make the video pop up in a modal box using ColorBox.</p>
<p><strong>Okay enough intro&#8230;let&#8217;s code!</strong></p>
<p>First, we&#8217;re going to start off with the basic structure of every jQuery plugin:</p>
<pre class="brush:js">(function($){
    $.fn.videa = function(){
    }
})(jQuery)</pre>
<p><strong>Note:</strong> We use this format for the purpose of ease and using the familiar $ variable, however we can just do this.</p>
<pre class="brush:js">jQuery.fn.videa = function(){
   //How ever, within here we can only use the jQuery object,
   // $ is undefined.
}</pre>
<p>First we&#8217;re going to need the embed code for each service. This is readily<br />
available on their websites as well as the format for their actual flv&#8217;s.<br />
I&#8217;ve already done this below.</p>
<p><strong>Note:</strong> I&#8217;ve made use of JSON. If you don&#8217;t know about this, read up <a href="http://en.wikipedia.org/wiki/JSON" target="_blank">here</a>.</p>
<pre class="brush:js">(function($){

    $.fn.videa= function(){

        var supportedSites = {
            'youtube' : '&lt;object width="560" height="340"&gt;&lt;param name="movie" value="[URL]&amp;hl=en&amp;fs=1&amp;"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="[URL]&amp;hl=en&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"&gt;&lt;/embed&gt;&lt;/object&gt;',
            'dailymotion': '&lt;object width="480" height="275"&gt;&lt;param name="movie" value="[URL]"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowScriptAccess" value="always"&gt;&lt;/param&gt;&lt;embed src="[URL]" type="application/x-shockwave-flash" width="480" height="275" allowfullscreen="true" allowscriptaccess="always"&gt;&lt;/object&gt;',
            'megavideo': '&lt;object width="640" height="467"&gt;&lt;param name="movie" value="[URL]"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;embed src="[URL]" type="application/x-shockwave-flash" allowfullscreen="true" width="640" height="467"&gt;&lt;/embed&gt;&lt;/object&gt;',
            'vimeo'    : '&lt;object width="400" height="225"&gt;&lt;param name="allowfullscreen" value="true" /&gt;&lt;param name="allowscriptaccess" value="always" /&gt;&lt;param name="movie" value="[URL]" /&gt;&lt;embed src="[URL]" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"&gt;&lt;/embed&gt;&lt;/object&gt;'
        }

        var handle = {
            'youtube'    : ['watch?v=', 'v/'],
            'dailymotion': [/.*dailymotion.*\/video\/(.*?)_.*/, 'http://dailymotion.com/swf/$1'],
            'megavideo'  : ['?v=', 'v/'],
            'vimeo'      : [/.*vimeo.*\/(.*[0-9])/, 'http://vimeo.com/moogaloop.swf?clip_id=$1&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00adef&amp;fullscreen=1']
        }</pre>
<p>Next, we&#8217;ll need to make use of each utility; this is something that will be used in nearly <strong>ALL </strong>your plugins. The reason why is because you want the plugin to affect more than one HTML element.</p>
<pre class="brush:js">(function($){
    $.fn.videa= function(){
        // We're of course assuming the aforementioned json is here
        // ommited for the purposes of readability
      this.each(function(){
      })
})(jQuery)</pre>
<p>Now, for our purposes we don&#8217;t actually need to instantiate a jQuery object per &lt;a&gt; element &#8211; yet. All we need is the href and class attributes which are easily available via JavaScript&#8217;s native <a href="https://developer.mozilla.org/En/DOM/Element.getAttribute" target="_blank">getAttribute</a> function.</p>
<pre class="brush:js">(function($){
    $.fn.videa= function(){
        // We're of course assuming the aforementioned json is here
        // ommited for the purposes of readability
      this.each(function(){
           var href    = this.getAttribute('href');
           var aClass  = this.getAttribute('class');
           var key     = href.match(/(http:\/\/www\.|http:\/\/|www\.|)(.*?)\.(\.com|\.*)/)[2];

          // we now have the base such as 'youtube' and 'vimeo' in key
          // check if we support it
          if( supportedSites[key] )
          {
          }
      })
})(jQuery)</pre>
<p><strong>Now I&#8217;ll explain the regex </strong></p>
<p><strong><em>The first bit:</em></strong></p>
<p>This matches the http protocol with or without the www or just the www</p>
<ul>
<li> &#8216;(http:\/\/www\.|http:\/\/|www\.|)&#8217;</li>
</ul>
<p><strong><em>The second bit:</em></strong><br />
Simply grabs everything AFTER the http://www.</p>
<ul>
<li>&#8216;(.*?)&#8217;</li>
</ul>
<p><strong><em>The third bit:</em></strong></p>
<p>This matches against any .com or .* extension</p>
<ul>
<li>&#8216;\.(\.com|\.*)&#8217;</li>
</ul>
<p>The rest is rather straight forward and can be explained in code comments.</p>
<pre class="brush:js">// Check if the site is supported
if( supportedSites[key] )
            {
               // Construct properly url to the flv
               if( href.indexOf('http://www.') == -1 )
                        href = 'http://www.' + href.replace('www.', '');

                // Check if we want a modal box
                if( aClass.indexOf('modal') &gt; -1 )
                {
                    // Bind to the click and fire off colorbox
                    $(this).click(function(){
                        $.fn.colorbox({
                            html: supportedSites[key].replace(/\[URL\]/ig, href.replace(handle[key][0], handle[key][1])) ,
                            open:true
                        });
                                return false;
                     })
                }else{
                    // We don't want a modal box so just do the html
                    $(this).before(supportedSites[key].replace(/\[URL\]/ig, href.replace(handle[key][0], handle[key][1]))).remove();
                }</pre>
<p><strong>Note:</strong> jQuery is a jackhammer, sometimes all we have is nails.</p>
<p>That&#8217;s the plugin broken down into the important sections. The end result is as expected. Everything which has a videa class is converted. The modal class is optional (it creates a link that opens a modal box with your video).</p>
<p>As for plugin development, once you understand  <a href="https://developer.mozilla.org/en/DOM" target="_blank">DOM </a>and the <a href="http://docs.jquery.com/Main_Page" target="_blank">entire jQuery API</a>, you can pretty much make anything you can come up with. JavaScript support is strong and growing.</p>
<p><span style="text-decoration: underline;"><strong>The Entire plugin below:</strong></span></p>
<pre class="brush:js">(function($){

    $.fn.embeddedables = function(){

        var supportedSites = {
            'youtube' : '&lt;object width="560" height="340"&gt;&lt;param name="movie" value="[URL]&amp;hl=en&amp;fs=1&amp;"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="[URL]&amp;hl=en&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"&gt;&lt;/embed&gt;&lt;/object&gt;',
            'dailymotion': '&lt;object width="480" height="275"&gt;&lt;param name="movie" value="[URL]"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowScriptAccess" value="always"&gt;&lt;/param&gt;&lt;embed src="[URL]" type="application/x-shockwave-flash" width="480" height="275" allowfullscreen="true" allowscriptaccess="always"&gt;&lt;/object&gt;',
            'megavideo': '&lt;object width="640" height="467"&gt;&lt;param name="movie" value="[URL]"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;embed src="[URL]" type="application/x-shockwave-flash" allowfullscreen="true" width="640" height="467"&gt;&lt;/embed&gt;&lt;/object&gt;',
            'vimeo'    : '&lt;object width="400" height="225"&gt;&lt;param name="allowfullscreen" value="true" /&gt;&lt;param name="allowscriptaccess" value="always" /&gt;&lt;param name="movie" value="[URL]" /&gt;&lt;embed src="[URL]" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"&gt;&lt;/embed&gt;&lt;/object&gt;'
        }

        var handle = {
            'youtube'    : ['watch?v=', 'v/'],
            'dailymotion': [/.*dailymotion.*\/video\/(.*?)_.*/, 'http://dailymotion.com/swf/$1'],
            'megavideo'  : ['?v=', 'v/'],
            'vimeo'      : [/.*vimeo.*\/(.*[0-9])/, 'http://vimeo.com/moogaloop.swf?clip_id=$1&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00adef&amp;fullscreen=1']
        }

        // Looad in this element for all a tags
        this.each(function(){

            var href   = this.getAttribute('href');
            var aClass = this.getAttribute('class');
            var key = href.match(/(http:\/\/www\.|http:\/\/|www\.|)(.*?)\.(\.com|\.*)/)[2];

            if( supportedSites[key] )
            {
               if( href.indexOf('http://www.') == -1 )
                        href = 'http://www.' + href.replace('www.', '');

                if( aClass.indexOf('modal') &gt; -1 )
                {
                    $(this).click(function(){
                        $.fn.colorbox({
                            html: supportedSites[key].replace(/\[URL\]/ig, href.replace(handle[key][0], handle[key][1])) ,
                            open:true
                        });
                                return false;
                     })
                }else{
                    $(this).before(supportedSites[key].replace(/\[URL\]/ig, href.replace(handle[key][0], handle[key][1]))).remove();
                }
            }

        });

    }

})(jQuery)</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.teckpert.com/blog/jquery-development-your-own-plugins-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery development: Your own plugins</title>
		<link>http://www.teckpert.com/blog/jquery-development-your-own-plugins/</link>
		<comments>http://www.teckpert.com/blog/jquery-development-your-own-plugins/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 17:32:19 +0000</pubDate>
		<dc:creator>Andres</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Coding Tips]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery developer]]></category>
		<category><![CDATA[jquery plugin]]></category>
		<category><![CDATA[jquery plugins howto]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[web 2.0]]></category>

		<guid isPermaLink="false">http://www.teckpert.com/?p=283</guid>
		<description><![CDATA[Often there is a need for functionality that goes above jQuery's core set of methods, there are a host of plugins which can be used to attain this functionality. Such as ColorBox when a modal box is needed or jFlow for a basic gallery. However, sometimes these plugins cannot preform exactly the way we need them to without attacking the plugin's source and in the end causing a fair bit of frustration, confusion, and a plugin that not only doesn't do what you want but doesn't do anything. ]]></description>
			<content:encoded><![CDATA[<p>Often there is a need for functionality that goes above jQuery&#8217;s core set of methods, there are a host of plugins which can be used to attain this functionality. Such as ColorBox when a modal box is needed or jFlow for a basic gallery. However, sometimes these plugins cannot perform exactly the way we need them to without attacking the plugin&#8217;s source and in the end causing a fair bit of frustration, confusion, and a plugin that not only doesn&#8217;t do what you want but doesn&#8217;t do <em>anything. </em></p>
<p>Solution?  <strong>Develop your own plugin </strong></p>
<p>The benefits of this course are:</p>
<ol></ol>
<ul>
<li>Extreme familiarization with the code base ( You wrote it! )</li>
<li>Makes for identifying the area where bugs are coming from a fair bit easier</li>
<li>You don&#8217;t have to worry about licencing, you wrote it, it&#8217;s yours.</li>
</ul>
<ol></ol>
<p>The above being said, I suppose it&#8217;s about time to get on to exactly <em>how </em> one goes about developing their own plugin and it&#8217;s assuming familiarization with their core and some basic JavaScript it can be done rather easily. For the purpose of this blog post we&#8217;ll be doing a plugin that transforms every YouTube link into a nice embedded video.</p>
<p>First, we start off with a basic structure which is necessary to eliminate the possibility of Namespace conflicts ( libraries aside from jQuery make use of the dollar sign $ )</p>
<pre class="brush:js">(function($){

})(jQuery)</pre>
<p>Now we must define our function, with jQuery however we&#8217;ll be making use of closures or anonymous functions and than assign it to a variable. This is how:</p>
<pre class="brush:js">(function($){
    $.fn.youtubualize = function(){

    }
})(jQuery)</pre>
<p>Now we get to the fun bits! jQuery feeds us a copy of itself ( available though the this supervariable )  ready to be manipulated, seeing as we&#8217;ll most likely have more than 1 link we&#8217;ll make use of jQuery&#8217;s <a href="http://docs.jquery.com/Utilities/jQuery.each" target="_blank">each</a> utility to cycle through all the links.</p>
<pre class="brush:js">(function($){
     $.fn.youtubualize = function(){

        this.each(function(){

        });
    }
})(jQuery)</pre>
<p>Now we must be away that within the callback we&#8217;re providing to the each utility <strong>this </strong>is only a string representing the YouTube link so we need to use jQuery&#8217;s CSS selectors to single out the element and modify it accordingly, this will be the most verbose part</p>
<pre class="brush:js">(function($){
     $.fn.youtubualize = function(){

        this.each(function(){
            var url    = String(this);
            var flvURL = url.replace('watch?v=','v/');
            $('a[href="'+ url +'"]')
                .before('&lt;object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"\
 width="560" height="340"\
 codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt;\
 &lt;param name="allowFullScreen" value="true" /&gt;\
 &lt;param name="allowscriptaccess" value="always" /&gt;\
 &lt;param name="src" value="'+ flvURL +'&amp;amp;hl=en&amp;amp;fs=1&amp;amp;" /&gt;\
 &lt;param name="allowfullscreen" value="true" /&gt;\
 &lt;embed type="application/x-shockwave-flash" width="560" height="340" src="'+ flvURL +'&amp;amp;hl=en&amp;amp;fs=1&amp;amp;" allowscriptaccess="always" allowfullscreen="true"&gt;&lt;/embed&gt;\
 &lt;/object&gt;')
                .remove();
        });
    }
})(jQuery)</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.teckpert.com/blog/jquery-development-your-own-plugins/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
