<?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>The Braindonor Network &#187; Javascript</title>
	<atom:link href="http://www.braindonor.net/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.braindonor.net</link>
	<description>Send us your brain...let us do the thinking for you!</description>
	<lastBuildDate>Mon, 30 Jan 2012 20:26:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Visual Studio Fun: Chirpy is Awesome!</title>
		<link>http://www.braindonor.net/coding-blog/visual-studio-fun-chirpy-is-awesome/356/</link>
		<comments>http://www.braindonor.net/coding-blog/visual-studio-fun-chirpy-is-awesome/356/#comments</comments>
		<pubDate>Fri, 20 Jan 2012 17:11:46 +0000</pubDate>
		<dc:creator>John Hoff</dc:creator>
				<category><![CDATA[Coding Blog]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[VisualStudio]]></category>

		<guid isPermaLink="false">http://www.braindonor.net/?p=356</guid>
		<description><![CDATA[Managing the CSS and JavaScript files of a website can be an extremely frustrating, but rewarding experience. I've been introducing tools at work to help manage this experience such as SASS, LESS, CoffeeScript, etc, with mixed results. By themselves, each of these tools do a great job, but how to I leverage their power together? [...]]]></description>
			<content:encoded><![CDATA[<p>Managing the CSS and JavaScript files of a website can be an extremely frustrating, but rewarding experience.  I've been introducing tools at work to help manage this experience such as SASS, LESS, CoffeeScript, etc, with mixed results.  By themselves, each of these tools do a great job, but how to I leverage their power together?  Having fifteen JS and CSS includes in a site is still extremely inefficient.  <a href="http://chirpy.codeplex.com/" target="_blank">Chirpy</a> is a tool that helps to bring all of this together in one plugin in Visual Studio...but I see a lot of people completely missing the most powerful feature of Chirpy: <a href="http://www.weirdlover.com/2010/07/18/chirpy-attains-godlike-abilities-in-version-1-0-0-4/#mash">FileGroups</a>.</p><span id="more-356"></span>

<p>This CSS and JS on my blog is admittedly inefficient.  I'm here to write about code, not spend all my time coding so that I can write about code.  However, it is a great example of how NOT to keep your JavaScript and CSS files organized: 10 individual CSS requests and 15 individual JavaScript requests.  If all of the JavaScript files were being called at just before the end of the body tag and if all of the CSS files are part of the same media type they can be combined into a single file.  I try to follow the <a href="http://developer.yahoo.com/yslow/" target="_blank">YSlow</a> conventions where possible.</p>

<img src="http://www.braindonor.net/wp-content/uploads/2012/01/chirpy_bundle.png" alt="" title="chirpy_bundle" width="358" height="538" class="alignright size-full wp-image-357" />

<p>Chirpy allows for this to be done in a Visual Studio project at build time.  If you have already gone through the tutorial and are starting to work through adding your own chirp files into Visual Studio.  You need to start maching files together with Chirpy using *.chirp.config files.  I've included a quick screenshot that I am using on a project to illustrate how I've organized things.  This is an ASP.NET MVC3 project.  I have included folders for css and javscript inside ~/Content/.  The Bundle.chirp.config file is pulling everything from there and generating five virtual files...and that is all of the content required for the entire site!</p>

<p>I have to mention that the ~/Scripts folder has been left alone as that is where the conventions dictate that JavaScript files should go. I break this convention and say that only <i>Library</i> JavaScript files that are provided by NuGet reside. In my projects, NuGet loads script files into ~/Scripts/, I load script files into ~/Content/js/.  I also have to note that my less files are <i>not</i> *.chirp.less files.  As we are using the FileGroup to  bundle everything together, Chirpy will parse those files when it compiles the group! You can also mix-and-match CSS, LESS and SESS files to your hearts content.</p>

<p>So what does my Bundle.chirp.config look like?</p>
<pre class="brush: plain">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;root xmlns=&quot;urn:ChirpyConfig&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;urn:ChirpyConfig http://www.weirdlover.com/chirpy/chirp.xsd&quot;&gt;
    &lt;FileGroup Name=&quot;style.css&quot;&gt;
        &lt;File Path=&quot;css/fontface.less&quot; /&gt;
        &lt;File Path=&quot;css/reset.less&quot; /&gt;
        &lt;File Path=&quot;css/base.less&quot; /&gt;
        &lt;File Path=&quot;css/fonts.less&quot; /&gt;
        &lt;File Path=&quot;css/mixins.less&quot; Minify=&quot;false&quot; /&gt;
        &lt;File Path=&quot;css/layout.less&quot; Minify=&quot;false&quot; /&gt;
        &lt;File Path=&quot;css/homepage.less&quot; Minify=&quot;false&quot; /&gt;
        &lt;File Path=&quot;css/registration.less&quot; Minify=&quot;false&quot; /&gt;
    &lt;/FileGroup&gt;
    &lt;FileGroup Name=&quot;ie.css&quot;&gt;
        &lt;File Path=&quot;css/ie.less&quot; /&gt;
    &lt;/FileGroup&gt;
    &lt;FileGroup Name=&quot;script.js&quot;&gt;
        &lt;File Path=&quot;../Scripts/jquery-1.5.1.min.js&quot; Minify=&quot;false&quot; /&gt;
        &lt;File Path=&quot;js/ui.js&quot; Minify=&quot;false&quot; /&gt;
    &lt;/FileGroup&gt;--&gt;
    &lt;FileGroup Name=&quot;ielt9.js&quot;&gt;
        &lt;File Path=&quot;js/html5.js&quot; Minify=&quot;false&quot; /&gt;
    &lt;/FileGroup&gt;
    &lt;FileGroup Name=&quot;ie.js&quot;&gt;
        &lt;File Path=&quot;js/PIE.js&quot; Minify=&quot;false&quot; /&gt;
        &lt;File Path=&quot;js/selectivizr-min.js&quot; Minify=&quot;false&quot; /&gt;
    &lt;/FileGroup&gt;
&lt;/root&gt;</pre>

<p>Now i have the 5 files that I am ready to include.  Why 5 files?  I need to support IE 7,8,9.  The labels on the bundle give some hints to the logic, but looking at the HTML in the template should clear it all up.</p>

<p>Header HTML:</p>
<pre class="brush: plain">    &lt;link rel=&quot;stylesheet&quot; media=&quot;all&quot; href=&quot;/Content/style.css&quot; /&gt;
    &lt;!--[if lt IE 9]&gt;
        &lt;script src=&quot;/content/ielt9.js&quot;&gt;&lt;/script&gt;
    &lt;![endif]--&gt;
    &lt;!--[if lte IE 9]&gt;
        &lt;link rel=&quot;stylesheet&quot; media=&quot;all&quot; href=&quot;/Content/ie.min.css&quot; /&gt;
    &lt;![endif]--&gt;
&lt;/head&gt;</pre>

<p>Footer HTML:</p>
<pre class="brush: plain">    &lt;script src=&quot;/Content/script.js&quot;&gt;&lt;/script&gt;
    &lt;!--[if lte IE 9]&gt;
        &lt;script type=&quot;/Content/ie.js&quot;&gt;&lt;/script&gt;
    &lt;![endif]--&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>

<p>Now I have 13 files being reduced to 5.  I'm confident that I will be adding more files as I complete the project&#8212;and I am even more confident that 5 files are all that I will be including in my HTML pages.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.braindonor.net/coding-blog/visual-studio-fun-chirpy-is-awesome/356/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hijack the WordPress Media Gallery</title>
		<link>http://www.braindonor.net/coding-blog/hijack-the-wordpress-media-gallery/228/</link>
		<comments>http://www.braindonor.net/coding-blog/hijack-the-wordpress-media-gallery/228/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 21:05:57 +0000</pubDate>
		<dc:creator>John Hoff</dc:creator>
				<category><![CDATA[Coding Blog]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.braindonor.net/?p=228</guid>
		<description><![CDATA[I have a friend that wants to update a banner on his WordPress blog homepage without changing the template files. I tell him I'd be glad to help him out and start working on a plugin that provides a custom administration page to allow for this. As I am a firm believer in the power [...]]]></description>
			<content:encoded><![CDATA[I have a friend that wants to update a banner on his <a href="http://wordpress.org/" target="_blank">WordPress</a> blog homepage without changing the template files. I tell him I'd be glad to help him out and start working on a plugin that provides a <a href="http://codex.wordpress.org/Adding_Administration_Menus" target="_blank">custom administration page</a> to allow for this. As I am a firm believer in the power of a lazy coder to get things done faster and better, my first thought was: "Why don't I just see if I can hijack the 'Select Image From Gallery' page to do this?"<span id="more-228"></span> After all, it just pops up into an Iframe page and tells post editer to insert an image. Hijacking that process for my own ends shouldn't take me but a few minutes...and a lot less time than trying to build my own media selector. Like nearly all the content on my site, if you are reading this, I wasn't able to find my answer elsewhere.<br /><br />

First, we will examine how the media library iframe is launched from the post editor and how we can duplicate that in our plugin. So lets crack open the page source and see what we can find:<br />
<code>&lt;a href="media-upload.php?post_id=-1249588072&amp;type=image&amp;TB_iframe=true" id="add_image" class="thickbox" title='Add an Image' onclick="return false;"&gt;&lt;img src='images/media-button-image.gif' alt='Add an Image' /&gt;&lt;/a&gt;
</code><br />

Looks like a standard hyperlink, with some extra information in the tag. What first jumps out at me is the <b>onclick="return false;"</b>. By having an onclick return <i>false</i> in a hyperlink, you ensure that a browser with javascript will never open the link when it is clicked. Sounds pretty silly until you remember that WordPress is a big user of <a href="http://jquery.com/" target="_blank">JQuery</a>. From there, we know to look at the class of the hyperlink, <b>class="thickbox"</b>.  This is a reference to the Thickbox 3.1 library found at /wp-includes/js/thickbox/thickbox.js.  This is a library to load an iframe inside an overlay with the background greyed out.  Further, it uses the href of the anchor tag to know where to pull the iframe content from.
<br /><br />

A quick check of the source on our custom page shows that Thickbox is being loaded in the footer, so we do not have to call it directly from our custom wp-admin page. I can just call up the media library with some html code:
<code>&lt;a href="media-upload.php?type=image&amp;TB_iframe=true" class="thickbox" title="Select A Banner Image From Gallery" onclick="return false;" class="thickbox"&gt;
&lt;img id="banner_image" src="&lt;?php echo(htmlentities($banner_image)); ?&gt;" width="450" height="25" /&gt;
&lt;/a&gt;
&lt;input type="hidden" id="banner_image_input" name="banner_image" value="&lt;?php echo(htmlentities($banner_image)); ?&gt;" /&gt;
</code><br />

The snippet displays an image with the source of <b>$banner_image</b> that can be clicked on to open the Media Gallery iframe. I have also added a hidden form input to store the resulting information passed back from the media library.  Once done, I now have an image on my admin page that I can click on to call up the media library.<br /><br />

We're half-way there now! All we have to do next is figure out how the media library sends an image to the post editor. This part isn't as easy. Thanks to thickbox, the media library opens up in an inline-iframe. This means that we can't just view the page source and find our answer. Instead, the only clue we have to hunt for is the "Insert into Post" button.  A quick file search turns that up in /wp-admin/includes/media.php. From there, we discover the onclick of the button, <b>onclick="addExtImage.insert();"</b>, which is thankfully also in the media.php file. At the end of the function, we see exactly what we are looking for:
<code>var win = window.dialogArguments || opener || parent || top;
win.send_to_editor(html);
</code><br />

The media library calls the function <b>send_to_editor</b> in the scope of the window that opened the media library--our admin page! We can add our own function into the admin page and collect the html that the media library sends us:
<code>function send_to_editor(html) {
    alert(html);
    tb_remove();
}
</code><br />

Once done, we can open the media library from our admin page and select an image.  When we hit the "Insert into Post" button, we get an alert with the following content:<br />
<code>&lt;img src="http://127.0.0.1/devel/wp-content/uploads/2009/09/banner.jpg" alt="banner" title="banner" class="alignnone size-full wp-image-494" /&gt;
</code><br />

All we have to do now, is strip out everything but the image source and updates the form.  First, the function eliminates all of the text returned except the image source.  It then updates the banner image and the hidden input value that I am using in my form:
<code>function send_to_editor(html) {
    var source = html.match(/src=\".*\" alt/);
    source = source[0].replace(/^src=\"/, "").replace(/" alt$/, "");
    $("#banner_image").attr('src', source);
    $('#banner_image_input").attr('value', source);
    tb_remove();
}
</code><br />

I fire up the admin page a final time, and everything works! I now have a custom admin page that allows my friend to update a banner on his wordpress site using the media gallery.
]]></content:encoded>
			<wfw:commentRss>http://www.braindonor.net/coding-blog/hijack-the-wordpress-media-gallery/228/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

