refapp
Version:
Parse Refract JSON to API Reference Jquery SPA
1 lines • 6.16 kB
JavaScript
!function(R){"use strict";function T(e){return"object"==typeof e&&void 0!==e.content?e.content:e}function E(e,t){if("object"==typeof e&&null!==e){var a=e.meta;if("object"==typeof a&&null!==a){if(!t)return a;if(a[t]){var r=Array.isArray(a[t])?a[t][0]:a[t];return r?T(r):a[t]}}}return t?"":{}}function O(e,t){if("object"==typeof e&&(e=e.content,Array.isArray(e))){t.headers={};for(var a=0;a<e.length;a++){var r=e[a];try{t.headers[r.content.key.content]=r.content.value.content}catch(e){console.error("Malformed HTTP header object",r,e)}}}}var P=0,S=[{}],H=[{}],L=function(e,t,a,r,s,n,o){var c,i,l,f,h;if("object"==typeof e&&null!==e){var d=e.element,u=e.content,p=e.attributes,m=S[P],b=H[P];if("httpResponse"!==d){if("object"==typeof p&&null!==p&&("string"==typeof T(p.method)&&(m.method=T(p.method)),"string"==typeof T(p.href)&&(m.href=T(p.href)),p.headers&&O(p.headers,m),p.hrefVariables)){var v=p.hrefVariables;if("object"==typeof v&&(v=v.content,Array.isArray(v)))for(m.params=[],c=0;c<v.length;c++){var y=v[c];try{var g={key:y.content.key.content,required:!("optional"===y.attributes.typeAttributes[0])};y.content.value?g.value=y.content.value.content:g.value="",y.meta?(g.type=E(y,"title")||"",g.description=E(y,"description")||""):g.type=g.description="",m.params.push(g)}catch(e){console.error("Malformed URL param object",y,e)}}}switch(d){case"copy":"string"==typeof u&&r.append('<div class="pb-2">'+a.mdParser(u)+"</div>");break;case"category":case"resource":l=E(e,"classes");var k,w=t+(f=E(e,"title")).toLowerCase().replace(/[^\w\s]/g,"").replace(/\s+/g,"-");if(""!==f){var C;switch(l){case"api":C=1;break;case"resourceGroup":C=2;break;default:C=3}r.append(R("<h"+C+">",{class:"my-3",html:R("<a>",{class:"anchor-link text-body",href:"#"+w,text:f}),id:w})),C<=2&&r.append("<hr>"),k=R("<li>",{html:R("<a>",{href:"#"+w,text:f})}),s.append(k),i=function(){var e=R("<ul>");k.append(e);var t=R("<div>",{class:"mb-5"});r.append(t),r=t,s=e}}m.href&&(o=m.href);break;case"transition":f=E(e,"title");var j=R("<div>",{class:"card-body",html:'<h5 class="card-title">'+f+"</h5>"});r.append(R("<a>",{href:"javascript:;",class:"mt-2 card",html:j,click:(h=P,function(){a.actionCallback(S[h],H[h])})})),i=function(){r=j},m.title=f;break;case"httpRequest":if(m.method){var x;switch(m.method){case"POST":x="success";break;case"PATCH":x="warning";break;case"PUT":x="secondary";break;case"DELETE":x="danger";break;case"GET":x="info";break;default:x="light"}r.parent().addClass("text-white bg-"+x).find("h3,h4,h5").append(R("<small>",{class:"text-monospace ml-1 float-right",text:m.method}))}break;case"asset":if("string"==typeof u){var A;switch(n){case"httpRequest":A=m;break;case"httpResponse":A=b}A&&("messageBodySchema"===(l=E(e,"classes"))?A.schema=u:A.body=u)}break;case"parseResult":if(Array.isArray(u))return u[0]}}else p.headers?O(p.headers,b):b.headers=[],p.statusCode?b.status=parseInt(p.statusCode,10):b.status=200,S.push({href:o||m.href}),H.push({});if(Array.isArray(u)&&u.length)for(i&&i(),c=0;c<u.length;c++)L(u[c],t,a,r,s,d,o);"httpResponse"===d&&P++}return null};window.consumeRefract=L}(jQuery),function(C){"use strict";C.fn.refapp=function(a,e){var s={asideClasses:"",articleClasses:"",baseHash:"/",mdParser:function(e){return e},refractCallback:null,actionCallback:function(e,t){console.log(e,t)}};e&&Object.assign(s,e);var r,n,t=Math.floor(8999*Math.random())+1e3,o=C("<article>",{class:s.articleClasses}),c=C("<div>",{class:"list-group my-3 mr-md-5 pr-lg-3 pr-xl-5 ref-resources"}),i=[],l=C("<ol>",{class:"ref-anchors"}),f=C("<aside>",{class:s.asideClasses,html:["<h5>Summary</h5>",l,"<h5>Reference</h5>",c]}),h=s.baseHash;C(window).on("hashchange",function(){r&&!new RegExp("^#"+r).test(location.hash)&&d()});var d=function(){var e=location.hash;if(e){if(-1===e.slice(h.length+1).indexOf("/"))return window.location.hash=e+"/",d();for(var t=0;t<i.length;t++){var a=i[t];if(new RegExp("^#"+a.data("anchor")).test(e))return a.hasClass("active")||(n=e,a.click()),!0}var r=e.match(/^#(reference|introduction)\/(.*)$/);if(r)return window.location.hash="#"+h+r[2],d()}return!1};if(Array.isArray(a))for(var u=!0,p=function(e,t,a){alert("Cannot GET Refract JSON: "+t),console.error(a)},m=function(e,r){var t=a[e].src;"string"==typeof t&&""!==t?C.getJSON(t,function(e){var t,a;t=e,a=r,l.slideUp(200,function(){C(this).html(""),o.fadeOut(200,function(){C(this).html("");for(var e=Object.assign({},t);e;)e=consumeRefract(e,a,s,o,l);o.fadeIn("slow",function(){u?u=!1:C("html, body").animate({scrollTop:o.offset().top-20},"slow"),"function"==typeof s.refractCallback&&s.refractCallback(t)}),l.slideDown("slow",function(){if(n){if(n!==location.hash){var e=C(this).find('a[href="'+n+'"]');e.length&&setTimeout(function(){e[0].click()},100)}n=null}}),o.find("a").filter(function(){var e=C(this).attr("href");return"#"!==e.charAt(0)&&"javascript:;"!==e}).attr("target","_blank")})})}).fail(p):console.error(new Error("Invalid or undefined src string on refract ("+e+"), ignored"))},b=0;b<a.length;b++)if("object"==typeof a[b]&&null!==a[b]){var v=a[b].title;if(v&&"string"==typeof(v="object"==typeof v?v.content:v)&&""!==v.trim()){var y=h+v.toLowerCase().replace(/\s/g,"-")+"/";i.push(C("<a>",{class:"list-group-item list-group-item-action",href:"javascript:;",text:v,"data-anchor":y,click:function(e,t){return function(){c.find("a.active").removeClass("active"),C(this).addClass("active"),m(e,t),C("html, body").animate({scrollTop:0},"slow","swing",function(){r=t,window.location.hash="#"+t})}}(b,y)}))}c.append(i)}var g="ref-anchors-"+t;f.addClass("collapse d-md-block").attr("id",g);var k=[C("<a>",{class:"btn btn-xl btn-outline-primary btn-block d-md-none mb-3","data-toggle":"collapse","aria-expanded":"false","aria-control":g,href:"#"+g,role:"button",html:'<i class="ti-angle-down mr-1"></i> Content'}),f],w=[];s.apiTitle&&w.push(C("<h1>",{class:"mt-3 mb-4 text-muted",text:s.apiTitle})),w.push(o),this.html(C("<div>",{class:"container",html:C("<div>",{class:"row",html:[C("<div>",{class:"col-md-5 col-xl-4 ref-sidebar",html:[C("<section>",{class:"py-4 sticky-top",html:k})]}),C("<div>",{class:"col-md-7 col-xl-8 ref-body",html:w})]})})),d()||i[0].click()}}(jQuery);