Vesica Blog - Taking museum and art collections to the cloud

March 23, 2013

Simplifying JQuery Dialogs

Filed under: Tech Talk — Tags: , , , , , , — Asif N @ 12:08 am

Working on a web application that uses multiple JQuery dialogs?

You’re probably sick of rendering dialog boxes with JQuery’s .dialog() function and all of its parameters.

So here’s a tiny little function that you can include in your AJAX application at the top level via a tag, and then simply call each time you need to render a dialog. So you can effectively accomplish what you need to do in one line instead of 10.

The function below requires multiple parameters:

  1. target_class – This is the name of the class you want the dialog rendered in. It doesn’t have to exist in the DOM, the function will create it.
  2. title – The title of your dialog
  3. width – The width of the dialog
  4. height – the height of the dialog
  5. load_file – An external file or view that you want to load inside your dialog
  6. buttons – A JavaScript array containing all the buttons your dialog needs. This array should contain a button id and button text, and can be formatted as:
    buttons[0]['id'] = ‘save’ ;
    buttons[0]['id'] = ‘Save this Content’ ;
    buttons[1]['id'] = ‘default_cancel’ ;
    buttons[2]['id'] = ‘button3′ ;
    buttons[2]['text'] = ‘A 3rd Button’ ; 

As shown above, you can add as many buttons as you want using an array.  The function also created a default cancel button that will close and destroy the dialog if you pass the id ‘default_cancel’ to it.

Note that once you have the dialog rendered, you can use more JavaScript driven by the ID of each button to decide what happens when that ID gets clicked. This should ideally go in a JS file tied to the view or page you load into the load_file parameter of the function.

There are several ways to to tweak and improve the function depending on what you are rendering your dialogs for,  so please feel free to make any changes and / or share your thoughts. If you have any questions, please don’t hesitate to ask.

 

January 21, 2013

Tech Talk: URL Hashing and JQuery UI Tabs

Whilst Vesica uses JQuery, what’s discussed in this article is not used in Vesica, but in a similar application we helped build at the  NHS (National Health Service) which borrows from the Vesica interface.

The application in question is a single page application, so virtually everything is done via AJAX. The URL receives different parameters via a URL hash, which updates different sections of the page DOM or accesses different parts of the interface, including JQuery UI tabs. These single page, multiple hash URLs can be called via a bookmark URL, or will trigger on a url change in the browser. What’s important to note here is that in an application like this, when you submit data, all you really need to do is change the URL – your hashing managing JavaScript should do the rest for you (in terms of routing your data to the server side, not actual processing). I’ll also show you some JavaScript to deal with JQuery UI tabs after your page loads as working with hashing will disable the default URL behaviour that allows you to call tabs via the URL.

Requirements

If it’s not self-explanatory, you need the following to accomplish this:

  • JQuery - http://jquery.com/download/
  • JQuery UI - http://jqueryui.com/
  • JQuery Hashchange plugin from Ben Alman - http://github.com/cowboy/jquery-hashchange/raw/v1.3/jquery.ba-hashchange.js and http://benalman.com/projects/jquery-hashchange-plugin/
Please load the JavaScript libraries in your page (preferably in the header) in the order listed.

How it works

Here is what we need to cover:

  1. Someone either loads a URL in their browser which is part of your single page application (this could be via a bookmarked URL or by simply typing or pasting in the address bar), in which case you simply need to pick up the URL, process, and update the page.
  2. Someone performs an action that will change the URL of your application – which should either update a database or update the page, or whatever it is supposed to do in your application.
The JQuery Haschange plugin will only help you accomplish number 2. Number 1 is simple JavaScript / JQuery.
So how does your application know that the URL in the browser has changed after the #? With the following code.

So if your existing URL looked like http://example.com/#Page1 and you changed it to http://example.com/#Page2 with an or a

This is the easy bit – now for how you would pick up different parts of your URL, splice them up and send them to your scripts as needed to process, and then perhaps load a JQuery UI tab on your page.

Assumptions

Let’s clarify what we have in our example.

We are trying to load a different type of product based on a type parameter, followed by an ID, followed by a bunch of parameters that style the image and options that get displayed for the product.

So, if you were using PHP and building your URL query string, it might look a bit like:

http://example.com/product.php?type=bottle&id=3426&colour=green

If you wanted to load a particular JQuery tab on this page that had the id ‘dimensions’, your URL would become:

http://example.com/product.php?type=bottle&id=3426&colour=green/#dimensions

When the page loaded, JQuery UI would know you want to load the dimensions tab and it would simply make it active.

But using the $(window).hashchange() function above will break this default JQuery UI functionality.

So, our goal, given our single page application, is to be able to reload a certain part of our page based on a URL that looks like the following:

http://example.com/index.php#product/type=bottle&id=3426&colour=green/dimensions

The way to read the above your URL is: Give me the product page with bottle No. 3426 in the colour green and display the dimensions tab on the product page. You could change any of the values above to process different data – so you could have product, category or generic types of pages, hundreds of different products and hundreds of different colours.

Sounds simple enough. This would be accomplished with the following code, and please note, as mentioned above, that to trigger this change for bookmarked URLs or those pasted in the browser you can enter this code outside the $(window).hashchange() function in a tag directly in index.php (where index.php is the file that runs your single page application) or create a another JavaScript file and include it in your index.php file via the

The above code now gives you access to to all the different parts of the URL hash, and will do so every time it changes. All you need to do now is use the $.ajax() function in JQuery to process this data, get the results back, and set the active tab as shown below:

That’s it, that code should pick up any changes to the URL and process them according to the code above.

As this is JQuery, remember to wrap all of the above code in $(function(){ {); as shown below. I’ve also added the code above outside the hashchange function so you can see how to setup the default page handler as well as trigger it off when the page URL changes.

I’ve stated before that you can (and probably should) write a function to do the above so you don’t have to reproduce your code twice – but this should give you an idea of how to handle hash changes and process them.

January 18, 2013

Introducing Vesica Tech Talk

After a quiet holiday blogging season, we have a bit of a twist and some regular updates planned for the Vesica blog, including a new section called Tech Talk.

Tech Talk will be discussing some web based technical implementations that we at Vesica have used in the application and in exteral projects (our team gets to work on several external projects that are modeled after or integrate with Vesica in a variety of industries) – small snippets of code or technical advice that could save you (or your technical team) hours or days if you’re building something similar.

In addition, we’ll be rolling out some videos (not in the Tech Talk but the Using Vesica and Upcoming Features Sections) to demo some of the existing functionality, new functionality (that was released over Christmas and has just recently made it to the features page – https://vesica.ws/features/timeline/), and upcoming functionality like the Research and Bibliography tab.

The first Tech Talk article (coming this Monday) will discuss some JQuery implementations adapted from Vesica for use by one of the applications for the National Health Service (NHS) in the UK, the interface for which was inspired by Vesica (and we helped design and build it).

Home    •    Blog    •    Contact Us    •    Developers    •    Education    •    Partners    •    About    •    Help & Support    •    News    •    Privacy Policy    •    Terms of Use