hugo/docs/static/js/buttons.js

1 line
No EOL
6.8 KiB
JavaScript

(function(){var Button,ButtonElement,Config,CountElement,Iframe,QueryString,link,links,_i,_len;if(Config={api:"https://api.github.com",buttonClass:"github-button",iconClass:"octicon",icon:"octicon-mark-github",scriptId:"github-bjs",styles:["default","mega"]},(Config.script=document.getElementById(Config.scriptId))&&(Config.url=Config.script.src.replace(/buttons.js$/,"")),QueryString=function(){function QueryString(){}return QueryString.stringify=function(obj){var key,results,value;results=[];for(key in obj)value=obj[key],null==value&&(value=""),results.push(""+encodeURIComponent(key)+"="+encodeURIComponent(value));return results.join("&")},QueryString.parse=function(str){var obj,pair,_i,_len,_ref;for(obj={},_ref=str.split("&"),_i=0,_len=_ref.length;_len>_i;_i++)pair=_ref[_i],obj[decodeURIComponent(pair.split("=")[0])]=decodeURIComponent(pair.split("=").slice(1).join("="));return obj},QueryString}(),Iframe=function(){function Iframe(callback){var key,value,_ref,_ref1;this.iframe=document.createElement("iframe"),_ref={allowtransparency:!0,scrolling:"no",frameBorder:0};for(key in _ref)value=_ref[key],this.iframe.setAttribute(key,value);_ref1={border:"none",height:"0",width:"1px"};for(key in _ref1)value=_ref1[key],this.iframe.style[key]=value;callback&&callback(this.iframe)}return Iframe.prototype.on=function(event,func){return this.iframe.addEventListener?this.iframe.addEventListener(""+event,func):this.iframe.attachEvent&&this.iframe.attachEvent("on"+event,func),this},Iframe.prototype.once=function(event,func){var once;return once=function(_this){return function(){_this.iframe.removeEventListener?_this.iframe.removeEventListener(""+event,once):_this.iframe.detachEvent&&_this.iframe.detachEvent("on"+event,once),func()}}(this),this.on(event,once)},Iframe}(),Button=function(){function Button(a){a?(this.data=this.parseAnchor(a),this.iframe=new Iframe(function(iframe){a.parentNode.insertBefore(iframe,a)}),this.iframe.once("load",function(_this){return function(){var body,html,iframe,style;iframe=_this.iframe.iframe,html=iframe.contentWindow.document.documentElement,body=iframe.contentWindow.document.body,html.style.overflow=body.style.overflow="visible",style={height:""+body.scrollHeight+"px",width:""+body.scrollWidth+"px"},html.style.overflow=body.style.overflow="",_this.iframe.once("load",function(){var key,value;a.parentNode.removeChild(a),a=null;for(key in style)value=style[key],iframe.style[key]=value}),iframe.src=""+Config.url+"buttons.html"+_this.hash(_this.data)}}(this)),this.iframe.iframe.contentWindow.document.open(),this.iframe.iframe.contentWindow.document.write(this.html()),this.iframe.iframe.contentWindow.document.close()):(this.data=this.parseHash(),document.body.className=this.data.style,document.getElementsByTagName("base")[0].href=this.data.href,new ButtonElement(this.data,function(buttonElement){document.body.appendChild(buttonElement)}),new CountElement(this.data,function(countElement){document.body.appendChild(countElement)}))}return Button.prototype.parseAnchor=function(a){var filter_js,protocolPattern;return protocolPattern=/^([a-z0-9.+-]+:)/i,filter_js=function(href){var protocol;return protocol=protocolPattern.exec(href),protocol&&"javascript:"===protocol[0].toLowerCase()?"":href},{countApi:function(){var api;return api=a.getAttribute("data-count-api"),api?("/"!==api.charAt(0)&&(api="/"+api),api):null}(),countHref:function(){var href;return href=a.getAttribute("data-count-href"),href&&(href=filter_js(href))?href:filter_js(a.href)}(),href:function(){return filter_js(a.href)}(),style:function(){var i,style,_i,_len,_ref;if(style=a.getAttribute("data-style"))for(_ref=Config.styles,_i=0,_len=_ref.length;_len>_i;_i++)if(i=_ref[_i],i===style)return style;return Config.styles[0]}(),text:a.getAttribute("data-text")||a.textContent||a.innerText,icon:a.getAttribute("data-icon")||Config.icon}},Button.prototype.hash=function(data){return"#"+encodeURIComponent(QueryString.stringify(data))},Button.prototype.parseHash=function(){return QueryString.parse(decodeURIComponent(document.location.hash.replace(/^#/,"")))},Button.prototype.html=function(){return'<!DOCTYPE html>\n<html>\n <head>\n <meta charset="utf-8">\n <title></title>\n <base target="_blank"><!--[if lte IE 6]></base><![endif]-->\n <link rel="stylesheet" href="'+Config.url+'buttons.css">\n <style>html{visibility:hidden;}</style>\n <script>document.location.hash = "'+this.hash(this.data)+'";</script>\n </head>\n <body>\n <script src="'+Config.script.src+'"></script>\n </body>\n</html>'},Button}(),ButtonElement=function(){function ButtonElement(data,callback){var icon,text;this.data=data,icon=document.createElement("i"),icon.className=""+(Config.iconClass?""+Config.iconClass+" ":"")+data.icon,text=document.createElement("span"),data.text&&text.appendChild(document.createTextNode(" "+data.text+" ")),this.a=document.createElement("a"),this.a.className="button",data.href&&(this.a.href=data.href),this.a.appendChild(icon),this.a.appendChild(text),callback&&callback(this.a)}return ButtonElement}(),CountElement=function(){function CountElement(data,callback){var api,s;this.data=data,this.callback=callback,data.countApi&&(this.a=document.createElement("a"),this.a.className="count",data.countHref&&(this.a.href=data.countHref),this.a.appendChild(document.createElement("b")),this.a.appendChild(document.createElement("i")),window.callback=function(_this){return function(json){return _this.json(json)}}(this),api=function(){var query,url;return url=data.countApi.split("#")[0],query=QueryString.parse(url.split("?").slice(1).join("?")),query.callback="callback",""+url.split("?")[0]+"?"+QueryString.stringify(query)}(),this.script=document.createElement("script"),this.script.async=!0,this.script.src=""+Config.api+api,s=document.getElementsByTagName("script")[0],s.parentNode.insertBefore(this.script,s))}return CountElement.prototype.json=function(json){var i,text,_i,_len,_ref;if(window.callback=null,this.script.parentNode.removeChild(this.script),this.script=null,200===json.meta.status){for(_ref=this.data.countApi.split("#").slice(1).join("#").split("."),_i=0,_len=_ref.length;_len>_i;_i++)i=_ref[_i],json.data=json.data[i];!isNaN(parseFloat(json.data))&&isFinite(json.data)&&(json.data=json.data.toString().replace(/\B(?=(\d{3})+(?!\d))/g,",")),text=document.createElement("span"),text.appendChild(document.createTextNode(" "+json.data+" ")),this.a.appendChild(text),this.callback&&this.callback(this.a)}},CountElement}(),Config.script)for(links=document.querySelectorAll?document.querySelectorAll("a."+Config.buttonClass):function(){var link,_i,_len,_ref;for(links=[],_ref=document.getElementsByTagName("a"),_i=0,_len=_ref.length;_len>_i;_i++)link=_ref[_i],~(" "+link.className+" ").replace(/[\t\r\n]/g," ").indexOf(" "+Config.buttonClass+" ")&&links.push(link);return links}(),_i=0,_len=links.length;_len>_i;_i++)link=links[_i],new Button(link);else new Button}).call(this);