2017-04-04 01:24:46 +02:00
|
|
|
/*
|
|
|
|
========================================================================
|
2022-04-09 04:45:47 +02:00
|
|
|
The 2022 /r/place Atlas
|
|
|
|
|
|
|
|
An Atlas of Reddit's 2022 /r/place, with information to each
|
2017-04-04 01:24:46 +02:00
|
|
|
artwork of the canvas provided by the community.
|
2022-04-09 04:45:47 +02:00
|
|
|
|
|
|
|
Copyright (c) 2017 Roland Rytz <roland@draemm.li>
|
|
|
|
Copyright (c) 2022 r/placeAtlas2 contributors
|
|
|
|
|
2017-04-04 01:24:46 +02:00
|
|
|
Licensed under the GNU Affero General Public License Version 3
|
2022-04-09 03:23:55 +02:00
|
|
|
https://place-atlas.stefanocoding.me/license.txt
|
2017-04-04 01:24:46 +02:00
|
|
|
========================================================================
|
|
|
|
*/
|
|
|
|
|
2022-04-14 08:41:13 +02:00
|
|
|
var finishButton = document.getElementById("finishButton");
|
|
|
|
var resetButton = document.getElementById("resetButton");
|
|
|
|
var undoButton = document.getElementById("undoButton");
|
|
|
|
var redoButton = document.getElementById("redoButton");
|
|
|
|
var highlightUnchartedLabel = document.getElementById("highlightUnchartedLabel");
|
|
|
|
var entryId = 0
|
|
|
|
|
|
|
|
var objectInfoBox = document.getElementById("objectInfo");
|
|
|
|
var hintText = document.getElementById("hint");
|
|
|
|
|
2022-04-14 09:56:30 +02:00
|
|
|
var periodsStatus = document.getElementById('periodsStatus')
|
2022-04-14 08:41:13 +02:00
|
|
|
var periodGroups = document.getElementById('periodGroups')
|
|
|
|
var periodGroupTemplate = document.getElementById('period-group').content.firstElementChild.cloneNode(true)
|
2022-04-14 09:56:30 +02:00
|
|
|
var periodsAdd = document.getElementById('periodsAdd')
|
|
|
|
|
2022-04-14 08:41:13 +02:00
|
|
|
var exportButton = document.getElementById("exportButton");
|
|
|
|
var cancelButton = document.getElementById("cancelButton");
|
|
|
|
|
|
|
|
var exportOverlay = document.getElementById("exportOverlay");
|
|
|
|
var exportCloseButton = document.getElementById("exportCloseButton");
|
|
|
|
var exportBackButton = document.getElementById("exportBackButton")
|
|
|
|
|
|
|
|
var path = [];
|
2022-04-15 09:24:08 +02:00
|
|
|
var center = [1000, 1000];
|
2022-04-14 08:41:13 +02:00
|
|
|
|
|
|
|
var pathWithPeriods = []
|
|
|
|
var periodGroupElements = []
|
|
|
|
|
2022-04-14 10:37:29 +02:00
|
|
|
var disableDrawingOverride = false
|
2022-04-14 08:41:13 +02:00
|
|
|
var drawing = true;
|
|
|
|
|
2022-04-15 09:24:08 +02:00
|
|
|
[...document.querySelectorAll("#drawControlsContainer textarea")].forEach(el => {
|
|
|
|
el.addEventListener("input", function() {
|
|
|
|
this.style.height = "auto";
|
|
|
|
this.style.height = (this.scrollHeight) + "px"
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-04-04 01:24:46 +02:00
|
|
|
function initDraw(){
|
2022-04-09 11:27:14 +02:00
|
|
|
|
|
|
|
wrapper.classList.remove('listHidden')
|
|
|
|
|
|
|
|
window.render = render
|
|
|
|
window.renderBackground = renderBackground
|
|
|
|
window.updateHovering = updateHovering
|
2017-04-04 01:24:46 +02:00
|
|
|
|
2022-04-14 08:41:13 +02:00
|
|
|
// var startPeriodField = document.getElementById('startPeriodField')
|
|
|
|
// var endPeriodField = document.getElementById('endPeriodField')
|
|
|
|
// var periodVisbilityInfo = document.getElementById('periodVisbilityInfo')
|
2017-04-04 01:24:46 +02:00
|
|
|
|
2017-04-06 19:15:16 +02:00
|
|
|
var rShiftPressed = false;
|
|
|
|
var lShiftPressed = false;
|
|
|
|
var shiftPressed = false;
|
|
|
|
|
2017-04-05 03:47:36 +02:00
|
|
|
var highlightUncharted = true;
|
|
|
|
|
|
|
|
renderBackground();
|
2022-04-09 11:27:14 +02:00
|
|
|
applyView();
|
2017-04-05 03:47:36 +02:00
|
|
|
|
2017-04-04 01:24:46 +02:00
|
|
|
container.style.cursor = "crosshair";
|
|
|
|
|
|
|
|
var undoHistory = [];
|
|
|
|
|
|
|
|
render(path);
|
|
|
|
|
|
|
|
container.addEventListener("mousedown", function(e){
|
|
|
|
lastPos = [
|
2022-04-09 16:07:01 +02:00
|
|
|
e.clientX,
|
|
|
|
e.clientY
|
2017-04-04 01:24:46 +02:00
|
|
|
];
|
|
|
|
});
|
|
|
|
|
2017-04-06 19:15:16 +02:00
|
|
|
function getCanvasCoords(x, y){
|
|
|
|
x = x - container.offsetLeft;
|
|
|
|
y = y - container.offsetTop;
|
|
|
|
|
|
|
|
var pos = [
|
2022-04-09 16:07:01 +02:00
|
|
|
~~((x - (container.clientWidth/2 - innerContainer.clientWidth/2 + zoomOrigin[0]))/zoom)+0.5,
|
|
|
|
~~((y - (container.clientHeight/2 - innerContainer.clientHeight/2 + zoomOrigin[1]))/zoom)+0.5
|
2017-04-06 19:15:16 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
if(shiftPressed && path.length > 0){
|
|
|
|
var previous = path[path.length-1];
|
|
|
|
|
|
|
|
if(Math.abs(pos[1] - previous[1]) > Math.abs(pos[0] - previous[0]) ){
|
|
|
|
pos[0] = previous[0];
|
|
|
|
} else {
|
|
|
|
pos[1] = previous[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2017-04-04 01:24:46 +02:00
|
|
|
container.addEventListener("mouseup", function(e){
|
2017-04-06 19:15:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
if(Math.abs(lastPos[0] - e.clientX) + Math.abs(lastPos[1] - e.clientY) <= 4 && drawing){
|
|
|
|
|
|
|
|
var coords = getCanvasCoords(e.clientX, e.clientY);
|
2017-04-04 01:24:46 +02:00
|
|
|
|
2017-04-06 19:15:16 +02:00
|
|
|
path.push(coords);
|
|
|
|
render(path);
|
|
|
|
|
2017-04-04 01:24:46 +02:00
|
|
|
undoHistory = [];
|
|
|
|
redoButton.disabled = true;
|
|
|
|
undoButton.disabled = false;
|
2017-04-06 19:15:16 +02:00
|
|
|
|
2017-04-04 01:24:46 +02:00
|
|
|
if(path.length >= 3){
|
|
|
|
finishButton.disabled = false;
|
|
|
|
}
|
2022-04-14 19:06:13 +02:00
|
|
|
|
|
|
|
updatePath()
|
2017-04-04 01:24:46 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
window.addEventListener("mousemove", function(e){
|
2017-04-06 19:15:16 +02:00
|
|
|
|
|
|
|
if(!dragging && drawing && path.length > 0){
|
2022-04-09 11:27:14 +02:00
|
|
|
|
2017-04-06 19:15:16 +02:00
|
|
|
var coords = getCanvasCoords(e.clientX, e.clientY);
|
|
|
|
render(path.concat([coords]));
|
2017-04-04 01:24:46 +02:00
|
|
|
}
|
2017-04-06 19:15:16 +02:00
|
|
|
|
2017-04-04 01:24:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
window.addEventListener("keyup", function(e){
|
|
|
|
if(e.key == "Enter"){
|
|
|
|
finish();
|
|
|
|
} else if(e.key == "z" && e.ctrlKey){
|
|
|
|
undo();
|
|
|
|
} else if(e.key == "y" && e.ctrlKey){
|
|
|
|
redo();
|
|
|
|
} else if(e.key == "Escape"){
|
|
|
|
exportOverlay.style.display = "none";
|
2017-04-04 12:13:23 +02:00
|
|
|
} else if (e.key === "Shift" ){
|
2017-04-06 19:15:16 +02:00
|
|
|
if(e.code === "ShiftRight"){
|
|
|
|
rShiftPressed = false;
|
|
|
|
} else if(e.code === "ShiftLeft"){
|
|
|
|
lShiftPressed = false;
|
|
|
|
}
|
|
|
|
shiftPressed = rShiftPressed || lShiftPressed;
|
|
|
|
}
|
2017-04-04 01:24:46 +02:00
|
|
|
});
|
|
|
|
|
2017-04-04 12:13:23 +02:00
|
|
|
window.addEventListener("keydown", function(e){
|
2017-04-06 19:15:16 +02:00
|
|
|
if (e.key === "Shift" ){
|
|
|
|
if(e.code === "ShiftRight"){
|
|
|
|
rShiftPressed = true;
|
|
|
|
} else if(e.code === "ShiftLeft"){
|
|
|
|
lShiftPressed = true;
|
|
|
|
}
|
|
|
|
shiftPressed = rShiftPressed || lShiftPressed;
|
|
|
|
}
|
|
|
|
});
|
2017-04-04 12:13:23 +02:00
|
|
|
|
2017-04-04 01:24:46 +02:00
|
|
|
finishButton.addEventListener("click", function(e){
|
|
|
|
finish();
|
|
|
|
});
|
|
|
|
|
|
|
|
undoButton.addEventListener("click", function(e){
|
|
|
|
undo();
|
|
|
|
});
|
|
|
|
|
|
|
|
redoButton.addEventListener("click", function(e){
|
|
|
|
redo();
|
|
|
|
});
|
|
|
|
|
|
|
|
resetButton.addEventListener("click", function(e){
|
|
|
|
reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
cancelButton.addEventListener("click", function(e){
|
2022-04-09 16:07:01 +02:00
|
|
|
back();
|
2017-04-04 01:24:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
document.getElementById("nameField").addEventListener("keyup", function(e){
|
|
|
|
if(e.key == "Enter"){
|
|
|
|
exportJson();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-04-15 09:24:08 +02:00
|
|
|
// document.getElementById("websiteField").addEventListener("keyup", function(e){
|
|
|
|
// if(e.key == "Enter"){
|
|
|
|
// exportJson();
|
|
|
|
// }
|
|
|
|
// });
|
2017-04-04 01:24:46 +02:00
|
|
|
|
2022-04-15 09:24:08 +02:00
|
|
|
// document.getElementById("subredditField").addEventListener("keyup", function(e){
|
|
|
|
// if(e.key == "Enter"){
|
|
|
|
// exportJson();
|
|
|
|
// }
|
|
|
|
// });
|
2017-04-04 01:24:46 +02:00
|
|
|
|
|
|
|
exportButton.addEventListener("click", function(e){
|
|
|
|
exportJson();
|
|
|
|
});
|
|
|
|
|
|
|
|
exportCloseButton.addEventListener("click", function(e){
|
|
|
|
reset();
|
|
|
|
exportOverlay.style.display = "none";
|
|
|
|
});
|
|
|
|
|
2022-04-09 16:07:01 +02:00
|
|
|
exportBackButton.addEventListener("click", function(e){
|
|
|
|
finish();
|
|
|
|
exportOverlay.style.display = "none";
|
|
|
|
});
|
2022-04-04 15:43:06 +02:00
|
|
|
|
2017-04-05 03:47:36 +02:00
|
|
|
document.getElementById("highlightUncharted").addEventListener("click", function(e){
|
|
|
|
highlightUncharted = this.checked;
|
|
|
|
render(path);
|
|
|
|
});
|
|
|
|
|
2017-04-04 01:24:46 +02:00
|
|
|
function exportJson(){
|
|
|
|
var exportObject = {
|
2022-04-09 16:07:01 +02:00
|
|
|
id: entryId,
|
|
|
|
name: document.getElementById("nameField").value,
|
|
|
|
description: document.getElementById("descriptionField").value,
|
2022-04-15 05:46:27 +02:00
|
|
|
links: {},
|
2022-04-14 16:03:17 +02:00
|
|
|
center: {},
|
|
|
|
path: {},
|
2017-04-04 01:24:46 +02:00
|
|
|
};
|
2022-04-10 11:11:34 +02:00
|
|
|
|
2022-04-14 16:03:17 +02:00
|
|
|
pathWithPeriodsTemp = pathWithPeriods.concat()
|
|
|
|
|
2022-04-14 19:06:13 +02:00
|
|
|
// console.log(pathWithPeriodsTemp)
|
2022-04-14 16:03:17 +02:00
|
|
|
|
|
|
|
// calculateCenter(path)
|
|
|
|
|
|
|
|
for (let i = pathWithPeriodsTemp.length - 1; i > 0; i--) {
|
|
|
|
for (let j = 0; j < i; j++) {
|
|
|
|
if (JSON.stringify(pathWithPeriodsTemp[i][1]) === JSON.stringify(pathWithPeriodsTemp[j][1])) {
|
|
|
|
pathWithPeriodsTemp[j][0] = pathWithPeriodsTemp[i][0] + ', ' + pathWithPeriodsTemp[j][0]
|
|
|
|
pathWithPeriodsTemp.splice(i, 1)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-04-10 11:11:34 +02:00
|
|
|
}
|
2022-04-14 16:03:17 +02:00
|
|
|
|
|
|
|
pathWithPeriodsTemp.forEach(([key, value]) => {
|
|
|
|
// TODO: Compress periods on something like 0-13, 14.
|
|
|
|
exportObject.path[key] = value
|
|
|
|
exportObject.center[key] = calculateCenter(value)
|
|
|
|
})
|
|
|
|
|
2022-04-15 05:46:27 +02:00
|
|
|
let inputWebsite = document.getElementById("websiteField").value.split('\n').map(line => line.trim()).filter(line => line)
|
|
|
|
let inputSubreddit = document.getElementById("subredditField").value.split('\n').map(line => line.trim().replace(/^\/?r\//, '')).filter(line => line)
|
|
|
|
|
|
|
|
if (inputWebsite.length) exportObject.links.website = inputWebsite
|
|
|
|
if (inputSubreddit.length) exportObject.links.subreddit = inputSubreddit
|
|
|
|
|
2017-04-04 01:24:46 +02:00
|
|
|
var jsonString = JSON.stringify(exportObject, null, "\t");
|
|
|
|
var textarea = document.getElementById("exportString");
|
|
|
|
jsonString = jsonString.split("\n");
|
|
|
|
jsonString = jsonString.join("\n ");
|
|
|
|
jsonString = " "+jsonString;
|
|
|
|
textarea.value = jsonString;
|
2022-04-04 15:43:06 +02:00
|
|
|
var directPostUrl = "https://www.reddit.com/r/placeAtlas2/submit?selftext=true&title=New%20Submission&text="+encodeURIComponent(document.getElementById("exportString").value);
|
2022-04-09 16:07:01 +02:00
|
|
|
if (jsonString.length > 7493) {
|
|
|
|
directPostUrl = "https://www.reddit.com/r/placeAtlas2/submit?selftext=true&title=New%20Submission&text="+encodeURIComponent(" " + JSON.stringify(exportObject));
|
|
|
|
}
|
2022-04-04 15:43:06 +02:00
|
|
|
document.getElementById("exportDirectPost").href=directPostUrl;
|
2017-04-04 01:24:46 +02:00
|
|
|
|
2017-05-01 17:54:19 +02:00
|
|
|
exportOverlay.style.display = "flex";
|
2017-04-04 01:24:46 +02:00
|
|
|
|
|
|
|
textarea.focus();
|
|
|
|
textarea.select();
|
|
|
|
}
|
|
|
|
|
|
|
|
function undo(){
|
|
|
|
if(path.length > 0 && drawing){
|
|
|
|
undoHistory.push(path.pop());
|
|
|
|
redoButton.disabled = false;
|
2022-04-14 09:56:30 +02:00
|
|
|
updatePath()
|
2017-04-04 01:24:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function redo(){
|
|
|
|
if(undoHistory.length > 0 && drawing){
|
|
|
|
path.push(undoHistory.pop());
|
|
|
|
undoButton.disabled = false;
|
2022-04-14 09:56:30 +02:00
|
|
|
updatePath()
|
2017-04-04 01:24:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function finish(){
|
2022-04-15 09:24:08 +02:00
|
|
|
if (objectInfoBox.style.display === "block") return
|
2022-04-14 09:56:30 +02:00
|
|
|
updatePath()
|
2022-04-14 10:37:29 +02:00
|
|
|
drawing = false;
|
|
|
|
disableDrawingOverride = true;
|
2017-04-04 01:24:46 +02:00
|
|
|
objectInfoBox.style.display = "block";
|
2022-04-09 16:07:01 +02:00
|
|
|
objectDraw.style.display = "none";
|
2017-04-04 01:24:46 +02:00
|
|
|
hintText.style.display = "none";
|
2022-04-15 09:24:08 +02:00
|
|
|
[...document.querySelectorAll("#drawControlsContainer textarea")].forEach(el => {
|
|
|
|
if (el.value) el.style.height = (el.scrollHeight) + "px"
|
|
|
|
})
|
2022-04-14 08:41:13 +02:00
|
|
|
// if (isOnPeriod()) {
|
|
|
|
// periodVisbilityInfo.textContent = ""
|
|
|
|
// } else {
|
|
|
|
// periodVisbilityInfo.textContent = "Not visible during this period!"
|
|
|
|
// }
|
2017-04-04 01:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function reset(){
|
2022-04-14 08:41:13 +02:00
|
|
|
updatePath([])
|
2017-04-04 01:24:46 +02:00
|
|
|
undoHistory = [];
|
|
|
|
drawing = true;
|
2022-04-14 10:37:29 +02:00
|
|
|
disableDrawingOverride = false;
|
2017-04-04 01:24:46 +02:00
|
|
|
objectInfoBox.style.display = "none";
|
2022-04-09 16:07:01 +02:00
|
|
|
objectDraw.style.display = "block";
|
2017-04-04 01:24:46 +02:00
|
|
|
hintText.style.display = "block";
|
|
|
|
|
|
|
|
document.getElementById("nameField").value = "";
|
|
|
|
document.getElementById("descriptionField").value = "";
|
|
|
|
document.getElementById("websiteField").value = "";
|
|
|
|
document.getElementById("subredditField").value = "";
|
|
|
|
}
|
|
|
|
|
2022-04-09 16:07:01 +02:00
|
|
|
function back(){
|
|
|
|
drawing = true;
|
2022-04-14 10:37:29 +02:00
|
|
|
disableDrawingOverride = false;
|
2022-04-14 09:56:30 +02:00
|
|
|
updatePath()
|
2022-04-09 16:07:01 +02:00
|
|
|
objectInfoBox.style.display = "none";
|
|
|
|
objectDraw.style.display = "block";
|
|
|
|
hintText.style.display = "block";
|
|
|
|
}
|
|
|
|
|
2017-04-05 03:47:36 +02:00
|
|
|
function renderBackground(){
|
|
|
|
|
|
|
|
backgroundContext.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
|
2017-04-05 04:01:24 +02:00
|
|
|
backgroundContext.fillStyle = "rgba(0, 0, 0, 1)";
|
2017-04-05 03:47:36 +02:00
|
|
|
//backgroundContext.fillRect(0, 0, canvas.width, canvas.height);
|
|
|
|
|
|
|
|
for(var i = 0; i < atlas.length; i++){
|
|
|
|
|
|
|
|
var path = atlas[i].path;
|
|
|
|
|
|
|
|
backgroundContext.beginPath();
|
|
|
|
|
|
|
|
if(path[0]){
|
|
|
|
backgroundContext.moveTo(path[0][0], path[0][1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(var p = 1; p < path.length; p++){
|
|
|
|
backgroundContext.lineTo(path[p][0], path[p][1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
backgroundContext.closePath();
|
|
|
|
|
|
|
|
backgroundContext.fill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 01:24:46 +02:00
|
|
|
function render(path){
|
|
|
|
|
|
|
|
context.globalCompositeOperation = "source-over";
|
|
|
|
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
|
2017-04-05 03:47:36 +02:00
|
|
|
if(highlightUncharted){
|
|
|
|
context.drawImage(backgroundCanvas, 0, 0);
|
|
|
|
context.fillStyle = "rgba(0, 0, 0, 0.4)";
|
|
|
|
} else {
|
|
|
|
context.fillStyle = "rgba(0, 0, 0, 0.6)";
|
|
|
|
}
|
|
|
|
|
2017-04-04 01:24:46 +02:00
|
|
|
context.fillRect(0, 0, canvas.width, canvas.height);
|
|
|
|
|
|
|
|
context.beginPath();
|
|
|
|
|
|
|
|
if(path[0]){
|
|
|
|
context.moveTo(path[0][0], path[0][1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(var i = 1; i < path.length; i++){
|
|
|
|
context.lineTo(path[i][0], path[i][1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
context.closePath();
|
|
|
|
|
|
|
|
context.strokeStyle = "rgba(255, 255, 255, 1)";
|
|
|
|
context.stroke();
|
|
|
|
|
|
|
|
context.globalCompositeOperation = "destination-out";
|
|
|
|
|
|
|
|
context.fillStyle = "rgba(0, 0, 0, 1)";
|
|
|
|
context.fill();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-09 11:27:14 +02:00
|
|
|
function updateHovering(e, tapped){
|
|
|
|
if(!dragging && (!fixed || tapped)){
|
|
|
|
var pos = [
|
|
|
|
(e.clientX - (container.clientWidth/2 - innerContainer.clientWidth/2 + zoomOrigin[0] + container.offsetLeft))/zoom
|
|
|
|
,(e.clientY - (container.clientHeight/2 - innerContainer.clientHeight/2 + zoomOrigin[1] + container.offsetTop))/zoom
|
|
|
|
];
|
|
|
|
var coords_p = document.getElementById("coords_p");
|
|
|
|
coords_p.innerText = Math.ceil(pos[0]) + ", " + Math.ceil(pos[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-09 16:07:01 +02:00
|
|
|
const getEntry = id => {
|
2022-04-15 09:24:08 +02:00
|
|
|
const entries = atlasAll.filter(entry => entry.id === id)
|
2022-04-09 16:07:01 +02:00
|
|
|
if (entries.length === 1) return entries[0]
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
let params = new URLSearchParams(document.location.search)
|
|
|
|
|
|
|
|
if (params.has('id')) {
|
|
|
|
entry = getEntry(params.get('id'))
|
|
|
|
document.getElementById("nameField").value = entry.name
|
|
|
|
document.getElementById("descriptionField").value = entry.description
|
2022-04-15 05:46:27 +02:00
|
|
|
document.getElementById("websiteField").value = entry.links.website.join('\n')
|
|
|
|
document.getElementById("subredditField").value = entry.links.subreddit.map(sub => '/r/' + sub).join('\n')
|
2022-04-15 09:24:08 +02:00
|
|
|
pathWithPeriods = Object.entries(entry.path)
|
2022-04-09 16:07:01 +02:00
|
|
|
redoButton.disabled = true;
|
|
|
|
undoButton.disabled = false;
|
|
|
|
entryId = params.get('id')
|
|
|
|
|
2022-04-10 15:46:44 +02:00
|
|
|
if (typeof entry.period === "string") {
|
|
|
|
entry.period.split(', ').some(period => {
|
|
|
|
if (period.search('-') + 1) {
|
|
|
|
var [before, after] = period.split('-')
|
|
|
|
startPeriodField.value = before
|
|
|
|
endPeriodField.value = after
|
2022-04-14 19:06:13 +02:00
|
|
|
// console.log(before, after)
|
2022-04-10 15:46:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-14 08:44:39 +02:00
|
|
|
} else {
|
|
|
|
pathWithPeriods.push([defaultPeriod, []])
|
2022-04-09 16:07:01 +02:00
|
|
|
}
|
|
|
|
|
2022-04-14 08:44:39 +02:00
|
|
|
initPeriodGroups()
|
|
|
|
|
2022-04-14 19:06:13 +02:00
|
|
|
zoom = 4;
|
|
|
|
|
|
|
|
zoomOrigin = [
|
|
|
|
innerContainer.clientWidth/2 - center[0] * zoom,// + container.offsetLeft
|
|
|
|
innerContainer.clientHeight/2 - center[1] * zoom// + container.offsetTop
|
|
|
|
];
|
|
|
|
|
|
|
|
scaleZoomOrigin = [
|
|
|
|
2000/2 - center[0],// + container.offsetLeft
|
|
|
|
2000/2 - center[1]// + container.offsetTop
|
|
|
|
];
|
|
|
|
|
|
|
|
applyView();
|
|
|
|
|
2022-04-10 11:11:34 +02:00
|
|
|
document.addEventListener('timeupdate', (event) => {
|
2022-04-14 10:37:29 +02:00
|
|
|
renderBackground()
|
2022-04-14 08:41:13 +02:00
|
|
|
updatePeriodGroups()
|
|
|
|
})
|
|
|
|
|
2022-04-14 09:56:30 +02:00
|
|
|
periodsAdd.addEventListener('click', () => {
|
|
|
|
pathWithPeriods.push([defaultPeriod, []])
|
|
|
|
initPeriodGroups()
|
|
|
|
})
|
|
|
|
|
2022-04-14 08:41:13 +02:00
|
|
|
}
|
|
|
|
|
2022-04-14 19:06:13 +02:00
|
|
|
function calculateCenter(path){
|
|
|
|
|
|
|
|
var area = 0,
|
|
|
|
i,
|
|
|
|
j,
|
|
|
|
point1,
|
|
|
|
point2,
|
|
|
|
x = 0,
|
|
|
|
y = 0,
|
|
|
|
f;
|
|
|
|
|
|
|
|
for (i = 0, j = path.length - 1; i < path.length; j=i,i++) {
|
|
|
|
point1 = path[i];
|
|
|
|
point2 = path[j];
|
|
|
|
f = point1[0] * point2[1] - point2[0] * point1[1];
|
|
|
|
area += f;
|
|
|
|
x += (point1[0] + point2[0]) * f;
|
|
|
|
y += (point1[1] + point2[1]) * f;
|
|
|
|
}
|
|
|
|
area *= 3;
|
|
|
|
|
|
|
|
return [Math.floor(x / area)+0.5, Math.floor(y / area)+0.5];
|
|
|
|
}
|
|
|
|
|
2022-04-14 08:41:13 +02:00
|
|
|
function isOnPeriod(start = parseInt(startPeriodField.value), end = parseInt(endPeriodField.value), current = period) {
|
2022-04-14 19:06:13 +02:00
|
|
|
// console.log(start, end, current, current >= start && current <= end)
|
2022-04-14 08:41:13 +02:00
|
|
|
return current >= start && current <= end
|
|
|
|
}
|
|
|
|
|
|
|
|
function initPeriodGroups() {
|
|
|
|
|
|
|
|
periodGroupElements = []
|
2022-04-14 09:56:30 +02:00
|
|
|
periodGroups.textContent = ''
|
2022-04-14 08:41:13 +02:00
|
|
|
|
2022-04-14 19:06:13 +02:00
|
|
|
// console.log(pathWithPeriods)
|
2022-04-14 08:41:13 +02:00
|
|
|
|
|
|
|
pathWithPeriods.forEach(([period, path], index) => {
|
|
|
|
let periodGroupEl = periodGroupTemplate.cloneNode(true)
|
|
|
|
periodGroupEl.id = "periodGroup" + index
|
|
|
|
|
|
|
|
let startPeriodEl = periodGroupEl.querySelector('.period-start')
|
|
|
|
let endPeriodEl = periodGroupEl.querySelector('.period-end')
|
|
|
|
let periodVisibilityEl = periodGroupEl.querySelector('.period-visible')
|
2022-04-14 09:56:30 +02:00
|
|
|
let periodDeleteEl = periodGroupEl.querySelector('.period-delete')
|
|
|
|
let periodDuplicateEl = periodGroupEl.querySelector('.period-duplicate')
|
2022-04-14 08:41:13 +02:00
|
|
|
|
|
|
|
let [start, end] = parsePeriod(period)
|
|
|
|
|
|
|
|
startPeriodEl.id = "periodStart" + index
|
|
|
|
startPeriodEl.previousSibling.for = startPeriodEl.id
|
|
|
|
endPeriodEl.id = "periodEnd" + index
|
|
|
|
endPeriodEl.previousSibling.for = endPeriodEl.id
|
|
|
|
periodVisibilityEl.id = "periodVisibility" + index
|
|
|
|
|
|
|
|
startPeriodEl.value = start
|
|
|
|
startPeriodEl.max = maxPeriod
|
|
|
|
endPeriodEl.value = end
|
|
|
|
endPeriodEl.max = maxPeriod
|
|
|
|
|
2022-04-14 10:37:29 +02:00
|
|
|
startPeriodEl.addEventListener('input', event => {
|
|
|
|
timelineSlider.value = parseInt(event.target.value)
|
|
|
|
updateTime(parseInt(event.target.value))
|
2022-04-14 19:06:13 +02:00
|
|
|
// console.log(parseInt(event.target.value))
|
2022-04-14 08:41:13 +02:00
|
|
|
})
|
2022-04-14 10:37:29 +02:00
|
|
|
endPeriodEl.addEventListener('input', event => {
|
|
|
|
timelineSlider.value = parseInt(event.target.value)
|
|
|
|
updateTime(parseInt(event.target.value))
|
2022-04-14 19:06:13 +02:00
|
|
|
// console.log(parseInt(event.target.value))
|
2022-04-14 08:41:13 +02:00
|
|
|
})
|
2022-04-14 09:56:30 +02:00
|
|
|
periodDeleteEl.addEventListener('click', () => {
|
|
|
|
if (pathWithPeriods.length === 1) return
|
|
|
|
pathWithPeriods = pathWithPeriods.filter((_, i) => i !== index)
|
|
|
|
initPeriodGroups()
|
|
|
|
})
|
|
|
|
periodDuplicateEl.addEventListener('click', () => {
|
|
|
|
pathWithPeriods.push([pathWithPeriods[index][0], [...pathWithPeriods[index][1]]])
|
|
|
|
initPeriodGroups()
|
|
|
|
})
|
2022-04-14 08:41:13 +02:00
|
|
|
|
|
|
|
periodGroups.appendChild(periodGroupEl)
|
|
|
|
periodGroupElements.push({
|
|
|
|
periodGroupEl,
|
|
|
|
startPeriodEl,
|
|
|
|
endPeriodEl,
|
|
|
|
periodVisibilityEl
|
|
|
|
})
|
|
|
|
})
|
2022-04-14 19:06:13 +02:00
|
|
|
// console.log(periodGroupTemplate)
|
2022-04-15 09:24:08 +02:00
|
|
|
|
2022-04-14 08:41:13 +02:00
|
|
|
updatePeriodGroups()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function updatePeriodGroups() {
|
2022-04-15 09:24:08 +02:00
|
|
|
// console.log('updatePeriodGroups')
|
2022-04-14 08:41:13 +02:00
|
|
|
var pathToActive = []
|
|
|
|
var lastActivePathIndex
|
|
|
|
var currentActivePathIndex
|
2022-04-14 19:06:13 +02:00
|
|
|
var currentActivePathIndexes = []
|
2022-04-14 08:41:13 +02:00
|
|
|
|
|
|
|
periodGroupElements.forEach((elements, index) => {
|
|
|
|
let {
|
|
|
|
periodGroupEl,
|
|
|
|
startPeriodEl,
|
|
|
|
endPeriodEl,
|
|
|
|
periodVisibilityEl
|
|
|
|
} = elements
|
|
|
|
|
|
|
|
if (periodGroupEl.dataset.active === "true") lastActivePathIndex = index
|
2022-04-14 19:06:13 +02:00
|
|
|
periodGroupEl.dataset.active = ""
|
2022-04-14 08:41:13 +02:00
|
|
|
|
|
|
|
if (isOnPeriod(
|
|
|
|
parseInt(startPeriodEl.value),
|
|
|
|
parseInt(endPeriodEl.value),
|
|
|
|
period
|
|
|
|
)) {
|
|
|
|
pathToActive = pathWithPeriods[index][1]
|
|
|
|
currentActivePathIndex = index
|
2022-04-14 19:06:13 +02:00
|
|
|
currentActivePathIndexes.push(index)
|
|
|
|
periodGroupEl.dataset.active = "true"
|
2022-04-10 11:11:34 +02:00
|
|
|
}
|
2022-04-14 08:41:13 +02:00
|
|
|
|
|
|
|
pathWithPeriods[index][0] = formatPeriod(
|
|
|
|
parseInt(startPeriodEl.value),
|
|
|
|
parseInt(endPeriodEl.value),
|
|
|
|
period
|
|
|
|
)
|
2022-04-10 11:11:34 +02:00
|
|
|
})
|
|
|
|
|
2022-04-15 09:24:08 +02:00
|
|
|
// console.log('updatePeriodGroups searcher', pathToActive, lastActivePathIndex, currentActivePathIndex, period)
|
|
|
|
|
2022-04-14 09:56:30 +02:00
|
|
|
periodsStatus.textContent = ""
|
2022-04-14 19:06:13 +02:00
|
|
|
|
|
|
|
// if (currentActivePathIndexes.length > 1) {
|
|
|
|
// periodsStatus.textContent = "Collision detected! Please resolve it."
|
|
|
|
// currentActivePathIndexes.forEach(index => {
|
|
|
|
// periodGroupElements[index].periodGroupEl.dataset.status = "error"
|
|
|
|
// })
|
|
|
|
// currentActivePathIndex = undefined
|
|
|
|
// }
|
|
|
|
|
2022-04-15 09:24:08 +02:00
|
|
|
// console.log(lastActivePathIndex)
|
2022-04-14 08:41:13 +02:00
|
|
|
if (lastActivePathIndex !== undefined) {
|
|
|
|
if (lastActivePathIndex === currentActivePathIndex) {
|
|
|
|
// just update the path
|
|
|
|
pathWithPeriods[currentActivePathIndex] = [
|
|
|
|
formatPeriod(
|
|
|
|
parseInt(periodGroupElements[currentActivePathIndex].startPeriodEl.value),
|
|
|
|
parseInt(periodGroupElements[currentActivePathIndex].endPeriodEl.value)
|
|
|
|
),
|
|
|
|
path
|
|
|
|
]
|
2022-04-14 19:06:13 +02:00
|
|
|
updatePath()
|
2022-04-14 08:41:13 +02:00
|
|
|
} else if (currentActivePathIndex === undefined) {
|
2022-04-14 09:56:30 +02:00
|
|
|
pathWithPeriods[lastActivePathIndex][1] = path
|
2022-04-14 08:41:13 +02:00
|
|
|
updatePath([])
|
|
|
|
} else {
|
|
|
|
// switch the path
|
2022-04-14 09:56:30 +02:00
|
|
|
pathWithPeriods[lastActivePathIndex][1] = path
|
|
|
|
updatePath(pathToActive)
|
2022-04-14 08:41:13 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-15 09:24:08 +02:00
|
|
|
console.log('direct active', pathToActive)
|
2022-04-14 08:41:13 +02:00
|
|
|
updatePath(pathToActive)
|
|
|
|
}
|
|
|
|
|
2022-04-14 10:37:29 +02:00
|
|
|
drawing = disableDrawingOverride ? false : currentActivePathIndex !== undefined
|
2022-04-14 08:41:13 +02:00
|
|
|
|
2017-04-04 01:24:46 +02:00
|
|
|
}
|
|
|
|
|
2022-04-14 08:41:13 +02:00
|
|
|
function parsePeriod(periodString) {
|
2022-04-14 09:56:30 +02:00
|
|
|
periodString = periodString + ""
|
2022-04-14 08:41:13 +02:00
|
|
|
// TODO: Support for multiple/alternative types of canvas
|
2022-04-14 09:56:30 +02:00
|
|
|
if (periodString.search('-') + 1) {
|
|
|
|
var [start, end] = periodString.split('-').map(i => parseInt(i))
|
2022-04-14 08:41:13 +02:00
|
|
|
return [start, end]
|
|
|
|
} else {
|
2022-04-14 09:56:30 +02:00
|
|
|
let periodNew = parseInt(periodString)
|
|
|
|
return [periodNew, periodNew]
|
2022-04-14 08:41:13 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-04 01:24:46 +02:00
|
|
|
|
2022-04-14 08:41:13 +02:00
|
|
|
function formatPeriod(start, end) {
|
|
|
|
if (start === end) return start
|
|
|
|
else return start + "-" + end
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:24:08 +02:00
|
|
|
function updatePath(newPath) {
|
|
|
|
// console.log('updatePath', path, newPath)
|
|
|
|
if (newPath) path = newPath
|
|
|
|
// console.log('updatePath', path, newPath)
|
|
|
|
if (path.length > 3) center = calculateCenter(path)
|
2022-04-14 08:41:13 +02:00
|
|
|
render(path)
|
2022-04-14 13:23:06 +02:00
|
|
|
undoButton.disabled = path.length == 0; // Maybe make it undo the cancel action in the future
|
|
|
|
undoHistory = []
|
2022-04-14 19:06:13 +02:00
|
|
|
|
2022-04-15 09:24:08 +02:00
|
|
|
updateErrors()
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateErrors() {
|
2022-04-14 19:06:13 +02:00
|
|
|
if (path.length === 0) {
|
|
|
|
periodsStatus.textContent = "No paths available on this period!"
|
|
|
|
}
|
|
|
|
|
|
|
|
let [conflicts, invalidPaths, allErrors] = getErrors()
|
|
|
|
|
|
|
|
if (allErrors.length > 0) {
|
|
|
|
periodsStatus.textContent = `Problems detected. Please check the groups indicated by red.`
|
2022-04-15 09:24:08 +02:00
|
|
|
if (conflicts.length > 0) {
|
|
|
|
periodsStatus.textContent += `\nConflicts on ${conflicts.join(', ')}.`
|
|
|
|
currentActivePathIndex = undefined
|
|
|
|
}
|
2022-04-14 19:06:13 +02:00
|
|
|
if (invalidPaths.length > 0) periodsStatus.textContent += `\nInsufficient paths on ${invalidPaths.join(', ')}.`
|
|
|
|
allErrors.forEach(index => {
|
|
|
|
periodGroupElements[index].periodGroupEl.dataset.status = "error"
|
|
|
|
})
|
|
|
|
finishButton.disabled = true
|
|
|
|
} else {
|
|
|
|
periodsStatus.textContent = ``
|
|
|
|
finishButton.disabled = false
|
|
|
|
periodGroupElements.forEach((elements, index) => {
|
|
|
|
let {
|
|
|
|
periodGroupEl,
|
|
|
|
startPeriodEl,
|
|
|
|
endPeriodEl,
|
|
|
|
periodVisibilityEl
|
|
|
|
} = elements
|
|
|
|
if (periodGroupEl.dataset.active === "true") periodGroupEl.dataset.status = "active"
|
|
|
|
else periodGroupEl.dataset.status = ""
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 09:24:08 +02:00
|
|
|
|
2022-04-14 19:06:13 +02:00
|
|
|
function getConflicts() {
|
|
|
|
|
|
|
|
let conflicts = new Set()
|
|
|
|
|
|
|
|
for (let i = pathWithPeriods.length - 1; i > 0; i--) {
|
|
|
|
for (let j = 0; j < i; j++) {
|
|
|
|
let [start1, end1] = parsePeriod(pathWithPeriods[i][0])
|
|
|
|
let [start2, end2] = parsePeriod(pathWithPeriods[j][0])
|
|
|
|
if (
|
|
|
|
(start2 <= start1 && start1 <= end2) ||
|
|
|
|
(start2 <= end1 && end1 <= end2) ||
|
|
|
|
(start1 <= start2 && start2 <= end1) ||
|
|
|
|
(start1 <= end2 && end2 <= end1)
|
|
|
|
) {
|
|
|
|
conflicts.add(i)
|
|
|
|
conflicts.add(j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conflicts = [...conflicts]
|
|
|
|
|
|
|
|
return conflicts
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function getErrors() {
|
|
|
|
let conflicts = getConflicts()
|
|
|
|
let invalidPaths = []
|
|
|
|
|
|
|
|
pathWithPeriods.forEach(([period, path], i) => {
|
|
|
|
if (path.length < 3) invalidPaths.push(i)
|
|
|
|
})
|
|
|
|
|
2022-04-15 09:24:08 +02:00
|
|
|
// console.info('conflicts', conflicts)
|
|
|
|
// console.info('invalid paths', invalidPaths)
|
2022-04-14 19:06:13 +02:00
|
|
|
|
|
|
|
return [conflicts, invalidPaths, [...new Set([...conflicts, ...invalidPaths])]]
|
2022-04-14 16:03:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// function compressPeriod(periodsString) {
|
|
|
|
// let periodStrings = periodsString.split(", ")
|
|
|
|
// let validPeriods = new Set()
|
|
|
|
// periodStrings.forEach(periodString => {
|
|
|
|
// let [start, end] = parsePeriod(periodString)
|
|
|
|
// for (var i = start; i <= end; i++) {
|
|
|
|
// validPeriods.add(i)
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// validPeriods = [...validPeriods].sort()
|
|
|
|
// }
|