let pointConfig = [ { title: "Shoulder", top: 45, left: 80, }, { title: "Biceps", top: 85, left: 170, }, { title: "Abs", top: 115, left: 121, }, { title: "Quadriceps", top: 200, left: 146, }, { title: "Back", top: 75, left: 347, }, { title: "Shoulder", second: "1", top: 43, left: 392, }, { title: "Buttocks", top: 145, left: 327, }, { title: "Triceps", top: 76, left: 300, }, { title: "Hamstrings", top: 208, left: 375, }, ]; let Point = ( function () { let Point = function ( conf, callback ) { this.config = conf; for ( let i = 0; i < this.config.length; i++ ) { if ( this.config[ i ].second ) { this.config[ i ].class = this.config[ i ].title.toLowerCase() + this.config[ i ].second; } else { this.config[ i ].class = this.config[ i ].title.toLowerCase(); } } this.callback = callback; this.addSpecificCSS(); this.adHtml(); }; let proto = Point.prototype; proto.addSpecificCSS = function () { let css = ``; if ( $( "#point-css" ).length === 0 ) { $( "head" ).append( css.replace( / |\n/g, "" ) ); } }; proto.adHtml = function () { let container = $( "#point-container" ); let html = ``; for ( let conf of this.config ) { html += `
`; } container.append( html.replace( / |\n/g, "" ) ); this.callback( this ); }; proto.listen = function () { // Click on a point not selected $( document ).on( "click", ".point:not(.selected)", ( elem ) => { let $elem = $( elem.target ); let storageData = localStorage.getItem( "muscles" ); if ( storageData ) { storageData = JSON.parse( storageData ); } $( ".point" ).each( ( i, e ) => { let $e = $( e ); $e.removeClass( "selected" ); } ); $elem.addClass( "selected" ); $( "#exercise-title" ).text( $elem.attr( "title" ) ); if ( storageData && storageData[ $elem.data( "name" ) ] ) { let data = storageData[ $elem.data( "name" ) ]; let c = $( "#exercises-container" ); c.empty(); if ( data.length > 0 ) { for ( let exercise of data ) { c.append( `
${ exercise.title }
` ); } } else { c.append( "
Please select a muscle
" ); } } else { $.ajax( { url: "/api/muscles/get-list", method: "POST", data: { muscle: $elem.data( "name" ) }, success: function ( data ) { data = JSON.parse( data ); if ( storageData === null ) { storageData = {}; } storageData[ $elem.data( "name" ) ] = data; localStorage.setItem( "muscles", JSON.stringify( storageData ) ); let c = $( "#exercises-container" ); c.empty(); if ( data.length > 0 ) { for ( let exercise of data ) { c.append( `
${ exercise.title }
` ); } } else { c.append( "
Please select a muscle
" ); } } } ); } } ); // Click on a point selected $( document ).on( "click", ".point.selected", ( elem ) => { let $elem = $( elem.currentTarget ); let c = $( "#exercises-container" ); $elem.removeClass( "selected" ); $( "#exercise-title" ).text( "Selected part of the body" ); c.html( "
Please select a muscle
" ); } ); // Click on an exercise $( document ).on( "click", "#exercises-container div", ( elem ) => { let $elem = $( elem.target ); let storageData = localStorage.getItem( "exercises" ); if ( storageData ) { storageData = JSON.parse( storageData ); } else { storageData = {}; } if ( $elem.data( "id" ) ) { if ( storageData[ $elem.data( "id" ) ] ) { let data = storageData[ $elem.data( "id" ) ]; let main = $( "#main-container" ); let favStorage = localStorage.getItem( "fav" ); if ( favStorage ) { favStorage = JSON.parse( favStorage ); } else { favStorage = {}; } let fav = favStorage[ $elem.data( "id" ) ]; main.html( `

${ data.title }

${ ( data.description ? data.description : "" ) }
` ); } else { $.ajax( { url: "/api/muscles/get-exercise", method: "POST", data: { id: $elem.data( "id" ), }, success: function ( data ) { data = JSON.parse( data ); storageData[ $elem.data( "id" ) ] = data; localStorage.setItem( "exercises", JSON.stringify( storageData ) ); let main = $( "#main-container" ); let favStorage = localStorage.getItem( "fav" ); if ( favStorage ) { favStorage = JSON.parse( favStorage ); } else { favStorage = {}; } main.html( `

${ data.title }

${ ( data.description ? data.description : "" ) }
` ); } } ); } } } ); // On click on favorite $( document ).on( "click", ".favorite", ( elem ) => { let userId = localStorage.getItem( "userId" ); let favStorage = localStorage.getItem( "fav" ); let $elem = $( elem.currentTarget ); if ( favStorage ) { favStorage = JSON.parse( favStorage ); } else { favStorage = {}; } if ( $elem.data( "fav" ) === "true" ) { favStorage[ $elem.data( "id" ) ] = false; $elem.find( "img" ).attr( "src", "/img/no-favorite.png" ); $elem.data( "fav", "false" ); $.ajax( { url: "/api/user/favorite-delete", method: "POST", data: { "id_user": userId, "id_exercise": $elem.data( "id" ) } } ); } else { favStorage[ $elem.data( "id" ) ] = true; $elem.data( "fav", "true" ); $elem.find( "img" ).attr( "src", "/img/favorite.png" ); $.ajax( { url: "/api/user/favorite-add", method: "POST", data: { "id_user": userId, "id_exercise": $elem.data( "id" ) } } ); } localStorage.setItem( "fav", JSON.stringify( favStorage ) ); } ); }; proto.start = function () { let container = $( "#point-container" ); container.addClass( "start" ); this.listen(); }; return Point; }() ); $( document ).ready( () => { let userId = localStorage.getItem( "userId" ); let userName = localStorage.getItem( "userName" ); $( ".dropdown-trigger" ).dropdown(); let point; new Point( pointConfig, ( p ) => { point = p; point.start(); } ); if ( userId === null ) { $.ajax( { url: "/api/user/create", method: "POST", success: function ( data ) { localStorage.setItem( "userId", data.id ); userId = data.id; }, error: function () { } } ); } if ( userName !== null ) { $( "a[data-target=user]" ).html( userName + `arrow_drop_down` ); } $( document ).on( "click", "#change-name", function () { let modal = $( "#modal" ); modal.find( ".md-title" ).text( "Change Your Name" ); modal.find( ".md-container" ).html( `
` ); modal.find( "#md-validate" ).click( () => { $.ajax( { url: "/api/user/modify", method: "POST", data: { "user_id": userId, "name": $( "#name" ).val() }, success: function ( data ) { localStorage.setItem( "userId", data.is ); localStorage.setItem( "userName", data.name ); $( "a[data-target=user]" ).html( data.name + `arrow_drop_down` ); modal.removeClass( "md-show" ); modal.find( "md-title" ).empty(); modal.find( ".md-container" ).empty(); }, } ); } ); modal.find( "#md-cancel" ).click( () => { modal.removeClass( "md-show" ); modal.find( "md-title" ).empty(); modal.find( ".md-container" ).empty(); } ); modal.addClass( "md-show" ); } ); } );