blob: 9868438704eec3b1734c0e2757807263489cea9a [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.
*/import{keyboard as M}from"../keyboard/keyboard";const o=document.querySelector(".JumpDialog"),a=o==null?void 0:o.querySelector(".JumpDialog-body"),i=o==null?void 0:o.querySelector(".JumpDialog-list"),c=o==null?void 0:o.querySelector(".JumpDialog-input"),T=document.querySelector(".js-documentation");let l;function L(){const t=[];if(!!T){for(const e of T.querySelectorAll("[data-kind]"))t.push(w(e));for(const e of t)e.link.addEventListener("click",function(){o==null||o.close()});return t.sort(function(e,n){return e.lower.localeCompare(n.lower)}),t}}function w(t){var f;const e=document.createElement("a"),n=t.getAttribute("id");e.setAttribute("href","#"+n),e.setAttribute("tabindex","-1"),e.setAttribute("data-gtmc","jump to link");const s=t.getAttribute("data-kind");return{link:e,name:n!=null?n:"",kind:s!=null?s:"",lower:(f=n==null?void 0:n.toLowerCase())!=null?f:""}}let v,u=-1;function p(t){for(v=t,l||(l=L()),k(-1);i==null?void 0:i.firstChild;)i.firstChild.remove();if(t){const e=t.toLowerCase(),n=[],s=[],f=[],h=(r,d,m)=>r.name.substring(0,d)+"<b>"+r.name.substring(d,m)+"</b>"+r.name.substring(m);for(const r of l!=null?l:[]){const d=r.name.toLowerCase();if(d===e)r.link.innerHTML=h(r,0,r.name.length),n.push(r);else if(d.startsWith(e))r.link.innerHTML=h(r,0,t.length),s.push(r);else{const m=d.indexOf(e);m>-1&&(r.link.innerHTML=h(r,m,m+t.length),f.push(r))}}for(const r of n.concat(s).concat(f))i==null||i.appendChild(r.link)}else{if(!l||l.length===0){const e=document.createElement("i");e.innerHTML="There are no symbols on this page.",i==null||i.appendChild(e)}for(const e of l!=null?l:[])e.link.innerHTML=e.name+" <i>"+e.kind+"</i>",i==null||i.appendChild(e.link)}a&&(a.scrollTop=0),(l==null?void 0:l.length)&&i&&i.children.length>0&&k(0)}function k(t){const e=i==null?void 0:i.children;if(!(!e||!a)){if(u>=0&&e[u].classList.remove("JumpDialog-active"),t>=e.length&&(t=e.length-1),t>=0){e[t].classList.add("JumpDialog-active");const n=e[t].offsetTop-e[0].offsetTop,s=n+e[t].clientHeight;n<a.scrollTop?a.scrollTop=n:s>a.scrollTop+a.clientHeight&&(a.scrollTop=s-a.clientHeight)}u=t}}function b(t){if(u<0)return;let e=u+t;e<0&&(e=0),k(e)}function y(){c==null||c.addEventListener("keyup",function(){c.value.toUpperCase()!=v.toUpperCase()&&p(c.value)}),c==null||c.addEventListener("keydown",function(n){const s=38,f=40,h=13;switch(n.which){case s:b(-1),n.preventDefault();break;case f:b(1),n.preventDefault();break;case h:u>=0&&i&&(i.children[u].click(),n.preventDefault());break}});const t=document.querySelector(".ShortcutsDialog");M.on("f","open jump to modal",n=>{(o==null?void 0:o.open)||(t==null?void 0:t.open)||(n.preventDefault(),c&&(c.value=""),o==null||o.showModal(),c==null||c.focus(),p(""))}).on("?","open shortcuts modal",()=>{(o==null?void 0:o.open)||(t==null?void 0:t.open)||t==null||t.showModal()});const e=document.querySelector(".js-jumpToInput");e&&e.addEventListener("click",()=>{c&&(c.value=""),p("")})}export{y as initModals};
//# sourceMappingURL=jump.js.map