{"size": 5501, "ext": "js", "max_stars_repo_path": "web/modules/custom/sfgov_doc_html/js/docsearch.js", "max_stars_repo_name": "SFDigitalServices/sfgov", "max_stars_repo_stars_event_min_datetime": "2018-08-14T23:02:51.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-18T04:08:47.000Z", "max_issues_repo_path": "web/modules/custom/sfgov_doc_html/js/docsearch.js", "max_issues_repo_name": "SFDigitalServices/sfgov", "max_issues_repo_issues_event_min_datetime": "2018-05-16T15:47:43.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T21:57:47.000Z", "max_forks_repo_path": "web/modules/custom/sfgov_doc_html/js/docsearch.js", "max_forks_repo_name": "SFDigitalServices/sfgov", "max_forks_repo_forks_event_min_datetime": "2018-05-14T23:09:15.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-09T14:33:15.000Z"}
{"max_stars_count": 7, "max_issues_count": 233, "max_forks_count": 10, "avg_line_length": 30.904494382, "max_line_length": 153, "alphanum_fraction": 0.521723323}
(function($, Drupal) {

  Drupal.behaviors.sfgovDocsearch = {
    attach: function (context) {
      const $form = $("form#docsearch", context);
      if (!$form.length) {
        return;
      }

      const $searchTarget = $($form.data('search-target'), context);
      if (!$searchTarget.length) {
        return;
      }

      const $input = $form.find("input[name=\"keywords\"]");
      const $closeButton = $form.find("button");

      const $searchInfo = $form.find('.results-info');
      const $searchIndex = $searchInfo.find('.results-index');
      const $searchNav = $searchInfo.find('.results-nav');
      const $searchResultNext = $searchInfo.find('.results-index-next');
      const $searchResultPrev = $searchInfo.find('.results-index-prev');
      let currentText = '';
      let currentIndex = 1;

      $searchNav.hide();

      $input.on("focus", function () {
        $form.addClass('is-focused');
      }).on("blur", function () {
        $form.removeClass('is-focused');
      });

      $closeButton.on("click", function () {
        $('.report--full').find('mark').contents().unwrap();
        $searchInfo.hide();
        $input.val('');
        reset();
      });

      const $searchTargets = $searchTarget.find("a, p, h2, h3, h5, h5, li").map(function () {
        const $target = $(this);
        return {
          el: $target,
          source: $target.html(),
          text: $target.html()
        };
      });

      function search(event) {
        event.preventDefault();
        const keywords = $input.val().trim().replace(/(\s+)/, "(<[^>]+>)*$1(<[^>]+>)*");
        if (keywords.length <= 1) {
          reset();
          return;
        }

        $searchInfo.show();

        const pattern = new RegExp("(" + keywords + ")", "gi");
        if (currentText.length != 0 && currentText !== keywords) {
          currentIndex = 0;
        }
        $searchTargets.each(function (index, target) {
          let resultText = target.source.replace(pattern, "<mark>$1</mark>");
          resultText.replace(/(<mark>[^<>]*)((<[^>]+>)+)([^<>]*<\/mark>)/, "$1</mark>$2<mark>$4");
          target.el.html(resultText);
        });

        $form.addClass('has-results');
        $form.addClass('has-input');
      }

      function reset() {
        $form.removeClass('has-results');
        $form.removeClass('has-input');
        $searchTargets.each(function (index, target) {
          target.el.html(target.source)
        });
        $searchInfo.hide();
      }

      function numberWithCommas(x) {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
      }

      function airaLabel(currentIndex, total) {
        return Drupal.t('Match @current of @total. Click to get to the previous match.', {'@current': currentIndex, '@total': numberWithCommas(total) });
      }

      function searchStatsText(currentIndex, total) {
        return Drupal.t('@current of @total', {'@current': currentIndex, '@total': numberWithCommas(total) });
      }

      function jumpToMatch(index) {
        const $el = $('mark').eq( (index == 0) ? index : index - 1 );
        $('mark').removeClass('current');
        $el.addClass('current');
        $('html, body').stop().animate({
          scrollTop: $el.offset().top - 100
        }, 500);
      }

      function resultsInfo() {
        const total = $('.report--full').find('mark').length;
        if (total == 0) {
          currentIndex = 0;
          $input.attr('aria-label', Drupal.t('No results. Please refine your keywords'));
          $searchNav.hide();
        }
        else {
          currentIndex = 1;
          if (total > 1) {
            $searchNav.show();
          }
        }
        $searchIndex.html(searchStatsText(currentIndex, total));
        jumpToMatch(currentIndex);
      }

      $searchResultNext.on('click', function(e) {
        e.preventDefault();
        const total = $('.report--full').find('mark').length;
        if (currentIndex == total) {
          currentIndex = 0;
        }
        currentIndex++;
        $(this).attr('aria-label', airaLabel(currentIndex, total));
        $searchIndex.html(searchStatsText(currentIndex, total));
        jumpToMatch(currentIndex);
      });

      $searchResultPrev.on('click', function(e) {
        e.preventDefault();
        const total = $('.report--full').find('mark').length;
        if (currentIndex < 2) {
          currentIndex = total;
        }
        else {
          currentIndex--;
        }
        $(this).attr('aria-label', airaLabel(currentIndex, total));
        $searchIndex.html(searchStatsText(currentIndex, total));
        jumpToMatch(currentIndex);
      });

      $input.on("keyup", function (event) {
        if (event.key == 'Escape') {
          $closeButton.trigger('click');
        }
        const keyword = $(this).val();
        if (currentText.length != 0 && currentText === keyword) {
          if (event.key == 'Enter' || event.key == 'ArrowDown') {
            $searchResultNext.trigger('click');
          }
          else if (event.key == 'ArrowUp') {
            $searchResultPrev.trigger('click');
          }
          $form.addClass('has-input');
        }
        else {
          $form.removeClass('has-input');
          search(event);
          resultsInfo();
          currentText = keyword;
        }
      })

      $form.on('submit', function (event) {
        event.preventDefault();
        //search(event);
        return false;
      });
    }
  };

}(jQuery, Drupal));
