-
Notifications
You must be signed in to change notification settings - Fork 3
/
github-element.js
1 lines (1 loc) · 3.37 KB
/
github-element.js
1
(function(){"use strict";function e(e){e&&(this.status=e.status,this._parseHeaders(e.getAllResponseHeaders()),this.body=this.headers["Content-Type"]&&/application\/json/.test(this.headers["Content-Type"])?JSON.parse(e.responseText):e.responseText||"")}function t(e,t){t=t||{},this.url=e,this.headers=t.headers||{},this.method=t.method||"get"}function s(e){e=e||{},this.baseUrl="https://api.github.com",this.cacheNamespace=e.cacheNamespace||"ghapi"}e.prototype._parseHeaders=function(e){var t=e.split("\n"),s=/^(.*):(.*)$/,r={};return t.forEach(function(e){var t=e.replace(/\s/gm,""),n=s.exec(t);n&&n.length>=3&&(r[n[1]]=n[2])}),this.headers=r,this},t.prototype.send=function(t){var s=new XMLHttpRequest,r=this;return s.onload=function(){var n=new e(s);t.call(r,n)},s.open(this.method,this.url),Object.keys(this.headers).forEach(function(e){s.setRequestHeader(e,this.headers[e])},this),s.send(),this},t.get=function(e,s,r){return arguments.length<3&&(r=s),new t(e,{method:"get",headers:s||{}}).send(r)},s.prototype.repo=function(e,t){var r=this.baseUrl+"/repos/"+e;return this._requestOrReadCache(r,function(r){if(!r)throw new Error("No repository found for "+e+" !");return t(new s.Repository({name:r.name,fullName:r.full_name,forks:r.forks_count,description:r.description,url:r.html_url,stars:r.stargazers_count,language:r.language}))}),this},s.prototype._requestOrReadCache=function(e,s){var r=this.cacheNamespace+"-"+e+"-",n=r+"etag",a=r+"content",o=localStorage.getItem(n);return o?(t.get(e,{"If-None-Match":o},function(t){return 304!==t.status?(window.localStorage.removeItem(n),window.localStorage.removeItem(a),this._requestOrReadCache(e,s)):s(JSON.parse(localStorage.getItem(a)))}.bind(this)),this):(t.get(e,function(e){return 200!==e.status?void s(null):(window.localStorage.setItem(n,e.headers.ETag),window.localStorage.setItem(a,JSON.stringify(e.body)),s(e.body))}),this)},s.Repository=function(e){e=e||{},this.name=e.name,this.fullName=e.fullName,this.description=e.description,this.forks=e.forks,this.language=e.language,this.url=e.url,this.stars=e.stars},function(e,t){"function"==typeof define&&define.amd?define(["skate"],t):"object"==typeof exports?module.exports=t(require("skate")):e.GithubRepoElement=t(e.skate,e.skateTemplateHtml)}(this,function(e){if("function"!=typeof e)throw new Error(["Github-repo-element requires Skate.js! Make sure you either include","it in on your page, or use the bundled version. See","https://github.com/stevenschobert/github-repo-element#installing"].join(" "));var t=['<a class="ghrepo" data-ghrepo="url">','<div class="ghrepo-title" data-ghrepo="fullName"></div>','<div class="ghrepo-description">','<div data-ghrepo="description"></div>',"</div>",'<div class="ghrepo-meta">','<span class="ghrepo-stars">','<span data-ghrepo="stars"></span> stars',"</span>",'<span class="ghrepo-forks">','<span data-ghrepo="forks"></span> forks',"</span>","</div>","</a>"].join(""),r=new s,n=e("github-repo",{template:function(e){0===e.innerHTML.replace(/\s/,"").length&&(e.innerHTML=t)},attributes:{src:function(e,t){r.repo(t.newValue,function(t){e.displayRepo(t)})}},prototype:{displayRepo:function(e){var t=this.querySelector('[data-ghrepo="url"]');t&&t.setAttribute("href",e.url),["name","fullName","description","watchers","forks","language","stars"].forEach(function(t){var s=this.querySelector('[data-ghrepo="'+t+'"]');s&&(s.innerHTML=e[t])},this)}}});return n})}).bind("undefined"!=typeof window?window:{})();