Guest User

Datepicker.js

a guest
Jan 18th, 2016
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery( function( $ ) {
  2.  
  3.     var wc_bookings_date_picker = {
  4.         init: function() {
  5.             $( 'body' ).on( 'change', '#wc_bookings_field_duration, #wc_bookings_field_resource', this.date_picker_init );
  6.             $( 'body' ).on( 'click', '.wc-bookings-date-picker legend small.wc-bookings-date-picker-choose-date', this.toggle_calendar );
  7.             $( 'body' ).on( 'input', '.booking_date_year, .booking_date_month, .booking_date_day', this.input_date_trigger );
  8.             $( 'body' ).on( 'change', '.booking_to_date_year, .booking_to_date_month, .booking_to_date_day', this.input_date_trigger );
  9.             $( '.wc-bookings-date-picker legend small.wc-bookings-date-picker-choose-date' ).show();
  10.             $( '.wc-bookings-date-picker' ).each( function() {
  11.                 var form     = $( this ).closest( 'form' ),
  12.                     picker   = form.find( '.picker' ),
  13.                     fieldset = $( this ).closest( 'fieldset' );
  14.  
  15.                 wc_bookings_date_picker.date_picker_init( picker );
  16.  
  17.                 if ( picker.data( 'display' ) == 'always_visible' ) {
  18.                     $( '.wc-bookings-date-picker-date-fields', fieldset ).hide();
  19.                     $( '.wc-bookings-date-picker-choose-date', fieldset ).hide();
  20.                 } else {
  21.                     picker.hide();
  22.                 }
  23.  
  24.                 if ( picker.data( 'is_range_picker_enabled' ) ) {
  25.                     form.find( 'p.wc_bookings_field_duration' ).hide();
  26.                     form.find( '.wc_bookings_field_start_date legend span.label' ).text( 'always_visible' !== picker.data( 'display' ) ? booking_form_params.i18n_dates : booking_form_params.i18n_start_date );
  27.                 }
  28.             } );
  29.         },
  30.         calc_duration: function( picker ) {
  31.             var form     = picker.closest('form');
  32.             var fieldset = picker.closest('fieldset');
  33.             setTimeout( function() {
  34.                 var days    = 1;
  35.                 var e_year  = parseInt( fieldset.find( 'input.booking_to_date_year' ).val() );
  36.                 var e_month = parseInt( fieldset.find( 'input.booking_to_date_month' ).val() );
  37.                 var e_day   = parseInt( fieldset.find( 'input.booking_to_date_day' ).val() );
  38.                 var s_year  = parseInt( fieldset.find( 'input.booking_date_year' ).val() );
  39.                 var s_month = parseInt( fieldset.find( 'input.booking_date_month' ).val() );
  40.                 var s_day   = parseInt( fieldset.find( 'input.booking_date_day' ).val() );
  41.  
  42.                 if ( e_year && e_month >= 0 && e_day && s_year && s_month >= 0 && s_day ) {
  43.                     var s_date = new Date( s_year, s_month - 1, s_day );
  44.                     var e_date = new Date( e_year, e_month - 1, e_day );
  45.                     s_date.setUTCHours(0);
  46.                     e_date.setUTCHours(0);
  47.                     days = Math.floor( ( e_date.getTime() - s_date.getTime() ) / ( 1000*60*60*24 ) ) + 1;
  48.                 }
  49.  
  50.                 form.find( '#wc_bookings_field_duration' ).val( days).change();
  51.             } );
  52.  
  53.         },
  54.         toggle_calendar: function() {
  55.             $picker = $( this ).closest( 'fieldset' ).find( '.picker:eq(0)' );
  56.             wc_bookings_date_picker.date_picker_init( $picker );
  57.             $picker.slideToggle();
  58.         },
  59.         input_date_trigger: function() {
  60.             var $fieldset = $(this).closest('fieldset');
  61.             var $picker   = $fieldset.find( '.picker:eq(0)' );
  62.             var $form     = $(this).closest('form');
  63.  
  64.             var year      = parseInt( $fieldset.find( 'input.booking_date_year' ).val(), 10 );
  65.             var month     = parseInt( $fieldset.find( 'input.booking_date_month' ).val(), 10 );
  66.             var day       = parseInt( $fieldset.find( 'input.booking_date_day' ).val(), 10 );
  67.  
  68.             if ( year && month && day ) {
  69.                 var date = new Date( year, month - 1, day );
  70.                 $picker.datepicker( "setDate", date );
  71.  
  72.                 if ( $picker.data( 'is_range_picker_enabled' ) ) {
  73.                     var to_year      = parseInt( $fieldset.find( 'input.booking_to_date_year' ).val(), 10 );
  74.                     var to_month     = parseInt( $fieldset.find( 'input.booking_to_date_month' ).val(), 10 );
  75.                     var to_day       = parseInt( $fieldset.find( 'input.booking_to_date_day' ).val(), 10 );
  76.  
  77.                     var to_date = new Date( to_year, to_month - 1, to_day );
  78.  
  79.                     if ( ! to_date || to_date < date ) {
  80.                         $fieldset.find( 'input.booking_to_date_year' ).val( '' ).addClass( 'error' );
  81.                         $fieldset.find( 'input.booking_to_date_month' ).val( '' ).addClass( 'error' );
  82.                         $fieldset.find( 'input.booking_to_date_day' ).val( '' ).addClass( 'error' );
  83.                     } else {
  84.                         $fieldset.find( 'input' ).removeClass( 'error' );
  85.                         wc_bookings_date_picker.calc_duration( $picker );
  86.                     }
  87.                 }
  88.                 $form.find( '.wc-bookings-booking-form').triggerHandler( 'date-selected', date );
  89.             }
  90.         },
  91.         select_date_trigger: function( date ) {
  92.             var fieldset          = $( this ).closest('fieldset');
  93.             var picker            = fieldset.find( '.picker:eq(0)' );
  94.             var form              = $( this ).closest( 'form' );
  95.             var parsed_date       = date.split( '-' );
  96.             var start_or_end_date = picker.data( 'start_or_end_date' );
  97.  
  98.             if ( ! picker.data( 'is_range_picker_enabled' ) || ! start_or_end_date ) {
  99.                 start_or_end_date = 'start';
  100.             }
  101.  
  102.             // End date selected
  103.             if ( start_or_end_date === 'end' ) {
  104.  
  105.                 // Set min date to default
  106.                 picker.data( 'min_date', picker.data( 'o_min_date' ) );
  107.  
  108.                 // Set fields
  109.                 fieldset.find( 'input.booking_to_date_year' ).val( parsed_date[0] );
  110.                 fieldset.find( 'input.booking_to_date_month' ).val( parsed_date[1] );
  111.                 fieldset.find( 'input.booking_to_date_day' ).val( parsed_date[2] ).change();
  112.  
  113.                 // Calc duration
  114.                 if ( picker.data( 'is_range_picker_enabled' ) ) {
  115.                     wc_bookings_date_picker.calc_duration( picker );
  116.                 }
  117.  
  118.                 // Next click will be start date
  119.                 picker.data( 'start_or_end_date', 'start' );
  120.  
  121.                 if ( picker.data( 'is_range_picker_enabled' ) ) {
  122.                     form.find( '.wc_bookings_field_start_date legend span.label' ).text( 'always_visible' !== picker.data( 'display' ) ? booking_form_params.i18n_dates : booking_form_params.i18n_start_date );
  123.                 }
  124.  
  125.                 if ( 'always_visible' !== picker.data( 'display' ) ) {
  126.                     $( this ).hide();
  127.                 }
  128.  
  129.             // Start date selected
  130.             } else {
  131.                 // Set min date to today
  132.                 if ( picker.data( 'is_range_picker_enabled' ) ) {
  133.                     picker.data( 'o_min_date', picker.data( 'min_date' ) )
  134.                     picker.data( 'min_date', date )
  135.                 }
  136.  
  137.                 // Set fields
  138.                 fieldset.find( 'input.booking_to_date_year' ).val( '' );
  139.                 fieldset.find( 'input.booking_to_date_month' ).val( '' );
  140.                 fieldset.find( 'input.booking_to_date_day' ).val( '' );
  141.  
  142.                 fieldset.find( 'input.booking_date_year' ).val( parsed_date[0] );
  143.                 fieldset.find( 'input.booking_date_month' ).val( parsed_date[1] );
  144.                 fieldset.find( 'input.booking_date_day' ).val( parsed_date[2] ).change();
  145.  
  146.                 // Calc duration
  147.                 if ( picker.data( 'is_range_picker_enabled' ) ) {
  148.                     wc_bookings_date_picker.calc_duration( picker );
  149.                 }
  150.  
  151.                 // Next click will be end date
  152.                 picker.data( 'start_or_end_date', 'end' );
  153.  
  154.                 if ( picker.data( 'is_range_picker_enabled' ) ) {
  155.                     form.find( '.wc_bookings_field_start_date legend span.label' ).text( booking_form_params.i18n_end_date );
  156.                 }
  157.  
  158.                 if ( 'always_visible' !== picker.data( 'display' ) && ! picker.data( 'is_range_picker_enabled' ) ) {
  159.                     $( this ).hide();
  160.                 }
  161.             }
  162.  
  163.             form.find( '.wc-bookings-booking-form' ).triggerHandler( 'date-selected', date, start_or_end_date );
  164.         },
  165.         date_picker_init: function( element ) {
  166.             if ( $( element ).is( '.picker' ) ) {
  167.                 var $picker = $( element );
  168.             } else {
  169.                 var $picker = $( this ).closest('form').find( '.picker:eq(0)' );
  170.             }
  171.  
  172.             $picker.empty().removeClass('hasDatepicker').datepicker({
  173.                 dateFormat: $.datepicker.ISO_8601,
  174.                 showWeek: false,
  175.                 showOn: false,
  176.                 beforeShowDay: wc_bookings_date_picker.is_bookable,
  177.                 onSelect: wc_bookings_date_picker.select_date_trigger,
  178.                 minDate: $picker.data( 'min_date' ),
  179.                 maxDate: $picker.data( 'max_date' ),
  180.                 defaultDate: $picker.data( 'default_date'),
  181.                 numberOfMonths: 1,
  182.                 showButtonPanel: false,
  183.                 showOtherMonths: true,
  184.                 selectOtherMonths: true,
  185.                 closeText: wc_bookings_booking_form.closeText,
  186.                 currentText: wc_bookings_booking_form.currentText,
  187.                 prevText: wc_bookings_booking_form.prevText,
  188.                 nextText: wc_bookings_booking_form.nextText,
  189.                 monthNames: wc_bookings_booking_form.monthNames,
  190.                 monthNamesShort: wc_bookings_booking_form.monthNamesShort,
  191.                 dayNames: wc_bookings_booking_form.dayNames,
  192.                 dayNamesShort: wc_bookings_booking_form.dayNamesShort,
  193.                 dayNamesMin: wc_bookings_booking_form.dayNamesMin,
  194.                 firstDay: wc_bookings_booking_form.firstDay,
  195.                 gotoCurrent: true
  196.             });
  197.  
  198.             $( '.ui-datepicker-current-day' ).removeClass( 'ui-datepicker-current-day' );
  199.  
  200.             var form  = $picker.closest( 'form' );
  201.             var year  = parseInt( form.find( 'input.booking_date_year' ).val(), 10 );
  202.             var month = parseInt( form.find( 'input.booking_date_month' ).val(), 10 );
  203.             var day   = parseInt( form.find( 'input.booking_date_day' ).val(), 10 );
  204.  
  205.             if ( year && month && day ) {
  206.                 var date = new Date( year, month - 1, day );
  207.                 $picker.datepicker( "setDate", date );
  208.             }
  209.         },
  210.         get_input_date: function( fieldset, where ) {
  211.             var year  = fieldset.find( 'input.booking_' + where + 'date_year' ),
  212.                 month = fieldset.find( 'input.booking_' + where + 'date_month' ),
  213.                 day   = fieldset.find( 'input.booking_' + where + 'date_day' );
  214.  
  215.             if ( 0 !== year.val().length && 0 !== month.val().length && 0 !== day.val().length ) {
  216.                 return year.val() + '-' + month.val() + '-' + day.val();
  217.             } else {
  218.                 return '';
  219.             }
  220.         },
  221.         is_bookable: function( date ) {
  222.             var $form                      = $( this ).closest('form');
  223.             var $picker                    = $form.find( '.picker:eq(0)' );
  224.             var availability               = $( this ).data( 'availability' );
  225.             var default_availability       = $( this ).data( 'default-availability' );
  226.             var fully_booked_days          = $( this ).data( 'fully-booked-days' );
  227.             var buffer_days                = $( this ).data( 'buffer-days' );
  228.             var partially_booked_days      = $( this ).data( 'partially-booked-days' );
  229.             var check_availability_against = wc_bookings_booking_form.check_availability_against;
  230.             var css_classes                = '';
  231.             var title                      = '';
  232.  
  233.             // Get selected resource
  234.             if ( $form.find('select#wc_bookings_field_resource').val() > 0 ) {
  235.                 var resource_id = $form.find('select#wc_bookings_field_resource').val();
  236.             } else {
  237.                 var resource_id = 0;
  238.             }
  239.  
  240.             // Get days needed for block - this affects availability
  241.             var duration = wc_bookings_booking_form.booking_duration;
  242.             var the_date = new Date( date );
  243.             var year     = the_date.getFullYear();
  244.             var month    = the_date.getMonth() + 1;
  245.             var day      = the_date.getDate();
  246.  
  247.             // Fully booked?
  248.             if ( fully_booked_days[ year + '-' + month + '-' + day ] ) {
  249.                 if ( fully_booked_days[ year + '-' + month + '-' + day ][0] || fully_booked_days[ year + '-' + month + '-' + day ][ resource_id ] ) {
  250.                     return [ false, 'fully_booked', booking_form_params.i18n_date_fully_booked ];
  251.                 }
  252.             }
  253.  
  254.             // Buffer days?
  255.             if ( 'undefined' !== typeof buffer_days && buffer_days[ year + '-' + month + '-' + day ] ) {
  256.                 return [ false, 'not_bookable', booking_form_params.i18n_date_unavailable ];
  257.             }
  258.  
  259.             if ( '' + year + month + day < wc_bookings_booking_form.current_time ) {
  260.                 return [ false, 'not_bookable', booking_form_params.i18n_date_unavailable ];
  261.             }
  262.  
  263.             // Partially booked?
  264.             if ( partially_booked_days && partially_booked_days[ year + '-' + month + '-' + day ] ) {
  265.                 if ( partially_booked_days[ year + '-' + month + '-' + day ][0] || partially_booked_days[ year + '-' + month + '-' + day ][ resource_id ] ) {
  266.                     css_classes = css_classes + 'partial_booked ';
  267.                 }
  268.             }
  269.  
  270.             if ( $form.find('#wc_bookings_field_duration').size() > 0 && wc_bookings_booking_form.duration_unit != 'minute' && wc_bookings_booking_form.duration_unit != 'hour' && ! $picker.data( 'is_range_picker_enabled' ) ) {
  271.                 var user_duration = $form.find('#wc_bookings_field_duration').val();
  272.                 var days_needed   = duration * user_duration;
  273.             } else {
  274.                 var days_needed   = duration;
  275.             }
  276.  
  277.             if ( days_needed < 1 || check_availability_against == 'start' ) {
  278.                 days_needed = 1;
  279.             }
  280.  
  281.             var bookable = default_availability;
  282.  
  283.             // Loop all the days we need to check for this block
  284.             for ( var i = 0; i < days_needed; i++ ) {
  285.                 var the_date     = new Date( date );
  286.                 the_date.setDate( the_date.getDate() + i );
  287.  
  288.                 var year        = the_date.getFullYear();
  289.                 var month       = the_date.getMonth() + 1;
  290.                 var day         = the_date.getDate();
  291.                 var day_of_week = the_date.getDay();
  292.                 var week        = $.datepicker.iso8601Week( the_date );
  293.  
  294.                 // Reset bookable for each day being checked
  295.                 bookable = default_availability;
  296.  
  297.                 // Sunday is 0, Monday is 1, and so on.
  298.                 if ( day_of_week == 0 ) {
  299.                     day_of_week = 7;
  300.                 }
  301.  
  302.                 $.each( availability[ resource_id ], function( index, rule ) {
  303.                     var type  = rule[0];
  304.                     var rules = rule[1];
  305.                     try {
  306.                         switch ( type ) {
  307.                             case 'months':
  308.                                 if ( typeof rules[ month ] != 'undefined' ) {
  309.                                     bookable = rules[ month ];
  310.  
  311.                                     return false;
  312.                                 }
  313.                             break;
  314.                             case 'weeks':
  315.                                 if ( typeof rules[ week ] != 'undefined' ) {
  316.                                     bookable = rules[ week ];
  317.  
  318.                                     return false;
  319.                                 }
  320.                             break;
  321.                             case 'days':
  322.                                 if ( typeof rules[ day_of_week ] != 'undefined' ) {
  323.                                     bookable = rules[ day_of_week ];
  324.  
  325.                                     return false;
  326.                                 }
  327.                             break;
  328.                             case 'custom':
  329.                                 if ( typeof rules[ year ][ month ][ day ] != 'undefined' ) {
  330.                                     bookable = rules[ year ][ month ][ day ];
  331.  
  332.                                     return false;
  333.                                 }
  334.                             break;
  335.                         }
  336.  
  337.                     } catch( err ) {}
  338.  
  339.                     return true;
  340.                 });
  341.  
  342.                 // Fully booked in entire block?
  343.                 if ( fully_booked_days[ year + '-' + month + '-' + day ] ) {
  344.                     if ( fully_booked_days[ year + '-' + month + '-' + day ][0] || fully_booked_days[ year + '-' + month + '-' + day ][ resource_id ] ) {
  345.                         bookable = false;
  346.                     }
  347.                 }
  348.  
  349.                 if ( ! bookable ) {
  350.                     break;
  351.                 }
  352.             }
  353.  
  354.             if ( ! bookable ) {
  355.                 return [ bookable, 'not_bookable', booking_form_params.i18n_date_unavailable ];
  356.             } else {
  357.  
  358.                 if ( css_classes.indexOf( 'partial_booked' ) > -1 ) {
  359.                     title = booking_form_params.i18n_date_partially_booked;
  360.                 } else {
  361.                     title = booking_form_params.i18n_date_available;
  362.                 }
  363.  
  364.                 if ( $picker.data( 'is_range_picker_enabled' ) ) {
  365.                     var fieldset     = $(this).closest( 'fieldset' ),
  366.                         start_date   = $.datepicker.parseDate( $.datepicker.ISO_8601, wc_bookings_date_picker.get_input_date( fieldset, '' ) ),
  367.                         end_date     = $.datepicker.parseDate( $.datepicker.ISO_8601, wc_bookings_date_picker.get_input_date( fieldset, 'to_' ) );
  368.  
  369.                     return [ bookable, start_date && ( ( date.getTime() === start_date.getTime() ) || ( end_date && date >= start_date && date <= end_date ) ) ? css_classes + 'bookable-range' : css_classes + 'bookable', title ];
  370.                 } else {
  371.                     return [ bookable, css_classes + 'bookable', title ];
  372.                 }
  373.             }
  374.         }
  375.     };
  376.  
  377.     wc_bookings_date_picker.init();
  378. });
Advertisement
Add Comment
Please, Sign In to add comment