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.

Building a Module<br />

}<br />

});<br />

var sel = BetterEditor.selection;<br />

if(sel.id == txtareaID && sel.start != sel.end) {<br />

var buttonName = $(this).html();<br />

var targetButton = null;<br />

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

if (BetterEditor.buttons[i].name == buttonName) {<br />

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

break;<br />

}<br />

}<br />

if (targetButton) {<br />

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

sel.start,<br />

sel.end,<br />

targetButton,<br />

txtareaEle.value<br />

);<br />

}<br />

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

The code above has been highlighted even more carefully to show the particular<br />

lines that were changed.<br />

This function h<strong>and</strong>les the response to a button click. For example, when a user<br />

clicks on, the B button, this event h<strong>and</strong>ler needs to figure out what button was<br />

clicked. Then it takes the appropriate action, which means it finds the selected<br />

text <strong>and</strong> surrounds it with the tags.<br />

The first if statement in this function determines whether or not a portion of the<br />

document has been selected. As you may recall, our initial design for the editor only<br />

wrapped HTML around the existing selected text (it did not insert empty tags).<br />

Once inside of the if statement, we need to find out what button was called. To do<br />

this, we will match the button's name with the text inside of the button element.<br />

Here's why this works.<br />

We know that the bold button will be named B. That is, we know that the text<br />

displayed to the user will be B. We also know that this value <strong>com</strong>es directly from<br />

the button object's name property.<br />

[ 276 ]<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!