blob: 9c97010044c89990d9824313568814bf98d158b3 [file] [log] [blame]
var o={PLAY_HREF:".js-exampleHref",PLAY_CONTAINER:".js-exampleContainer",EXAMPLE_INPUT:".Documentation-exampleCode",EXAMPLE_OUTPUT:".Documentation-exampleOutput",EXAMPLE_ERROR:".Documentation-exampleError",PLAY_BUTTON:".Documentation-examplePlayButton",SHARE_BUTTON:".Documentation-exampleShareButton",FORMAT_BUTTON:".Documentation-exampleFormatButton",RUN_BUTTON:".Documentation-exampleRunButton"},T=class{constructor(e){this.exampleEl=e;this.exampleEl=e,this.anchorEl=e.querySelector("a"),this.errorEl=e.querySelector(o.EXAMPLE_ERROR),this.playButtonEl=e.querySelector(o.PLAY_BUTTON),this.shareButtonEl=e.querySelector(o.SHARE_BUTTON),this.formatButtonEl=e.querySelector(o.FORMAT_BUTTON),this.runButtonEl=e.querySelector(o.RUN_BUTTON),this.inputEl=this.makeTextArea(e.querySelector(o.EXAMPLE_INPUT)),this.outputEl=e.querySelector(o.EXAMPLE_OUTPUT),this.playButtonEl?.addEventListener("click",()=>this.handleShareButtonClick()),this.shareButtonEl?.addEventListener("click",()=>this.handleShareButtonClick()),this.formatButtonEl?.addEventListener("click",()=>this.handleFormatButtonClick()),this.runButtonEl?.addEventListener("click",()=>this.handleRunButtonClick()),this.inputEl&&(this.resize(),this.inputEl.addEventListener("keyup",()=>this.resize()),this.inputEl.addEventListener("keydown",t=>this.onKeydown(t)))}makeTextArea(e){let t=document.createElement("textarea");return t.classList.add("Documentation-exampleCode","code"),t.spellcheck=!1,t.value=e?.textContent??"",e?.parentElement?.replaceChild(t,e),t}getAnchorHash(){return this.anchorEl?.hash}expand(){this.exampleEl.open=!0}resize(){if(this.inputEl?.value){let e=(this.inputEl.value.match(/\n/g)||[]).length;this.inputEl.style.height=`${(20+e*20+12+2)/16}rem`}}onKeydown(e){e.key==="Tab"&&(document.execCommand("insertText",!1," "),e.preventDefault())}setInputText(e){this.inputEl&&(this.inputEl.value=e)}setOutputText(e){this.outputEl&&(this.outputEl.textContent=e)}appendToOutputText(e){this.outputEl&&(this.outputEl.textContent+=e)}setOutputHTML(e){this.outputEl&&(this.outputEl.innerHTML=e)}setErrorText(e){this.errorEl&&(this.errorEl.textContent=e),this.setOutputText("An error has occurred\u2026")}getCodeWithModFile(){let e=this.inputEl?.value??"",t=document.querySelector(".js-playgroundVars")?.dataset??{};return t.modulepath!=="std"&&(e=e.concat(`
-- go.mod --
module play.ground
require ${t.modulepath} ${t.version}
`)),e}handleShareButtonClick(){let e="https://play.golang.org/p/";this.setOutputText("Waiting for remote server\u2026"),fetch("/play/share",{method:"POST",body:this.getCodeWithModFile()}).then(t=>t.text()).then(t=>{let i=e+t;this.setOutputHTML(`<a href="${i}">${i}</a>`),window.open(i)}).catch(t=>{this.setErrorText(t)})}handleFormatButtonClick(){this.setOutputText("Waiting for remote server\u2026");let e=new FormData;e.append("body",this.inputEl?.value??""),fetch("/play/fmt",{method:"POST",body:e}).then(t=>t.json()).then(({Body:t,Error:i})=>{this.setOutputText(i||"Done."),t&&(this.setInputText(t),this.resize())}).catch(t=>{this.setErrorText(t)})}handleRunButtonClick(){this.setOutputText("Waiting for remote server\u2026"),fetch("/play/compile",{method:"POST",body:JSON.stringify({body:this.getCodeWithModFile(),version:2})}).then(e=>e.json()).then(async({Events:e,Errors:t})=>{this.setOutputText(t||"");for(let i of e||[])this.appendToOutputText(i.Message),await new Promise(s=>setTimeout(s,i.Delay/1e6))}).catch(e=>{this.setErrorText(e)})}};function v(){let l=location.hash.match(/^#(example-.*)$/);if(l){let i=document.getElementById(l[1]);i&&(i.open=!0)}let e=[...document.querySelectorAll(o.PLAY_HREF)],t=i=>e.find(s=>s.hash===i.getAnchorHash());for(let i of document.querySelectorAll(o.PLAY_CONTAINER)){let s=new T(i),r=t(s);r?r.addEventListener("click",()=>{s.expand()}):console.warn("example href not found")}}var c=class{constructor(e){this.el=e;this.el.addEventListener("change",t=>{let i=t.target,s=i.value;i.value.startsWith("/")||(s="/"+s),window.location.href=s})}};function x(l){let e=document.createElement("label");e.classList.add("go-Label"),e.setAttribute("aria-label","Menu");let t=document.createElement("select");t.classList.add("go-Select","js-selectNav"),e.appendChild(t);let i=document.createElement("optgroup");i.label="Outline",t.appendChild(i);let s={},r;for(let a of l.treeitems){if(Number(a.depth)>4)continue;a.groupTreeitem?(r=s[a.groupTreeitem.label],r||(r=s[a.groupTreeitem.label]=document.createElement("optgroup"),r.label=a.groupTreeitem.label,t.appendChild(r))):r=i;let n=document.createElement("option");n.label=a.label,n.textContent=a.label,n.value=a.el.href.replace(window.location.origin,"").replace("/",""),r.appendChild(n)}return l.addObserver(a=>{let n=a.el.hash,g=t.querySelector(`[value$="${n}"]`)?.value;g&&(t.value=g)},50),e}var m=class{constructor(e){this.el=e;this.handleResize=()=>{this.el.style.setProperty("--js-tree-height","100vh"),this.el.style.setProperty("--js-tree-height",this.el.clientHeight+"px")};this.treeitems=[],this.firstChars=[],this.firstTreeitem=null,this.lastTreeitem=null,this.observerCallbacks=[],this.init()}init(){this.handleResize(),window.addEventListener("resize",this.handleResize),this.findTreeItems(),this.updateVisibleTreeitems(),this.observeTargets(),this.firstTreeitem&&(this.firstTreeitem.el.tabIndex=0)}observeTargets(){this.addObserver(i=>{this.expandTreeitem(i),this.setSelected(i)});let e=new Map,t=new IntersectionObserver(i=>{for(let s of i)e.set(s.target.id,s.isIntersecting||s.intersectionRatio===1);for(let[s,r]of e)if(r){let a=this.treeitems.find(n=>n.el?.href.endsWith(`#${s}`));if(a)for(let n of this.observerCallbacks)n(a);break}},{threshold:1,rootMargin:"-60px 0px 0px 0px"});for(let i of this.treeitems.map(s=>s.el.getAttribute("href")))if(i){let s=i.replace(window.location.origin,"").replace("/","").replace("#",""),r=document.getElementById(s);r&&t.observe(r)}}addObserver(e,t=200){this.observerCallbacks.push(C(e,t))}setFocusToNextItem(e){let t=null;for(let i=e.index+1;i<this.treeitems.length;i++){let s=this.treeitems[i];if(s.isVisible){t=s;break}}t&&this.setFocusToItem(t)}setFocusToPreviousItem(e){let t=null;for(let i=e.index-1;i>-1;i--){let s=this.treeitems[i];if(s.isVisible){t=s;break}}t&&this.setFocusToItem(t)}setFocusToParentItem(e){e.groupTreeitem&&this.setFocusToItem(e.groupTreeitem)}setFocusToFirstItem(){this.firstTreeitem&&this.setFocusToItem(this.firstTreeitem)}setFocusToLastItem(){this.lastTreeitem&&this.setFocusToItem(this.lastTreeitem)}setSelected(e){for(let t of this.el.querySelectorAll('[aria-expanded="true"]'))t!==e.el&&(t.nextElementSibling?.contains(e.el)||t.setAttribute("aria-expanded","false"));for(let t of this.el.querySelectorAll("[aria-selected]"))t!==e.el&&t.setAttribute("aria-selected","false");e.el.setAttribute("aria-selected","true"),this.updateVisibleTreeitems(),this.setFocusToItem(e,!1)}expandTreeitem(e){let t=e;for(;t;)t.isExpandable&&t.el.setAttribute("aria-expanded","true"),t=t.groupTreeitem;this.updateVisibleTreeitems()}expandAllSiblingItems(e){for(let t of this.treeitems)t.groupTreeitem===e.groupTreeitem&&t.isExpandable&&this.expandTreeitem(t)}collapseTreeitem(e){let t=null;e.isExpanded()?t=e:t=e.groupTreeitem,t&&(t.el.setAttribute("aria-expanded","false"),this.updateVisibleTreeitems(),this.setFocusToItem(t))}setFocusByFirstCharacter(e,t){let i,s;t=t.toLowerCase(),i=e.index+1,i===this.treeitems.length&&(i=0),s=this.getIndexFirstChars(i,t),s===-1&&(s=this.getIndexFirstChars(0,t)),s>-1&&this.setFocusToItem(this.treeitems[s])}findTreeItems(){let e=(t,i)=>{let s=i,r=t.firstElementChild;for(;r;)(r.tagName==="A"||r.tagName==="SPAN")&&(s=new E(r,this,i),this.treeitems.push(s),this.firstChars.push(s.label.substring(0,1).toLowerCase())),r.firstElementChild&&e(r,s),r=r.nextElementSibling};e(this.el,null),this.treeitems.map((t,i)=>t.index=i)}updateVisibleTreeitems(){this.firstTreeitem=this.treeitems[0];for(let e of this.treeitems){let t=e.groupTreeitem;for(e.isVisible=!0;t&&t.el!==this.el;)t.isExpanded()||(e.isVisible=!1),t=t.groupTreeitem;e.isVisible&&(this.lastTreeitem=e)}}setFocusToItem(e,t=!0){e.el.tabIndex=0,t&&e.el.focus();for(let i of this.treeitems)i!==e&&(i.el.tabIndex=-1)}getIndexFirstChars(e,t){for(let i=e;i<this.firstChars.length;i++)if(this.treeitems[i].isVisible&&t===this.firstChars[i])return i;return-1}},E=class{constructor(e,t,i){e.tabIndex=-1,this.el=e,this.groupTreeitem=i,this.label=e.textContent?.trim()??"",this.tree=t,this.depth=(i?.depth||0)+1,this.index=0;let s=e.parentElement;s?.tagName.toLowerCase()==="li"&&s?.setAttribute("role","none"),e.setAttribute("aria-level",this.depth+""),e.getAttribute("aria-label")&&(this.label=e?.getAttribute("aria-label")?.trim()??""),this.isExpandable=!1,this.isVisible=!1,this.isInGroup=!!i;let r=e.nextElementSibling;for(;r;){if(r.tagName.toLowerCase()=="ul"){let a=`${i?.label??""} nav group ${this.label}`.replace(/[\W_]+/g,"_");e.setAttribute("aria-owns",a),e.setAttribute("aria-expanded","false"),r.setAttribute("role","group"),r.setAttribute("id",a),this.isExpandable=!0;break}r=r.nextElementSibling}this.init()}init(){this.el.tabIndex=-1,this.el.getAttribute("role")||this.el.setAttribute("role","treeitem"),this.el.addEventListener("keydown",this.handleKeydown.bind(this)),this.el.addEventListener("click",this.handleClick.bind(this)),this.el.addEventListener("focus",this.handleFocus.bind(this)),this.el.addEventListener("blur",this.handleBlur.bind(this))}isExpanded(){return this.isExpandable?this.el.getAttribute("aria-expanded")==="true":!1}isSelected(){return this.el.getAttribute("aria-selected")==="true"}handleClick(e){e.target!==this.el&&e.target!==this.el.firstElementChild||(this.isExpandable&&(this.isExpanded()&&this.isSelected()?this.tree.collapseTreeitem(this):this.tree.expandTreeitem(this),e.stopPropagation()),this.tree.setSelected(this))}handleFocus(){let e=this.el;this.isExpandable&&(e=e.firstElementChild??e),e.classList.add("focus")}handleBlur(){let e=this.el;this.isExpandable&&(e=e.firstElementChild??e),e.classList.remove("focus")}handleKeydown(e){if(e.altKey||e.ctrlKey||e.metaKey)return;let t=!1;switch(e.key){case" ":case"Enter":this.isExpandable?(this.isExpanded()&&this.isSelected()?this.tree.collapseTreeitem(this):this.tree.expandTreeitem(this),t=!0):e.stopPropagation(),this.tree.setSelected(this);break;case"ArrowUp":this.tree.setFocusToPreviousItem(this),t=!0;break;case"ArrowDown":this.tree.setFocusToNextItem(this),t=!0;break;case"ArrowRight":this.isExpandable&&(this.isExpanded()?this.tree.setFocusToNextItem(this):this.tree.expandTreeitem(this)),t=!0;break;case"ArrowLeft":this.isExpandable&&this.isExpanded()?(this.tree.collapseTreeitem(this),t=!0):this.isInGroup&&(this.tree.setFocusToParentItem(this),t=!0);break;case"Home":this.tree.setFocusToFirstItem(),t=!0;break;case"End":this.tree.setFocusToLastItem(),t=!0;break;default:e.key.length===1&&e.key.match(/\S/)&&(e.key=="*"?this.tree.expandAllSiblingItems(this):this.tree.setFocusByFirstCharacter(this,e.key),t=!0);break}t&&(e.stopPropagation(),e.preventDefault())}};function C(l,e){let t;return(...i)=>{let s=()=>{t=null,l(...i)};t&&clearTimeout(t),t=setTimeout(s,e)}}var p=class{constructor(e,t){this.table=e;this.toggleAll=t;this.expandAllItems=()=>{this.toggles.map(e=>e.setAttribute("aria-expanded","true")),this.update()};this.collapseAllItems=()=>{this.toggles.map(e=>e.setAttribute("aria-expanded","false")),this.update()};this.update=()=>{this.updateVisibleItems(),setTimeout(()=>this.updateGlobalToggle())};this.rows=Array.from(e.querySelectorAll("[data-aria-controls]")),this.toggles=Array.from(this.table.querySelectorAll("[aria-expanded]")),this.setAttributes(),this.attachEventListeners(),this.update()}setAttributes(){for(let e of["data-aria-controls","data-aria-labelledby","data-id"])this.table.querySelectorAll(`[${e}]`).forEach(t=>{t.setAttribute(e.replace("data-",""),t.getAttribute(e)??""),t.removeAttribute(e)})}attachEventListeners(){this.rows.forEach(e=>{e.addEventListener("click",t=>{this.handleToggleClick(t)})}),this.toggleAll?.addEventListener("click",()=>{this.expandAllItems()}),document.addEventListener("keydown",e=>{(e.ctrlKey||e.metaKey)&&e.key==="f"&&this.expandAllItems()})}handleToggleClick(e){let t=e.currentTarget;t?.hasAttribute("aria-expanded")||(t=this.table.querySelector(`button[aria-controls="${t?.getAttribute("aria-controls")}"]`));let i=t?.getAttribute("aria-expanded")==="true";t?.setAttribute("aria-expanded",i?"false":"true"),e.stopPropagation(),this.update()}updateVisibleItems(){this.rows.map(e=>{let t=e?.getAttribute("aria-expanded")==="true";e?.getAttribute("aria-controls")?.trimEnd().split(" ")?.map(s=>{let r=document.getElementById(`${s}`);t?(r?.classList.add("visible"),r?.classList.remove("hidden")):(r?.classList.add("hidden"),r?.classList.remove("visible"))})})}updateGlobalToggle(){if(!this.toggleAll)return;this.rows.some(t=>t.hasAttribute("aria-expanded"))&&(this.toggleAll.style.display="block"),this.toggles.some(t=>t.getAttribute("aria-expanded")==="false")?(this.toggleAll.innerText="Expand all",this.toggleAll.onclick=this.expandAllItems,this.toggleAll.setAttribute("aria-label","Expand all directories"),this.toggleAll.setAttribute("aria-live","polite")):(this.toggleAll.innerText="Collapse all",this.toggleAll.onclick=this.collapseAllItems,this.toggleAll.setAttribute("aria-label","Collapse all directories"),this.toggleAll.setAttribute("aria-live","polite"))}};v();var u=document.querySelector(".js-expandableTable");if(u){let l=new p(u,document.querySelector(".js-expandAllDirectories"));window.location.search.includes("expand-directories")&&l.expandAllItems();let e=document.querySelector(".js-showInternalDirectories");e&&(document.querySelector(".UnitDirectories-internal")&&(e.style.display="block",e.setAttribute("aria-label","Show Internal Directories"),e.setAttribute("aria-describedby","showInternal-description")),e.addEventListener("click",()=>{u.classList.contains("UnitDirectories-showInternal")?(u.classList.remove("UnitDirectories-showInternal"),e.innerText="Show internal",e.setAttribute("aria-label","Show Internal Directories"),e.setAttribute("aria-live","polite"),e.setAttribute("aria-describedby","showInternal-description")):(u.classList.add("UnitDirectories-showInternal"),e.innerText="Hide internal",e.setAttribute("aria-label","Hide Internal Directories"),e.setAttribute("aria-live","polite"),e.setAttribute("aria-describedby","hideInternal-description"))})),document.querySelector('html[data-local="true"]')&&e?.click()}var y=document.querySelector(".js-tree");if(y){let l=new m(y),e=x(l),t=document.querySelector(".js-mainNavMobile");t&&t.firstElementChild&&t?.replaceChild(e,t.firstElementChild),e.firstElementChild&&new c(e.firstElementChild)}var h=document.querySelector(".js-readme"),b=document.querySelector(".js-readmeContent"),A=document.querySelector(".js-readmeOutline"),f=document.querySelectorAll(".js-readmeExpand"),L=document.querySelector(".js-readmeCollapse"),S=document.querySelector(".DocNavMobile-select");h&&b&&A&&f.length&&L&&(h.clientHeight>320&&(h?.classList.remove("UnitReadme--expanded"),h?.classList.add("UnitReadme--toggle")),window.location.hash.includes("readme")&&d(),S?.addEventListener("change",l=>{l.target.value.startsWith("readme-")&&d()}),f.forEach(l=>l.addEventListener("click",e=>{e.preventDefault(),d(),h.scrollIntoView()})),L.addEventListener("click",l=>{l.preventDefault(),h.classList.remove("UnitReadme--expanded"),f[1]&&f[1].scrollIntoView({block:"center"})}),b.addEventListener("keyup",()=>{d()}),b.addEventListener("click",()=>{d()}),A.addEventListener("click",()=>{d()}),document.addEventListener("keydown",l=>{(l.ctrlKey||l.metaKey)&&l.key==="f"&&d()}));function d(){history.replaceState(null,"",`${location.pathname}#section-readme`),h?.classList.add("UnitReadme--expanded")}function I(){if(!location.hash)return;let e=document.getElementById(location.hash.slice(1))?.parentElement?.parentElement;e?.nodeName==="DETAILS"&&(e.open=!0)}I();window.addEventListener("hashchange",()=>I());document.querySelectorAll(".js-buildContextSelect").forEach(l=>{l.addEventListener("change",e=>{window.location.search=`?GOOS=${e.target.value}`})});
/*!
* @license
* Copyright 2021 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.
*/
/**
* @license
* Copyright 2021 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.
*/
/*!
* @license
* Copyright 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.
*/
//# sourceMappingURL=main.js.map