Advertisement
BakerMan

Temp Workaround TEC/ECP Bug #17868

Oct 18th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * This is a modified version of the file found at:
  3.  * the-events-calendar/resources/events-admin.js
  4.  * and contains temporary workaround for bug #17868.
  5.  *
  6.  * Important! Back-up your previous version and restore
  7.  * it if this does not function as you expect and wait
  8.  * for the issue to be fully resolved in a future
  9.  * release instead.
  10.  */
  11.  
  12. jQuery(document).ready(function($) {
  13.  
  14.     // Load the Chosen JQuery plugin for all select elements with the class 'chosen'.
  15.     $('.chosen, .tribe-field-dropdown_chosen select').chosen();
  16.  
  17.     //not done by default on front end
  18.     $('.hide-if-js').hide();
  19.  
  20.     if(typeof(TEC) != 'undefined'){
  21.         var datepickerOpts = {
  22.             dateFormat: 'yy-mm-dd',
  23.             showAnim: 'fadeIn',
  24.             changeMonth: true,
  25.             changeYear: true,
  26.             numberOfMonths: 3,
  27.             showButtonPanel: true,
  28.             onSelect: function(selectedDate) {
  29.                 var option = this.id == "EventStartDate" ? "minDate" : "maxDate";
  30.                 var instance = $(this).data("datepicker");
  31.                 var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
  32.                 dates.not(this).not('#recurrence_end').datepicker("option", option, date);
  33.             }
  34.         };
  35.         $.extend(datepickerOpts, TEC);
  36.         var dates = $("#EventStartDate, #EventEndDate, .datepicker").datepicker(datepickerOpts);
  37.  
  38.         // toggle time input
  39.         function toggleDayTimeDisplay(){
  40.             if( $('#allDayCheckbox').attr("checked") === true || $('#allDayCheckbox').attr("checked") === "checked" ) {
  41.                 $(".timeofdayoptions").hide();
  42.                 $("#EventTimeFormatDiv").hide();
  43.             } else {
  44.                 $(".timeofdayoptions").show();
  45.                 $("#EventTimeFormatDiv").show();
  46.             }
  47.         }
  48.         // check on click
  49.         $('#allDayCheckbox').click(function(){
  50.             toggleDayTimeDisplay();
  51.         });
  52.         // check on load
  53.         toggleDayTimeDisplay();
  54.  
  55.         var tribeDaysPerMonth = [29,31,28,31,30,31,30,31,31,30,31,30,31];
  56.  
  57.         // start and end date select sections
  58.         var tribeStartDays = [ $('#28StartDays'), $('#29StartDays'), $('#30StartDays'), $('#31StartDays') ];
  59.         var tribeEndDays = [ $('#28EndDays'), $('#29EndDays'), $('#30EndDays'), $('#31EndDays') ];
  60.  
  61.         $("select[name='EventStartMonth'], select[name='EventEndMonth']").change(function() {
  62.             var t = $(this);
  63.             var startEnd = t.attr("name");
  64.             // get changed select field
  65.             if( startEnd == 'EventStartMonth' ) startEnd = 'Start';
  66.             else startEnd = 'End';
  67.             // show/hide date lists according to month
  68.             var chosenMonth = t.attr("value");
  69.             if( chosenMonth.charAt(0) == '0' ) chosenMonth = chosenMonth.replace('0', '');
  70.             // leap year
  71.             var remainder = $("select[name='Event" + startEnd + "Year']").attr("value") % 4;
  72.             if( chosenMonth == 2 && remainder == 0 ) chosenMonth = 0;
  73.             // preserve selected option
  74.             var currentDateField = $("select[name='Event" + startEnd + "Day']");
  75.  
  76.             $('.event' + startEnd + 'DateField').remove();
  77.             if( startEnd == "Start") {
  78.                 var selectObject = tribeStartDays[ tribeDaysPerMonth[ chosenMonth ] - 28 ];
  79.                 selectObject.val( currentDateField.val() );
  80.                 $("select[name='EventStartMonth']").after( selectObject );
  81.             } else {
  82.                 var selectObject = tribeEndDays[ tribeDaysPerMonth[ chosenMonth ] - 28 ];
  83.                 selectObject.val( currentDateField.val() );
  84.                 $('select[name="EventEndMonth"]').after( selectObject );
  85.             }
  86.         });
  87.  
  88.         $("select[name='EventStartMonth'], select[name='EventEndMonth']").change();
  89.  
  90.         $("select[name='EventStartYear']").change(function() {
  91.             $("select[name='EventStartMonth']").change();
  92.         });
  93.  
  94.         $("select[name='EventEndYear']").change(function() {
  95.             $("select[name='EventEndMonth']").change();
  96.         });
  97.  
  98.         // Form validation
  99.         $("form[name='post']").submit(function() {
  100.             if( $("#isEventNo").attr('checked') === true || $("#isEventNo").attr('checked') === "checked" ) {
  101.                 // do not validate since this is not an event
  102.                 return true;
  103.             }
  104.             return true;
  105.         });
  106.  
  107.         // hide unnecessary fields
  108.         var venueFields = $(".venue");
  109.  
  110.         var savedVenue = $("#saved_venue");
  111.  
  112.         if ( savedVenue.size() > 0 && savedVenue.val() != '0' && !$('.nosaved').get(0) ) {
  113.             venueFields.hide();
  114.             $('input',venueFields).val('');
  115.         }
  116.  
  117.         savedVenue.change(function() {
  118.             if ( $(this).val() == '0' ) {
  119.                 venueFields.fadeIn()
  120.                     //.find("input, select").val('').removeAttr('checked');
  121.             }
  122.             else {
  123.                 venueFields.fadeOut();
  124.             }
  125.         });
  126.         // hide unnecessary fields
  127.         var organizerFields = $(".organizer");
  128.  
  129.         var savedorganizer = $("#saved_organizer");
  130.  
  131.         if ( savedorganizer.size() > 0 && savedorganizer.val() != '0' && !$('.nosaved_organizer').get(0) ) {
  132.             organizerFields.hide();
  133.             $('input',organizerFields).val('');
  134.         }
  135.  
  136.         savedorganizer.change(function() {
  137.             if ( $(this).val() == '0' ) {
  138.                 organizerFields.fadeIn()
  139.                     //.find("input, select").val('').removeAttr('checked');
  140.             }
  141.             else {
  142.                 organizerFields.fadeOut();
  143.             }
  144.         });
  145.     }
  146.  
  147.     //show state/province input based on first option in countries list, or based on user input of country
  148.     function tribeShowHideCorrectStateProvinceInput(country) {
  149.         if (country == 'US' || country == 'United States') {
  150.             $("#StateProvinceSelect_chzn").show();
  151.             $("#StateProvinceText").hide();
  152.         } else if ( country != '' ) {
  153.             $("#StateProvinceText").show();
  154.             $("#StateProvinceSelect_chzn").hide();
  155.         } else {
  156.             $("#StateProvinceText").hide();
  157.             $("#StateProvinceSelect_chzn").hide();
  158.         }
  159.     }
  160.  
  161.     tribeShowHideCorrectStateProvinceInput( $("#EventCountry > option:selected").val() );
  162.  
  163.     $("#EventCountry").change(function() {
  164.         var countryLabel = $(this).find('option:selected').val();
  165.         tribeShowHideCorrectStateProvinceInput( countryLabel );
  166.     });
  167.  
  168.     // If recurrence changes on a recurring event, then show warning, and automatically change whole recurrence
  169.     if($('[name="is_recurring"]').val() == "true" && !$('[name="recurrence_action"]').val() ) {
  170.         function recurrenceChanged() {
  171.             $('#recurrence-changed-row').show();
  172.             $('[name="recurrence_action"]').val(2);
  173.         }
  174.  
  175.         $('.recurrence-row input, .custom-recurrence-row input,.recurrence-row select, .custom-recurrence-row select').change(recurrenceChanged)
  176.         $( '[name="recurrence[end]"]' ).datepicker('option', 'onSelect', recurrenceChanged);
  177.     }
  178.  
  179.     $( '[name="recurrence[end]"]' ).datepicker('option', 'onSelect', function() {
  180.         $('[name="recurrence[end]"]').removeClass('placeholder');
  181.     });
  182.  
  183.     /* Fix for deleting multiple events */
  184.     $('.wp-admin.events-cal.edit-php #doaction').click(function(e) {
  185.         if($("[name='action'] option:selected").val() == "trash") {
  186.             if(confirm("Are you sure you want to trash all occurrences of these events? All recurrence data will be lost.")) {
  187.                 var ids = new Array();
  188.  
  189.                 $('[name="post[]"]:checked').each(function() {
  190.                     var curval = $(this).val();
  191.                     if(ids[curval]) {
  192.                         $(this).prop('checked', false);
  193.                     }
  194.  
  195.                     ids[curval] = true;
  196.                 });
  197.             } else {
  198.                 e.preventDefault();
  199.             }
  200.         }
  201.     });
  202.  
  203.     function isExistingRecurringEvent() {
  204.         return $('[name="is_recurring"]').val() == "true" && !$('[name="recurrence_action"]').val() && !$('[name="recurrence_action"]').val()
  205.     }
  206.  
  207.     function resetSubmitButton() {
  208.         $('#publishing-action .button-primary-disabled').removeClass('button-primary-disabled');
  209.         $('#publishing-action #ajax-loading').css('visibility', 'hidden');
  210.  
  211.     }
  212.  
  213.     $('#EventInfo input, #EventInfo select').change(function() {
  214.         $('.rec-error').hide();
  215.     })
  216.  
  217.     var eventSubmitButton = $('.wp-admin.events-cal #post #publishing-action input[type="submit"]');
  218.     eventSubmitButton.click(function(e) {
  219.         $(this).data('clicked', true);
  220.     });
  221.  
  222.     /* Recurring Events Dialog */
  223.     $('.wp-admin.events-cal #post').submit(function(e) {
  224.         var self = $(this);
  225.  
  226.         // Workaround for bug #17868:
  227.         // this will not solve the basic problem, but it should allow editing of
  228.         // recurring events that have been incorrectly marked as not recurring
  229.         if (document.getElementById("recurring-dialog") === null)
  230.             return;
  231.  
  232.         if( isExistingRecurringEvent() ) { // not a new event
  233.             e.preventDefault();
  234.             $('#recurring-dialog').dialog({
  235.                 modal: true,
  236.                 buttons: [{
  237.                         text:"Only This Event",
  238.                         click: function() {
  239.                             $('[name="recurrence_action"]').val(3);
  240.  
  241.                             if (eventSubmitButton.data('clicked') )
  242.                                 $('<input type="hidden" name="' + eventSubmitButton.attr('name') + '" value="' + eventSubmitButton.val() + '"/>').appendTo(self);
  243.  
  244.                             $(this).dialog("close");
  245.                             self.submit();
  246.                         }
  247.                 }, {
  248.                         text:"All Events",
  249.                         click: function() {
  250.                             $('[name="recurrence_action"]').val(2);
  251.  
  252.                             if (eventSubmitButton.data('clicked') )
  253.                                 $('<input type="hidden" name="' + eventSubmitButton.attr('name') + '" value="' + eventSubmitButton.val() + '"/>').appendTo(self);
  254.  
  255.                             $(this).dialog("close");
  256.                             self.submit();
  257.                         }
  258.                 }],
  259.                 close: function() {
  260.                     eventSubmitButton.data('clicked', null);
  261.                 }
  262.             });
  263.         }
  264.     });
  265.  
  266.     function setupSubmitButton() {
  267.         //publishing-action
  268.     }
  269.  
  270.     $('.wp-admin.events-cal .submitdelete').click(function(e) {
  271.  
  272.         var link = $(this);
  273.         var isRecurringLink = $(this).attr('href').split('&eventDate');
  274.  
  275.         if(isRecurringLink[1]) {
  276.             e.preventDefault();
  277.  
  278.             $('#deletion-dialog').dialog({
  279.                 //submitdelete
  280.                 modal: true,
  281.                 buttons: [{
  282.                     text: "Only This Event",
  283.                     click: function() {
  284.                         document.location = link.attr('href') + '&event_start=' + $(this).data('start');
  285.                     }
  286.                 },
  287.                 {
  288.                     text: "All Events",
  289.                     click: function() {
  290.                         document.location = link.attr('href') + '&deleteAll';
  291.                     }
  292.                 }]
  293.             });
  294.         }
  295.  
  296.     });
  297.  
  298.     // recurrence ui
  299.     $('[name="recurrence[type]"]').change(function() {
  300.         var curOption =  $(this).find("option:selected").val();
  301.         $('.custom-recurrence-row').hide();
  302.  
  303.         if (curOption == "Custom" ) {
  304.             $('#recurrence-end').show();
  305.             $('#custom-recurrence-frequency').show();
  306.             $('[name="recurrence[custom-type]"]').change();
  307.         } else if (curOption == "None") {
  308.             $('#recurrence-end').hide();
  309.             $('#custom-recurrence-frequency').hide();
  310.         } else {
  311.             $('#recurrence-end').show();
  312.             $('#custom-recurrence-frequency').hide();
  313.         }
  314.     });
  315.  
  316.     $('[name="recurrence[end-type]"]').change(function() {
  317.         var val = $(this).find('option:selected').val();
  318.  
  319.         if (val == "On") {
  320.             $('#rec-count').hide();
  321.             $('#recurrence_end').show();
  322.         } else {
  323.             $('#recurrence_end').hide();
  324.             $('#rec-count').show();
  325.         }
  326.     });
  327.  
  328.     $('[name="recurrence[custom-type]"]').change(function() {
  329.         $('.custom-recurrence-row').hide();
  330.         var option = $(this).find('option:selected'), customSelector = option.data('tablerow');
  331.         $(customSelector).show()
  332.         $('#recurrence-interval-type').text(option.data('plural'));
  333.         $('[name="recurrence[custom-type-text]"]').val(option.data('plural'));
  334.     });
  335.  
  336.     $('#recurrence_end_count').change(function() {
  337.         $('[name="recurrence[type]"]').change();
  338.     });
  339.  
  340.     $('[name="recurrence[type]"]').change(function() {
  341.         var option = $(this).find('option:selected'), numOccurrences = $('#recurrence_end_count').val();
  342.         $('#occurence-count-text').text(numOccurrences == 1 ? option.data('single') : option.data('plural'));
  343.         $('[name="recurrence[occurrence-count-text]"]').val($('#occurence-count-text').text());
  344.     });
  345.  
  346.     $('[name="recurrence[custom-month-number]"]').change(function() {
  347.         var option = $(this).find('option:selected'), dayselect = $('[name="recurrence[custom-month-day]"]');
  348.  
  349.         if(isNaN(option.val())) {
  350.             dayselect.show();
  351.         } else {
  352.             dayselect.hide();
  353.         }
  354.     });
  355.  
  356.     function maybeDisplayPressTrendsDialogue() {
  357.         return $('[name="maybeDisplayPressTrendsDialogue"]').val() == "1"
  358.     }
  359.  
  360.     if( maybeDisplayPressTrendsDialogue() ) {
  361.             $('#presstrends-dialog').dialog({
  362.                 modal: true,
  363.                 buttons: [{
  364.                         text:"Send data",
  365.                         click: function() {
  366.                             $('[name="presstrends_action"]').val(1);
  367.                             $(this).dialog("close");
  368.                             $('[name="sendPressTrendsData"]').prop("checked", true);
  369.                             $('#tribeSaveSettings').click();
  370.                         }
  371.                 }, {
  372.                         text:"Do not send data",
  373.                         click: function() {
  374.                             $('[name="presstrends_action"]').val(0);
  375.                             $(this).dialog("close");
  376.                             $('[name="sendPressTrendsData"]').prop("checked", false);
  377.                         }
  378.                 }]
  379.             });
  380.  
  381.         }
  382.  
  383. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement