Skip to content

Instantly share code, notes, and snippets.

@gabriel-lopez
Created May 2, 2018 05:54
Show Gist options
  • Select an option

  • Save gabriel-lopez/ef433c609df91057907a26b72d2c5c13 to your computer and use it in GitHub Desktop.

Select an option

Save gabriel-lopez/ef433c609df91057907a26b72d2c5c13 to your computer and use it in GitHub Desktop.
ProgCli - Examen M45
// Aide pour les URL des Web Services
var WS = "https://chabloz.eu/movies/";
var WS_THUMB = "https://chabloz.eu/movies/thumb/?id=";
var WS_RATING = "https://chabloz.eu/movies/rating/get/";
var WS_RATEIT = "https://chabloz.eu/movies/rating/post/";
// Aide: pour arrondir un nombre à l'entier le plus proche: Math.round(nb_ici);
// Variables globales
var templateMovie;
$(document).ready(function()
{
// Copie des templates
templateMovie = $(".templateMovie").clone();
// Suppression des templates
$(".templateMovie").remove();
$("#btnSearch").click(function()
{
getMovies($("#search").val());
});
$("#search").pressEnter(function()
{
getMovies($(this).val());
});
$("body").on("click", "span.rateIt", function()
{
var parent = $(this).parent().parent();
var id = parent.attr("id");
var dom = $(".review", parent);
var rating = $(this).attr("data-rating");
sendRating(id, rating, dom);
});
});
function getMovies(query)
{
$.getJSON(WS + "?query=" + query, function(movies)
{
$("#results").empty();
var empty = $.isEmptyObject(movies);
if(empty)
{
$("#results").append("<p>Aucun film à l'affiche dans la ville selectionnée !</p>");
}
$.each(movies, function(i, movie)
{
var tmpl = templateMovie.clone();
tmpl.attr("id", movie.id);
$(".title", tmpl).text(movie.title);
$(".description", tmpl).text(movie.description);
$('.thumb', tmpl).attr("src", WS_THUMB + movie.id);
var dom = $('.review', tmpl);
getRating(movie.id, dom);
tmpl.appendTo("#results");
});
});
}
function getRating(id, dom)
{
$.get(WS_RATING, {"id": id}, function(xml)
{
updateRatingDom(xml, dom);
});
}
function updateRatingDom(xml, dom)
{
var root = $("rating", xml);
var status = $("status", root).text();
if(status === 'success')
{
var rating = $("rating", root).text();
var votes = $("votes", root).text();
var round = Math.round(rating);
$(".votes", dom).text(votes);
$(".rating", dom).text(rating);
$(".rating", dom).removeClass("rating0 rating1 rating2 rating3 rating4 rating5");
$(".rating", dom).addClass("rating" + round);
}
else
{
dom.hide();
}
}
function sendRating(id, rating, dom)
{
$.post(WS_RATEIT, {"id" : id, "rating": rating} , function(xml)
{
updateRatingDom(xml, dom);
});
}
$.fn.pressEnter = function(fn)
{
return this.each(function()
{
$(this).bind("enterPress", fn);
$(this).keyup(function(e)
{
if(e.keyCode == 13)
{
$(this).trigger("enterPress");
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment