website/static/bower_components/talaria/dist/talaria.min.js

1 line
9.3 KiB
JavaScript

var talaria=function(t){"use strict";function e(t){this.name="RateLimitedError",this.message=t||"Rate-Limit exceeded."}function a(t){this.name="NotFoundError",this.message=t||"Object not found."}function n(t){return 403===t.status}var r={},o={COMMENTABLE_CONTENT_PATH_PREFIX:"_posts/",CONTENT_SUFFIX:".md",CACHE_TIMEOUT:36e5,PAGINATION_SCHEME:/\/page\d+\//,LOCAL_STORAGE_SUPPORTED:!1,PERMALINK_IDENTIFIER:"a.permalink",PERMALINK_STYLE:/[\.\w\-_:\/]+\/(\d+)\/(\d+)\/(\d+)\/([\w\-\.]+)$/},i=function(){switch(r.PERMALINK_STYLE){case"pretty":return/[\.\w\-_:\/]+\/(\d+)\/(\d+)\/(\d+)\/([\w\-\.]+)\/$/;case"date":return/[\.\w\-_:\/]+\/(\d+)\/(\d+)\/(\d+)\/([\w\-\.]+)\.html$/;case"none":if(!r.USE_GISTS)throw new Error("When using commit-based comments, talaria requires the use of permalinks that include the date of the post");return/[\.\w\-_:\/]+\/([\w\-\.]+)\.html$/;default:return r.PERMALINK_STYLE}},c=function(t){return t.replace(r.PERMALINK_STYLE,r.COMMENTABLE_CONTENT_PATH_PREFIX+"$1-$2-$3-$4"+r.CONTENT_SUFFIX)},m=function(t){return t.substr(0,7)},s=function(){try{return sessionStorage.setItem("dummy","dummy"),sessionStorage.removeItem("dummy"),!0}catch(t){return!1}},u=function(t){return(new Date).getTime()-t.timestamp>r.CACHE_TIMEOUT},l=function(e){var a;return r.LOCAL_STORAGE_SUPPORTED&&(a=sessionStorage.getItem(e),a&&(a=JSON.parse(a),!u(a)))?t.resolve(a.commentData):t.reject("cache miss")},d=function(t,e){r.LOCAL_STORAGE_SUPPORTED&&sessionStorage.setItem(t,JSON.stringify({timestamp:(new Date).getTime(),commentData:e}))},h=function(t){return t.length>0?t.sort(function(t,e){return new Date(t.commit.committer.date)>new Date(e.commit.committer.date)})[0]:void 0},p=function(t,e){var a=function(t){return 1===t?"":"s"},n=6e4,r=60*n,o=24*r,i=30*o,c=365*o,m=15e3,s=t-e,u=0;return n>s?m>s?" just now":Math.round(s/1e3)+" seconds ago":r>s?(u=Math.round(s/n),u+" minute"+a(u)+" ago"):o>s?(u=Math.round(s/r),u+" hour"+a(u)+" ago"):i>s?(u=Math.round(s/o),u+" day"+a(u)+" ago"):c>s?(u=Math.round(s/i),"about "+u+" month"+a(u)+" ago"):(u=Math.round(s/c),"about "+u+" year"+a(u)+" ago")},f=function(t,e,a){document.getElementById("talaria-show-"+t).addEventListener("click",function(e){a&&(e.preventDefault(),document.querySelector("#talaria-wrap-"+t+" .talaria-comment-list-wrapper").classList.remove("hide"),this.classList.add("hide"))}),document.getElementById("talaria-add-"+t).addEventListener("click",function(){r.LOCAL_STORAGE_SUPPORTED&&sessionStorage.removeItem(e)})},E=function(t){t=t||{};for(var e=1;e<arguments.length;e++)if(arguments[e])for(var a in arguments[e])arguments[e].hasOwnProperty(a)&&(t[a]=arguments[e][a]);return t},v=function(t){return new Promise(function(e,a){var n=new XMLHttpRequest;n.open("GET",t,!0),n.setRequestHeader("Accept","application/vnd.github.v3.html+json"),n.onload=function(){n.status>=200&&n.status<400?e(JSON.parse(n.responseText)):a(n)},n.onerror=function(){a(n)},n.send()})},T=function(t){for(var e=t;null!==e.parentNode;){if("ARTICLE"===e.parentElement.tagName)return e.parentNode;e=e.parentNode}return e};e.prototype=Object.create(Error.prototype),e.prototype.constructor=e,a.prototype=Object.create(Error.prototype),a.prototype.constructor=a;var _=function(t,e,a,n){var r=document.createElement("div");return n=0===a||n,r.innerHTML='<div id="talaria-wrap-'+t+'" class="talaria-wrapper"> <div class="talaria-load-error hide"> Unable to retrieve comments for this post. </div> <div class="talaria-comment-count'+(n?"":" hide")+'"> <a id="talaria-show-'+t+'" href="'+e+'">'+(0===a?"Be the first to comment":a+" comment"+(1===a?"":"s"))+'</a> </div> <div class="talaria-comment-list-wrapper'+(n?" hide":"")+'"> <div class="talaria-header"> <h3>Comments <small>via <a class="talaria-last-commit-href" href="'+e+'">github</a></small></h3> </div> <div class="talaria-comment-list" id="talaria-comment-list-'+t+'"> <!-- comments are dynamically added here --> </div> <div class="talaria-align-right"> <a id="talaria-add-'+t+'" class="talaria-add-comment-button" href="'+e+'"> <button type="submit">Add a Comment</button> </a> </div> </div></div>',r},I=function(t){var e,a=(new Date).getTime();return e=void 0!==t.commit_id?'<span class="talaria-header-left">&nbsp;commented on <a class="talaria-commit-sha" href="'+t.html_url+'"><code>'+m(t.commit_id)+"</code></a></span>":'<span class="talaria-header-left">&nbsp;wrote</span>','<div id="'+t.id+'" class="talaria-comment-bubble"> <a class="talaria-author-nick" href="'+t.user.html_url+'"> <img class="talaria-comment-author-avatar" height="48" width="48" src="'+t.user.avatar_url+'" /> </a> <div class="talaria-comment-bubble-content"> <div class="talaria-comment-bubble-inner"> <div class="talaria-comment-header"> <a class="talaria-author-nick" href="'+t.user.html_url+'"><b>'+t.user.login+"</b></a>"+e+' <span class="talaria-header-right">'+p(a,new Date(t.updated_at))+'</span> </div> <div class="talaria-comment-body">'+(t.body_html||t.body)+"</div> </div> </div></div>"},g=function(){return O().map(function(t){return l(t.getAttribute("href")).then(function(e){return A(t,e),e.comments}).catch(function(){return S(t).map(function(t){return L(t)}).reduce(function(t,e){var a=e.commit;return t.comments=t.comments.concat(e.comments),t.commits.push({sha:a.sha,commit:{committer:{date:a.commit.committer.date},comment_count:a.commit.comment_count}}),t},{comments:[],commits:[]}).then(function(e){var a={comments:e.comments,commits:e.commits};return d(t.getAttribute("href"),a),A(t,a),e.comments}).catch(e,function(){var e=T(t),a=_("","",0,!1);e.appendChild(a);var n=e.querySelector("div.talaria-load-error"),r=e.querySelector("div.talaria-comment-count");n.innerHTML="The Github API rate-limit has been reached. Unable to load comments.",n.classList.remove("hide"),r.classList.add("hide")}).catch(a,function(){var e=T(t),a=_("","",0,!1);e.appendChild(a);var n=e.querySelector("div.talaria-load-error"),r=e.querySelector("div.talaria-comment-count");n.innerHTML="Unable to find commits for this post.",n.classList.remove("hide"),r.classList.add("hide")})})},{concurrency:2})},O=function(){for(var e=[],a=document.querySelectorAll(r.PERMALINK_IDENTIFIER),n=0;n<a.length;n++)e.push(a[n]);return t.resolve(e)},S=function(o){var i=c(o.href);return v(r.COMMIT_API_ENDPOINT+"?path="+i).then(function(e){if(0===e.length)throw new a;return t.resolve(e)}).catch(n,function(){throw new e})},L=function(t){return t.commit.comment_count>0?v(r.COMMIT_API_ENDPOINT+"/"+t.sha+"/comments").then(function(e){return{commit:t,comments:e}}).catch(n,function(){throw new e}).catch(function(){return{commit:t,comments:[]}}):{commit:t,comments:[]}},A=function(t,e){var a=e.comments,n=h(e.commits),o=r.REPO_COMMIT_URL_ROOT+n.sha+"#all_commit_comments",i=_(n.sha,o,a.length,"/"===location.pathname||r.PAGINATION_SCHEME.test(location.pathname)),c=a.map(I).join(""),m=T(t);m.appendChild(i),document.getElementById("talaria-comment-list-"+n.sha).innerHTML=c,f(n.sha,t.href,a.length>0)},N=function(){return v(r.GIST_MAPPINGS).then(function(t){return b(t)}).map(function(t){var e=t.gist;return l(e.permalink).catch(function(){return M(e)}).then(function(e){return w(t.linkobj,e),e}).catch(function(a){return e.comments=[],P(t.linkobj,a,e),e})},{concurrency:2}).catch(function(){return R(),[]})},M=function(t){return v("https://api.github.com/gists/"+t.id+"/comments").then(function(e){return t.comments=e,d(t.permalink,t),t})},b=function(t){var e,a=[];for(var n in t)if(t.hasOwnProperty(n)){e=t[n];var o=document.querySelector(r.PERMALINK_IDENTIFIER+'[href="'+e.permalink+'"]');null!==o&&a.push({gist:t[n],linkobj:o})}return a},R=function(){for(var t=_("","",0,!1),e=document.querySelectorAll(r.PERMALINK_IDENTIFIER),a=0;a<e.length;a++)e[a].appendChild(t.cloneNode(!0));for(var n=document.querySelectorAll("div.talaria-wrapper div.talaria-load-error"),o=0;o<n.length;o++)n[o].innerHTML="Unable to load comments.",n[o].classList.remove("hide");document.querySelector("div.talaria-wrapper div.talaria-comment-count").classList.add("hide")},P=function(t,e,a){var n=r.GIST_URL_ROOT+a.id+"#comments",o=_(a.id,n,a.comments.length,"/"===location.pathname||r.PAGINATION_SCHEME.test(location.pathname));T(t).appendChild(o);var i=document.querySelector("#talaria-wrap-"+a.id+" div.talaria-load-error");switch(i.classList.remove("hide"),document.querySelector("#talaria-wrap-"+a.id+" div.talaria-comment-count").classList.add("hide"),e.status){case 403:i.innerHTML="The Github API rate-limit has been reached. Unable to load comments.";break;case 404:i.innerHTML="Unable to find a matching gist.";break;default:i.innerHTML="An error occurred retrieving comments for this post."}},w=function(t,e){var a=r.GIST_URL_ROOT+e.id+"#comments",n=_(e.id,a,e.comments.length,"/"===location.pathname||r.PAGINATION_SCHEME.test(location.pathname)),o=e.comments.map(I).join("");T(t).appendChild(n),document.getElementById("talaria-comment-list-"+e.id).innerHTML=o,f(e.id,e.permalink,e.comments.length>0)},y=function(t){r=E({},o,t),r.GISTS_API_ENDPOINT="https://api.github.com/users/"+r.GITHUB_USERNAME+"/gists",r.COMMIT_API_ENDPOINT="https://api.github.com/repos/"+r.GITHUB_USERNAME+"/"+r.REPOSITORY_NAME+"/commits",r.REPO_COMMIT_URL_ROOT="https://github.com/"+r.GITHUB_USERNAME+"/"+r.REPOSITORY_NAME+"/commit/",r.GIST_URL_ROOT="https://gist.github.com/"+r.GITHUB_USERNAME+"/",r.PERMALINK_STYLE=i()},C=function(t){return y(t),s&&(r.LOCAL_STORAGE_SUPPORTED=!0),r.USE_GISTS?N():g()};return{init:C}}(P);