JAVASCRIPT   102

vps property search js

Guest on 13th July 2022 08:30:43 AM

  1. /* global Modernizr: true */
  2.  
  3. $(document).ready(function () {
  4.     prepare_search_form_validation();
  5.     prepare_datepickers();
  6.     prepare_search_form();
  7.     prepare_property_improvement_links();
  8.     prepare_date_fields();
  9. });
  10.  
  11. // This is only necessary because IE8 & other browsers that don't support HTML5 Forms
  12. function prepare_search_form_validation() {
  13.     if (!Modernizr.input.pattern) {
  14.         // Add a class rule to avoid having to address multiple copies of the field individually
  15.         jQuery.validator.addClassRules('search-range', {
  16.           number: true
  17.         });
  18.  
  19.         // each() over these, as it'll only map validation to the first one otherwise.
  20.         $('.sales-form').each(function () {
  21.             $(this).validate();
  22.         });
  23.     }
  24. }
  25.  
  26. function prepare_datepickers() {
  27.     $('.datepicker').each(function (index, value) {
  28.         var $this = $(this);
  29.         var options = {
  30.             format: 'mm/dd/yyyy',
  31.             yearSelector: true,
  32.             monthSelector: true
  33.         };
  34.  
  35.         if ($this.data('min-date') !== undefined) {
  36.             options.dateMin = $this.data('min-date').split(',').map(function (value) {
  37.                 return parseInt(value, 10);
  38.             });
  39.         }
  40.  
  41.         if ($this.data('max-date') !== undefined) {
  42.             options.dateMax = $this.data('max-date').split(',').map(function (value) {
  43.                 return parseInt(value, 10);
  44.             });
  45.         }
  46.  
  47.         $this.pickadate(options);
  48.     });
  49. }
  50.  
  51. function prepare_search_form() {
  52.     $('[name="psfldPtype"]').change(function (e) {
  53.         var property_type = this.options[this.selectedIndex].getAttribute('data-property-type');
  54.         toggle_sales_search_fieldsets(property_type);
  55.     });
  56. }
  57.  
  58. function toggle_sales_search_fieldsets(show) {
  59.     var res = $('[name="residentialSalesSearch"]');
  60.     var com = $('[name="commercialSalesSearch"]');
  61.  
  62.     if (show == 'com') {
  63.         com.show();
  64.         res.hide();
  65.     } else if (show == 'res') {
  66.         res.show();
  67.         com.hide();
  68.     } else {
  69.         res.hide();
  70.         com.hide();
  71.     }
  72. }
  73.  
  74. // Attach AJAX click handler for property improvement accordion headers
  75. function prepare_property_improvement_links() {
  76.     $('#improvements .accordion-toggle').one('click', function (e) {
  77.         e.preventDefault();
  78.         $(this).find('.improvement-icon i').removeClass('icon-chevron-down').addClass('icon-spinner icon-spin');
  79.  
  80.         get_property_improvement($('#lrsn').val(), $(this).attr('data-improvement-id'), $($(this).attr('href') + ' .accordion-inner'),
  81.             function (data, $results_container) {
  82.                 show_property_improvement(data, $results_container);
  83.             }
  84.         );
  85.  
  86.         return false;
  87.     });
  88. }
  89.  
  90. // Takes loaded property improvement and inserts it into DOM
  91. function show_property_improvement(data, $results_container) {
  92.     $('<div/>')
  93.         .html(data)
  94.         .appendTo($results_container)
  95.         .parents('.accordion-body')
  96.         .collapse();
  97.     $('.improvement-icon i')
  98.         .removeClass('icon-spinner icon-spin')
  99.         .addClass('icon-chevron-down');
  100. }
  101.  
  102. // Fetch HTML for property improvements
  103. function get_property_improvement(lrsn, improvement_id, $results_container, callback) {
  104.     $.get(
  105.         vps.ajaxurl,
  106.         {
  107.             'lrsn': lrsn,
  108.             'action': 'vps-get-improvement',
  109.             'improvement_id': improvement_id
  110.         },
  111.         function (data) {
  112.             callback(data, $results_container);
  113.         },
  114.         'html'
  115.     );
  116. }
  117.  
  118. function getDaysInMonth(year, month) {
  119.     return new Date(year, month, 0).getDate();
  120. }
  121.  
  122. function prepare_date_fields() {
  123.     var from_year_selector = 'select[name="psfldFromYear"]';
  124.     var from_month_selector = 'select[name="psfldFromMonth"]';
  125.     var to_year_selector = 'select[name="psfldToYear"]';
  126.     var to_month_selector = 'select[name="psfldToMonth"]';
  127.  
  128.     var from_year = $(from_year_selector).val();
  129.     var from_month = $(from_month_selector).val();
  130.     var to_year = $(to_year_selector).val();
  131.     var to_month = $(to_month_selector).val();
  132.  
  133.     $(document).on('change', from_year_selector, function (e) {
  134.         from_year = $(this).val();
  135.     });
  136.  
  137.     $(document).on('change', to_year_selector, function (e) {
  138.         to_year = $(this).val();
  139.     });
  140.  
  141.     $(document).on('change', from_month_selector, function (e) {
  142.         from_month = $(this).val();
  143.         var daysInMonth = getDaysInMonth(from_year, from_month);
  144.         $(this).closest('form').find('select[name="psfldFromDay"] option').each(function (i) {
  145.             $(this).prop('disabled', this.value > daysInMonth);
  146.             if (this.selected && this.value > daysInMonth) {
  147.                 $(this).closest('form').find('select[name="psfldFromDay"] option[value="' + daysInMonth + '"]').prop('selected', true);
  148.             }
  149.         });
  150.     });
  151.  
  152.     $(document).on('change', to_month_selector, function (e) {
  153.         to_month = $(this).val();
  154.         var daysInMonth = getDaysInMonth(to_year, to_month);
  155.         $(this).closest('form').find('select[name="psfldToDay"] option').each(function (i) {
  156.             $(this).prop('disabled', this.value > daysInMonth);
  157.             if (this.selected && this.value > daysInMonth) {
  158.                 $(this).closest('form').find('select[name="psfldToDay"] option[value="' + daysInMonth + '"]').prop('selected', true);
  159.             }
  160.         });
  161.     });
  162. }

Raw Paste


Login or Register to edit or fork this paste. It's free.