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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

});<br />

sel.start = sel.end = -1;<br />

The basic job of this function is to attach the editor to the passed-in textarea.<br />

The highlighted sections represent the areas where changes have been made.<br />

[ 275 ]<br />

Chapter 8<br />

The function starts by setting up a few variables with information about the target<br />

textarea element. After this is a for loop:<br />

var buttons = [];<br />

for (i = 0; i < BetterEditor.buttons.length; ++i) {<br />

button = BetterEditor.buttons[i];<br />

buttons.push(<strong>Drupal</strong>.theme('button', button));<br />

}<br />

The buttons array, which was declared on the first line, will hold all of the themed<br />

buttons. How do we get those? We loop through the values in BetterEditor.<br />

buttons (this is the array we created on the server) <strong>and</strong> pass that data into a theming<br />

function. Later, we will see how the <strong>Drupal</strong>.theme.prototype.button() function<br />

themes buttons.<br />

We now have information about the target text areas <strong>and</strong> a themed list of buttons.<br />

The next step is to turn our buttons into a button bar:<br />

var bar = $(<strong>Drupal</strong>.theme('buttonBar', buttons, id));<br />

This uses another new theme, <strong>Drupal</strong>.theme.prototype.buttonBar(). This<br />

takes a list of buttons <strong>and</strong> returns a single chunk of HTML which will function<br />

as the button bar.<br />

The next thing to do is insert our new button bar into the document immediately<br />

before the textarea. This is also done with a <strong>jQuery</strong> chain:<br />

$(bar).insertBefore('#' + txtareaID)<br />

.children('.editor-button')<br />

.click( /* anonymous function here */ );<br />

This inserts the new button bar, <strong>and</strong> then adds a click h<strong>and</strong>ler to every child of the<br />

button bar. What are the children of the button bar? Those are the nodes one-level<br />

down (the individual buttons we created before. Therefore, each one of these buttons<br />

will be assigned a click event h<strong>and</strong>ler.<br />

The click h<strong>and</strong>ler has changed since the SimpleEditor version. Let's take a look at<br />

this new version:<br />

function () {<br />

var txtareaEle = $('#' + txtareaID).get(0);<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!