-
-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up javascript importmaps for Dashboard; refactor Polling
- Loading branch information
1 parent
bd5cd69
commit ee89c21
Showing
9 changed files
with
164 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
function renderCharts(animate) { | ||
const charts = document.querySelectorAll('.chart'); | ||
|
||
for (let i = 0; i < charts.length; i++) { | ||
const chartEl = charts[i]; | ||
const chartData = JSON.parse(chartEl.dataset.json); | ||
|
||
const ctx = chartEl.getContext('2d'); | ||
const chart = new Chart(ctx, { | ||
type: 'line', | ||
data: { | ||
labels: chartData.labels, | ||
datasets: chartData.datasets | ||
}, | ||
options: { | ||
animation: animate, | ||
responsive: true, | ||
maintainAspectRatio: false, | ||
scales: { | ||
y: { | ||
beginAtZero: true | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export { renderCharts as default }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function documentReady(callback) { | ||
if (document.readyState !== "loading") { | ||
callback(); | ||
} else { | ||
document.addEventListener("DOMContentLoaded", callback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/*jshint esversion: 6, strict: false */ | ||
import renderCharts from "charts"; | ||
|
||
// NOTE: this file is a bit disorganized. Please do not use it as a template for how to organize a JS module. | ||
|
||
const DEFAULT_POLL_INTERVAL_SECONDS = 30; | ||
const MINIMUM_POLL_INTERVAL = 1000; | ||
|
||
function getStorage(key) { | ||
const value = localStorage.getItem('good_job-' + key); | ||
|
||
if (value === 'true') { | ||
return true; | ||
} else if (value === 'false') { | ||
return false; | ||
} else { | ||
return value; | ||
} | ||
} | ||
|
||
function setStorage(key, value) { | ||
localStorage.setItem('good_job-' + key, value); | ||
} | ||
|
||
function updatePageContent(newContent) { | ||
const domParser = new DOMParser(); | ||
const parsedDOM = domParser.parseFromString(newContent, "text/html"); | ||
|
||
const newElements = parsedDOM.querySelectorAll('[data-gj-poll-replace]'); | ||
|
||
for (let i = 0; i < newElements.length; i++) { | ||
const newEl = newElements[i]; | ||
const oldEl = document.getElementById(newEl.id); | ||
|
||
if (oldEl) { | ||
oldEl.replaceWith(newEl); | ||
} | ||
} | ||
|
||
renderCharts(false); | ||
} | ||
|
||
function refreshPage() { | ||
fetch(window.location.href) | ||
.then(resp => resp.text()) | ||
.then(updatePageContent); | ||
} | ||
|
||
const Poller = { | ||
start: () => { | ||
Poller.updateSettings(); | ||
Poller.pollUpdates(); | ||
|
||
const checkbox = document.querySelector('input[name="toggle-poll"]'); | ||
checkbox.addEventListener('change', Poller.togglePoll) | ||
}, | ||
|
||
togglePoll: (event) => { | ||
console.log("toggle"); | ||
Poller.pollEnabled = event.currentTarget.checked; | ||
setStorage('pollEnabled', Poller.pollEnabled); | ||
}, | ||
|
||
updateSettings: () => { | ||
const queryString = window.location.search; | ||
const urlParams = new URLSearchParams(queryString); | ||
|
||
if (urlParams.has('poll')) { | ||
const parsedInterval = (parseInt(urlParams.get('poll')) || DEFAULT_POLL_INTERVAL_SECONDS) * 1000; | ||
Poller.pollInterval = Math.max(parsedInterval, MINIMUM_POLL_INTERVAL); | ||
setStorage('pollInterval', Poller.pollInterval); | ||
|
||
Poller.pollEnabled = true; | ||
} else { | ||
Poller.pollInterval = getStorage('pollInterval') || (DEFAULT_POLL_INTERVAL_SECONDS * 1000); | ||
Poller.pollEnabled = getStorage('pollEnabled') || false; | ||
} | ||
|
||
document.getElementById('toggle-poll').checked = Poller.pollEnabled; | ||
}, | ||
|
||
pollUpdates: () => { | ||
setTimeout(() => { | ||
if (Poller.pollEnabled === true) { | ||
refreshPage(); | ||
Poller.pollUpdates(); | ||
} else { | ||
Poller.pollUpdates(); | ||
} | ||
}, Poller.pollInterval); | ||
}, | ||
}; | ||
|
||
export { Poller as default }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,11 @@ | ||
/*jshint esversion: 6, strict: false */ | ||
const GOOD_JOB_DEFAULT_POLL_INTERVAL_SECONDS = 30; | ||
const GOOD_JOB_MINIMUM_POLL_INTERVAL = 1000; | ||
|
||
const GoodJob = { | ||
// Register functions to execute when the DOM is ready | ||
ready: (callback) => { | ||
if (document.readyState !== "loading") { | ||
callback(); | ||
} else { | ||
document.addEventListener("DOMContentLoaded", callback); | ||
} | ||
}, | ||
import renderCharts from "charts"; | ||
import documentReady from "document_ready" | ||
import Poller from "poller"; | ||
import start from "./modules/poller"; | ||
|
||
init: () => { | ||
GoodJob.updateSettings(); | ||
GoodJob.addListeners(); | ||
GoodJob.pollUpdates(); | ||
GoodJob.renderCharts(true); | ||
}, | ||
|
||
addListeners: () => { | ||
const gjActionEls = document.querySelectorAll('[data-gj-action]'); | ||
|
||
for (let i = 0; i < gjActionEls.length; i++) { | ||
const el = gjActionEls[i]; | ||
const [eventName, func] = el.dataset.gjAction.split('#'); | ||
|
||
el.addEventListener(eventName, GoodJob[func]); | ||
} | ||
}, | ||
|
||
updateSettings: () => { | ||
const queryString = window.location.search; | ||
const urlParams = new URLSearchParams(queryString); | ||
|
||
// live poll interval and enablement | ||
if (urlParams.has('poll')) { | ||
const parsedInterval = (parseInt(urlParams.get('poll')) || GOOD_JOB_DEFAULT_POLL_INTERVAL_SECONDS) * 1000; | ||
GoodJob.pollInterval = Math.max(parsedInterval, GOOD_JOB_MINIMUM_POLL_INTERVAL); | ||
GoodJob.setStorage('pollInterval', GoodJob.pollInterval); | ||
|
||
GoodJob.pollEnabled = true; | ||
} else { | ||
GoodJob.pollInterval = GoodJob.getStorage('pollInterval') || (GOOD_JOB_DEFAULT_POLL_INTERVAL_SECONDS * 1000); | ||
GoodJob.pollEnabled = GoodJob.getStorage('pollEnabled') || false; | ||
} | ||
|
||
document.getElementById('toggle-poll').checked = GoodJob.pollEnabled; | ||
}, | ||
|
||
togglePoll: (ev) => { | ||
GoodJob.pollEnabled = ev.currentTarget.checked; | ||
GoodJob.setStorage('pollEnabled', GoodJob.pollEnabled); | ||
}, | ||
|
||
pollUpdates: () => { | ||
setTimeout(() => { | ||
if (GoodJob.pollEnabled === true) { | ||
fetch(window.location.href) | ||
.then(resp => resp.text()) | ||
.then(GoodJob.updateContent) | ||
.finally(GoodJob.pollUpdates); | ||
} else { | ||
GoodJob.pollUpdates(); | ||
} | ||
}, GoodJob.pollInterval); | ||
}, | ||
|
||
updateContent: (newContent) => { | ||
const domParser = new DOMParser(); | ||
const parsedDOM = domParser.parseFromString(newContent, "text/html"); | ||
|
||
const newElements = parsedDOM.querySelectorAll('[data-gj-poll-replace]'); | ||
|
||
for (let i = 0; i < newElements.length; i++) { | ||
const newEl = newElements[i]; | ||
const oldEl = document.getElementById(newEl.id); | ||
|
||
if (oldEl) { | ||
oldEl.replaceWith(newEl); | ||
} | ||
} | ||
|
||
GoodJob.renderCharts(false); | ||
}, | ||
|
||
renderCharts: (animate) => { | ||
const charts = document.querySelectorAll('.chart'); | ||
|
||
for (let i = 0; i < charts.length; i++) { | ||
const chartEl = charts[i]; | ||
const chartData = JSON.parse(chartEl.dataset.json); | ||
|
||
const ctx = chartEl.getContext('2d'); | ||
const chart = new Chart(ctx, { | ||
type: 'line', | ||
data: { | ||
labels: chartData.labels, | ||
datasets: chartData.datasets | ||
}, | ||
options: { | ||
animation: animate, | ||
responsive: true, | ||
maintainAspectRatio: false, | ||
scales: { | ||
y: { | ||
beginAtZero: true | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
}, | ||
|
||
getStorage: (key) => { | ||
const value = localStorage.getItem('good_job-' + key); | ||
|
||
if (value === 'true') { | ||
return true; | ||
} else if (value === 'false') { | ||
return false; | ||
} else { | ||
return value; | ||
} | ||
}, | ||
|
||
setStorage: (key, value) => { | ||
localStorage.setItem('good_job-' + key, value); | ||
} | ||
}; | ||
|
||
GoodJob.ready(GoodJob.init); | ||
documentReady(function() { | ||
renderCharts(); | ||
Poller.start(); | ||
}); |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters