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.

[ 277 ]<br />

Chapter 8<br />

So in the click h<strong>and</strong>ler function, we can get the HTML value of the button using<br />

$(this).html(). This gets stored in buttonName. We can then loop through all of<br />

the buttons in BetterEditor.buttons looking for an object with a name equal to<br />

the value of buttonName. Using our previous example, if a user clicks on a button<br />

with the name B, buttonName would have the value B. As this code loops through<br />

BetterEditor.buttons, it will hit the first object:<br />

{"name":"B","tag":"strong","style":"font-weight: bold"},<br />

It has the name B. So that object will be stored in targetButton <strong>and</strong> the loop will<br />

be terminated by the break statement. After all, there's no point continuing to loop<br />

through the list of buttons if we have already found a match.<br />

Now that we have identified the correct button object, we can find what tag(s) we<br />

should use when inserting that button.<br />

The following part of the code simply checks to make sure a button has been found.<br />

There is the possibility that something might go wrong <strong>and</strong> a bogus button may be<br />

displayed on the button bar:<br />

if (targetButton) {<br />

txtareaEle.value = BetterEditor.insertTag(<br />

sel.start,<br />

sel.end,<br />

targetButton,<br />

txtareaEle.value<br />

);<br />

}<br />

If a button object has been found to answer the click event, the BetterEditor.<br />

insertTag() function is executed. In the old version, some HTML formatting was<br />

passed in the third argument of the function. Now, the target button object is passed.<br />

Let's turn to that function <strong>and</strong> see what it does.<br />

The insertTag() function<br />

This function is responsible for finding the correct piece of text <strong>and</strong> inserting<br />

the HTML. It is largely unchanged from the Simple Editor version in Chapter 4.<br />

However, we will cover some differences that exist here:<br />

BetterEditor.insertTag = function (start, end, tag, value) {<br />

var front = value.substring(0, start);<br />

var middle = value.substring(start, end);<br />

var back = value.substring(end);<br />

var formatted = <strong>Drupal</strong>.theme('addTag', tag, middle);<br />

return front + formatted + back;<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!