ExtSearch/assets/out/src/js/apex/d3extsearch.min.js

14 lines
8.9 KiB
JavaScript

/**
* This Software is the property of Data Development and is protected
* by copyright law - it is NOT Freeware.
* Any unauthorized use of this software without a valid license
* is a violation of the license agreement and will be prosecuted by
* civil and criminal law.
* https://www.d3data.de
*
* @license
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
*/
let d3ExtsearchSuggest=function(){"use strict";return function(e){let t={inputFieldId:"searchParam",currentEvent:null,oAjaxResponseElement:null,isSend:0,coloredId:null,sWaitMessage:"",oldColoredId:null,iActLine:0,iCode:null,blNavigate:null,iRet:null,sSelection:null,oSelection:null,iDelay:600,iMinCharCount:3,sD3SearchBoxDefault:"",sParentThemeId:"apex",sRequestUrl:null,sSearchFormId:"searchForm",sCloseBtnId:"d3extsearch_suggest_closebtn",sResultListId:"#searchItemList",sResultItemClass:".d3QSItem",sStartSearchButtonId:"d3extsearch_suggest_startsearch",sResponseElementId:"xajax_resp",sResponseElementClass:"xajax_resp_cl",sWaitMsgIdentificator:"#d3_extsearch_quicksearch.searchWaitMsg",sRequestFncName:"getSuggestContent",sSearchParamName:"searchParam",sActClassName:"item_act",sInactClassName:"item_inact",sActiveElementClassName:null,blSetActiveElementDimensions:!0,sActiveElementStyleTop:null,sActiveElementStyleTopImportant:"",sActiveElementStyleLeft:null,sActiveElementStyleLeftImportant:"",blAutomatedActiveElementStyleWidth:!1,sActiveElementStyleWidth:null,sActiveElementStyleWidthImportant:"",iScrollTopOffset:29,blEnableLeftRightNavigation:!0,blToggleLeftRightDirection:!1},n=function(){console.debug("add response element");let e=document.querySelector("#"+t.sResponseElementId);t.oAjaxResponseElement=e||function(){let e=document.querySelector("body"),n=document.createElement("div");return n.id=t.sResponseElementId,n.className=t.sResponseElementClass+" xajax_resp_"+t.sParentThemeId,e.append(n),n}()},o=function(){return document.querySelector(t.sResultListId).querySelectorAll("a"+t.sResultItemClass)},l=function(e){let t=null;return o().forEach(function(n,o){o===e&&(t=n.id)}),t},s=function(e,n){if(-1!==n&&n!==e){let e=document.querySelector("#"+n);if(e){let n=e.classList;n.add(t.sInactClassName),n.remove(t.sActClassName)}}if(n!==e){let n=document.querySelector("#"+e);if(n){let e=n.classList;e.add(t.sActClassName),e.remove(t.sInactClassName)}}!function(e){let n=t.currentEvent,o=document.querySelector("#"+e);0===o.length||n.type.toLowerCase().indexOf("mouse")>=0||o.scrollIntoView({behavior:"smooth",block:"end",inline:"nearest"})}(e)},r=function(){console.debug("arrow down key handled");let e=o().length;!1===t.iActLine?t.iActLine=0:t.iActLine=t.iActLine<e-1?t.iActLine+1:e-1,t.coloredId&&(t.oldColoredId=t.coloredId),t.blNavigate=!0,t.coloredId=l(t.iActLine),s(t.coloredId,t.oldColoredId)},i=function(e,n,l){let r=function(e){let n=Array.from(o()).find(t=>t.getAttribute("data-object-type")===e);return t.iActLine=Array.from(o()).indexOf(n),n.getAttribute("id")}(l[Math.min(Math.max(0,e+n),l.length-1)]);null!==r&&(t.coloredId&&(t.oldColoredId=t.coloredId),t.coloredId=r,s(t.coloredId,t.oldColoredId),t.oldColoredId=t.coloredId)},c=function(){let e=[];return o().forEach(function(t){let n=t.getAttribute("data-object-type");n&&n.length?-1===e.indexOf(n)&&e.push(n):console.warn("no data-object-type attributes for grouping found")}),e},a=function(){t.oAjaxResponseElement.classList.remove("suggestVisible")},d=function(){console.debug("enter key without selected item handled"),t.isSend&&(window.clearTimeout(t.isSend),a());let e=document.querySelector("#"+t.sSearchFormId);e.addEventListener("submit",function(){},!1),e.submit()},u=function(e){if(!e.getClientRects().length)return{top:0,left:0};let t=e.getBoundingClientRect(),n=e.ownerDocument.defaultView;return{top:t.top+n.scrollY,left:t.left+n.scrollX}},m=function(){let e,n;!function(){console.debug("set result DOM elements position");let e=document.querySelector("#"+t.inputFieldId);t.sActiveElementClassName&&t.oAjaxResponseElement.addClass(t.sActiveElementClassName),t.blSetActiveElementDimensions&&(t.oAjaxResponseElement.style.setProperty("top",t.sActiveElementStyleTop?t.sActiveElementStyleTop:e.hasAttribute("suggestTopOffsetPx")?u(e).top+(parseInt(getComputedStyle(e).getPropertyValue("height"))+5)+parseInt(e.getAttribute("suggestTopOffsetPx"),10)+"px":u(e).top+(parseInt(getComputedStyle(e).getPropertyValue("height"))+5)+"px",t.sActiveElementStyleTopImportant),t.oAjaxResponseElement.style.setProperty("left",t.sActiveElementStyleLeft?t.sActiveElementStyleLeft:e.hasAttribute("suggestLeftOffsetPx")?u(e).left+parseInt(e.getAttribute("suggestLeftOffsetPx"),10)+"px":u(e).left+"px",t.sActiveElementStyleLeftImportant),t.blAutomatedActiveElementStyleWidth&&t.oAjaxResponseElement.style.setProperty("width",t.sActiveElementStyleWidth?t.sActiveElementStyleWidth:parseInt(getComputedStyle(e).getPropertyValue("width"))+"px",t.sActiveElementStyleWidthImportant))}(),t.oAjaxResponseElement.classList.add("suggestVisible"),t.oAjaxResponseElement.addEventListener("click",e=>{e.stopPropagation()}),(e=document.querySelector("#"+t.inputFieldId)).addEventListener("click",e=>{e.stopPropagation()}),document.querySelector("body").addEventListener("click",()=>{a()}),(n=document.querySelector("#"+t.sCloseBtnId))&&n.addEventListener("click",e=>{e.stopPropagation(),a()})},p=function(){console.debug("set mouseover handler set for every"),o().forEach(function(e,n){var o,r;e.addEventListener("mouseover",e=>{t.currentEvent=e;let i=l(n);o=i,r=n,t.oldColoredId=t.coloredId,t.coloredId=o,t.iActLine=r,t.blNavigate=!0,s(t.coloredId,t.oldColoredId)})})},g=function(){let e=document.querySelector("#"+t.inputFieldId);if(!1===Number.isFinite(t.iMinCharCount)||e.value.length<t.iMinCharCount)return console.debug("insufficient search term length"),void a();let n=t.sRequestUrl+"fnc="+t.sRequestFncName+"&"+t.sSearchParamName+"="+e.value;const o=new Request(n.replace("&amp;","&"));fetch(o).then(function(n){console.debug("successfully requested"),n.json().then(n=>{console.debug("successfully parsed"),e.value===n.searchparam?(t.oAjaxResponseElement.innerHTML=n.content,p(),m(),function(){let e=document.querySelector("#"+t.sStartSearchButtonId);e&&e.addEventListener("click",e=>{e.preventDefault(),e.stopPropagation(),d()})}()):console.debug("outdated response")}).catch(e=>console.error(e))}).catch(e=>console.error(e)),t.iActLine=!1,t.coloredId=!1,t.oldColoredId=!1,t.blNavigate=!1},f=function(){"enter"!==t.currentEvent.key.toLowerCase()&&(function(){let e,n=document.createElement("div");n.innerHTML=t.sWaitMessage,e=n.textContent,t.oAjaxResponseElement.innerHTML=e,m()}(),t.isSend&&document.querySelector(t.sWaitMsgIdentificator)&&window.clearTimeout(t.isSend),t.isSend=setTimeout(function(){g()}.bind(this),t.iDelay))},h=function(e){e.preventDefault(),t.currentEvent=e;let n="submit"===e.type.toLowerCase(),o=n?"Enter":e.key;var u;n||"enter"===o.toLowerCase()||(u=t.oAjaxResponseElement,"none"!==window.getComputedStyle(u).display)?"arrowup"===o.toLowerCase()?(console.debug("arrow up key handled"),t.iActLine=t.iActLine>0?t.iActLine-1:0,t.coloredId&&(t.oldColoredId=t.coloredId),t.blNavigate=!0,t.coloredId=l(t.iActLine),s(t.coloredId,t.oldColoredId)):"arrowleft"!==o.toLowerCase()&&"pageup"!==o.toLowerCase()||!t.blEnableLeftRightNavigation?"arrowright"!==o.toLowerCase()&&"pagedown"!==o.toLowerCase()||!t.blEnableLeftRightNavigation?"arrowdown"===o.toLowerCase()?r():"enter"===o.toLowerCase()?t.blNavigate?function(){console.debug("enter key on selected item handled");let e=document.querySelector("#"+l(t.iActLine));window.location.href=e.getAttribute("href")}():d():"escape"===o.toLowerCase()?(console.debug("escape key handled"),a()):function(){let e=t.currentEvent,n=e.originalEvent?e.originalEvent.key:"";"ArrowLeft"!==n&&"ArrowRight"!==n&&f()}():function(){console.debug("arrow right key handled");let e=null,n=null,o=c();if(t.iActLine<0)e=0,n=0;else if(l(t.iActLine)){let s=document.querySelector("#"+l(t.iActLine)).getAttribute("data-object-type");s&&s.length&&o.length>1?(e=o.indexOf(s),n=t.blToggleLeftRightDirection?-1:1):console.warn("selected item has no data-object-type attribute, can not switch to next group")}null!==e&&i(e,n,o)}():function(){console.debug("arrow left key handled");let e=null,n=null,o=c();if(t.iActLine<0)e=0,n=0;else{let s=document.querySelector("#"+l(t.iActLine)).getAttribute("data-object-type");s&&s.length&&o.length>1?(e=o.indexOf(s),n=t.blToggleLeftRightDirection?1:-1):console.warn("selected item has no data-object-type attribute, can not switch to next group")}null!==e&&i(e,n,o)}():f()};!function(e){console.debug("initialized"),t={...t,...e},n(),function(){if(console.debug("add event listener"),!t.inputFieldId)throw new Error("Error: Please provide a valid input field selector");if(!document.querySelector("#"+t.inputFieldId))throw new Error('Error: extsearch: no DOM element with id "'+t.inputFieldId+'" found');document.querySelector("#"+t.inputFieldId).addEventListener("keyup",e=>h(e))}()}(e)}}();