lbry.tech/views/partials/tour-scripts.js

219 lines
5.9 KiB
JavaScript
Raw Normal View History

2018-07-16 23:06:37 +02:00
/* global $, send */ "use strict";
2018-07-25 21:44:02 +02:00
initializeTour();
2018-07-16 23:06:37 +02:00
2018-07-23 23:46:58 +02:00
$("body").on("click", "[data-action]", event => {
2018-07-16 23:06:37 +02:00
event.preventDefault();
const data = event.currentTarget.dataset;
switch(data.action) {
2018-07-25 00:29:25 +02:00
case "choose claim":
fetchMetadata(1, data.claimId);
2018-07-16 23:06:37 +02:00
break;
2018-07-25 00:29:25 +02:00
case "execute claim":
if (!$("#fetch-claim-uri").val()) return;
fetchMetadata(1, $("#fetch-claim-uri").val());
2018-07-16 23:06:37 +02:00
break;
2018-07-25 00:29:25 +02:00
case "tour, step 1":
2018-07-23 23:46:58 +02:00
$(".hook__navigation__step").removeClass("active");
$(".hook__navigation__step:nth-child(1)").addClass("active");
$("#step1-page").show();
$("#step2-page").hide();
$("#step3-page").hide();
break;
2018-07-25 00:29:25 +02:00
case "tour, step 2":
2018-07-23 23:46:58 +02:00
$(".hook__navigation__step").removeClass("active");
$(".hook__navigation__step:nth-child(2)").addClass("active");
$("#step1-page").hide();
$("#step2-page").show();
2018-07-25 00:29:25 +02:00
$(".hook__page__content__meme__thumbnail").click(); // preload canvas
2018-07-23 23:46:58 +02:00
$("#step3-page").hide();
break;
2018-07-25 00:29:25 +02:00
case "tour, step 3":
2018-07-23 23:46:58 +02:00
$(".hook__navigation__step").removeClass("active");
$(".hook__navigation__step:nth-child(3)").addClass("active");
$("#step1-page").hide();
$("#step2-page").hide();
$("#step3-page").show();
break;
2018-07-25 00:29:25 +02:00
case "upload image":
fetchMetadata(2, getMemeInfo());
break;
2018-07-16 23:06:37 +02:00
default:
break;
}
});
2018-07-24 18:44:32 +02:00
$("body").on("click", ".hook__page__content__meme__thumbnail", event => {
$(".hook__page__content__meme__thumbnail").removeClass("selected");
event.currentTarget.className += " selected";
updateCanvas(event.currentTarget);
});
$("#meme-top-line, #meme-bottom-line").on("keyup", () => updateCanvas());
2018-07-18 22:31:05 +02:00
2018-07-23 23:46:58 +02:00
// H E L P E R S
function detectLanguageAndUpdate() {
const compare = (array1, array2) => array2.filter(value => array2.indexOf(value)); // compare two arrays and get match(es)
const memeLocaleObject = $("#meme-language").children();
const memeLocales = [];
const userLocales = [];
for (const language of navigator.languages) userLocales.push(language);
for (const key in memeLocaleObject) {
if (memeLocaleObject[key] && memeLocaleObject[key].value) memeLocales.push(memeLocaleObject[key].value);
}
if (
memeLocales.length &&
userLocales.length &&
compare(memeLocales, userLocales).length
) $("#meme-language").children(`option[value="${compare(memeLocales, userLocales)[0]}"]`).attr("selected", true);
}
2018-07-25 21:44:02 +02:00
function initializeTour() {
$("#fetch-claim-uri").val(""); // reset
$(".hook__navigation__step:nth-child(1)").addClass("active");
detectLanguageAndUpdate();
initCanvas();
}
2018-07-18 22:31:05 +02:00
2018-07-25 00:29:25 +02:00
function fetchMetadata(stepNumber, data) {
2018-07-18 22:31:05 +02:00
/**
TODO:
2018-07-25 00:29:25 +02:00
- Style code with highlightjs
2018-07-18 22:31:05 +02:00
*/
2018-07-25 00:29:25 +02:00
if (!stepNumber) return;
switch(stepNumber) {
case 1:
send(JSON.stringify({
"claim": data,
"message": "fetch metadata",
"method": "resolve",
"step": stepNumber
}));
if (!$("#fetch-claim-uri").val()) $("#fetch-claim-uri").val(data);
$("#step1-placeholder").html(`
2018-07-18 22:31:05 +02:00
<pre><code class="bash">
# The LBRY app must be running on your computer for this example to work
2018-07-25 00:29:25 +02:00
curl "http://localhost:5279" --data "{ 'method': 'resolve', 'params': { 'uri': '${data}' } }"
2018-07-18 22:31:05 +02:00
</code></pre>
<div class="loader" id="temp-loader"></div>
<div id="step1-result"></div>
2018-07-25 00:29:25 +02:00
`);
$("#step1-selections").hide();
break;
case 2:
send(JSON.stringify({
"data": data,
"message": "fetch metadata",
"method": "publish",
"step": stepNumber
}));
2018-07-25 00:29:25 +02:00
break;
default:
break;
}
}
2018-07-18 22:31:05 +02:00
function getMemeInfo() { // TODO: Error handling
2018-07-25 00:29:25 +02:00
const info = {};
info.description = $("#meme-description").val();
info.file_path = $("#meme-canvas")[0].toDataURL("image/jpeg", 0.6);
info.language = $("#meme-language").val();
info.license = $("#meme-license").val();
info.name = $("#meme-title").val();
info.nsfw = $("#meme-nsfw-flag")[0].checked;
2018-07-25 00:29:25 +02:00
info.title = $("#meme-title").val();
return info;
2018-07-18 22:31:05 +02:00
}
2018-07-25 00:29:25 +02:00
2018-07-24 18:44:32 +02:00
function clearCanvas(canvas) {
const ctx = canvas.getContext("2d");
ctx.save();
ctx.globalCompositeOperation = "copy";
ctx.strokeStyle = "transparent";
ctx.beginPath();
ctx.lineTo(0, 0);
ctx.stroke();
ctx.restore();
}
function initCanvas() {
const canvas = document.getElementById("meme-canvas");
const canvasWidth = 400;
const canvasHeight = 300;
const ctx = canvas.getContext("2d");
const img = document.getElementById("base-image");
clearCanvas(canvas);
ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight, 0, 0, canvasWidth, canvasHeight);
ctx.fillStyle = "white";
ctx.font = "bold 28px Karla";
ctx.lineJoin = "round";
ctx.lineWidth = 4;
ctx.strokeStyle = "black";
ctx.textAlign = "center";
ctx.textBaseline = "top";
ctx.strokeText($("#meme-top-line").val().toUpperCase(), canvasWidth / 2, 20);
ctx.strokeText($("#meme-bottom-line").val().toUpperCase(), canvasWidth / 2, (canvasHeight - 40));
ctx.fillText($("#meme-top-line").val().toUpperCase(), canvasWidth / 2, 20);
ctx.fillText($("#meme-bottom-line").val().toUpperCase(), canvasWidth / 2, (canvasHeight - 40));
}
function updateCanvas(imageSource) {
const canvas = document.getElementById("meme-canvas");
const canvasWidth = 400;
const canvasHeight = 300;
const ctx = canvas.getContext("2d");
const img = document.getElementById("base-image");
clearCanvas(canvas);
if (imageSource) {
ctx.drawImage(imageSource, 0, 0, canvasWidth, canvasHeight, 0, 0, canvasWidth, canvasHeight);
img.src = imageSource.src;
} ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight, 0, 0, canvasWidth, canvasHeight);
ctx.strokeText($("#meme-top-line").val().toUpperCase(), canvasWidth / 2, 20);
ctx.strokeText($("#meme-bottom-line").val().toUpperCase(), canvasWidth / 2, (canvasHeight - 40));
ctx.fillText($("#meme-top-line").val().toUpperCase(), canvasWidth / 2, 20);
ctx.fillText($("#meme-bottom-line").val().toUpperCase(), canvasWidth / 2, (canvasHeight - 40));
}