06.05.2013 Views

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

function accordion_init() {<br />

$path = drupal_get_path('module', 'accordion');<br />

drupal_add_css($path . '/accordion.css');<br />

drupal_add_js($path . '/accordion.js');<br />

drupal_add_js($path . '/ui/ui.core.js');<br />

drupal_add_js($path . '/ui/ui.accordion.js');<br />

}<br />

The accordion_init() function adds four files.<br />

First, the accordion.css file (which we will create in a moment) contains styling<br />

information. It is a CSS file, not a <strong>JavaScript</strong> file. Therefore, we add it with the<br />

drupal_add_css() function.<br />

Next, we have three <strong>JavaScript</strong> files that need to be added:<br />

[ 303 ]<br />

Chapter 9<br />

1. accordion.js: This holds our custom <strong>JavaScript</strong> code. We will take a close<br />

look at the contents of this file shortly.<br />

2. ui/ui.core.js: This is the base library for <strong>jQuery</strong> UI. It contains functions<br />

used by the rest of the <strong>jQuery</strong> UI <strong>com</strong>ponents. Any time you use <strong>jQuery</strong> UI<br />

you will need to include this library.<br />

3. ui/ui.accordion.js: This contains the <strong>jQuery</strong> UI accordion widget code.<br />

That is all there is to our module. Next, we will look at the <strong>JavaScript</strong>.<br />

The accordion <strong>JavaScript</strong><br />

Our module code was short, but our <strong>JavaScript</strong> code is going to be even shorter. In<br />

accordion.js, we need to write the necessary glue code to find the right part of our<br />

document <strong>and</strong> turn it into an accordion. With <strong>jQuery</strong> at our disposal, <strong>and</strong> <strong>jQuery</strong> UI<br />

tightly integrated, this process is as easy as writing a simple <strong>jQuery</strong> chain.<br />

To make things even simpler, we will wrap this in a <strong>Drupal</strong> behavior <strong>and</strong> allow<br />

<strong>Drupal</strong> to control the initialization of our widget:<br />

// $Id$<br />

/**<br />

* <strong>JavaScript</strong> for initializing <strong>and</strong> adding accordion effect.<br />

* @file<br />

*/<br />

<strong>Drupal</strong>.behaviors.accordion = function () {<br />

$('#sidebar-left:not(.ui-accordion)').accordion({<br />

header: 'h2'<br />

});<br />

};<br />

This material is copyright <strong>and</strong> is licensed for the sole use by Richard Ostheimer on 18th June 2009<br />

2205 hilda ave., , missoula, , 59801

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!