Showing posts with label Web Control. Show all posts
Showing posts with label Web Control. Show all posts

Tuesday, 27 May 2008

JavaScript Library and File Loader

OK so tonight I was going to blog about adding paging to a repeater using Linq, however Google today announced that they are now hosting JS Libraries and I couldnt resist whipping together a webcontrol for use in my projects. So I have postponed that post until tomorrow or later on in the week.

JSLoader 2.0

A few months ago I released a fairly basic webcontrol that you could use to import JavaScript files into your .Net pages, it had fairly configurable options and ensured that the file was only loaded once. This worked great, but at the time I thought wouldn't it be great to load JS Libraries from a shared location. At the time the libraries were all over the place and I couldn't decide on the best way to load the files so shelved the project. Today Google announced they are now hosting AJAX libraries, I immediately thought back to upgrading the JSLoader and tonight after a few bits and pieces I fired up Visual Studio 2008 and hacked together JSLoader 2.0

Using JSLoader 2.0

The basic principles remain the same as JSLoader. You reference the control and then create a new webcontrol when you want to load some JavaScript. The difference now is you can use the old way of specifying a directory, file, version type etc, see my JSLoader post on using the old technique, but you can now load up Libraries from Google. Google currently hosts dojo, prototype, jQuery, script.aculo.us and MooTools and as such these are the only libraries that JSLoader 2.0 supports. In time I'll add YUI but for now these will do. JSloader now includes an extra attribute on the control, JSLibrary, here you can specify the AJAX library to load.
<mjjames:jsLoader ID="jsLoader2" runat="server" JSLibrary="dojo"  />
This would then add the following code to your page:
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load("dojo" , "1");
</script>
You can also tell the loader to get Google to render the uncompressed version of the library, this is done by setting the type attribute to uncompressed
<mjjames:jsLoader ID="jsLoader1" runat="server" JSLibrary="mootools" Type="uncompressed" />
Thus:
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load("mootools" , "1" , {uncompressed:true});
</script>
Note: only jQuery, MooTools and Dojo have uncompressed versions, if you specify uncompressed for the other libraries the compressed versions are still served up. Finally you specify a version number, by default JSLoader 2.0 tries to load the latest 1.x release of each library however if you want the 1.2.3 release of jQuery simply set the Version Attribute to 1.2.3
<mjjames:jsLoader ID="jsLoader3" runat="server" JSLibrary="jquery" Version="1.2.3" />
Giving you:
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load("jquery" , "1.2.3");
</script>
It really is simple, it ensures only one version of each library is loaded and its quick and simple to use. Get the latest JSLoader 2.0, add a reference to it in your code, add the control to your site's web.config or register it on the page, and you are away.
<add tagPrefix="mjjames" namespace="mjjames.WebControls" assembly="jsLoader"/>
Hope you like it, any questions or issues let me know.

Side Note

Script.aculo.us depends on Prototype, however JSLoader 2.0 doesn't currently enforce this dependancy, would you like to see JSLoader 2.0 enforce this dependancy and load Prototype before Script.aculo.us or should it load it anyway and let the developer ensure Prototype is loaded? Would be good to see arguements for and against, then I can make a final decision some point this week.

Monday, 4 February 2008

More Web Controls - jQuery

Ok so I have an obsession with web controls and since my last one, Yahoo! Media Player, I have been toying with the idea of doing one for John Resig's jQuery. Tonight I got around to doing this and am now releasing version 1 of it. I made the web control as often when I write Web User Controls that contain / use jQuery to enhance the controls I'm not sure if jQuery exists in my web.master or web page itself. This often leads to me having to go back and change pages or include it in the web control only to find one page already uses it. And yes I'm rubbish at remember what JS is used where :S. So this web control not only checks to see if the jQuery control has been rendered before, if not render it but also allows you to simply specify the jQuery version, path to the file and which type to use, normal, minified or packed. Download jQueryWebControl.zip

How To Use It

So add a register assembly into the top of your aspx file
<%@ Register Assembly="jQueryWebControl" Namespace="jQueryWebControl" TagPrefix="jQuery" %>
Then which ever page, master page or even web control will need jQuery add the jQuery Web Control.
<jQuery:jQuery runat="server" ID="jQuery"/>
As the control checks to see if a jQuery control has rendered before you don't have to worry about only including the control once. This should ensure you don't have to waste time checking but also means your controls can be used independantly in different sites.

Using the three optional parameters, Version, Type and Path.

Version - this is just a string parameter that allows you to specify the version of jQuery to use. E.G "1.2.2" - if this is left out then the control defaults to 1.2.0 Type - again a string parameter, this time with three options, empty string "" = default / development version, "min" = minified version, "pack" - the packed version for people who cant serve up gzipped files. - if left the control defaults to the normal development version Path - a string parameter indicating the path to the javascript files. E.G "/javascript/jquery". - if left the control defaults to the /javascript/ It is worth noting that the control expects you to host the jQuery files, so in order to render the script tag for version 1.2.2 pack'd jQuery file you will need jquery.1.2.2.pack.js in the location you specify with the path parameter.

Example of use using parameters

<jQuery:jQuery runat="server" ID="jQuery" Version="1.2.2" Type="min" Path="/javascript/jQuery/"/>
I personally have made use of the version parameter the most as it has means different sites that have been developed against different jQuery releases can be updated / forced to use one version of the library. It also works best if all your jQuery files are in one centralised location, using a virtual directory or a shared location. So hope you like the control, let me know any thoughts, comments or improvements and enjoy. Download jQueryWebControl.zip

Tuesday, 15 January 2008

Yahoo! Media Player Web Control

Last week Yahoo! released there new Yahoo! Media Player and I had a little play with it, and I must admit found it to be quite cool. Basic but still very functional. Well I wanted to use this in a small project I'm working on and initially just added the JS to my page. However I thought wouldn't this be useful if I could just add an ASP.Net Web Control into any page I want this on, would save me x lines of code. So I created myself a quick Web Control, YMPWebControl. Its quite simple to use and by default all you have to do is put it into a page, and it renders the appropriate JavaScript. So here it is. YMP-1.1.zip

How To Use It

So add a register assembly into the top of your aspx file
<%@ Register Assembly="YMP" Namespace="YMP" TagPrefix="YMP" %>
Then where ever you want the YMP to render, I advise it be the last control in your page as it "adds to your page" and you want the page to load as quick as possible.
<YMP:YMPControl runat="server" ID="ympcontrol"/>
I have also added two optional parameters, DevelopmentVersion and EmbedInHead. These two boolean parameters allow you to turn on the Development version of the player, which supposedly is buggy has all the latest stuff, and allows you to embed the control into the header of your page from where ever it was added. Now this second parameter I would advise against using as you can just manually stick the control code into your header. The option was added in the case where you don't have the ability to change your master page or get access to the head section of code, i.e your using templates or something. This solves that. So an example of wanting the development version of the YMP but also loading it into the head
<YMP:YMPControl runat="server" ID="ympcontrol" EmbedInHead="true" DevelopmentVersion="true"/>
Download the YMP-1.1.zip And that's it, try it out if you like it and use it please let me know, below.

Update

The control has now been updated to version 1.1, this new version only allows one instance of the control to be rendered on a page. All previous links have been updated to this new version. Download the YMP-1.1.zip