<script>
'use strict';
/* ------------------------------------
アンド検索 v1.0.0
------------------------------------ */
document.addEventListener('DOMContentLoaded', function() {
/* チェックタグを囲む設定 */
function replaceTagWithCheckbox(tag, index) {
if (!tag.closest('.js-andSearch_row')) {
/* divを作成 */
const wrapperDiv = document.createElement('div');
wrapperDiv.classList.add('checkTag-wrap');
/* checkbox inputを作成 */
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = `checkbox-${index + 1}`;
checkbox.name = 'checkTag';
/* labelを作成 */
const label = document.createElement('label');
label.htmlFor = checkbox.id;
label.textContent = tag.textContent;
/* divで囲む */
wrapperDiv.appendChild(checkbox);
wrapperDiv.appendChild(label);
tag.parentNode.replaceChild(wrapperDiv, tag);
}
};
/* DOM変更を監視するMutationObserverの設定 */
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
/* 新しく追加されたノードが`.tag`クラスを持つか確認 */
if (node.nodeType === 1 && node.classList.contains('tag')) {
replaceTagWithCheckbox(node, document.querySelectorAll(
'.checkTag-wrap').length);
}
});
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
setTimeout(() => {
/*------------- 変数定義 START -------------*/
/* 絞り込み検索用ボタン */
const searchBtn = document.querySelector('.js-andSearch_btn a');
/* 絞り込み対象のカラム */
const searchCols = document.querySelectorAll('.js-andSearch_row .col');
/* 検索結果の該当要素を格納する用のrow */
const searchResultsRow = document.querySelector('.js-searchResults_row');
/* チェックが入ったタグを入れる配列 */
let checkTagTextLists = [];
/*------------- 変数定義 END -------------*/
/* チェックタグに上記のdivで囲む */
document.querySelectorAll('.js-checkTag_list .tag').forEach((tag, index) => {
if (tag.closest('.js-checkTag_list')) {
replaceTagWithCheckbox(tag, index);
}
});
/* inputタグを取得 */
document.getElementsByName('checkTag').forEach(checkTag => {
checkTag.addEventListener('change', function() {
const tagText = this.nextElementSibling.textContent;
if (this.checked) {
/* チェックが入った場合は配列へ追加 */
if (!checkTagTextLists.includes(tagText)) {
checkTagTextLists.push(tagText);
}
} else {
/* チェックが外れた場合は配列から削除 */
const index = checkTagTextLists.indexOf(tagText);
if (index !== -1) {
checkTagTextLists.splice(index, 1);
}
}
});
});
/* 絞り込むボタンクリック後の処理 */
searchBtn.addEventListener('click', function(e) {
e.preventDefault();
/* ちらつき防止用の非表示クラス削除 */
searchResultsRow.classList.remove('js-displayNone');
/* 既存の検索結果を削除する */
document.querySelectorAll('.search-result-item').forEach(item => item.remove());
searchCols.forEach(searchCol => {
/* 各カラム内のタグを取得 */
let searchTagTextLists = Array.from(searchCol.querySelectorAll(
'.tag')).map(tag => tag.textContent);
/* 該当するタグが一致したカラムを複製して格納用rowに入れる */
if (checkTagTextLists.every(tag => searchTagTextLists.includes(tag))) {
let result = searchCol.cloneNode(true);
result.classList.add("search-result-item");
searchResultsRow.appendChild(result);
}
});
/*検索結果の有無の表示*/
const noResultText = document.querySelector('.js-searchNone');
if (searchResultsRow.querySelectorAll('.search-result-item').length === 0) {
/*検索結果がない場合*/
noResultText.style.display = '';
searchResultsRow.classList.add('js-searchNone_row');
} else {
/*検索結果がある場合*/
noResultText.style.display = 'none';
searchResultsRow.classList.remove('js-searchNone_row');
}
});
}, 300); /* 読み込み速度が遅ければ数値上げる */
});
/* ------------------------------------
アンド検索 END
------------------------------------ */
</script>