(function ($, Drupal) {

  'use strict';

  Drupal.behaviors.usgs_group_view = {
    attach: function(context, settings) {
      if (settings.views && settings.views.ajaxViews) {
        var instances = settings.views.ajaxViews;
        $.each(instances, function(view, settings) {
          var view_name = settings.view_name;
          var display_id = settings.view_display_id;

          // Only for  Related Topics Group Vocab view
          if (view_name === 'group_vocab' && display_id === 'related_topics') {
            // Replicate the action of exposed filters value change
            $('.view-group-topic-link').click(function(e) {
              e.preventDefault();
              var valClicked = $(this).data('tid');
              $("select[data-drupal-selector='edit-node-group-topics']").val(valClicked).change();
            });
          }

        });
      }
    }
  };

})(jQuery, Drupal);
