Files
cms-backend/Resources/Public/JavaScript/utility/collapse-state-search.js
T

14 lines
3.7 KiB
JavaScript

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
import h from"@typo3/backend/storage/client.js";import f from"@typo3/core/document-service.js";import m from"@typo3/core/event/debounce-event.js";import d from"@typo3/core/event/regular-event.js";import p from"mark.js";import S from"@typo3/backend/utility/dom-helper.js";import y from"~labels/backend.messages";class g{constructor(){this.searchValueSelector=".t3js-collapse-search-term",this.searchValue="",this.markInstances=[],f.ready().then(()=>{if(this.treeContainers=document.querySelectorAll(".t3js-collapse-states-search-tree"),this.treeContainers.length!==0){this.numberOfSearchMatchesContainer=document.querySelectorAll(".t3js-collapse-states-search-numberOfSearchMatches"),this.searchField=document.querySelector(this.searchValueSelector),this.searchForm=this.searchField.closest("form"),this.searchSessionKey=this.searchField.dataset.persistCollapseSearchKey,this.searchValue=h.get(this.searchSessionKey)??"",this.registerEvents();for(let e=0;e<this.treeContainers.length;e++)this.markInstances[e]=new p(this.treeContainers[e]);this.searchValue!==""&&(this.searchField.value=this.searchValue,this.searchField.dispatchEvent(new Event("keyup")),this.searchForm.requestSubmit())}})}registerEvents(){new d("search",()=>{this.searchField.value===""&&this.searchForm.requestSubmit()}).bindTo(this.searchField),new m("input",()=>{this.searchForm.requestSubmit()}).bindTo(this.searchField),new d("submit",e=>{e.preventDefault();for(let t=0;t<this.treeContainers.length;t++)this.filterTree(this.searchField.value,this.treeContainers[t],this.numberOfSearchMatchesContainer[t],this.markInstances[t])}).bindTo(this.searchForm)}filterTree(e,t,s,a){if(e=e.toLowerCase(),a.unmark(),h.set(this.searchSessionKey,e),e.length<3){s.classList.add("hidden");return}const n=new Set,c=[...this.findNodesByIdentifier(e,t),...this.findNodesByValue(e,t),...this.findNodesByComment(e,t),...this.findNodesByConstantSubstitution(e,t)];s.innerText=y.get("collapse_state_search.numberOfSearchMatches",{count:c.length}),s.classList.remove("hidden"),c.forEach(r=>{if(r===null)return;const o=r.parentElement.querySelector('[data-bs-toggle="collapse"]')?.dataset.bsTarget;o!==void 0&&n.add(o.substring(1));const l=S.parents(r,".collapse");for(const i of l)n.add(i.id)});const u=Array.from(t.querySelectorAll(".collapse"));for(const r of u){const o=r.classList.contains("show"),l=r.id;if(n.has(l)){if(!o){const i=document.querySelector('[data-bs-target="#'+l+'"]');i.classList.remove("collapsed"),i.setAttribute("aria-expanded","true"),r.classList.add("show")}}else if(o){const i=document.querySelector('[data-bs-target="#'+l+'"]');i.classList.add("collapsed"),i.setAttribute("aria-expanded","false"),r.classList.remove("show")}}a.mark(e,{element:"span",className:"text-highlight"})}findNodesByIdentifier(e,t){return Array.from(t.querySelectorAll(".treelist-label")).filter(s=>s.textContent.toLowerCase().includes(e))}findNodesByValue(e,t){return Array.from(t.querySelectorAll(".treelist-value")).filter(a=>a.textContent.toLowerCase().includes(e)).map(a=>a.previousElementSibling)}findNodesByComment(e,t){return Array.from(t.querySelectorAll(".treelist-comment")).filter(s=>s.textContent.toLowerCase().includes(e))}findNodesByConstantSubstitution(e,t){return Array.from(t.querySelectorAll(".treelist-constant-substitution")).filter(s=>s.textContent.toLowerCase().includes(e))}}var b=new g;export{b as default};