custom_search-8.x-1.x-dev/js/custom_search.js
js/custom_search.js
(function ($) {
Drupal.behaviors.custom_search = {
attach(context) {
if (
typeof drupalSettings.custom_search === 'undefined' ||
(typeof drupalSettings.custom_search !== 'undefined' &&
!drupalSettings.custom_search.solr)
) {
// Check if the search box is not empty on submit
$('.block-custom-search form', context).on('submit', function () {
const box = $(this).find('input.custom_search-keys');
if (box.prop('value') !== 'undefined' && box.prop('value') === '') {
$(this).find('input.custom_search-keys').addClass('error');
return false;
}
});
$('form#search-form', context).on('submit', function () {
const $this = $(this);
// If basic search is hidden, copy or value to the keys
if (
$this.find('#edit-keys').parents('div.hidden').attr('class') ===
'hidden'
) {
$this
.find('#edit-keys')
.prop('value', $this.find('#edit-or').prop('value'));
$this.find('#edit-or').prop('value', '');
}
return true;
});
}
// Displays Popup.
let $parentForm;
$('input.custom_search-keys', context).on('click focus', function (e) {
$parentForm = $(this).parents('form');
// check if there's something in the popup and displays it
const popup = $parentForm.find('fieldset.custom_search-popup');
if (popup.find('input,select').length && !popup.hasClass('opened')) {
popup.fadeIn().addClass('opened');
}
e.stopPropagation();
});
$(document).on('click focus', function () {
$('fieldset.custom_search-popup').hide().removeClass('opened');
});
const popup = $(
'fieldset.custom_search-popup:not(.custom_search-processed)',
context,
).addClass('custom_search-processed');
popup.click(function (e) {
e.stopPropagation();
});
popup.append(
`<a class="custom_search-popup-close" href="#">${Drupal.t(
'Close',
)}</a>`,
);
$('a.custom_search-popup-close').click(function (e) {
$('fieldset.custom_search-popup.opened').hide().removeClass('opened');
e.preventDefault();
});
// Handle checkboxes
$('.custom-search-selector input:checkbox', context).each(function () {
const el = $(this);
if (el.prop('value') === 'c-all') {
el.change(function () {
$(this)
.parents('.custom-search-selector')
.find('input:checkbox[value!=c-all]')
.attr('checked', false);
});
} else if (el.prop('value').substr(0, 2) === 'c-') {
el.change(function () {
$('.custom-search-selector input:checkbox').each(function () {
if ($(this).prop('value').substr(0, 2) === 'o-') {
$(this).attr('checked', false);
}
});
$(this)
.parents('.custom-search-selector')
.find('input:checkbox[value=c-all]')
.attr('checked', false);
});
} else {
el.change(function () {
$(this)
.parents('.custom-search-selector')
.find(`input:checkbox[value!=${el.prop('value')}]`)
.attr('checked', false);
});
}
});
},
};
})(jQuery);
