From 47f52a308901dd2388e3e2b48aeee4861f9bdaf8 Mon Sep 17 00:00:00 2001
From: M Parker <mparker17@536298.no-reply.drupal.org>
Date: Sat, 7 May 2016 09:57:14 -0400
Subject: [PATCH] 1526020-39

---
 facetapi.js                       | 22 +++++++++++------
 plugins/facetapi/widget_links.inc | 52 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+), 8 deletions(-)

diff --git a/facetapi.js b/facetapi.js
index 85d941e..3508ccb 100644
--- a/facetapi.js
+++ b/facetapi.js
@@ -9,7 +9,7 @@ Drupal.behaviors.facetapi = {
     if (settings.facetapi) {
       for (var index in settings.facetapi.facets) {
         if (null != settings.facetapi.facets[index].makeCheckboxes) {
-          Drupal.facetapi.makeCheckboxes(settings.facetapi.facets[index].id);
+          Drupal.facetapi.makeCheckboxes(settings.facetapi.facets[index].id, settings.facetapi.facets[index]);
         }
         if (null != settings.facetapi.facets[index].limit) {
           // Applies soft limit to the list.
@@ -74,12 +74,14 @@ Drupal.facetapi.Redirect.prototype.gotoHref = function() {
  * Turns all facet links into checkboxes.
  * Ensures the facet is disabled if a link is clicked.
  */
-Drupal.facetapi.makeCheckboxes = function(facet_id) {
+Drupal.facetapi.makeCheckboxes = function(facet_id, facet_settings) {
   var $facet = $('#' + facet_id),
       $links = $('a.facetapi-checkbox', $facet);
 
   // Find all checkbox facet links and give them a checkbox.
-  $links.once('facetapi-makeCheckbox').each(Drupal.facetapi.makeCheckbox);
+  $links.once('facetapi-makeCheckbox').each(function(index, element) {
+    Drupal.facetapi.makeCheckbox(element, facet_settings);
+  });
   $links.once('facetapi-disableClick').click(function (e) {
     Drupal.facetapi.disableFacet($facet);
   });
@@ -105,8 +107,8 @@ Drupal.facetapi.preventDefault = function (e) {
 /**
  * Replace an unclick link with a checked checkbox.
  */
-Drupal.facetapi.makeCheckbox = function() {
-  var $link = $(this),
+Drupal.facetapi.makeCheckbox = function(element, facet_settings) {
+  var $link = $(element),
       active = $link.hasClass('facetapi-active');
 
   if (!active && !$link.hasClass('facetapi-inactive')) {
@@ -117,7 +119,7 @@ Drupal.facetapi.makeCheckbox = function() {
   // Derive an ID and label for the checkbox based on the associated link.
   // The label is required for accessibility, but it duplicates information
   // in the link itself, so it should only be shown to screen reader users.
-  var id = this.id + '--checkbox',
+  var id = element.id + '--checkbox',
       description = $link.find('.element-invisible').html(),
       label = $('<label class="element-invisible" for="' + id + '">' + description + '</label>'),
       checkbox = $('<input type="checkbox" class="facetapi-checkbox" id="' + id + '" />'),
@@ -132,8 +134,12 @@ Drupal.facetapi.makeCheckbox = function() {
 
   if (active) {
     checkbox.attr('checked', true);
-    // Add the checkbox and label, hide the link.
-    $link.before(label).before(checkbox).hide();
+    $link.before(label).before(checkbox);
+
+    // Hide the link if set to do so.
+    if (!facet_settings.show_active_label) {
+      $link.hide();
+    }
   }
   else {
     $link.before(label).before(checkbox);
diff --git a/plugins/facetapi/widget_links.inc b/plugins/facetapi/widget_links.inc
index ec436ef..fbcc869 100644
--- a/plugins/facetapi/widget_links.inc
+++ b/plugins/facetapi/widget_links.inc
@@ -74,6 +74,19 @@ class FacetapiWidgetLinks extends FacetapiWidget {
   }
 
   /**
+   * Gets the base class array for a facet item.
+   *
+   * Classes that extend FacetapiWidgetLinks will often overide this method to
+   * alter the link displays via CSS without having to touch the render array.
+   *
+   * @return array
+   *   An array of classes.
+   */
+  function getItemClasses() {
+    return array();
+  }
+
+  /**
    * Transforms the render array for use with theme_item_list().
    *
    * The recursion allows this function to act on the various levels of a
@@ -261,4 +274,43 @@ class FacetapiWidgetCheckboxLinks extends FacetapiWidgetLinks {
   public function getItemClasses() {
     return array('facetapi-checkbox');
   }
+
+  /**
+   * {@inheritdoc}
+   */
+  function buildListItems($build) {
+    // Pass some of this facet's settings to the front-end.
+    $this->jsSettings['show_active_label'] = $this->settings->settings['show_active_label'];
+
+    return parent::buildListItems($build);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  function settingsForm(&$form, &$form_state) {
+    parent::settingsForm($form, $form_state);
+
+    // Allow users to choose whether the labels of active checkboxes are hidden.
+    $form['widget']['widget_settings']['links']['facetapi_checkbox_links']['show_active_label'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Show the labels of active checkboxes'),
+      '#default_value' => !empty($this->settings->settings['show_active_label']),
+      '#description' => t('Displays an active item as a checkbox followed by a link.'),
+      '#states' => array(
+        'visible' => array(
+          ':input[name="widget"]' => array('value' => 'facetapi_checkbox_links'),
+        ),
+      ),
+    );
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  function getDefaultSettings() {
+    return parent::getDefaultSettings() + array(
+      'show_active_label' => 0,
+    );
+  }
 }
-- 
2.8.0

