/[drupal]/contributions/modules/advpoll/advpoll-form.js
ViewVC logotype

Contents of /contributions/modules/advpoll/advpoll-form.js

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Wed Nov 29 02:27:06 2006 UTC (3 years ago) by chriskennedy
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
File MIME type: text/javascript
#99252 Cast votes using Ajax; refactor some results handling and split javascript into two main files - patch by fajerstarter and me
1 // $Id: advpoll.js,v 1.6 2006/11/28 22:05:33 fajerstarter Exp $
2
3 if (!Drupal.advpoll) {
4 Drupal.advpoll = {};
5 }
6
7 // Update maxchoices, called when adding and removing choices
8 Drupal.advpoll.maxChoices = function(newChoiceN) {
9 var selected = $("#edit-settings-maxchoices").val();
10 var label = $("#edit-settings-maxchoices").prev();
11 // Hard-code the HTML (not clone) as .html() doesn't work for select fields in IE and Opera.
12 var newMaxChoices = '<select id="edit-settings-maxchoices" class="form-select" name="settings[maxchoices]">';
13 // Build the options
14 for (var i = 0; i <= newChoiceN; i++) {
15 var name = (i ? i : Drupal.settings.advPoll.noLimit);
16 newMaxChoices += '<option ';
17 // Set the option user had selected
18 if (i == selected) {
19 newMaxChoices += 'selected="selected" ';
20 }
21 newMaxChoices += 'value="' + i + '">' + name + '</option>';
22 }
23 newMaxChoices += '</select>';
24 // Remove old maxchoices
25 $("#edit-settings-maxchoices").remove();
26
27 $(newMaxChoices).insertAfter(label);
28 }
29
30 // Click event for Remove link, called on pageload and when Add choice is clicked
31 Drupal.advpoll.removeChoiceClick = function() {
32 $('a.remove-choice').unclick().click(function() {
33 var nextRemoveLink = $(this).parent().next().find(".remove-choice");
34 // Set focus at next or previous Remove link
35 if(nextRemoveLink.html()) {
36 nextRemoveLink[0].focus();
37 }
38 else {
39 $(this).parent().prev().find(".remove-choice")[0].focus();
40 }
41 // Remove choice
42 $(this).parent().remove();
43 var i = 1;
44 $("input.choices").prev().each(function() {
45 // Give each label it's correct number
46 $(this).html($(this).html().replace(/\d+(?=<)/g, i++));
47 });
48
49 Drupal.advpoll.maxChoices(i-1);
50
51 return false;
52 });
53 }
54
55 Drupal.advpoll.updateStartDate = function() {
56 if ($("#edit-settings-usestart").attr("checked")) {
57 $(".edit-settings-startdate").show();
58 $("#edit-settings-startdate-year").removeAttr("disabled");
59 $("#edit-settings-startdate-month").removeAttr("disabled");
60 $("#edit-settings-startdate-day").removeAttr("disabled");
61 }
62 else {
63 $(".edit-settings-startdate").hide();
64 $("#edit-settings-startdate-year").attr("disabled", "disabled");
65 $("#edit-settings-startdate-month").attr("disabled", "disabled");
66 $("#edit-settings-startdate-day").attr("disabled", "disabled");
67 }
68 }
69
70 Drupal.advpoll.nodeFormAutoAttach = function() {
71 // Hide "need more choices" checkbox
72 $("#morechoices").hide();
73
74 // Disable starting date if necessary
75 Drupal.advpoll.updateStartDate();
76 $("#edit-settings-usestart").click(Drupal.advpoll.updateStartDate);
77
78 // Insert Remove links
79 $('<a class="remove-choice" href="#">' + Drupal.settings.advPoll.remove + '</a>').insertAfter("input.choices");
80 Drupal.advpoll.removeChoiceClick();
81
82 // "Backup" of the first choice
83 var newChoice = $("input.choices:first").parent().clone();
84
85 $('<a class="add-choice" href="#">' + Drupal.settings.advPoll.addChoice + '</a>').insertAfter("#morechoices").click(function() {
86 var newChoiceN = $("input.choices").length + 1;
87 // If all choices are removed, use a "backup" of the first choice, else clone the first.
88 newChoice = ($("input.choices:first").parent().html() ? $("input.choices:first").parent().clone() : newChoice);
89 // Replace choice numbers in label, name and id with the new choice number
90 newChoice.html(newChoice.html().replace(/\d+(?=<)|\d+(?=-)|\d+(?=\])/g, newChoiceN));
91 // Clear the value, insert and fade in.
92 newChoice.find("input").val("").end().insertBefore("#morechoices").fadeIn();
93 // Update hidden form values
94 $("#edit-choices").val(newChoiceN);
95 $("#edit-changed").val($("#edit-changed").val() + 1);
96
97 Drupal.advpoll.removeChoiceClick();
98
99 Drupal.advpoll.maxChoices(newChoiceN);
100
101 return false;
102 });
103 }
104
105 // Global Killswitch
106 if (Drupal.jsEnabled) {
107 $(document).ready(function() {
108 Drupal.advpoll.nodeFormAutoAttach();
109 });
110 }

  ViewVC Help
Powered by ViewVC 1.1.2