forked from lesterchan/wp-polls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polls-js.dev.js
143 lines (137 loc) · 5.13 KB
/
polls-js.dev.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Variables
var poll_id = 0;
var poll_answer_id = '';
var is_being_voted = false;
pollsL10n.show_loading = parseInt(pollsL10n.show_loading);
pollsL10n.show_fading = parseInt(pollsL10n.show_fading);
// When User Vote For Poll
function poll_vote(current_poll_id) {
jQuery(document).ready(function($) {
if(!is_being_voted) {
set_is_being_voted(true);
poll_id = current_poll_id;
poll_answer_id = '';
poll_multiple_ans = 0;
poll_multiple_ans_count = 0;
if($('#poll_multiple_ans_' + poll_id).length) {
poll_multiple_ans = parseInt($('#poll_multiple_ans_' + poll_id).val());
}
$('#polls_form_' + poll_id + ' input:checkbox, #polls_form_' + poll_id + ' input:radio, #polls_form_' + poll_id + ' option').each(function(i){
if ($(this).is(':checked') || $(this).is(':selected')) {
if(poll_multiple_ans > 0) {
poll_answer_id = $(this).val() + ',' + poll_answer_id;
poll_multiple_ans_count++;
} else {
poll_answer_id = parseInt($(this).val());
}
}
});
if(poll_multiple_ans > 0) {
if(poll_multiple_ans_count > 0 && poll_multiple_ans_count <= poll_multiple_ans) {
poll_answer_id = poll_answer_id.substring(0, (poll_answer_id.length-1));
poll_process();
} else if(poll_multiple_ans_count == 0) {
set_is_being_voted(false);
alert(pollsL10n.text_valid);
} else {
set_is_being_voted(false);
alert(pollsL10n.text_multiple + ' ' + poll_multiple_ans);
}
} else {
if(poll_answer_id > 0) {
poll_process();
} else {
set_is_being_voted(false);
alert(pollsL10n.text_valid);
}
}
} else {
alert(pollsL10n.text_wait);
}
});
}
// Process Poll (User Click "Vote" Button)
function poll_process() {
jQuery(document).ready(function($) {
poll_nonce = $('#poll_' + poll_id + '_nonce').val();
if(pollsL10n.show_fading) {
$('#polls-' + poll_id).fadeTo('def', 0);
if(pollsL10n.show_loading) {
$('#polls-' + poll_id + '-loading').show();
}
$.ajax({type: 'POST', xhrFields: {withCredentials: true}, url: pollsL10n.ajax_url, data: 'action=polls&view=process&poll_id=' + poll_id + '&poll_' + poll_id + '=' + poll_answer_id + '&poll_' + poll_id + '_nonce=' + poll_nonce, cache: false, success: poll_process_success});
} else {
if(pollsL10n.show_loading) {
$('#polls-' + poll_id + '-loading').show();
}
$.ajax({type: 'POST', xhrFields: {withCredentials: true}, url: pollsL10n.ajax_url, data: 'action=polls&view=process&poll_id=' + poll_id + '&poll_' + poll_id + '=' + poll_answer_id + '&poll_' + poll_id + '_nonce=' + poll_nonce, cache: false, success: poll_process_success});
}
});
}
// Poll's Result (User Click "View Results" Link)
function poll_result(current_poll_id) {
jQuery(document).ready(function($) {
if(!is_being_voted) {
set_is_being_voted(true);
poll_id = current_poll_id;
poll_nonce = $('#poll_' + poll_id + '_nonce').val();
if(pollsL10n.show_fading) {
$('#polls-' + poll_id).fadeTo('def', 0);
if(pollsL10n.show_loading) {
$('#polls-' + poll_id + '-loading').show();
}
$.ajax({type: 'POST', xhrFields: {withCredentials: true}, url: pollsL10n.ajax_url, data: 'action=polls&view=result&poll_id=' + poll_id + '&poll_' + poll_id + '_nonce=' + poll_nonce, cache: false, success: poll_process_success});
} else {
if(pollsL10n.show_loading) {
$('#polls-' + poll_id + '-loading').show();
}
$.ajax({type: 'POST', xhrFields: {withCredentials: true}, url: pollsL10n.ajax_url, data: 'action=polls&view=result&poll_id=' + poll_id + '&poll_' + poll_id + '_nonce=' + poll_nonce, cache: false, success: poll_process_success});
}
} else {
alert(pollsL10n.text_wait);
}
});
}
// Poll's Voting Booth (User Click "Vote" Link)
function poll_booth(current_poll_id) {
jQuery(document).ready(function($) {
if(!is_being_voted) {
set_is_being_voted(true);
poll_id = current_poll_id;
poll_nonce = $('#poll_' + poll_id + '_nonce').val();
if(pollsL10n.show_fading) {
$('#polls-' + poll_id).fadeTo('def', 0);
if(pollsL10n.show_loading) {
$('#polls-' + poll_id + '-loading').show();
}
$.ajax({type: 'POST', xhrFields: {withCredentials: true}, url: pollsL10n.ajax_url, data: 'action=polls&view=booth&poll_id=' + poll_id + '&poll_' + poll_id + '_nonce=' + poll_nonce, cache: false, success: poll_process_success});
} else {
if(pollsL10n.show_loading) {
$('#polls-' + poll_id + '-loading').show();
}
$.ajax({type: 'POST', xhrFields: {withCredentials: true}, url: pollsL10n.ajax_url, data: 'action=polls&view=booth&poll_id=' + poll_id + '&poll_' + poll_id + '_nonce=' + poll_nonce, cache: false, success: poll_process_success});
}
} else {
alert(pollsL10n.text_wait);
}
});
}
// Poll Process Successfully
function poll_process_success(data) {
jQuery(document).ready(function($) {
$('#polls-' + poll_id).replaceWith(data);
if(pollsL10n.show_loading) {
$('#polls-' + poll_id + '-loading').hide();
}
if(pollsL10n.show_fading) {
$('#polls-' + poll_id).fadeTo('def', 1);
set_is_being_voted(false);
} else {
set_is_being_voted(false);
}
});
}
// Set is_being_voted Status
function set_is_being_voted(voted_status) {
is_being_voted = voted_status;
}