blob: fb8524bb6e444b9fab86c46c26db5b2a5981ad3e [file] [log] [blame]
/*!
* @license
* Copyright 2019-2020 The Go Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/const jumpDialog=document.querySelector(".JumpDialog"),jumpBody=jumpDialog?.querySelector(".JumpDialog-body"),jumpList=jumpDialog?.querySelector(".JumpDialog-list"),jumpFilter=jumpDialog?.querySelector(".JumpDialog-input"),searchInput=document.querySelector(".js-searchFocus"),doc=document.querySelector(".js-documentation");function loadPolyfill(){return import(`${window.location.origin}/third_party/dialog-polyfill/dialog-polyfill.esm.js`)}jumpDialog&&!jumpDialog.showModal&&loadPolyfill().then(({default:e})=>{e.registerDialog(jumpDialog)});let jumpListItems;function collectJumpListItems(){const e=[];if(!!doc){for(const t of doc.querySelectorAll("[data-kind]"))e.push(newJumpListItem(t));for(const t of e)t.link.addEventListener("click",function(){jumpDialog?.close()});return e.sort(function(t,n){return t.lower.localeCompare(n.lower)}),e}}function newJumpListItem(e){const t=document.createElement("a"),n=e.getAttribute("id");t.setAttribute("href","#"+n),t.setAttribute("tabindex","-1");const o=e.getAttribute("data-kind");return{link:t,name:n??"",kind:o??"",lower:n?.toLowerCase()??""}}let lastFilterValue,activeJumpItem=-1;function updateJumpList(e){for(lastFilterValue=e,jumpListItems||(jumpListItems=collectJumpListItems()),setActiveJumpItem(-1);jumpList?.firstChild;)jumpList.firstChild.remove();if(e){const t=e.toLowerCase(),n=[],o=[],c=[],l=(i,r,s)=>i.name.substring(0,r)+"<b>"+i.name.substring(r,s)+"</b>"+i.name.substring(s);for(const i of jumpListItems??[]){const r=i.name.toLowerCase();if(r===t)i.link.innerHTML=l(i,0,i.name.length),n.push(i);else if(r.startsWith(t))i.link.innerHTML=l(i,0,e.length),o.push(i);else{const s=r.indexOf(t);s>-1&&(i.link.innerHTML=l(i,s,s+e.length),c.push(i))}}for(const i of n.concat(o).concat(c))jumpList?.appendChild(i.link)}else for(const t of jumpListItems??[])t.link.innerHTML=t.name+" <i>"+t.kind+"</i>",jumpList?.appendChild(t.link);jumpBody&&(jumpBody.scrollTop=0),jumpList&&jumpList.children.length>0&&setActiveJumpItem(0)}function setActiveJumpItem(e){const t=jumpList?.children;if(!(!t||!jumpBody)){if(activeJumpItem>=0&&t[activeJumpItem].classList.remove("JumpDialog-active"),e>=t.length&&(e=t.length-1),e>=0){t[e].classList.add("JumpDialog-active");const n=t[e].offsetTop-t[0].offsetTop,o=n+t[e].clientHeight;n<jumpBody.scrollTop?jumpBody.scrollTop=n:o>jumpBody.scrollTop+jumpBody.clientHeight&&(jumpBody.scrollTop=o-jumpBody.clientHeight)}activeJumpItem=e}}function incActiveJumpItem(e){if(activeJumpItem<0)return;let t=activeJumpItem+e;t<0&&(t=0),setActiveJumpItem(t)}jumpFilter?.addEventListener("keyup",function(){jumpFilter.value.toUpperCase()!=lastFilterValue.toUpperCase()&&updateJumpList(jumpFilter.value)}),jumpFilter?.addEventListener("keydown",function(e){const t=38,n=40,o=13;switch(e.which){case t:incActiveJumpItem(-1),e.preventDefault();break;case n:incActiveJumpItem(1),e.preventDefault();break;case o:activeJumpItem>=0&&jumpList&&jumpList.children[activeJumpItem].click();break}});const shortcutsDialog=document.querySelector(".ShortcutsDialog");shortcutsDialog&&!shortcutsDialog.showModal&&loadPolyfill().then(({default:e})=>{e.registerDialog(shortcutsDialog)}),document.addEventListener("keypress",function(e){if(jumpDialog?.open||shortcutsDialog?.open||!doc)return;const t=e.target,n=t?.tagName;if(n=="INPUT"||n=="SELECT"||n=="TEXTAREA"||t?.contentEditable=="true"||e.metaKey||e.ctrlKey)return;switch(String.fromCharCode(e.which)){case"f":case"F":e.preventDefault(),jumpFilter&&(jumpFilter.value=""),jumpDialog?.showModal(),updateJumpList("");break;case"?":shortcutsDialog?.showModal();break;case"/":searchInput&&!window.navigator.userAgent.includes("Firefox")&&(e.preventDefault(),searchInput.focus());break}});const jumpOutlineInput=document.querySelector(".js-jumpToInput");jumpOutlineInput&&jumpOutlineInput.addEventListener("click",()=>{jumpFilter&&(jumpFilter.value=""),jumpDialog?.showModal(),updateJumpList("")});
//# sourceMappingURL=jump.js.map