jQuery UI slider

The past few weeks I had a lot of fun playing with jQuery UI for a client. They wanted to replace some elements in an application, sliders and such, with a more accessible solution. For that I turned to jQuery UI, as I had previously introduced jQuery as the standard javascript library for them.

As I started to play with the code I couldn't helped but be impressed by the great work that has been done by the UI team on this project. But as I looked at it more closely I found some things that could be improved.

So here are some of my grieves with it and a possible way to solve them. I am going to concentrate on the slider ( docs / demo ), as it was with that widget that I started to notice some things missing.

The way you use it is in a true jquery fashion nice and unobtrusive, once you have included the correct javascript files (for that see the documentation), you just do:

$("#slider").slider();

And shazaam, #slider has turned into a slider, nice... But, what happens to the input that people provide...

This solution is nothing without javascript, so in my book it is a nono. I can not imagine that I could use this widget on my sony erikkson k800 phone. So accessible it is not, at least not in the way the demo shows us. And as most people will just copy and paste from demo's, most sliders will not be accessible.

So what you say, a slider can never be accessible, rubbish I say. Stick with me and I will show you a very simple way to make this accessible. And in a few day I will put up some example sliders to accompany the code as suggested by Danny Lagrouw.

Improvement

First we will look at what a slider does, it provides a user the possibility to enter data. Whoa, a whole new concept... eh no.

Lets start with an simple < input type="text" /> that gives people a good opportunity to enter a value, no? But what about stepping you say, that we can solve with a few < input type="radio" />. All we have to do is write javascript that sends the value of the slider to the input and when the form is submitted, the server can sort it out.

If a user has no javascript a viable alternative is provided. So not only advantages on the user level, but did you notice that we just solved the data to server problem, in a way that requires us to write zero I admit it is boring but if you want your fancy interface to do something, it has to interface with the back-end as well.

So we start with the html, as that is the base for accessible solutions, we set up a free form slider. As we do this, we make sure that we create a fully functional option for everybody. That is, the purpose of the slider is to set a value.

<div class="slider free" id="slider">
<label for="text">
label
</label>
<input type="text" id="text" />
</div>

As you see a nice and clean solution where data can be entered and processed. What we miss is the fancy slider, which we will create with javascript like this:

// add a slider container div
// add a slider handle to slider container
// place a span to recieve the value
// and add a class to the container
// find and set the input to readonly
jQuery('.slider')
.append('<div class="slideContainer"> // br
<div class="ui-slider-handle""> // br
</div> // br
</div> // br
<span class="value"></span>') //br
.addClass('sliding') // br
.find('input').attr("readonly","readonly");
// linebreaks (br) added for readability

There you have it, an accessible solution for a slider, simple as that.

But wait, what if I have 5 steps that I want people to choose from. That I cannot do, so your solution sucks and I still am stuck with a pure javascript solution says the sceptic (I know who you are...). Oke, the second one is a slider with steps for you.

First let us see what that is, it is simply a option you choose from a limited number of options isn't it. The html solution for this is really easy, radio buttons. So of we go with html

<div class="slider steps" id="slider2">
<fieldset class="radios">
<legend>legend</legend>
<input type="radio" name="radio" value="0" id="v0" />
<label for="v0">zero</label>
<input type="radio" name="radio" value="1" id="v1" />
<label for="v1">one</label>
<input type="radio" name="radio" value="2" id="v2" />
<label for="v2">two</label>
<input type="radio" name="radio" value="3" id="v3" />
<label for="v3">three</label>
<input type="radio" name="radio" value="4" id="v4" />
<label for="v4">four</label>
</fieldset>
</div>

Just like that we have a viable and accessible solution in plain old semantic html, now for the javascript.

var sliderSteps = {
radiobuttons : ((jQuery(this) //br
.find('input[type="radio"]') //br
.length)-1) //br
.toFixed(0),
init : function(targ){
sliderSteps.createSlider(targ);
sliderSteps.set(targ)
},
set : function(targ){
var w = (100/((sliderSteps.radiobuttons*1)+1));
jQuery(targ) //br
.find('label, .ui-slider-handle') //br
.width(w+'%');
},
createSlider : function(targ){
jQuery(targ).slider({
// zoveel stappen als er radio button zijn...
steps : sliderSteps.radiobuttons,
change:function(e,ui){
var x = jQuery(this).slider('value');
var a = (x/100*((jQuery(this). //br
find('input[type="radio"]').//br
length)-1)).toFixed(0);
// set the value somewhere...
jQuery(this).find('.value').text(a);
// check the radiobutton.
var t = jQuery(this). //br
find('input[@type="radio"]')[a];
jQuery(t).attr('checked','true');
}}
);
}
}
sliderSteps.init('#slider2.steps');
// linebreaks (br) added for readability

So there you have it, a nice clean and accessible slider solution for the folks at jquery ui to include in their demos. And for everybody else to look at and maybe even use. So have fun with it and let me know if you like it.

If you see any faults in the code please let me know, but as I am on holiday now, don't except me to rush...

Here is all the demo code zip file wn.slider.zip for you to play with.

Technorati Tags:, , ,

Hidden advantage of event delegation

Jun 15, 2008 4 Comments
Tagged: , , and

Nowadays, everybody has (or should) heard of event delegation, the new gray in javascript. It has many advantages, such as cutting down on the event handlers javascript needs to set on a page. But there is one advantage, that not many people talk about. So I will.

First we will start with the most talked about stuff, to refresh your memory, or to give you a short introduction in the matter. In short there are two kinds of calling your events from your html, inline scripting and unobtrusive javascript. The latter has a variation called Event Delegation.

Inline scripting is the one we all know and the one that is not considered Best practice anymore. What you do is call the event from inside your html, like this:

<li onclick= "alert('foo');">inline script</li>

The disadvantages of this are multiple, one is maintenance, file size is another.

The correct way to do this, as all the cool kids know, is with unobtrusive javascript. That way you have smaller file size, easier maintenance and so on. So what you do in html is:

<li>foo</li>

And you set your events with the javascript on load, page ready or what not:

// go through the dom and attach a click on every li
jQuery('li').click(function(){
// do something when clicked.
alert('foo');
});

This is a step up, but it has a couple of disadvantages. As the number of event handlers grow, the time needed to add them increases. Not that this is a problem on any real browsers, but as long as IE6 continues to be used, you have to work with slow browsers. Enter Event Delegation.

With event delegation the amount of events you have to attach shrinks by a lot, as you can put them fairly high up, like on the body. So with identical html, in your javascript you do something like this:

// attach a click handler on the body (one)
jQuery('body').click(jQuery.delegate({
// when a li is the event target do stuff
'li': function() { alert('foo'); }
}));

This generates only one event handler, making it way faster another advantage of event delegation comes into play when you add elements to the dom, you don't need to attach the events again, as the handler is attached to a higher level.

So here is the deal, not only you get less code, you get even lesser code. Everybody wins!

Here are some links about event delegation should you be wanting to know more... (you can always ask me on twitter).

Here are the files I used for testing, first the inline scripting example, than the unobtrusive javascript one and last but not least the one with event delegation, have fun researching...

Technorati Tags:, , ,

on air tour (part 2)

Deploying and Updating AIR Applications (Serge Jespers)

Packaging, deploying and updating your application are probably the most important basics you'll need to know about. Serge will show you how to use Flex Builder 3 and the command line tools to sign and package your application, how to use the install badge, create custom install badges and how to keep your application up to date.

serge demostrates the world smallest video player, it plays hd in 16 by 16 pixels.

He then goes on with the presentation.

  1. step 1, get a certificate

    From thawte.com for 299 $ for one year with firefox. This to get a green (thrustworthy) icon in your installer.

  2. step 2 deploying your app

    using the standard install badge is one way.

    Another way is to use the beta install badge, looks nicer and has extra features. You can get it here

    The third way is to use a custom install badge, something you can build your self, so looks and functionality is up to you.

  3. step 3 Update your application

    He gives us 5 steps to do this, these I will hope to link to later, he will post them on his site. He explains how this is easy with flex in contrast with flash. As it already has a lot of stuff in it, to make development easier.

Adobe AIR API Overview (Daniel Dura)

Receive an overview of the new APIs that Adobe makes available to applications. Both and JavaScript examples will be shown.

Daniel Dura gives an overview of the most used api's, like window a subject you could spend a full day on covering it. He just covers the basics, normal window, lightweight window and utility window. Startmove() function is one of the reasons that I see myself writing air applications, it gives you so much extra functions...

After the html api, he goes to the File I/O api. This is the one that gives air so much extra over web applications. With this you have the possibility to read or write stuff to you computer. Make sure to close your filestream though, otherwise you will lock your computer..

Drag and drop / clipboard is another powerfull api to enhance the users experience. It enables the user to interact with your application in a normal manner, something that cost so much more trouble with just javascript.

For more info look at his site, which has the slides.

Data Intensive Enterprise AIR Applications (Enrique Duvos)

The need to optimize data handling and transfers in RIAs has become increasingly important in Enterprise applications. During this session Enrique will take a look at how the combination of Adobe AIR's offline local caches via SQLite, and its native connectivity to Adobe Livecycle Data Services data management services, gives developers a powerful framework to deliver data intensive RIAs outside the browser.

The first one which tell anything about preformance. Heard nothing about air's memory leak though. Explains the way that you can build flex and air apps with the same code base that talk to a java backend, very nice...

He shows a way to synchronize the data between client and server, in a nice way. You can check the connection and see if it's up or down, furthermore it can see if data is the same on or offline. Not sure if you have to use adobe's lifecycle though...

Maybe you can read about it on his site

Introduction to SQLite in Adobe AIR (Peter Elst)

Learn all about local database support with SQLite in Adobe AIR, how the use the available API's and build a complete data driven application from scratch. This session will help you get started with several tips and tricks on how to work with data in your desktop RIA's.

This is the one I hope to learn a lot from, if only pointers to where to start. It looks really easy, minimum amounts of code to do cool stuff. Paging is build in in a nice (not new) way, almost equal to mySql. Transactions look like a nice way to do lots of stuff in a fast and secure way.

I still don't know how the html is rendered in the air client, which is webkit and how it can or will affect performance... This will not be a problem with flex, which creates flash (imho).

Drag and drop is a part of flex/air that they are proud of showing, it is super easy and being shown now for the third time. Peter is using a trial version of flex builder, apparently all the money went to our lunch. Still this is by far the most interesting presentation so far, very good. The level is right for me, a bit over my head so I learn something but not too much.

He has a site, which has the slides on it.

on air tour (part 1)

this is a quick write up of the first part of the onair tour, I will clean this up later...

keynote Ryan stewart

Ryan Stewart, a Platform Evangelist at Adobe, provides an introduction to Adobe AIR and how it fits into the Adobe technology platform, and the larger RIA landscape.

marketing talk, adobe rules and stuff

focuses on the cross platform strength of air
talks about 'cocomo' and 'pacifica' server side stuff. nice, but still closed source...
air wants to bring the web developers to the desktop, by bringing the good parts of the web there.

shows a air app called uvlayer. Looks nice but still too much eye candy to me.
google analytics app is cool, adds animation in a nice unobtrusive way...
uses flash with html on one screen. pdf is used well, to render a print view.
mapcache is a cool app

why ria on the desktop?
branding, extended functionality. data access. you can use the flash player to access the same information on different platforms (web mobile desktop wii).

how
using existing tools like textmate or aptana.

new
1.1 more languages
post 1.1 three platforms the same...
max 2008 dec 1-4 2008 milan italy

Building your first Adobe AIR application with Adobe Flex (Mike Chambers)

Learn how to setup your development environment and build your first AIR application using Flex 3 and Flex Builder 3.

flexbuilder based on eclipse

configuration in xml looks easy.

flex looks nasty, absolute positioning and stuff

good warning on certification, get a good one if you go commercial

Building your first AIR application with HTML and JavaScript (Kevin Hoyt)

After this session you will know how to setup the Adobe AIR SDK to allow you to develop and package AIR applications from the command line. You will also be able to leverage the Adobe AIR command line tools to enable development of HTML and JavaScript based applications. Finally, the session will demonstrate built in support for AIR development from Adobe Dreamweaver and the Eclipse based Aptana.

shows how to build air apps with textmate and terminal. explains how app development is different than web development. app development has many things in common with java development

application sandbox? javascript is more insecure on the desktop, there is where the sandbox comes in. script injection and eval should be in the non-application sandbox and the application sandbox has the more advanced air js stuff.
html rules

Leveraging HTML and JavaScript within Adobe AIR (Kevin Hoyt)

Gain a better understanding of the HTML and JavaScript environments within Adobe AIR, and explore how these technologies can be leveraged in both Flash / Flex and HTML / JavaScript-based applications. JavaScript and ActionScript script bridging will be covered, as well as how to use AIR, Flash Player and ActionScript Library APIs directly from JavaScript.

even flex developers have the java bug, putting presentation in the structure, boe..
maybe it is for demo purposes, but the stupid thing is that tons of people will copy/paste this. so that is why it is important to build your demo's as clean as possible.

Kevin's site

A List Apart: new articles

With the introduction of a range of enhancements to form controls, APIs, multimedia, structure and semantics, HTML 5 promises to give authors more flexibility and greater interoperability.A preview of HTML 5

Flow, as a mental state, is characterized by a distorted sense of time, a lack of self-consciousness, and complete engagement in the task at hand. For designers, it’s exactly the feeling we hope to promote in the people who use our sites.A List Apart: Articles: Designing For Flow