altinn-designsystem
Version:
Altinn Design system based on Pattern Lab.
1 lines • 9.02 kB
JavaScript
function receiveIframeMessage(e){if("file:"==window.location.protocol||e.origin===window.location.protocol+"//"+window.location.host){var t,a={};try{a="string"!=typeof e.data?e.data:JSON.parse(e.data)}catch(n){}if(void 0!==a.event&&"patternLab.updatePath"==a.event)if(void 0!==patternData.patternPartial){var i=/(patterns|snapshots)\/(.*)$/;t=window.location.protocol+"//"+window.location.host+window.location.pathname.replace(i,"")+a.path+"?"+Date.now(),window.location.replace(t)}else t=window.location.protocol+"//"+window.location.host+window.location.pathname.replace("styleguide/html/styleguide.html","")+a.path+"?"+Date.now(),window.location.replace(t);else void 0!==a.event&&"patternLab.reload"==a.event&&window.location.reload()}}if(self!=top){var path=window.location.toString(),parts=path.split("?"),options={event:"patternLab.pageLoad",path:parts[0]};patternData=document.getElementById("sg-pattern-data-footer").innerHTML,patternData=JSON.parse(patternData),options.patternpartial=void 0!==patternData.patternPartial?patternData.patternPartial:"all",""!==patternData.lineage&&(options.lineage=patternData.lineage);var targetOrigin="file:"==window.location.protocol?"*":window.location.protocol+"//"+window.location.host;parent.postMessage(options,targetOrigin);for(var aTags=document.getElementsByTagName("a"),i=0;i<aTags.length;i++)aTags[i].onclick=function(e){var t=this.getAttribute("href"),a=this.getAttribute("target");if(void 0===a||"_parent"!=a&&"_blank"!=a){if(!t||"#"===t)return e.preventDefault(),!1;e.preventDefault(),window.location.replace(t)}else;}}window.addEventListener("message",receiveIframeMessage,!1);var urlHandler={skipBack:!1,targetOrigin:"file:"==window.location.protocol?"*":window.location.protocol+"//"+window.location.host,getFileName:function(e,t){var a="patterns",n="";if(void 0===e)return n;if(void 0===t&&(t=!0),"all"==e)return"styleguide/html/styleguide.html";if("snapshots"==e)return"snapshots/index.html";var i=e.indexOf("viewall-")!=-1?viewAllPaths:patternPaths,o=e.replace("viewall-",""),l=this.getPatternInfo(o,i),r=l[0],d=l[1];if(void 0!==i[r]&&void 0!==i[r][d])n=i[r][d];else if(void 0!==i[r])for(var s in i[r])if(s.indexOf(d)!=-1){n=i[r][s];break}if(""===n)return n;var c=/\//g;if(e.indexOf("viewall-")!==-1&&0===e.indexOf("viewall-")&&""!==n)n=a+"/"+n.replace(c,"-")+"/index.html";else if(""!==n&&(n=a+"/"+n.replace(c,"-")+"/"+n.replace(c,"-"),t)){var p=void 0!==config.outputFileSuffixes&&void 0!==config.outputFileSuffixes.rendered?config.outputFileSuffixes.rendered:"";n=n+p+".html"}return n},getPatternInfo:function(e,t){for(var a=e.split("-"),n=1,i=a.length,o=a[0];void 0===t[o]&&n<i;)o+="-"+a[n],n++;var l=e.slice(o.length+1,e.length);return[o,l]},getRequestVars:function(){var e=new function(e){if(e.length>1)for(var t,a=0,n=e.substr(1).split("&");a<n.length;a++)t=n[a].split("="),this[unescape(t[0])]=t.length>1?unescape(t[1]):""}(window.location.search);return e},pushPattern:function(e,t){var a={pattern:e},n=urlHandler.getFileName(e),i=window.location.pathname;i="file"===window.location.protocol?i.replace("/public/index.html","public/"):i.replace(/\/index\.html/,"/");var o=window.location.protocol+"//"+window.location.host+i+n;if(t!=o){var l=JSON.stringify({event:"patternLab.updatePath",path:n});document.getElementById("sg-viewport").contentWindow.postMessage(l,urlHandler.targetOrigin)}else{var r="file:"==window.location.protocol?null:window.location.protocol+"//"+window.location.host+window.location.pathname.replace("index.html","")+"?p="+e;void 0!==history.pushState&&history.pushState(a,null,r),document.getElementById("title").innerHTML="Pattern Lab - "+e,null!==document.getElementById("sg-raw")&&document.getElementById("sg-raw").setAttribute("href",urlHandler.getFileName(e))}},popPattern:function(e){var t,a=e.state;if(null===a)return void(this.skipBack=!1);null!==a&&(t=a.pattern);var n="";n=this.getFileName(t),""===n&&(n="styleguide/html/styleguide.html");var i=JSON.stringify({event:"patternLab.updatePath",path:n});document.getElementById("sg-viewport").contentWindow.postMessage(i,urlHandler.targetOrigin),document.getElementById("title").innerHTML="Pattern Lab - "+t,null!==document.getElementById("sg-raw")&&document.getElementById("sg-raw").setAttribute("href",urlHandler.getFileName(t))}};window.onpopstate=function(e){urlHandler.skipBack=!0,urlHandler.popPattern(e)};var panelsUtil={addClickEvents:function(e,t){for(var a=e.querySelectorAll("#sg-"+t+"-tabs li"),n=0;n<a.length;++n)a[n].onclick=function(e){e.preventDefault();var t=this.getAttribute("data-patternpartial"),a=this.getAttribute("data-panelid");panelsUtil.show(t,a)};return e},show:function(e,t){var a;for(a=document.querySelectorAll("#sg-"+e+"-tabs li"),i=0;i<a.length;++i)a[i].classList.remove("sg-tab-title-active");for(a=document.querySelectorAll("#sg-"+e+"-panels div.sg-tabs-panel"),i=0;i<a.length;++i)a[i].style.display="none";document.getElementById("sg-"+e+"-"+t+"-tab").classList.add("sg-tab-title-active"),document.getElementById("sg-"+e+"-"+t+"-panel").style.display="flex"}},modalStyleguide={active:[],targetOrigin:"file:"===window.location.protocol?"*":window.location.protocol+"//"+window.location.host,onReady:function(){for(var e=document.querySelectorAll(".sg-pattern-extra-toggle"),t=0;t<e.length;++t)e[t].onclick=function(e){e.preventDefault();var t=this.getAttribute("data-patternpartial");modalStyleguide.toggle(t)}},toggle:function(e){if(void 0!==modalStyleguide.active[e]&&modalStyleguide.active[e])modalStyleguide.highlightsHide(),modalStyleguide.close(e);else{var t=document.getElementById("sg-pattern-data-"+e);modalStyleguide.collectAndSend(t,!0,!1)}},open:function(e,t){var a=document.createElement("div");a.innerHTML=t,t=document.createElement("div").appendChild(a).querySelector("div"),t=panelsUtil.addClickEvents(t,e),modalStyleguide.close(e),modalStyleguide.active[e]=!0,a=document.getElementById("sg-pattern-extra-"+e),a.childNodes.length>0&&a.removeChild(a.childNodes[0]),document.getElementById("sg-pattern-extra-"+e).appendChild(t),document.getElementById("sg-pattern-extra-toggle-"+e).classList.add("active"),document.getElementById("sg-pattern-extra-"+e).classList.add("active")},close:function(e){modalStyleguide.active[e]=!1,document.getElementById("sg-pattern-extra-toggle-"+e).classList.remove("active"),document.getElementById("sg-pattern-extra-"+e).classList.remove("active")},collectAndSend:function(e,t,a){var n=JSON.parse(e.innerHTML);void 0!==n.patternName&&(patternMarkupEl=document.querySelector("#"+n.patternPartial+" > .sg-pattern-example"),n.patternMarkup=null!==patternMarkupEl?patternMarkupEl.innerHTML:document.querySelector("body").innerHTML,modalStyleguide.patternQueryInfo(n,t,a))},highlightsHide:function(e){var t=void 0!==e?"#"+e+" > ":"";for(elsToHide=document.querySelectorAll(t+".has-annotation"),i=0;i<elsToHide.length;i++)elsToHide[i].classList.remove("has-annotation");for(elsToHide=document.querySelectorAll(t+".annotation-tip"),i=0;i<elsToHide.length;i++)elsToHide[i].style.display="none"},patternQueryInfo:function(e,t,a){try{var n=JSON.stringify({event:"patternLab.patternQueryInfo",patternData:e,iframePassback:t,switchText:a});parent.postMessage(n,modalStyleguide.targetOrigin)}catch(i){}},receiveIframeMessage:function(e){var t;if("file:"===window.location.protocol||e.origin===window.location.protocol+"//"+window.location.host){var a={};try{a="string"!=typeof e.data?e.data:JSON.parse(e.data)}catch(n){}if(void 0!==a.event&&"patternLab.patternQuery"==a.event){var i,o;for(i=document.querySelectorAll(".sg-pattern-data"),o=i.length>1,t=0;t<i.length;t++)modalStyleguide.collectAndSend(i[t],o,a.switchText)}else if(void 0!==a.event&&"patternLab.patternModalInsert"==a.event)modalStyleguide.open(a.patternPartial,a.modalContent);else if(void 0!==a.event&&"patternLab.annotationsHighlightShow"==a.event){var l,r,d,s;for(t=0;t<a.annotations.length;t++)if(d=a.annotations[t],l=document.querySelectorAll(d.el),l.length>0)for(r=0;r<l.length;r++)l[r].classList.add("has-annotation"),s=document.createElement("span"),s.innerHTML=d.displayNumber,s.classList.add("annotation-tip"),"0px"==window.getComputedStyle(l[r],null).getPropertyValue("max-height")&&(s.style.display="none"),annotationTip=document.querySelector(d.el+" > span.annotation-tip"),null===annotationTip?l[r].insertBefore(s,l[r].firstChild):annotationTip.style.display="inline",l[r].onclick=function(e){return function(t){t.preventDefault(),t.stopPropagation();var a=JSON.stringify({event:"patternLab.annotationNumberClicked",displayNumber:e.displayNumber});parent.postMessage(a,modalStyleguide.targetOrigin)}}(d)}else if(void 0!==a.event&&"patternLab.annotationsHighlightHide"==a.event)modalStyleguide.highlightsHide();else if(void 0!==a.event&&"patternLab.patternModalClose"==a.event){var c=[];for(var p in modalStyleguide.active)c.push(p);for(t=0;t<c.length;t++){var g=c[t];modalStyleguide.active[g]&&modalStyleguide.close(g)}}}}};modalStyleguide.onReady(),window.addEventListener("message",modalStyleguide.receiveIframeMessage,!1);