| 1 |
jQuery(document).ready( function() { |
| 2 |
|
| 3 |
function getArgs() { |
| 4 |
var args = {}; |
| 5 |
|
| 6 |
args.fsel = jQuery( 'select#timed_content_rule_frequency option:selected' ).val(); |
| 7 |
args.tz = jQuery( 'select#timed_content_rule_timezone option:selected' ).val(); |
| 8 |
args.nr = jQuery( 'input#timed_content_rule_recurrence_duration_num_repeat' ).val(); |
| 9 |
args.ed = jQuery( 'input#timed_content_rule_recurrence_duration_end_date' ).val(); |
| 10 |
if ( jQuery( "#timed_content_rule_monthly_nth_weekday_of_month" ).prop( "checked" ) ) { |
| 11 |
args.nth = "yes"; |
| 12 |
} else { |
| 13 |
args.nth = "no"; |
| 14 |
} |
| 15 |
args.ntho = jQuery( 'select#timed_content_rule_monthly_nth_weekday_of_month_nth option:selected' ).val(); |
| 16 |
args.nthw = jQuery( 'select#timed_content_rule_monthly_nth_weekday_of_month_weekday option:selected' ).val(); |
| 17 |
|
| 18 |
args.im = 1; |
| 19 |
if ( 0 === args.fsel ) { args.im = jQuery( 'input#timed_content_rule_hourly_num_of_hours' ).val(); } |
| 20 |
if ( 1 == args.fsel ) { args.im = jQuery( 'input#timed_content_rule_daily_num_of_days' ).val(); } |
| 21 |
if ( 2 == args.fsel ) { args.im = jQuery( 'input#timed_content_rule_weekly_num_of_weeks' ).val(); } |
| 22 |
if ( 3 == args.fsel ) { args.im = jQuery( 'input#timed_content_rule_monthly_num_of_months' ).val(); } |
| 23 |
if ( 4 == args.fsel ) { args.im = jQuery( 'input#timed_content_rule_yearly_num_of_years' ).val(); } |
| 24 |
args.days = {}; |
| 25 |
var days_num = 0; |
| 26 |
jQuery( 'input[id^="timed_content_rule_weekly_days_of_week_to_repeat"]' ).each( function() { |
| 27 |
if ( jQuery( this ).prop( "checked" ) ) { |
| 28 |
args.days[days_num] = jQuery( this ).val(); |
| 29 |
} |
| 30 |
days_num++; |
| 31 |
}); |
| 32 |
args.start = { "date": jQuery( 'input#timed_content_rule_instance_start_date' ).val(), "time": jQuery( 'input#timed_content_rule_instance_start_time' ).val() }; |
| 33 |
args.end = { "date": jQuery( 'input#timed_content_rule_instance_end_date' ).val(), "time": jQuery( 'input#timed_content_rule_instance_end_time' ).val() }; |
| 34 |
if ( "recurrence_duration_end_date" == jQuery("input[name=timed_content_rule_recurrence_duration]:checked").val() ) { |
| 35 |
args.rd = "recurrence_duration_end_date"; |
| 36 |
} else { |
| 37 |
args.rd = "recurrence_duration_num_repeat"; |
| 38 |
} |
| 39 |
|
| 40 |
args.ex_days = {}; |
| 41 |
var ex_days_num = 0; |
| 42 |
jQuery( '#timed_content_rule_exceptions_dates option' ).each( function() { |
| 43 |
args.ex_days[ex_days_num] = jQuery( this ).val(); |
| 44 |
ex_days_num++; |
| 45 |
}); |
| 46 |
|
| 47 |
return args; |
| 48 |
} |
| 49 |
|
| 50 |
jQuery('#timed_content_rule_test').click( function() { |
| 51 |
var args = getArgs(); |
| 52 |
//This tag will the hold the dialog content. |
| 53 |
var tag = jQuery("div#tcr-dialogHolder"); |
| 54 |
tag.children().remove(); |
| 55 |
var dialogHTML; |
| 56 |
var button = jQuery('input#timed_content_rule_test'); |
| 57 |
|
| 58 |
button.prop('disabled', 'disabled') |
| 59 |
.removeClass("button-primary") |
| 60 |
.addClass("button-secondary") |
| 61 |
.val(timedContentRuleAjax.button_loading_label) |
| 62 |
.after(jQuery('<span class="spinner" style="float: left;"></span>').show()); |
| 63 |
|
| 64 |
jQuery.post( |
| 65 |
timedContentRuleAjax.ajaxurl, |
| 66 |
{ |
| 67 |
// additional data to be included along with the form fields |
| 68 |
action: 'timedContentPluginGetRulePeriodsAjax', |
| 69 |
timed_content_rule_human_readable: 'true', |
| 70 |
timed_content_rule_frequency: args.fsel, |
| 71 |
timed_content_rule_timezone: args.tz, |
| 72 |
timed_content_rule_recurrence_duration: args.rd, |
| 73 |
timed_content_rule_recurrence_duration_num_repeat: args.nr, |
| 74 |
timed_content_rule_recurrence_duration_end_date: args.ed, |
| 75 |
timed_content_rule_weekly_days_of_week_to_repeat: args.days, |
| 76 |
timed_content_rule_interval_multiplier: args.im, |
| 77 |
timed_content_rule_instance_start: args.start, |
| 78 |
timed_content_rule_instance_end: args.end, |
| 79 |
timed_content_rule_monthly_nth_weekday_of_month: args.nth, |
| 80 |
timed_content_rule_monthly_nth_weekday_of_month_nth: args.ntho, |
| 81 |
timed_content_rule_monthly_nth_weekday_of_month_weekday: args.nthw, |
| 82 |
timed_content_rule_exceptions_dates: args.ex_days |
| 83 |
|
| 84 |
}, |
| 85 |
function(data, textStatus, jqXHR) { |
| 86 |
// code that's executed when the request is processed successfully |
| 87 |
dialogHTML = "<table class='tcr-dates'>"; |
| 88 |
dialogHTML += "<tr class='heading_row'><th>" + timedContentRuleAjax.start_label + "</th><th>" + timedContentRuleAjax.end_label + "</th></tr>"; |
| 89 |
dialogHTML += '</table>'; |
| 90 |
|
| 91 |
tag.append(dialogHTML); |
| 92 |
var the_table = tag.find("table"); |
| 93 |
jQuery.each(data, function(key, value) { |
| 94 |
the_table.append('<tr class="day_row ' + value.status + '" title="' + value.time + '"><td>' + value.start + '</td><td>' + value.end + '</td></tr>'); |
| 95 |
}); |
| 96 |
tb_show(timedContentRuleAjax.dialog_label, "#TB_inline?width=" + timedContentRuleAjax.dialog_width + "&height=" + timedContentRuleAjax.dialog_height + "&inlineId=tcr-dialogHolder"); |
| 97 |
button.val(timedContentRuleAjax.button_finished_label) |
| 98 |
.removeClass("button-secondary") |
| 99 |
.addClass("button-primary") |
| 100 |
.removeAttr('disabled') |
| 101 |
.next() |
| 102 |
.remove(); |
| 103 |
}, |
| 104 |
'json' |
| 105 |
).fail( function(xhr, textStatus, errorThrown) { |
| 106 |
var errorText = '<p class="heading">' + timedContentRuleAjax.error_desc + '</p>'; |
| 107 |
errorText += '<div>' + xhr.responseText + '</div>'; |
| 108 |
tag.append(errorText); |
| 109 |
tb_show(timedContentRuleAjax.error, "#TB_inline?width=" + timedContentRuleAjax.dialog_width + "&height=" + timedContentRuleAjax.dialog_height + "&inlineId=tcr-dialogHolder"); |
| 110 |
button.html(timedContentRuleAjax.button_finished_label) |
| 111 |
.removeClass("button-secondary") |
| 112 |
.addClass("button-primary") |
| 113 |
.removeAttr('disabled') |
| 114 |
.next() |
| 115 |
.remove(); |
| 116 |
}); |
| 117 |
}); |
| 118 |
|
| 119 |
jQuery( 'input[id^="timed_content_rule_"], select[id^="timed_content_rule_"]' ).change(function(e) { |
| 120 |
var target = jQuery(e.target); |
| 121 |
if (target.is("#timed_content_rule_exceptions_dates")) |
| 122 |
return; |
| 123 |
var args = getArgs(); |
| 124 |
var tag = jQuery("div#schedule_desc"); |
| 125 |
var button = jQuery('input#timed_content_rule_test'); |
| 126 |
|
| 127 |
button.prop('disabled', 'disabled'); |
| 128 |
tag.html('<img src="' + timedContentRuleAjax.loadingimg + '" /> Loading...'); |
| 129 |
|
| 130 |
jQuery.post( |
| 131 |
timedContentRuleAjax.ajaxurl, |
| 132 |
{ |
| 133 |
// additional data to be included along with the form fields |
| 134 |
action: 'timedContentPluginGetScheduleDescriptionAjax', |
| 135 |
timed_content_rule_action: jQuery( "input[name=timed_content_rule_action]:checked" ).val(), |
| 136 |
timed_content_rule_frequency: args.fsel, |
| 137 |
timed_content_rule_timezone: args.tz, |
| 138 |
timed_content_rule_recurrence_duration: args.rd, |
| 139 |
timed_content_rule_recurrence_duration_num_repeat: args.nr, |
| 140 |
timed_content_rule_recurrence_duration_end_date: args.ed, |
| 141 |
timed_content_rule_weekly_days_of_week_to_repeat: args.days, |
| 142 |
timed_content_rule_interval_multiplier: args.im, |
| 143 |
timed_content_rule_instance_start: args.start, |
| 144 |
timed_content_rule_instance_end: args.end, |
| 145 |
timed_content_rule_monthly_nth_weekday_of_month: args.nth, |
| 146 |
timed_content_rule_monthly_nth_weekday_of_month_nth: args.ntho, |
| 147 |
timed_content_rule_monthly_nth_weekday_of_month_weekday: args.nthw, |
| 148 |
timed_content_rule_exceptions_dates: args.ex_days |
| 149 |
}, |
| 150 |
function(data, textStatus, jqXHR) { |
| 151 |
// code that's executed when the request is processed successfully |
| 152 |
tag.html(data); |
| 153 |
button.removeAttr('disabled'); |
| 154 |
}, |
| 155 |
'text' |
| 156 |
).fail( function(xhr, textStatus, errorThrown) { |
| 157 |
var errorText = '<div class="tcr-error"><p><strong>' + timedContentRuleAjax.error + '</strong></p>'; |
| 158 |
errorText += '<p class="heading">' + timedContentRuleAjax.error_desc + '</p>'; |
| 159 |
errorText += '<div>' + xhr.responseText + '</div></div>'; |
| 160 |
tag.append(errorText); |
| 161 |
}); |
| 162 |
}); |
| 163 |
}); |