// $Id$
if (!Drupal.decisions) {
Drupal.decisions = {};
}
// Update maxchoices, called when adding and removing choices
Drupal.decisions.maxChoices = function(numChoices) {
var selected = $("#edit-settings-maxchoices").val();
var label = $("#edit-settings-maxchoices").prev();
// Hard-code the HTML (not clone) as .html() doesn't work for select fields in IE and Opera.
var newMaxChoices = '';
// Remove old maxchoices
$("#edit-settings-maxchoices").remove();
label.after(newMaxChoices);
$("#edit-choice-choices").val(numChoices);
};
// Click event for Remove link, called on pageload and when Add choice is clicked
Drupal.decisions.removeChoiceClick = function() {
$("a.remove-choice").unbind().click(function() {
var currentChoice = $(this).parent();
var otherChoices = currentChoice.siblings(':has(input.choices)');
// Allow deletion of current Choice only if at least two other options still remain
if (otherChoices.length > 1) {
// Get next candidate for deletion and set the focus at its Remove link
//var nextChoice = currentChoice.next(':has(input.choices)');
//if (nextChoice.length == 0) {
// var nextChoice = currentChoice.prev();
//}
// nextChoice.find('.remove-choice').focus();
// Remove choice
currentChoice.remove();
// Give each label its correct number
var i = 1;
$("input.choices").prev().each(function() {
$(this).html($(this).html().replace(/\d+/g, i++));
});
Drupal.decisions.maxChoices(i - 1);
}
elseĀ {
alert(Drupal.settings.decisions.noRemove);
}
});
};
Drupal.decisions.nodeFormAutoAttach = function() {
// This code is used on the node edit page and the content-type settings page.
if ($("div.poll-form").length == 0) {
// We're just on the settings page.
return;
}
// Hide "need more choices" checkbox
$("#edit-choice-morechoices-wrapper").hide();
// Insert Remove links
$(''+ Drupal.settings.decisions.remove +'').insertAfter("input.choices");
Drupal.decisions.removeChoiceClick();
// "Backup" of the first choice
var newChoice = $("input.choices:first").parent().clone();
// Keep track of the highest choice id to use.
var highestId = 0;
$("input.choices").each(function() {
var id = parseInt($(this).attr("id").match(/\d+/));
if (id > highestId) {
highestId = id;
}
});
$(''+ Drupal.settings.decisions.addChoice +'').insertAfter("#edit-choice-morechoices-wrapper").click(function() {
var numChoices = $("input.choices").length + 1;
highestId++;
// If all choices are removed, use a "backup" of the first choice, else clone the first.
newChoice = ($("input.choices:first").parent().html()
? $("input.choices:first").parent().clone()
: newChoice);
newChoiceInput = $("input", newChoice);
newChoiceLabel = $("label", newChoice);
// Replace choice name, id and label reference with the new choice number
newChoice.attr("id", newChoice.attr("id").replace(/\d+/, highestId));
newChoiceInput.attr("name", newChoiceInput.attr("name").replace(/\d+/, highestId));
newChoiceInput.attr("id", newChoiceInput.attr("id").replace(/\d+/, highestId));
newChoiceLabel.attr("for", newChoiceLabel.attr("for").replace(/\d+/, highestId));
// Replace the label to use a more accurate count of choices.
newChoiceLabel.html(newChoiceLabel.html().replace(/\d+/g, numChoices));
// Clear the value, insert and fade in.
newChoiceInput.val("").end().insertBefore("#edit-choice-morechoices-wrapper").fadeIn();
// Set focus on new choice input
newChoiceInput.focus();
// Update hidden form values
$("#edit-changed").val($("#edit-changed").val() + 1);
Drupal.decisions.removeChoiceClick();
Drupal.decisions.maxChoices(numChoices);
return false;
});
};
// Global Killswitch
if (Drupal.jsEnabled) {
$(document).ready(function() {
Drupal.decisions.nodeFormAutoAttach();
});
}