blob: 239f4851750646ead7b455defc4c863e7b8e0546 [file] [log] [blame]
$(document).ready(function(){function t(e){const t=e.target,n=t.getBoundingClientRect().top;return{elPreviousOffsetFromViewPort:n,clickedLangSwitchEl:t}}function n(e){const{elPreviousOffsetFromViewPort:t,clickedLangSwitchEl:n}=e,s=window.pageYOffset+n.getBoundingClientRect().top;$("html, body").scrollTop(s-t)}const s=new Date;function e(e){const s=e.name;return{...e,uniqueValues:new Set,selector:`[class^=${s}-]:not(.no-toggle)`,wrapper:`${s}-switcher`,localStorageKey:s,navHtml:function(e){let t="",n="";for(const o of e){const i=this.valueToTabTitle(o);n+=` ${s}-${o}`,t+=`<li class="langSwitch-content" data-value="${o}">${i}</li>`}return`<div class="${this.wrapper+n}"><ul class="nav nav-tabs">${t}</ul></div>`},valueToTabTitle:function(e){switch(e){case"py":return"Python";case"scio":return"SCIO";case"typescript":return"TypeScript"}return e.charAt(0).toUpperCase()+e.slice(1)},addTabs:function(){var e=this;$("div"+e.selector).each(function(){if($(this).prev().is("div"+e.selector))return;const t=e.findNextSiblingsValues($(this));for(const n of t)e.uniqueValues.add(n);const o=e.navHtml(t);$(this).before(o);let n=$(this).parent().parent().children(".code-snippet-playground");if(n.length>0)for(const e of t)n.addClass(`${s}-${e}`)})},findNextSiblingsValues:function(e){if(!e.is("div"+this.selector))return[];const n=`${s}-`;for(const t of e[0].classList)if(t.startsWith(n))return[t.replace(n,""),...this.findNextSiblingsValues(e.next())];return[]},bindEvents:function(){var e=this;$(`.${e.wrapper} li`).click(function(s){localStorage.setItem(e.localStorageKey,$(this).data("value"));const o=t(s);e.toggle(!1),n(o)})},toggle:function(e){let t=localStorage.getItem(this.localStorageKey)||this.default,n=$(`.${this.wrapper} li[data-value="${t}"]`).length>0;n||(this.uniqueValues.has(this.default)?t=this.default:t=[...this.uniqueValues][0]),$(`.${this.wrapper} li[data-value="${t}"]`).addClass("active"),$(`.${this.wrapper} li[data-value!="${t}"]`).removeClass("active"),$(this.selector).hide(),s==="language"&&$(".code-snippet-playground").hide(),$(`.${s}-${t}`).show(),$("nav"+this.selector).show(),$("code"+this.selector).show(),$('[data-spy="scroll"]').each(function(){$(this).scrollspy("refresh")}),this.onChanged&&this.onChanged(t,e)},render:function(){this.addTabs(),this.bindEvents(),this.toggle(!0)}}}e({name:"language",default:"java",onChanged:function(e,t){t?this.onInit(e):this.onChangedAfterLoaded(e)},onInit:function(e){const t=document.getElementsByClassName("code-snippet-playground"),n=this.langToSdk(e);for(const s of t){const a=s.dataset.src,r=s.dataset.width,c=s.dataset.height,o=new URL(a),i=new URLSearchParams(o.search);i.set("sdk",n),o.search=i.toString();const e=document.createElement("iframe");e.src=o.href,e.width=r,e.height=c,e.className="playground",e.allow="clipboard-write",s.appendChild(e)}},onChangedAfterLoaded:function(e){const n=$(".code-snippet-playground iframe").get(),s={type:"SetSdk",sdk:this.langToSdk(e)},o=this;let i=30;clearInterval(this.interval);const t=function(){for(const e of n)e.contentWindow.postMessage(s,"*");i--===0&&clearInterval(o.interval)};this.areFramesLoaded()||(this.interval=setInterval(t,1e3)),t()},langToSdk:function(e){switch(e){case"py":return"python"}return e},areFramesLoaded:function(){const e=new Date-s;return e>=3e4}}).render(),e({name:"runner",default:"direct"}).render(),e({name:"shell",default:"unix"}).render(),e({name:"version"}).render()})