From 67fdc071d4a8ec9046616d96d295fdd0356f04d6 Mon Sep 17 00:00:00 2001 From: Henri Bergius Date: Mon, 4 Dec 2017 14:23:30 +0100 Subject: [PATCH] Web component for visualizing crew members online --- elements/userlist.js | 73 ++++++++++++++++++++++++++++++++++++++++++++ index.js | 2 ++ 2 files changed, 75 insertions(+) create mode 100644 elements/userlist.js diff --git a/elements/userlist.js b/elements/userlist.js new file mode 100644 index 0000000..a8d7f1e --- /dev/null +++ b/elements/userlist.js @@ -0,0 +1,73 @@ +import { withComponent, props } from 'skatejs'; + +const Component = withComponent(); +class UserList extends Component { + static props = { + data: props.array, + interval: props.number, + }; + + connected() { + this.fetchData(); + if (!this.interval) { + return; + } + this.runInterval = window.setInterval(() => { + this.fetchData(); + }, this.interval * 1000); + } + + disconnecting() { + if (this.runInterval) { + window.clearInterval(this.runInterval); + delete this.runInterval; + } + } + + fetchData() { + const url = 'https://c-beam.cbrp3.c-base.org/mechblast_json'; + fetch(url) + .then(data => data.json()) + .then((data) => { + this.data = data.userlist; + }); + } + + renderer(renderRoot, render) { + const root = renderRoot; + while (root.firstChild) { + root.removeChild(root.firstChild); + } + root.appendChild(render()); + const styles = document.createElement('style'); + styles.appendChild(document.createTextNode(` + @-webkit-keyframes blinker { + from {opacity: 1.0;} + to {opacity: 0.0;} + } + blink{ + text-decoration: blink; + -webkit-animation-name: blinker; + -webkit-animation-duration: 0.6s; + -webkit-animation-iteration-count:infinite; + -webkit-animation-timing-function:ease-in-out; + -webkit-animation-direction: alternate; + } + `)) + root.appendChild(styles); + } + + render({ data }) { + const el = document.createElement('div'); + el.className = 'terminal'; + el.innerHTML = ` +
user@c-beam> #who
+
${data.join(', ')}
+
total: ${data.length}
+
user@c-beam> _
+ `; + return el; + } +} + +customElements.define('cbase-userlist', UserList); diff --git a/index.js b/index.js index 04a6266..f1624c0 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ import heatmap from './elements/heatmap'; import linechart from './elements/linechart'; import polar from './elements/polar'; import time from './elements/time'; +import userlist from './elements/userlist'; export default { currentstate, @@ -10,4 +11,5 @@ export default { linechart, polar, time, + userlist, };