diff --git a/js/dropdown-widget.js b/js/dropdown-widget.js index 2c8fc60..9f75dee 100644 --- a/js/dropdown-widget.js +++ b/js/dropdown-widget.js @@ -7,7 +7,6 @@ 'use strict'; - var $dropdown = ''; + var has_active = false; + $links.each(function () { + var $link = $(this); + var active = $link.hasClass('is-active'); + var $option = $('') + .attr("value", $link.attr('href')); + if(active){ + has_active = true; + // Set empty text value to this link to unselect facet. + $default_option.attr("value", $link.attr('href')); - $('.js-facets-dropdown-links .item-list').html($dropdown); + $option.attr('selected', 'selected'); + $link.find('.facet-deactivate').remove() + } + $option.html($link.html()); + $dropdown.append($option); + }); - // Go to the selected option when it's clicked. - $('.facets-dropdown').on('change', function () { - window.location.href = this.value; - }); - }; + // Go to the selected option when it's clicked. + $dropdown.on('change', function () { + window.location.href = $(this).val(); + }); - /** - * Create an option for the dropdown list and append it to it. - */ - Drupal.facets.makeOption = function () { - var $link = $(this)[0]; - var href = $($link.innerHTML)[0].href; - var text = $link.innerText.replace('(-) ', ''); + // Append empty text option. + if(!has_active){ + $default_option.attr('selected', 'selected'); + } + $dropdown.prepend($default_option); - $dropdown += ''; + // Append dropdown. + $(this).find('.content').html($dropdown); + }); }; })(jQuery); diff --git a/src/Plugin/facets/widget/DropdownWidget.php b/src/Plugin/facets/widget/DropdownWidget.php index 0e48bdc..fb53ba8 100644 --- a/src/Plugin/facets/widget/DropdownWidget.php +++ b/src/Plugin/facets/widget/DropdownWidget.php @@ -51,7 +51,7 @@ class DropdownWidget extends LinksWidget{ $build = [ '#theme' => 'item_list', '#items' => $items, - '#attributes' => ['class' => ['js-facets-dropdown-links']], + '#attributes' => ['class' => ['js-facets-dropdown-links'], 'data-facet-default-option-label' => $configuration['default_option_label']], '#cache' => [ 'contexts' => [ 'url.path', @@ -64,4 +64,36 @@ class DropdownWidget extends LinksWidget{ return $build; } + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state, $config) { + $message = $this->t('This widget requires "Make sure only one result can be shown." to be enabled to behave as a standard dropdown.'); + $form['warning'] = [ + '#markup' => '
', + ]; + + $form['show_numbers'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Show the amount of results'), + ]; + + $form['default_option_label'] = [ + '#type' => 'textfield', + '#title' => $this->t('Default option label'), + ]; + + if (!is_null($config)) { + $widget_configs = $config->get('widget_configs'); + if (isset($widget_configs['show_numbers'])) { + $form['show_numbers']['#default_value'] = $widget_configs['show_numbers']; + } + if (isset($widget_configs['default_option_label'])) { + $form['default_option_label']['#default_value'] = $widget_configs['default_option_label']; + } + } + + return $form; + } + }