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 () { console.log( "Point addSpecificCSS" ); 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" ); 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" ); main.html( `

${ data.title }

${ ( data.description ? data.description : "" ) }
` ); } } ); } } } ); }; 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" ); } ); } );