Commit 028a5e04 authored by LockeExile's avatar LockeExile
Browse files

Use parameters and closures instead of globals

parent f9769f8f
// 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);
}
poll_answer_id = '';
poll_multiple_ans = 0;
poll_multiple_ans_count = 0;
if($('#poll_multiple_ans_' + current_poll_id).length) {
poll_multiple_ans = parseInt($('#poll_multiple_ans_' + current_poll_id).val());
}
$('#polls_form_' + current_poll_id + ' input:checkbox, #polls_form_' + current_poll_id + ' input:radio, #polls_form_' + current_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(current_poll_id, poll_answer_id);
} else if(poll_multiple_ans_count == 0) {
alert(pollsL10n.text_valid);
} else {
alert(pollsL10n.text_multiple + ' ' + poll_multiple_ans);
}
} else {
if(poll_answer_id > 0) {
poll_process(current_poll_id, poll_answer_id);
} else {
alert(pollsL10n.text_valid);
}
}
});
}
// Process Poll (User Click "Vote" Button)
function poll_process() {
function poll_process(current_poll_id, poll_answer_id) {
jQuery(document).ready(function($) {
poll_nonce = $('#poll_' + poll_id + '_nonce').val();
poll_nonce = $('#poll_' + current_poll_id + '_nonce').val();
if(pollsL10n.show_fading) {
$('#polls-' + poll_id).fadeTo('def', 0);
$('#polls-' + current_poll_id).fadeTo('def', 0);
if(pollsL10n.show_loading) {
$('#polls-' + poll_id + '-loading').show();
$('#polls-' + current_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});
$.ajax({type: 'POST', xhrFields: {withCredentials: true}, url: pollsL10n.ajax_url, data: 'action=polls&view=process&poll_id=' + current_poll_id + '&poll_' + current_poll_id + '=' + poll_answer_id + '&poll_' + current_poll_id + '_nonce=' + poll_nonce, cache: false, success: poll_process_success(current_poll_id)});
} else {
if(pollsL10n.show_loading) {
$('#polls-' + poll_id + '-loading').show();
$('#polls-' + current_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});
$.ajax({type: 'POST', xhrFields: {withCredentials: true}, url: pollsL10n.ajax_url, data: 'action=polls&view=process&poll_id=' + current_poll_id + '&poll_' + current_poll_id + '=' + poll_answer_id + '&poll_' + current_poll_id + '_nonce=' + poll_nonce, cache: false, success: poll_process_success(current_poll_id)});
}
});
}
......@@ -74,70 +62,52 @@ function poll_process() {
// 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_nonce = $('#poll_' + current_poll_id + '_nonce').val();
if(pollsL10n.show_fading) {
$('#polls-' + current_poll_id).fadeTo('def', 0);
if(pollsL10n.show_loading) {
$('#polls-' + current_poll_id + '-loading').show();
}
$.ajax({type: 'POST', xhrFields: {withCredentials: true}, url: pollsL10n.ajax_url, data: 'action=polls&view=result&poll_id=' + current_poll_id + '&poll_' + current_poll_id + '_nonce=' + poll_nonce, cache: false, success: poll_process_success(current_poll_id)});
} else {
if(pollsL10n.show_loading) {
$('#polls-' + current_poll_id + '-loading').show();
}
$.ajax({type: 'POST', xhrFields: {withCredentials: true}, url: pollsL10n.ajax_url, data: 'action=polls&view=result&poll_id=' + current_poll_id + '&poll_' + current_poll_id + '_nonce=' + poll_nonce, cache: false, success: poll_process_success(current_poll_id)});
}
});
}
// 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_nonce = $('#poll_' + current_poll_id + '_nonce').val();
if(pollsL10n.show_fading) {
$('#polls-' + current_poll_id).fadeTo('def', 0);
if(pollsL10n.show_loading) {
$('#polls-' + current_poll_id + '-loading').show();
}
$.ajax({type: 'POST', xhrFields: {withCredentials: true}, url: pollsL10n.ajax_url, data: 'action=polls&view=booth&poll_id=' + current_poll_id + '&poll_' + current_poll_id + '_nonce=' + poll_nonce, cache: false, success: poll_process_success(current_poll_id)});
} else {
if(pollsL10n.show_loading) {
$('#polls-' + current_poll_id + '-loading').show();
}
$.ajax({type: 'POST', xhrFields: {withCredentials: true}, url: pollsL10n.ajax_url, data: 'action=polls&view=booth&poll_id=' + current_poll_id + '&poll_' + current_poll_id + '_nonce=' + poll_nonce, cache: false, success: poll_process_success(current_poll_id)});
}
});
}
// 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;
function poll_process_success(current_poll_id) {
return function(data) {
jQuery(document).ready(function($) {
$('#polls-' + current_poll_id).replaceWith(data);
if(pollsL10n.show_loading) {
$('#polls-' + current_poll_id + '-loading').hide();
}
if(pollsL10n.show_fading) {
$('#polls-' + current_poll_id).fadeTo('def', 1);
}
});
}
}
\ No newline at end of file
var poll_id=0,poll_answer_id="",is_being_voted=!1;pollsL10n.show_loading=parseInt(pollsL10n.show_loading);pollsL10n.show_fading=parseInt(pollsL10n.show_fading);
function poll_vote(b){jQuery(document).ready(function(a){is_being_voted?alert(pollsL10n.text_wait):(set_is_being_voted(!0),poll_id=b,poll_answer_id="",poll_multiple_ans_count=poll_multiple_ans=0,a("#poll_multiple_ans_"+poll_id).length&&(poll_multiple_ans=parseInt(a("#poll_multiple_ans_"+poll_id).val())),a("#polls_form_"+poll_id+" input:checkbox, #polls_form_"+poll_id+" input:radio, #polls_form_"+poll_id+" option").each(function(b){if(a(this).is(":checked")||a(this).is(":selected"))0<poll_multiple_ans?
(poll_answer_id=a(this).val()+","+poll_answer_id,poll_multiple_ans_count++):poll_answer_id=parseInt(a(this).val())}),0<poll_multiple_ans?0<poll_multiple_ans_count&&poll_multiple_ans_count<=poll_multiple_ans?(poll_answer_id=poll_answer_id.substring(0,poll_answer_id.length-1),poll_process()):0==poll_multiple_ans_count?(set_is_being_voted(!1),alert(pollsL10n.text_valid)):(set_is_being_voted(!1),alert(pollsL10n.text_multiple+" "+poll_multiple_ans)):0<poll_answer_id?poll_process():(set_is_being_voted(!1),
alert(pollsL10n.text_valid)))})}function poll_process(){jQuery(document).ready(function(b){poll_nonce=b("#poll_"+poll_id+"_nonce").val();pollsL10n.show_fading&&b("#polls-"+poll_id).fadeTo("def",0);pollsL10n.show_loading&&b("#polls-"+poll_id+"-loading").show();b.ajax({type:"POST",xhrFields:{withCredentials:!0},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:!1,success:poll_process_success})})}
function poll_result(b){jQuery(document).ready(function(a){is_being_voted?alert(pollsL10n.text_wait):(set_is_being_voted(!0),poll_id=b,poll_nonce=a("#poll_"+poll_id+"_nonce").val(),pollsL10n.show_fading&&a("#polls-"+poll_id).fadeTo("def",0),pollsL10n.show_loading&&a("#polls-"+poll_id+"-loading").show(),a.ajax({type:"POST",xhrFields:{withCredentials:!0},url:pollsL10n.ajax_url,data:"action=polls&view=result&poll_id="+poll_id+"&poll_"+poll_id+"_nonce="+poll_nonce,cache:!1,success:poll_process_success}))})}
function poll_booth(b){jQuery(document).ready(function(a){is_being_voted?alert(pollsL10n.text_wait):(set_is_being_voted(!0),poll_id=b,poll_nonce=a("#poll_"+poll_id+"_nonce").val(),pollsL10n.show_fading&&a("#polls-"+poll_id).fadeTo("def",0),pollsL10n.show_loading&&a("#polls-"+poll_id+"-loading").show(),a.ajax({type:"POST",xhrFields:{withCredentials:!0},url:pollsL10n.ajax_url,data:"action=polls&view=booth&poll_id="+poll_id+"&poll_"+poll_id+"_nonce="+poll_nonce,cache:!1,success:poll_process_success}))})}
function poll_process_success(b){jQuery(document).ready(function(a){a("#polls-"+poll_id).replaceWith(b);pollsL10n.show_loading&&a("#polls-"+poll_id+"-loading").hide();pollsL10n.show_fading&&a("#polls-"+poll_id).fadeTo("def",1);set_is_being_voted(!1)})}function set_is_being_voted(b){is_being_voted=b};
\ No newline at end of file
function poll_vote(l){jQuery(document).ready(function(o){poll_answer_id="",poll_multiple_ans=0,poll_multiple_ans_count=0,o("#poll_multiple_ans_"+l).length&&(poll_multiple_ans=parseInt(o("#poll_multiple_ans_"+l).val())),o("#polls_form_"+l+" input:checkbox, #polls_form_"+l+" input:radio, #polls_form_"+l+" option").each(function(l){(o(this).is(":checked")||o(this).is(":selected"))&&(poll_multiple_ans>0?(poll_answer_id=o(this).val()+","+poll_answer_id,poll_multiple_ans_count++):poll_answer_id=parseInt(o(this).val()))}),poll_multiple_ans>0?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(l,poll_answer_id)):0==poll_multiple_ans_count?alert(pollsL10n.text_valid):alert(pollsL10n.text_multiple+" "+poll_multiple_ans):poll_answer_id>0?poll_process(l,poll_answer_id):alert(pollsL10n.text_valid)})}function poll_process(l,o){jQuery(document).ready(function(s){poll_nonce=s("#poll_"+l+"_nonce").val(),pollsL10n.show_fading?(s("#polls-"+l).fadeTo("def",0),pollsL10n.show_loading&&s("#polls-"+l+"-loading").show(),s.ajax({type:"POST",xhrFields:{withCredentials:!0},url:pollsL10n.ajax_url,data:"action=polls&view=process&poll_id="+l+"&poll_"+l+"="+o+"&poll_"+l+"_nonce="+poll_nonce,cache:!1,success:poll_process_success(l)})):(pollsL10n.show_loading&&s("#polls-"+l+"-loading").show(),s.ajax({type:"POST",xhrFields:{withCredentials:!0},url:pollsL10n.ajax_url,data:"action=polls&view=process&poll_id="+l+"&poll_"+l+"="+o+"&poll_"+l+"_nonce="+poll_nonce,cache:!1,success:poll_process_success(l)}))})}function poll_result(l){jQuery(document).ready(function(o){poll_nonce=o("#poll_"+l+"_nonce").val(),pollsL10n.show_fading?(o("#polls-"+l).fadeTo("def",0),pollsL10n.show_loading&&o("#polls-"+l+"-loading").show(),o.ajax({type:"POST",xhrFields:{withCredentials:!0},url:pollsL10n.ajax_url,data:"action=polls&view=result&poll_id="+l+"&poll_"+l+"_nonce="+poll_nonce,cache:!1,success:poll_process_success(l)})):(pollsL10n.show_loading&&o("#polls-"+l+"-loading").show(),o.ajax({type:"POST",xhrFields:{withCredentials:!0},url:pollsL10n.ajax_url,data:"action=polls&view=result&poll_id="+l+"&poll_"+l+"_nonce="+poll_nonce,cache:!1,success:poll_process_success(l)}))})}function poll_booth(l){jQuery(document).ready(function(o){poll_nonce=o("#poll_"+l+"_nonce").val(),pollsL10n.show_fading?(o("#polls-"+l).fadeTo("def",0),pollsL10n.show_loading&&o("#polls-"+l+"-loading").show(),o.ajax({type:"POST",xhrFields:{withCredentials:!0},url:pollsL10n.ajax_url,data:"action=polls&view=booth&poll_id="+l+"&poll_"+l+"_nonce="+poll_nonce,cache:!1,success:poll_process_success(l)})):(pollsL10n.show_loading&&o("#polls-"+l+"-loading").show(),o.ajax({type:"POST",xhrFields:{withCredentials:!0},url:pollsL10n.ajax_url,data:"action=polls&view=booth&poll_id="+l+"&poll_"+l+"_nonce="+poll_nonce,cache:!1,success:poll_process_success(l)}))})}function poll_process_success(l){return function(o){jQuery(document).ready(function(s){s("#polls-"+l).replaceWith(o),pollsL10n.show_loading&&s("#polls-"+l+"-loading").hide(),pollsL10n.show_fading&&s("#polls-"+l).fadeTo("def",1)})}}pollsL10n.show_loading=parseInt(pollsL10n.show_loading),pollsL10n.show_fading=parseInt(pollsL10n.show_fading);
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment