forked from zenorocha/jquery-github
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgithub-repos.min.js
1 lines (1 loc) · 2.3 KB
/
github-repos.min.js
1
(function(){function pad(n){return n<10?"0"+n:n}function render(template,data){return template.replace(/\{\{(\w+)\}\}/g,function(m,key){return data[key]})}var cid=0,template='<div class="github-box-header">\n <h3><a href="{{html_url}}">{{name}}</a></h3>\n <div class="github-stats">\n <a class="repo-watchers" href="{{html_url}}/watchers">{{watchers}}</a>\n <a class="repo-forks" href="{{html_url}}/forks">{{forks}}</a>\n </div>\n</div>\n<div class="github-box-content">\n <p>{{description}} — <a href="{{html_url}}#readme">Read More</a></p>\n</div>\n<div class="github-box-download">\n <p class="repo-update">Latest commit to <strong>master</strong> on {{pushed_at}}</p>\n <a class="repo-download" href="{{html_url}}/zipball/master">Download as zip</a>\n</div>';function JSONPCallback(context,cb){var name="GHWidgetLoaded"+ ++cid;window[name]=function(data){cb.call(context,data);delete window[name]};return name}function Repo(repo,target){this.repo=repo;this.callback=JSONPCallback(this,this.ready);this.target=target}Repo.prototype.load=function(){var cached,s;if(window.sessionStorage&&(cached=sessionStorage["gh-repos:"+this.repo])){window[this.callback](JSON.parse(cached));return}s=document.createElement("script");s.async=true;s.src="https://api.github.com/repos/"+this.repo+"?callback="+this.callback;document.body.appendChild(s)};Repo.prototype.ready=function(results){if(results.meta.status>=400&&results.data.message){console.warn(results.data.message);return}if(window.sessionStorage){sessionStorage["gh-repos:"+this.repo]=JSON.stringify(results)}var data=results.data,pushed_at=new Date(data.pushed_at),month=pushed_at.getMonth()+1,day=pushed_at.getDate(),year=pushed_at.getFullYear();data.pushed_at=pad(month)+"/"+pad(day)+"/"+year;data.repo_url="//github.com/"+this.repo;var box=document.createElement("div");box.className="github-box";box.innerHTML=render(template,data);this.target&&this.target.parentNode.replaceChild(box,this.target);return box};var GHRepos={create:function(selector){var els=document.querySelectorAll(selector),items=Array.prototype.slice.call(els,0);items.forEach(function(el){var repo=el.dataset&&el.dataset.repo||el.href.split("/").slice(-2).join("/");new Repo(repo,el).load()})},Repo:Repo};if(typeof exports!=="undefined"){exports=GHRepos}else{window.GHRepos=GHRepos}})();