| 1 |
// $Id: taxonomy_widget.js,v 1.1 2008/09/30 18:04:13 shannonlucas Exp $
|
| 2 |
|
| 3 |
/**
|
| 4 |
* @file Provides for the necessary Javascript for the taxonomy_widget module.
|
| 5 |
*/
|
| 6 |
|
| 7 |
/**
|
| 8 |
* Handle the vocabulary change event for the taxonomy select widget. The
|
| 9 |
* handler queries the taxonomy widget callback with the selected vocabulary
|
| 10 |
* ID and populates the term select element with the results.
|
| 11 |
*
|
| 12 |
* @param object event The select element's change event.
|
| 13 |
*/
|
| 14 |
function taxonomyWidgetVocabularyChange(event) {
|
| 15 |
var $target = $(event.target);
|
| 16 |
var vid = $target.val();
|
| 17 |
var $termSelect = $target.parent().siblings('div.form-item').children('select.taxonomy-widget-term');
|
| 18 |
var term = $termSelect.val();
|
| 19 |
|
| 20 |
if (vid > 0) {
|
| 21 |
// Populate the term select with this vocabulary's terms.
|
| 22 |
$.getJSON(event.data.callback + "/" + vid, function(data) {
|
| 23 |
var count = 0;
|
| 24 |
|
| 25 |
$termSelect.empty();
|
| 26 |
|
| 27 |
for (var tid in data) {
|
| 28 |
found = false;
|
| 29 |
|
| 30 |
// If multiple items are selected, the term value is an array.
|
| 31 |
if (term instanceof Array) {
|
| 32 |
for (i = 0; i < term.length; i++) {
|
| 33 |
if (tid == term[i]) {
|
| 34 |
found = true;
|
| 35 |
break;
|
| 36 |
}
|
| 37 |
}
|
| 38 |
}
|
| 39 |
// Otherwise we can just check for equality.
|
| 40 |
else {
|
| 41 |
found = (tid == term);
|
| 42 |
}
|
| 43 |
|
| 44 |
// Generate the options for the select element.
|
| 45 |
if (found) {
|
| 46 |
$termSelect.append('<option value="' + tid + '" selected>' + data[tid] + "</option>");
|
| 47 |
}
|
| 48 |
else {
|
| 49 |
$termSelect.append('<option value="' + tid + '">' + data[tid] + "</option>");
|
| 50 |
}
|
| 51 |
|
| 52 |
count++;
|
| 53 |
}
|
| 54 |
|
| 55 |
// Resize after populating using the same algorithm as Drupal.
|
| 56 |
if ($termSelect.attr("multiple") == "multiple") {
|
| 57 |
$termSelect.attr("size", Math.min(9, count));
|
| 58 |
}
|
| 59 |
});
|
| 60 |
}
|
| 61 |
else {
|
| 62 |
$termSelect.html('<option value="0">Select a vocabulary.</option>');
|
| 63 |
|
| 64 |
if ($termSelect.attr("multiple") == "multiple") {
|
| 65 |
$termSelect.attr("size", 3);
|
| 66 |
}
|
| 67 |
}
|
| 68 |
}
|