aboutsummaryrefslogtreecommitdiff
path: root/dist/utah
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2020-06-03 11:09:19 -0600
committerMelody Horn <melody@boringcactus.com>2020-06-03 11:09:19 -0600
commitc1dcdab60be115395b6730b0fae274b11f57b0eb (patch)
tree525fef9ade54f00fec43cc1988eb7405d5b86344 /dist/utah
parent7aa98542c5e871ae0cdd49e495312525cb1ee1e9 (diff)
downloadpig.observer-c1dcdab60be115395b6730b0fae274b11f57b0eb.tar.gz
pig.observer-c1dcdab60be115395b6730b0fae274b11f57b0eb.zip
refactor out common script pieces
Diffstat (limited to 'dist/utah')
-rw-r--r--dist/utah/index.html2
-rw-r--r--dist/utah/script.js166
-rw-r--r--dist/utah/sources.js1
3 files changed, 2 insertions, 167 deletions
diff --git a/dist/utah/index.html b/dist/utah/index.html
index 52876bb..8853b1f 100644
--- a/dist/utah/index.html
+++ b/dist/utah/index.html
@@ -40,7 +40,7 @@
</footer>
<script src="sources.js"></script>
- <script src="script.js" defer></script>
+ <script src="/script.js" defer></script>
<script src="/dragula.min.js" integrity="sha256-ug4bHfqHFAj2B5MESRxbLd3R3wdVMQzug2KHZqFEmFI=" crossorigin="anonymous"></script>
</body>
</html>
diff --git a/dist/utah/script.js b/dist/utah/script.js
deleted file mode 100644
index 1701a3a..0000000
--- a/dist/utah/script.js
+++ /dev/null
@@ -1,166 +0,0 @@
-/* global dragula, CAMERAS */
-
-const STATE = loadState();
-
-function loadState() {
- let hash = document.location.hash;
- if (hash.length === 0) {
- const DEFAULTS = [139];
- history.replaceState(null, "", "#" + DEFAULTS.join(","));
- return DEFAULTS;
- }
- hash = hash.replace(/^#/, "");
- if (hash.length === 0) {
- return [];
- }
- return hash.split(",").map(x => parseInt(x));
-}
-
-function handleCheckbox(evt) {
- const id = parseInt(evt.target.dataset.id);
- if (evt.target.checked) {
- if (!STATE.includes(id)) {
- appendCamera(id);
- }
- } else {
- if (STATE.includes(id)) {
- removeCamera(id);
- }
- }
-}
-
-function findCamera(id) {
- for (let cameras of Object.values(CAMERAS)) {
- let found = cameras.find(x => x.id === id);
- if (found !== undefined) {
- return found;
- }
- }
-}
-
-function loadData() {
- const nav = document.querySelector("nav");
- for (let neighborhood of Object.keys(CAMERAS)) {
- const section = document.createElement("section");
- const h2 = document.createElement("h2");
- h2.innerText = neighborhood;
- section.append(h2);
-
- const boxes = document.createElement("ul");
- section.append(boxes);
- for (let { id, stream, name } of CAMERAS[neighborhood]) {
- const label = document.createElement("label");
- const checkbox = document.createElement("input");
- checkbox.type = "checkbox";
- checkbox.dataset.id = id;
- checkbox.addEventListener("input", handleCheckbox);
- checkbox.checked = STATE.includes(id);
- label.append(checkbox);
- const text = document.createTextNode(name);
- label.append(text);
-
- const li = document.createElement("li");
- li.append(label);
- boxes.append(li);
- }
-
- nav.append(section);
- }
-
- for (let id of STATE) {
- let { url, name } = findCamera(id);
- document.querySelector("main").append(makeCameraStream(id, url, name));
- }
-}
-
-function makeCameraStream(id, url, title) {
- var section = document.createElement("section");
- section.dataset.id = id;
- var header = document.createElement("h2");
- header.innerText = title;
- var close = document.createElement("button");
- close.className = 'close';
- close.type = "button";
- close.innerText = "X";
- header.append(close);
- close.addEventListener('click', evt => {
- removeCamera(parseInt(evt.target.parentNode.parentNode.dataset.id));
- });
- section.append(header);
- var img = document.createElement("img");
- img.className = 'reload';
- img.src = url;
- section.append(img);
- return section;
-}
-
-function appendCamera(id) {
- STATE.push(id);
- history.replaceState(null, "", "#" + STATE.join(","));
- let { url, name } = findCamera(id);
- document.querySelector("main").append(makeCameraStream(id, url, name));
-}
-function removeCamera(id) {
- const oldIndex = STATE.indexOf(id);
- if (oldIndex > -1) {
- STATE.splice(oldIndex, 1);
- }
- history.replaceState(null, "", "#" + STATE.join(","));
- for (let video of document.querySelectorAll("main section")) {
- if (video.dataset.id === id) {
- video.remove();
- }
- }
- for (let checkbox of document.querySelectorAll('nav input')) {
- if (checkbox.dataset.id === id) {
- checkbox.checked = false;
- }
- }
-}
-
-document.getElementById("vidwidth").addEventListener("input", e => {
- document.body.style.setProperty("--video-width", e.target.value + "vw");
-});
-document.getElementById("filter").addEventListener("input", e => {
- let filter = e.target.value;
- for (let neighborhood of document.querySelectorAll("nav section")) {
- neighborhood.hidden = true;
- for (let box of neighborhood.querySelectorAll("li")) {
- const matches = box.innerText
- .toLowerCase()
- .includes(filter.toLowerCase());
- box.hidden = !matches;
- neighborhood.hidden = neighborhood.hidden && !matches;
- }
- }
-});
-
-loadData();
-
-const dragHandler = dragula([document.querySelector("main")], {
- moves(el, container, handle) {
- return handle.tagName.toLowerCase() === "h2";
- }
-});
-dragHandler.on("drop", (el, target, source, sibling) => {
- const myID = parseInt(el.dataset.id);
- const neighborID = parseInt(sibling.dataset.id);
- const oldIndex = STATE.indexOf(myID);
- if (oldIndex > -1) {
- STATE.splice(oldIndex, 1);
- }
- const newIndex = STATE.indexOf(neighborID);
- if (newIndex > -1) {
- STATE.splice(newIndex, 0, myID);
- } else {
- console.log("uhhhhh fuck");
- }
- history.replaceState(null, "", "#" + STATE.join(","));
-});
-
-function pokeImages() {
- for (let img of document.querySelectorAll('img.reload')) {
- img.src = img.src.replace(/(\?0\.\d+)?$/, '?' + Math.random());
- }
-}
-setInterval(pokeImages, 1000);
diff --git a/dist/utah/sources.js b/dist/utah/sources.js
index 712f4e4..d48603f 100644
--- a/dist/utah/sources.js
+++ b/dist/utah/sources.js
@@ -1,3 +1,4 @@
+const DEFAULTS = ["139"];
const CAMERAS = {
Richfield: [
{