CAU-Web-Fall-2018-Project-1/static/js/javascript.js

424 lines
16 KiB
JavaScript
Raw Permalink Normal View History

2018-12-13 01:18:24 +00:00
let pointConfig = [
{
2018-12-15 05:50:08 +00:00
title: "Shoulder",
2018-12-13 01:18:24 +00:00
top: 45,
left: 80,
},
{
2018-12-15 05:50:08 +00:00
title: "Biceps",
2018-12-13 01:18:24 +00:00
top: 85,
left: 170,
},
{
2018-12-15 05:50:08 +00:00
title: "Abs",
2018-12-13 01:18:24 +00:00
top: 115,
left: 121,
},
2018-12-15 05:50:08 +00:00
{
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,
},
2018-12-13 01:18:24 +00:00
];
2018-12-12 06:04:47 +00:00
2018-12-13 01:18:24 +00:00
let Point = ( function () {
2018-12-12 06:04:47 +00:00
2018-12-13 01:55:35 +00:00
let Point = function ( conf, callback ) {
2018-12-13 01:18:24 +00:00
this.config = conf;
2018-12-15 05:50:08 +00:00
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();
}
}
2018-12-13 01:55:35 +00:00
this.callback = callback;
2018-12-13 01:18:24 +00:00
this.addSpecificCSS();
this.adHtml();
};
let proto = Point.prototype;
proto.addSpecificCSS = function () {
let css = `<style id="point-css">`;
for ( let conf of this.config ) {
css += `
2018-12-15 05:50:08 +00:00
.${ conf.class }-css {
animation: ${ conf.class }-animation 1s linear 0s infinite normal none running;
2018-12-13 01:18:24 +00:00
top: ${ conf.top }px;
left: ${ conf.left }px;
}
2018-12-15 05:50:08 +00:00
.${ conf.class }-css.stop {
2018-12-13 01:18:24 +00:00
animation: none;
}
2018-12-15 05:50:08 +00:00
@keyframes ${ conf.class }-animation {
2018-12-13 01:18:24 +00:00
0% {
width: 10px;
height: 10px;
top: ${ conf.top }px;
left: ${ conf.left }px;
}
50% {
width: 20px;
height: 20px;
top: ${ conf.top - 5 }px;
left: ${ conf.left - 5 }px;
}
100% {
width: 10px;
height: 10px;
top: ${ conf.top }px;
left: ${ conf.left }px;
}
}`;
}
css += `</style>`;
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 ) {
2018-12-15 05:50:08 +00:00
html += `<div data-name="${ conf.title.toLowerCase() }" title="${ conf.title }" class="blue darken-3 point ${ conf.class }-css${ conf.noAnim ? " stop" : "" }"><div></div></div>`;
2018-12-13 01:18:24 +00:00
}
container.append( html.replace( / |\n/g, "" ) );
2018-12-13 01:55:35 +00:00
this.callback( this );
2018-12-13 01:18:24 +00:00
};
2018-12-15 05:50:08 +00:00
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" );
2018-12-15 05:50:08 +00:00
if ( storageData ) {
storageData = JSON.parse( storageData );
}
2018-12-15 05:50:08 +00:00
$( ".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( `<div data-id="${ exercise.id }">${ exercise.title }</div>` );
2018-12-15 05:50:08 +00:00
}
} else {
c.append( "<div>Please select a muscle</div>" );
2018-12-15 05:50:08 +00:00
}
} 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( `<div data-id="${ exercise.id }">${ exercise.title }</div>` );
}
} else {
c.append( "<div>Please select a muscle</div>" );
}
}
} );
}
2018-12-15 05:50:08 +00:00
} );
// 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( "<div>Please select a muscle</div>" );
} );
// Click on an exercise
$( document ).on( "click", "#exercises-container div", ( elem ) => {
let $elem = $( elem.target );
let storageData = localStorage.getItem( "exercises" );
2018-12-15 05:50:08 +00:00
if ( storageData ) {
storageData = JSON.parse( storageData );
} else {
storageData = {};
}
2018-12-15 05:50:08 +00:00
if ( $elem.data( "id" ) ) {
if ( storageData[ $elem.data( "id" ) ] ) {
let data = storageData[ $elem.data( "id" ) ];
let main = $( "#main-container" );
2018-12-15 06:45:24 +00:00
let favStorage = localStorage.getItem( "fav" );
if ( favStorage ) {
favStorage = JSON.parse( favStorage );
} else {
favStorage = {};
}
let fav = favStorage[ $elem.data( "id" ) ];
2018-12-15 05:50:08 +00:00
main.html( `
2018-12-15 06:45:24 +00:00
<div data-id="${ $elem.data( "id" ) }" data-fav="${ fav ? "true" : "false" }" class="right favorite">
<img src="/img/${ fav ? "" : "no-" }favorite.png" alt="">
</div>
2018-12-15 05:50:08 +00:00
<h2>${ data.title }</h2>
<div class="center-align">
<iframe width="560" height="315" src="${ data.video.replace( "https://youtu.be/", "https://www.youtube.com/embed/" ) }" frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe>
<div class="exercise-info-container blue-grey lighten-4 left-align">${ ( data.description ? data.description : "" ) }</div>
</div>
` );
} 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" );
2018-12-15 06:45:24 +00:00
let favStorage = localStorage.getItem( "fav" );
if ( favStorage ) {
favStorage = JSON.parse( favStorage );
} else {
favStorage = {};
}
2018-12-16 07:05:08 +00:00
let fav = favStorage[ $elem.data( "id" ) ];
main.html( `
2018-12-16 10:34:26 +00:00
<div data-id="${ $elem.data( "id" ) }" data-fav="${ fav ? "true" : "false" }" class="right favorite">
2018-12-16 07:05:08 +00:00
<img src="/img/${ fav ? "" : "no-" }favorite.png" alt="">
2018-12-15 06:45:24 +00:00
</div>
<h2>${ data.title }</h2>
<div class="center-align">
<iframe width="560" height="315" src="${ data.video.replace( "https://youtu.be/", "https://www.youtube.com/embed/" ) }" frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe>
<div class="exercise-info-container blue-grey lighten-4 left-align">${ ( data.description ? data.description : "" ) }</div>
</div>
` );
}
} );
}
2018-12-15 05:50:08 +00:00
}
} );
2018-12-15 06:45:24 +00:00
// 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 = {};
}
2018-12-16 10:34:26 +00:00
console.log( $elem );
if ( $elem.attr( "data-fav" ) === "true" ) {
2018-12-15 06:45:24 +00:00
favStorage[ $elem.data( "id" ) ] = false;
$elem.find( "img" ).attr( "src", "/img/no-favorite.png" );
2018-12-16 10:34:26 +00:00
$elem.attr( "data-fav", "false" );
2018-12-15 06:45:24 +00:00
$.ajax( {
url: "/api/user/favorite-delete",
method: "POST",
data: {
"id_user": userId,
"id_exercise": $elem.data( "id" )
}
} );
} else {
favStorage[ $elem.data( "id" ) ] = true;
2018-12-16 10:34:26 +00:00
$elem.attr( "data-fav", "true" );
2018-12-15 06:45:24 +00:00
$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 ) );
} );
2018-12-15 05:50:08 +00:00
};
2018-12-13 01:18:24 +00:00
proto.start = function () {
2018-12-13 01:55:35 +00:00
let container = $( "#point-container" );
2018-12-13 01:18:24 +00:00
2018-12-13 01:55:35 +00:00
container.addClass( "start" );
2018-12-15 05:50:08 +00:00
this.listen();
2018-12-13 01:18:24 +00:00
};
return Point;
}() );
$( document ).ready( () => {
2018-12-15 05:50:08 +00:00
let userId = localStorage.getItem( "userId" );
let userName = localStorage.getItem( "userName" );
2018-12-13 01:18:24 +00:00
$( ".dropdown-trigger" ).dropdown();
2018-12-13 01:55:35 +00:00
let point;
new Point( pointConfig, ( p ) => {
point = p;
point.start();
} );
2018-12-13 01:18:24 +00:00
2018-12-15 05:50:08 +00:00
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 + `<i class="material-icons right">arrow_drop_down</i>` );
}
2018-12-16 07:05:08 +00:00
$( document ).on( "click", "#change-name", () => {
2018-12-15 05:50:08 +00:00
let modal = $( "#modal" );
modal.find( ".md-title" ).text( "Change Your Name" );
modal.find( ".md-container" ).html( `
<div class="row">
<div class="input-field col s12">
<input id="name" type="text" class="validate" value="${ userName !== null ? userName : "" }">
<label for="name" ${ userName !== null ? `class="active"` : "" }>Your Name</label>
</div>
</div>
` );
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 + `<i class="material-icons right">arrow_drop_down</i>` );
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" );
} );
2018-12-16 07:05:08 +00:00
$( document ).on( "click", "#your-exercise", function () {
let favStorage = localStorage.getItem( "fav" );
let exercisesStorage = localStorage.getItem( "exercises" );
let main = $( "#main-container" );
main.empty();
if ( favStorage ) {
localStorage = JSON.parse( favStorage );
console.log( favStorage );
if ( exercisesStorage ) {
exercisesStorage = JSON.parse( exercisesStorage );
}
for ( let id in favStorage ) {
if ( favStorage.hasOwnProperty( id ) ) {
if ( favStorage[ id ] ) {
if ( exercisesStorage.hasOwnProperty( id ) ) {
main.append( `
2018-12-16 10:34:26 +00:00
<div data-id="${ id }" data-fav="true" class="right favorite">
2018-12-16 07:05:08 +00:00
<img src="/img/favorite.png" alt="">
</div>
<h2>${ exercisesStorage[ id ].title }</h2>
<div class="center-align">
<iframe width="560" height="315" src="${ exercisesStorage[ id ].video.replace( "https://youtu.be/", "https://www.youtube.com/embed/" ) }" frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe>
<div class="exercise-info-container blue-grey lighten-4 left-align">${ ( exercisesStorage[ id ].description ? exercisesStorage[ id ].description : "" ) }</div>
</div>
` );
}
}
}
}
} else {
}
} );
2018-12-13 01:18:24 +00:00
} );