Add GithubSourceFacts, which abstracts fact fetching for Github sources

This commit is contained in:
Brendan Abbott
2016-10-23 19:35:41 +10:00
parent 95435e7035
commit 3537235d78
10 changed files with 998 additions and 739 deletions

View File

@@ -29,6 +29,7 @@ import lunr from "lunr"
// import Expander from "./components/expander"
import GithubSourceFacts from "./components/GithubSourceFacts"
import Material from "./components/Material"
// import Search from './components/search';
@@ -39,12 +40,41 @@ import Material from "./components/Material"
class Application {
/**
* Constructor.
*
* @constructor
* @param {object} config Configuration object
* @return {void}
*/
constructor(config) {
this.config = config
}
/**
* @return {void}
*/
initialize() {
const material = new Material()
material.initialize()
if (this.hasGithubRepo()) {
const githubSource = new GithubSourceFacts(
this.config.storage,
this.config.repo.url
)
githubSource.initialize()
}
}
/**
* Is this application about a Github repository?
*
* @return {bool} - true if `repo.icon` or `repo.url` contains 'github'
*/
hasGithubRepo() {
return this.config.repo.icon === "github"
|| this.config.repo.url.includes("github")
}
}
@@ -185,48 +215,6 @@ document.addEventListener("DOMContentLoaded", () => {
})
// setTimeout(function() {
fetch("https://api.github.com/repos/squidfunk/mkdocs-material")
.then(response => {
return response.json()
})
.then(data => {
// console.log(data)
const stars = data.stargazers_count
const forks = data.forks_count
// store in session!!!
const lists = document.querySelectorAll(".md-source__facts"); // TODO 2x list in drawer and header
[].forEach.call(lists, list => {
let li = document.createElement("li")
li.className = "md-source__fact md-source__fact--hidden"
li.innerText = `${stars} Stars`
list.appendChild(li)
setTimeout(lix => {
lix.classList.remove("md-source__fact--hidden")
}, 100, li)
li = document.createElement("li")
li.className = "md-source__fact md-source__fact--hidden"
li.innerText = `${forks} Forks`
list.appendChild(li)
setTimeout(lix => {
lix.classList.remove("md-source__fact--hidden")
}, 500, li)
})
// setTimeout(function() {
// li.classList.remove('md-source__fact--hidden');
// }, 100);
})
.catch(() => {
// console.log("parsing failed", ex)
})
// setTimeout(function() {
fetch("/mkdocs/search_index.json") // TODO: prepend BASE URL!!!
.then(response => {
return response.json()

View File

@@ -0,0 +1,108 @@
/*
* Copyright (c) 2016 Martin Donath <martin.donath@squidfunk.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
/* ----------------------------------------------------------------------------
* Github Source
* ------------------------------------------------------------------------- */
export default
class GithubSourceFacts {
/**
* Constructor.
*
* @constructor
* @param {object} storage - Accessor to storage, eg. `window.sessionStorage`
* @param {string} repoUrl - URL to Github repository
*/
constructor(storage, repoUrl) {
this.storage = storage
this.storageKey = "github-source-facts"
this.apiRepoUrl = repoUrl.replace("github.com/", "api.github.com/repos/")
}
/**
* Retrieve stars and fork counts for the repository before invoking
* `GithubSourceFacts.paint`.
*
* @return {void}
*/
initialize() {
const facts = this.storage.getItem(this.storageKey)
// Retrieve the facts, then invoke paint
if (!facts) {
fetch(this.apiRepoUrl)
.then(response => {
return response.json()
})
.then(data => {
const repoFacts = {
stars: data.stargazers_count,
forks: data.forks_count
}
this.storage.setItem(this.storageKey, JSON.stringify(repoFacts))
GithubSourceFacts.paint(repoFacts)
})
.catch(() => {
// console.log("parsing failed", ex)
})
// Use the cached facts
} else {
GithubSourceFacts.paint(JSON.parse(facts))
}
}
/**
* Populates `.md-source__facts` with star and fork counts.
*
* @param {integer} options.stars - Stars count for the repo
* @param {integer} options.forks - Fork count for the repo
* @return {void}
*/
static paint({ stars, forks }) {
const lists = document.querySelectorAll(".md-source__facts"); // TODO 2x list in drawer and header
[].forEach.call(lists, list => {
let li = document.createElement("li")
li.className = "md-source__fact md-source__fact--hidden"
li.innerText = `${stars} Stars`
list.appendChild(li)
setTimeout(lix => {
lix.classList.remove("md-source__fact--hidden")
}, 100, li)
li = document.createElement("li")
li.className = "md-source__fact md-source__fact--hidden"
li.innerText = `${forks} Forks`
list.appendChild(li)
setTimeout(lix => {
lix.classList.remove("md-source__fact--hidden")
}, 500, li)
})
}
}