toot-my-t-shirt/static/js/sb.js

378 lines
11 KiB
JavaScript
Raw Permalink Normal View History

2019-12-22 10:46:03 +01:00
var DEFAULT_DELAY = 20;
2019-12-22 13:12:12 +01:00
var ACCOUNT_ID = "133328";
2019-11-17 10:45:00 +01:00
var Countdown = {
2018-11-28 21:55:54 +01:00
_timeout: null,
_stepCb: null,
_timeoutCb: null,
running: false,
2019-11-17 10:45:00 +01:00
seconds: DEFAULT_DELAY,
_initial_seconds: DEFAULT_DELAY,
2018-11-28 21:55:54 +01:00
start: function(seconds, timeoutCb, stepCb) {
Countdown.stop();
2019-11-17 10:45:00 +01:00
Countdown.seconds = Countdown._initial_seconds = seconds || DEFAULT_DELAY;
Countdown._timeoutCb = timeoutCb || Countdown._timeoutCb;
Countdown._stepCb = stepCb || Countdown._stepCb;
Countdown.running = true;
Countdown._step();
2018-11-28 21:55:54 +01:00
},
stop: function() {
if (Countdown._timeout) {
window.clearTimeout(Countdown._timeout);
2018-11-28 21:55:54 +01:00
}
Countdown.running = false;
2018-11-28 21:55:54 +01:00
},
restart: function() {
Countdown.start(Countdown._initial_seconds);
2018-11-28 21:55:54 +01:00
},
_step: function() {
if (Countdown._stepCb) {
Countdown._stepCb();
2018-11-28 21:55:54 +01:00
}
if (Countdown.seconds === 0) {
if (Countdown._timeoutCb) {
Countdown._timeoutCb();
2018-11-28 21:55:54 +01:00
}
Countdown.stop();
2018-11-28 21:55:54 +01:00
} else {
Countdown._decrement();
2018-11-28 21:55:54 +01:00
}
},
_decrement: function() {
Countdown.seconds = Countdown.seconds - 1;
Countdown._timeout = window.setTimeout(function() {
Countdown._step();
2018-11-28 21:55:54 +01:00
}, 1000);
}
};
function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
var UUID = uuidv4();
function getWSPath() {
var loc = window.location, new_uri;
if (loc.protocol === "https:") {
new_uri = "wss:";
} else {
new_uri = "ws:";
}
new_uri += "//" + loc.host + "/ws?uuid=" + UUID;
return new_uri;
}
2019-11-02 11:58:29 +01:00
var WS = new WebSocket(getWSPath());
WS.onerror = function(error) {
console.log('WebSocket Error ' + error);
};
WS.onmessage = function(e) {
console.log("received message on websocket: " + e.data);
var jdata = JSON.parse(e.data);
if (!(jdata && jdata.source == "button" && jdata.action == "clicked")) {
return;
}
if (!Countdown.running) {
takePhoto("press again to publish!");
} else {
sendPhoto();
}
};
2018-11-28 21:55:54 +01:00
function runCamera(stream) {
console.log("initialize the camera");
2018-12-01 11:18:30 +01:00
var video = document.querySelector("#sb-video");
2018-12-01 11:59:20 +01:00
var container = document.querySelector("#canvas-container");
container.width = video.videoWidth;
2018-11-28 21:55:54 +01:00
video.onloadedmetadata = function() {
video.play();
};
video.srcObject = stream;
2019-11-01 21:25:12 +01:00
resizeCanvas(video, 'sb-canvas');
2018-11-28 21:55:54 +01:00
}
function sendData(data) {
var formData = new FormData();
2018-12-01 11:18:30 +01:00
var msg = "";
2018-11-28 21:55:54 +01:00
formData.append("selfie", new Blob([data]), "selfie.jpeg");
fetch("/publish/", {
method: "POST",
body: formData
}).then(function(response) {
if (response.status !== 200) {
msg = response.status;
2018-12-01 11:18:30 +01:00
console.log(msg);
iziToast.error({
"title": "😭 something wrong sending the data 😭",
2019-12-22 10:46:03 +01:00
"target": "#izi-container",
"message": msg,
"titleSize": "3em",
"messageSize": "2em",
"close": false,
"drag": false,
"pauseOnHover": false,
2019-12-22 10:46:03 +01:00
"progressBarColor": "red",
"position": "topCenter"
});
2018-12-12 21:14:00 +01:00
}
cancelPhoto();
return response.json();
}).then(function(json) {
json = json || {};
console.log(json);
if (json && json.success) {
msg = "❤ ❤ ❤ photo sent successfully! ❤ ❤ ❤";
console.log(msg);
iziToast.destroy();
iziToast.success({
"title": msg,
2019-12-22 10:46:03 +01:00
"target": "#izi-container",
"titleSize": "3em",
"messageSize": "2em",
"close": false,
"drag": false,
"pauseOnHover": false,
2019-12-22 10:46:03 +01:00
"progressBarColor": "red",
"position": "topCenter"
});
2019-12-22 13:12:12 +01:00
updateFeed();
2018-11-28 21:55:54 +01:00
} else {
msg = json.message;
2018-12-12 21:14:00 +01:00
console.log(msg);
iziToast.error({
"title": "😭 backend error 😭",
2019-12-22 10:46:03 +01:00
"target": "#izi-container",
"message": msg,
"titleSize": "3em",
"messageSize": "2em",
"close": false,
"drag": false,
"pauseOnHover": false,
2019-12-22 10:46:03 +01:00
"progressBarColor": "red",
"position": "topCenter"
});
2018-11-28 21:55:54 +01:00
}
}).catch(function(err) {
2019-12-22 13:12:12 +01:00
console.log(err);
iziToast.error({
"title": "😭 error connecting to the server 😭",
2019-12-22 10:46:03 +01:00
"target": "#izi-container",
"message": err,
"titleSize": "3em",
"messageSize": "2em",
"close": false,
"drag": false,
"pauseOnHover": false,
2019-12-22 10:46:03 +01:00
"progressBarColor": "red",
"position": "topCenter"
});
2018-11-28 21:55:54 +01:00
cancelPhoto();
});
}
2019-12-22 13:12:12 +01:00
function cancelPhoto(clearToast) {
2018-11-28 21:55:54 +01:00
console.log("cancel photo");
2018-12-01 11:18:30 +01:00
document.querySelector("#send-photo-btn").classList.add("disabled");
document.querySelector("#cancel-photo-btn").classList.add("disabled");
var canvas = document.querySelector("#sb-canvas");
2018-11-28 21:55:54 +01:00
var context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
Countdown.stop();
2019-12-22 13:12:12 +01:00
if (clearToast) {
iziToast.destroy();
}
2018-11-28 21:55:54 +01:00
}
function isBlank(canvas) {
var blank = document.createElement("canvas");
blank.width = canvas.width;
blank.height = canvas.height;
return canvas.toDataURL() == blank.toDataURL();
}
function sendPhoto() {
console.log("send photo");
Countdown.stop();
2018-12-01 11:18:30 +01:00
var canvas = document.querySelector("#sb-canvas");
if (!canvas || isBlank(canvas)) {
2018-12-01 11:18:30 +01:00
var msg = "I cowardly refuse to send a blank image.";
console.log(msg);
iziToast.warning({
"title": msg,
2019-12-22 10:46:03 +01:00
"target": "#izi-container",
"titleSize": "3em",
"messageSize": "2em",
"close": false,
"drag": false,
"pauseOnHover": false,
2019-12-22 10:46:03 +01:00
"progressBarColor": "red",
"position": "topCenter"
});
2018-11-28 21:55:54 +01:00
return;
}
return sendData(canvas.toDataURL("image/jpeg"));
}
function _takePhoto(message) {
2018-11-28 21:55:54 +01:00
console.log("take photo");
2018-12-01 11:18:30 +01:00
var video = document.querySelector("#sb-video");
if (!(video.offsetWidth && video.offsetHeight)) {
var msg = "missing video";
console.log(msg);
iziToast.warning({
"title": msg,
2019-12-22 10:46:03 +01:00
"target": "#izi-container",
"message": "please grant camera permissions",
"titleSize": "3em",
"messageSize": "2em",
"close": false,
"drag": false,
"pauseOnHover": false,
2019-12-22 10:46:03 +01:00
"progressBarColor": "red",
"position": "topCenter"
});
return;
}
2018-12-01 11:18:30 +01:00
var canvas = document.querySelector("#sb-canvas");
2018-12-01 11:59:20 +01:00
var context = canvas.getContext("2d");
canvas.width = video.offsetWidth;
canvas.height = video.offsetHeight;
/*
2018-11-28 21:55:54 +01:00
var tmpCanvas = document.createElement("canvas");
tmpCanvas.width = video.offsetWidth;
tmpCanvas.height = video.offsetHeight;
var tmpContext = tmpCanvas.getContext("2d");
var tmpRatio = (tmpCanvas.height / tmpCanvas.width);
tmpContext.drawImage(video, 0, 0, video.offsetWidth, video.offsetHeight);
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
canvas.style.height = parseInt(canvas.offsetWidth * tmpRatio);
var scale = canvas.width / tmpCanvas.width;
context.scale(scale, scale);
context.drawImage(tmpCanvas, 0, 0);
2018-12-01 11:59:20 +01:00
*/
context.drawImage(video, 0, 0, video.offsetWidth, video.offsetHeight);
2018-12-01 11:18:30 +01:00
document.querySelector("#send-photo-btn").classList.remove("disabled");
document.querySelector("#cancel-photo-btn").classList.remove("disabled");
iziToast.question({
2019-12-22 10:46:03 +01:00
"title": "do you like it?",
"timeout": DEFAULT_DELAY * 1000,
"target": "#izi-container",
"message": message || "press \"share photo\" to publish!",
"titleSize": "3em",
"messageSize": "2em",
"close": false,
"drag": false,
"pauseOnHover": false,
2019-12-22 10:46:03 +01:00
"progressBarColor": "red",
"position": "topCenter"
});
2019-12-22 10:46:03 +01:00
Countdown.start(DEFAULT_DELAY, cancelPhoto);
2018-11-28 21:55:54 +01:00
}
function takePhoto(msg) {
2019-12-22 13:12:12 +01:00
_takePhoto(msg);
// in case we need to introduce a delay:
// window.setTimeout(function() { _takePhoto(msg); }, 1000);
}
function loadMedia(url) {
fetch(url)
.then((response) => response.text())
.then((jdata) => {
var data = JSON.parse(jdata);
var imgCont = document.getElementById("images-container");
imgCont.innerHTML = "";
for (var i = 0 ; i < data.length; i++) {
var elem = data[i];
if (!(elem.media_attachments && elem.media_attachments.length > 0)) {
continue;
}
var imgData = elem.media_attachments[0];
if (imgData.type != "image" || !imgData.preview_url) {
continue;
}
var imgUrl = imgData.preview_url;
imgCont.innerHTML += "<p><img class=\"feed-images\" src=" + imgUrl + "></p>";
}
})
.catch((error) => {
console.warn(error);
});
}
function updateFeed() {
loadMedia("https://chaos.social/api/v1/accounts/" + ACCOUNT_ID + "/statuses");
2018-12-22 16:20:06 +01:00
}
2018-11-28 21:55:54 +01:00
function initCamera() {
console.log("request camera permission");
2018-12-01 11:18:30 +01:00
document.querySelector("#canvas-container").style.display = "block";
2018-11-28 21:55:54 +01:00
var videoObj = {
"video": {
width: 800,
height: 600
},
"audio": false
};
navigator.mediaDevices.getUserMedia(videoObj).then(function(stream) {
runCamera(stream);
2018-12-01 11:18:30 +01:00
document.querySelector("#init-btn").style.display = "none";
document.querySelector("#take-photo-btn").classList.remove("disabled");
2018-11-28 21:55:54 +01:00
}).catch(function(err) {
console.log("unable to open camera");
console.log(err);
iziToast.error({
"title": "unable to open camera",
"message": "please reload this page: " + err,
"titleSize": "3em",
"messageSize": "2em",
"close": false,
"drag": false,
"pauseOnHover": false,
2019-12-22 10:46:03 +01:00
"progressBarColor": "red",
"position": "topCenter"
});
2018-11-28 21:55:54 +01:00
});
2019-12-22 13:12:12 +01:00
2018-11-28 21:55:54 +01:00
}
2019-11-01 21:25:12 +01:00
function resizeCanvas(el, canvasID) {
var canvas = document.getElementById(canvasID);
canvas.width = el.offsetWidth;
canvas.height = el.offsetHeighth;
}
2019-12-22 13:12:12 +01:00
window.onload = function(e) {
updateFeed();
}