forked from bensheldon/good_job
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for live polling the dashboard
Adds support for live polling the dashboard, adding replaceable content areas anywhere on the page, register event listeners which act upon the `GoodJob` javacript object, and a footer. ### Polling UX 1. Toggle the `Live Poll` checkbox in the navbar (every 5000ms by default) 2. Add a query parameter to the url `/good_job?poll=5000`. The integer (in ms) sets the poll interval. ### Register event listeners 1. Add `data-gj-action` attribute with the event name and the `GoodJob` function to call when the event is triggered. `change#togglePoll` => `element.addEventListener('change, GoodJob.togglePoll)` ### Adding replaceable content areas 1. Add the `data-gj-poll-replace` attribute to the element 2. Add a unique ID to the same element Fixes bensheldon#526
- Loading branch information
1 parent
b921729
commit cfc1674
Showing
2 changed files
with
100 additions
and
3 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 |
---|---|---|
@@ -1 +1,76 @@ | ||
GoodJob = {}; | ||
GoodJob = { | ||
// Register functions to execute when the DOM is ready | ||
ready: (callback) => { | ||
if (document.readyState != "loading"){ | ||
callback() | ||
} else { | ||
document.addEventListener("DOMContentLoaded", callback) | ||
} | ||
}, | ||
|
||
init: () => { | ||
GoodJob.updateSettings() | ||
GoodJob.addListeners() | ||
GoodJob.pollUpdates() | ||
}, | ||
|
||
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) | ||
|
||
// livepoll interval and enablement | ||
if (urlParams.has('poll')) { | ||
GoodJob.pollEnabled = true | ||
GoodJob.pollInterval = parseInt(urlParams.get('poll')) | ||
} else { | ||
GoodJob.pollEnabled = false | ||
GoodJob.pollInterval = 5000 // default 5sec | ||
} | ||
}, | ||
|
||
togglePoll: (ev) => { | ||
GoodJob.pollEnabled = ev.currentTarget.checked | ||
}, | ||
|
||
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.parentNode.replaceChild(newEl, oldEl) | ||
} | ||
} | ||
} | ||
}; | ||
|
||
GoodJob.ready(GoodJob.init) |
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