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-08-11 00:29:06 +02:00
if ( window . location . href . search && window . location . href . split ( "?url=" ) [ 1 ] ) { // pre-fill example one if search parameter exists
2018-07-27 00:18:26 +02:00
const searchParameter = window . location . href . split ( "?url=" ) [ 1 ] ;
fetchMetadata ( 1 , searchParameter ) ;
}
2018-09-27 18:11:26 +02:00
document . querySelector ( "body" ) . addEventListener ( "click" , event => {
if ( event . target . dataset . action ) {
event . preventDefault ( ) ;
document . querySelector ( ".tour" ) . classList . add ( "waiting" ) ;
handleExamples ( event . target ) ;
}
2018-07-25 00:29:25 +02:00
2018-09-27 18:11:26 +02:00
if (
event . explicitOriginalTarget . classList &&
event . explicitOriginalTarget . classList [ 0 ] === "tour__content__meme__canvas__thumbnail"
) {
for ( const thumbnail of document . querySelectorAll ( ".tour__content__meme__canvas__thumbnail" ) ) {
thumbnail . classList . remove ( "selected" ) ;
}
event . explicitOriginalTarget . classList . add ( "selected" ) ;
updateCanvas ( event . explicitOriginalTarget ) ;
}
2018-07-16 23:06:37 +02:00
} ) ;
2018-09-27 18:11:26 +02:00
document . getElementById ( "fetch-claim-uri" ) . addEventListener ( "keyup" , event => {
const key = event . keyCode ? event . keyCode : event . which ;
2018-07-24 18:44:32 +02:00
2018-09-27 22:46:59 +02:00
switch ( true ) {
case ( document . querySelector ( "[data-example='1']" ) . classList . contains ( "active" ) ) :
if (
key === 13 &&
document . getElementById ( "fetch-claim-uri" ) . value . length > 0
) fetchMetadata ( 1 , document . getElementById ( "fetch-claim-uri" ) . value ) ;
break ;
case ( document . querySelector ( "[data-example='3']" ) . classList . contains ( "active" ) ) :
if (
key === 13 &&
document . getElementById ( "fetch-claim-uri" ) . value . length > 0
) fetchMetadata ( 3 , document . getElementById ( "fetch-claim-uri" ) . value ) ;
break ;
}
2018-07-24 18:44:32 +02:00
} ) ;
2018-09-27 18:11:26 +02:00
document . querySelector ( "body" ) . addEventListener ( "keyup" , event => {
if (
event . target . id === "meme-top-line" ||
event . target . id === "meme-bottom-line"
) updateCanvas ( ) ;
2018-08-09 16:38:42 +02:00
} ) ;
2018-07-18 22:31:05 +02:00
2018-07-23 23:46:58 +02:00
// H E L P E R S
2018-08-21 23:22:31 +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 ( ) ;
}
2018-08-11 00:29:06 +02:00
function detectLanguageAndUpdate ( ) { // eslint-disable-line
2018-07-23 23:46:58 +02:00
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-08-21 23:22:31 +02:00
function debounce ( func , wait , immediate ) {
let timeout ;
return function ( ) {
const context = this ;
const args = arguments ;
const later = ( ) => {
timeout = null ;
if ( ! immediate ) func . apply ( context , args ) ;
} ;
const callNow = immediate && ! timeout ;
clearTimeout ( timeout ) ;
timeout = setTimeout ( later , wait ) ;
if ( callNow ) func . apply ( context , args ) ;
} ;
}
2018-07-25 21:44:02 +02:00
function initializeTour ( ) {
2018-09-27 18:11:26 +02:00
document . querySelector ( ".tour" ) . classList . add ( "waiting" ) ;
document . querySelector ( "#fetch-claim-uri" ) . value = "" ;
document . querySelector ( "#fetch-claim-uri" ) . focus ( ) ;
document . querySelector ( ".tour__navigation__example:nth-child(1)" ) . classList . add ( "active" ) ;
2018-07-25 21:44:02 +02:00
2018-08-08 01:15:34 +02:00
send ( JSON . stringify ( {
"message" : "landed on tour"
} ) ) ;
2018-08-21 23:22:31 +02:00
setTimeout ( ( ) => {
2018-09-27 18:11:26 +02:00
document . querySelector ( ".tour__navigation__example:nth-child(1)" ) . click ( ) ;
} , 300 ) ;
2018-07-25 21:44:02 +02:00
}
2018-07-18 22:31:05 +02:00
2018-08-10 00:17:47 +02:00
function fetchMetadata ( exampleNumber , data ) {
if ( ! exampleNumber ) return ;
2018-07-25 00:29:25 +02:00
2018-08-10 00:17:47 +02:00
switch ( exampleNumber ) {
2018-07-25 00:29:25 +02:00
case 1 :
send ( JSON . stringify ( {
"claim" : data ,
"message" : "fetch metadata" ,
"method" : "resolve" ,
2018-08-10 00:17:47 +02:00
"example" : exampleNumber
2018-07-25 00:29:25 +02:00
} ) ) ;
2018-08-11 00:29:06 +02:00
$ ( "#fetch-claim-uri" ) . val ( data ) ;
2018-07-25 00:29:25 +02:00
2018-08-08 22:20:53 +02:00
$ ( "#tour-results" ) . html ( `
< pre > < code class = "language-bash" >
2018-08-14 22:15:27 +02:00
< span class = "token comment" > # With the LBRY app / daemon running locally , you can use this in your Terminal < / s p a n >
curl -- header < span class = "token string" > "Content-Type: application/json" < / s p a n > - - d a t a < s p a n c l a s s = " t o k e n s t r i n g " > ' { " m e t h o d " : " r e s o l v e " , " p a r a m s " : { " u r i " : " $ { d a t a } " } } ' < / s p a n > < s p a n c l a s s = " t o k e n u r l " > h t t p : / / l o c a l h o s t : 5 2 7 9 < / s p a n >
2018-08-08 22:20:53 +02:00
< / c o d e > < / p r e >
2018-07-18 22:31:05 +02:00
2018-08-08 22:20:53 +02:00
< div class = "loader" id = "temp-loader" > < / d i v >
2018-08-10 00:17:47 +02:00
< div id = "example1-result" > < / d i v >
2018-07-25 00:29:25 +02:00
` );
2018-08-08 22:20:53 +02:00
$ ( "#tour-loader" ) . hide ( ) ;
2018-07-25 00:29:25 +02:00
break ;
case 2 :
2018-07-25 20:35:59 +02:00
send ( JSON . stringify ( {
"data" : data ,
"message" : "fetch metadata" ,
"method" : "publish" ,
2018-08-10 00:17:47 +02:00
"example" : exampleNumber
} ) ) ;
2018-08-18 00:22:19 +02:00
$ ( "#tour-results" ) . html ( `
< pre > < code class = "language-bash" >
< span class = "token comment" > # This will be updated soon < / s p a n >
< / c o d e > < / p r e >
< div class = "loader" id = "temp-loader" > < / d i v >
< div id = "example2-result" > < / d i v >
` );
$ ( "#tour-loader" ) . hide ( ) ;
2018-08-10 00:17:47 +02:00
break ;
case 3 :
send ( JSON . stringify ( {
"claim" : data ,
"message" : "fetch metadata" ,
"method" : "wallet_send" ,
"example" : exampleNumber
2018-07-25 20:35:59 +02:00
} ) ) ;
2018-08-10 00:17:47 +02:00
$ ( "#fetch-claim-uri" ) . val ( data ) ;
$ ( "#tour-results" ) . html ( `
< pre > < code class = "language-bash" >
2018-08-14 22:15:27 +02:00
< span class = "token comment" > # With the LBRY app / daemon running locally , you can use this in your Terminal < / s p a n >
curl -- header < span class = "token string" > "Content-Type: application/json" < / s p a n > - - d a t a < s p a n c l a s s = " t o k e n s t r i n g " > ' { " m e t h o d " : " w a l l e t _ s e n d " , " p a r a m s " : { " a m o u n t " : " 0 . 0 1 " , " c l a i m _ i d " : " $ { d a t a } " } } ' < / s p a n > < s p a n c l a s s = " t o k e n u r l " > h t t p : / / l o c a l h o s t : 5 2 7 9 < / s p a n >
2018-08-10 00:17:47 +02:00
< / c o d e > < / p r e >
< div class = "loader" id = "temp-loader" > < / d i v >
< div id = "example3-result" > < / d i v >
` );
$ ( "#tour-loader" ) . hide ( ) ;
2018-07-25 00:29:25 +02:00
break ;
default :
break ;
}
}
2018-07-18 22:31:05 +02:00
2018-07-25 20:35:59 +02:00
function getMemeInfo ( ) { // TODO: Error handling
2018-08-10 00:17:47 +02:00
const info = {
description : $ ( "#meme-description" ) . val ( ) ,
file _path : $ ( "#meme-canvas" ) [ 0 ] . toDataURL ( "image/jpeg" , 0.6 ) ,
language : $ ( "#meme-language" ) . val ( ) ,
license : $ ( "#meme-license" ) . val ( ) ,
name : $ ( "#meme-title" ) . val ( ) ,
nsfw : $ ( "#meme-nsfw-flag" ) [ 0 ] . checked ,
title : $ ( "#meme-title" ) . val ( )
} ;
2018-07-25 00:29:25 +02:00
return info ;
2018-07-18 22:31:05 +02:00
}
2018-08-21 23:22:31 +02:00
const handleExamples = debounce ( event => {
let exampleNumber ;
2018-09-27 18:11:26 +02:00
const data = event . dataset ;
2018-07-25 00:29:25 +02:00
2018-09-27 18:11:26 +02:00
if ( ! parseInt ( $ ( ".tour__navigation__example.active" ) [ 0 ] . dataset . example ) ) return ;
exampleNumber = parseInt ( $ ( ".tour__navigation__example.active" ) [ 0 ] . dataset . example ) ;
2018-07-25 00:29:25 +02:00
2018-08-21 23:22:31 +02:00
switch ( data . action ) {
case "choose claim" :
fetchMetadata ( exampleNumber , data . claimId ) ;
break ;
2018-07-24 18:44:32 +02:00
2018-08-21 23:22:31 +02:00
case "execute claim" :
if ( ! $ ( "#fetch-claim-uri" ) . val ( ) ) return ;
fetchMetadata ( exampleNumber , $ ( "#fetch-claim-uri" ) . val ( ) ) ;
break ;
case "tour, example 1" :
if ( $ ( "#tour-loader" ) . hasClass ( "tour__content__meme" ) ) {
$ ( "#tour-loader" ) . removeClass ( "tour__content__meme" ) . addClass ( "tour__content__trends" ) ;
}
$ ( "#fetch-claim-uri" ) . val ( "" ) ; // reset URL bar
2018-08-30 23:53:52 +02:00
$ ( "#tour-url button" ) . text ( "Resolve" ) ;
2018-08-21 23:22:31 +02:00
if ( $ ( "#tour-url" ) [ 0 ] . style . display === "none" ) $ ( "#tour-url" ) . show ( ) ;
2018-09-27 18:11:26 +02:00
$ ( ".tour__navigation__example" ) . removeClass ( "active" ) ;
$ ( ".tour__navigation__example:nth-child(1)" ) . addClass ( "active" ) ;
2018-08-21 23:22:31 +02:00
$ ( "#tour-loader" ) . empty ( ) . show ( ) ;
$ ( "#tour-results" ) . empty ( ) . show ( ) ;
send ( JSON . stringify ( {
"message" : ` request for ${ data . action } `
} ) ) ;
break ;
case "tour, example 2" :
if ( $ ( "#tour-loader" ) . hasClass ( "tour__content__trends" ) ) {
$ ( "#tour-loader" ) . removeClass ( "tour__content__trends" ) . addClass ( "tour__content__meme" ) ;
}
$ ( "#fetch-claim-uri" ) . val ( "" ) ; // reset URL bar
$ ( "#tour-url" ) . hide ( ) ;
2018-09-27 18:11:26 +02:00
$ ( ".tour__navigation__example" ) . removeClass ( "active" ) ;
$ ( ".tour__navigation__example:nth-child(2)" ) . addClass ( "active" ) ;
2018-08-21 23:22:31 +02:00
$ ( "#tour-loader" ) . empty ( ) . show ( ) ;
$ ( "#tour-results" ) . empty ( ) . show ( ) ;
send ( JSON . stringify ( {
"message" : ` request for ${ data . action } `
} ) ) ;
break ;
case "tour, example 3" :
if ( $ ( "#tour-loader" ) . hasClass ( "tour__content__meme" ) ) {
$ ( "#tour-loader" ) . removeClass ( "tour__content__meme" ) . addClass ( "tour__content__trends" ) ;
}
$ ( "#fetch-claim-uri" ) . val ( "" ) ; // reset URL bar
2018-08-30 23:53:52 +02:00
$ ( "#tour-url button" ) . text ( "Tip" ) ;
// $("#tour-url").after("<p>In the LBRY app, you can financially support your favorite creators by donating LBRY Coin (LBC). In this example, we are donating LBC in your stead.</p>");
2018-08-21 23:22:31 +02:00
if ( $ ( "#tour-url" ) [ 0 ] . style . display === "none" ) $ ( "#tour-url" ) . show ( ) ;
2018-09-27 18:11:26 +02:00
$ ( ".tour__navigation__example" ) . removeClass ( "active" ) ;
$ ( ".tour__navigation__example:nth-child(3)" ) . addClass ( "active" ) ;
2018-08-21 23:22:31 +02:00
$ ( "#tour-loader" ) . empty ( ) . show ( ) ;
$ ( "#tour-results" ) . empty ( ) . show ( ) ;
send ( JSON . stringify ( {
"message" : ` request for ${ data . action } `
} ) ) ;
break ;
case "upload image" :
fetchMetadata ( exampleNumber , getMemeInfo ( ) ) ;
break ;
default :
break ;
}
} , 10 ) ;
2018-07-24 18:44:32 +02:00
2018-08-11 00:29:06 +02:00
function initCanvas ( ) { // eslint-disable-line
2018-07-24 18:44:32 +02:00
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 ) ) ;
}