diff --git a/services/gui/app/views/public/app.js b/services/gui/app/views/public/app.js index 45ce4d9..93e566a 100644 --- a/services/gui/app/views/public/app.js +++ b/services/gui/app/views/public/app.js @@ -1,165 +1,5 @@ -//prepare modal to delete a title -function prepareDeleteModal(name, id) { - //set name of modal - $("#deleteModalName").text(name); - //set action to id of title - $("#deleteModalForm").attr("action", "/title/" + id + "/?_method=DELETE"); - //show delete modal - $("#deleteModal").modal("show"); -} - -//clicking the magnifier activates the search bar -function activateSearchBar() { - $("#searchNewTitle").focus(); -} - //trigger fadeout of flash messages window.onload = function() { $("#successAlert").fadeOut(5000); $("#errorAlert").fadeOut(5000); }; - -//update a title as seen or unseen -function toggleSeenStatus(id, name, seen) { - let form = document.createElement("form"); - form.action = "/title/" + id + "/?_method=PUT"; - form.method = "POST"; - if (seen) { - form.innerHTML = - ' '; - } else { - form.innerHTML = ''; - } - - //the form must be in the document to submit it, but should be invisible - form.hidden = true; - document.body.append(form); - - form.submit(); -} - -//suggestions from IMDB (via omdb api) for adding a new title -//needs to be globally available -var omdbRequest = null; -function suggestTitle() { - var searchString = $("#searchNewTitle").val(); - if (searchString.length > 2) { - //ask omdb to find a title based on the input provided - omdbRequest = jQuery.ajax({ - type: "GET", - url: `https://www.omdbapi.com/?s=${searchString}&apikey=b50af808`, - beforeSend: function() { - //if there is an unfinished request to omdb, abort it, to avoid overlapping responses - if (omdbRequest != null) { - omdbRequest.abort(); - } - }, - success: function(response) { - if (response.Response === "True" && response.Search.length > 0) { - const results = $("#results"); - //clear suggestion list - results.html(""); - for (let suggestion of response.Search) { - //some titles have no cover and some covers are hosted at imdb, seems like they don't allow external usage of those, we replace those with a default one - if ( - suggestion.Poster === "N/A" || - suggestion.Poster.includes("media-imdb.com") || - suggestion.Poster.includes("images-na") - ) { - suggestion.Poster = "/nocover.png"; - } - //build string for suggestion - let suggestionHtml = ` -
(${suggestion.Year})
-😰ooops we can't get results from iMDB, please notify us!
" - ); - $("#results").show(); - } - } - }); - } else { - $("#results").hide(0); - } -} - -//add a new title -function addTitle(name, imdbID, year, poster, genres) { - let form = document.createElement("form"); - form.action = "/title"; - form.method = "POST"; - - var ratingRequest = new XMLHttpRequest(); - ratingRequest.onreadystatechange = function() { - if (ratingRequest.readyState == 4 && ratingRequest.status == 200) { - let genreArray = JSON.parse(ratingRequest.responseText).Genre.split(","); - let genreInput = ""; - for (let i = 0; i < genreArray.length; i++) { - genreInput += ``; - } - - form.innerHTML = ` - - - - - - - ${genreInput} - `; - //the form must be in the document to submit it, but should be invisible - form.hidden = true; - document.body.append(form); - form.submit(); - } - }; - - ratingRequest.open( - "GET", - "https://www.omdbapi.com/?i=" + imdbID + "&apikey=b50af808&tomatoes=true" - ); - ratingRequest.send(); -} - -//hide suggestions when search field loses focus -function hideSuggestions() { - setTimeout(function() { - $("#results").hide(0); - }, 200); -} diff --git a/services/gui/app/views/public/moveez.js b/services/gui/app/views/public/moveez.js new file mode 100644 index 0000000..4b36f0b --- /dev/null +++ b/services/gui/app/views/public/moveez.js @@ -0,0 +1,15 @@ +import { + prepareDeleteModal, + activateSearchBar, + toggleSeenStatus, + suggestTitle, + hideSuggestions +} from "/title.js"; + +export const moveez = (window.moveez = { + prepareDeleteModal, + activateSearchBar, + toggleSeenStatus, + suggestTitle, + hideSuggestions +}); diff --git a/services/gui/app/views/public/title.js b/services/gui/app/views/public/title.js new file mode 100644 index 0000000..fce1292 --- /dev/null +++ b/services/gui/app/views/public/title.js @@ -0,0 +1,181 @@ +//prepare modal to delete a title +export function prepareDeleteModal(name, id) { + //set name of modal + $("#deleteModalName").text(name); + //set action to id of title + $("#deleteModalForm").attr("action", "/title/" + id + "/?_method=DELETE"); + //show delete modal + $("#deleteModal").modal("show"); +} + +//clicking the magnifier activates the search bar +export function activateSearchBar() { + $("#searchNewTitle").focus(); +} + +//update a title as seen or unseen +export function toggleSeenStatus(id, name, seen) { + let form = document.createElement("form"); + form.action = "/title/" + id + "/?_method=PUT"; + form.method = "POST"; + if (seen) { + form.innerHTML = + ' '; + } else { + form.innerHTML = ''; + } + + //the form must be in the document to submit it, but should be invisible + form.hidden = true; + document.body.append(form); + + form.submit(); +} + +//suggestions from IMDB (via omdb api) for adding a new title +//needs to be globally available +var omdbRequest = null; +export function suggestTitle() { + var searchString = $("#searchNewTitle").val(); + if (searchString.length > 2) { + //ask omdb to find a title based on the input provided + omdbRequest = jQuery.ajax({ + type: "GET", + url: `https://www.omdbapi.com/?s=${searchString}&apikey=b50af808`, + beforeSend: function() { + //if there is an unfinished request to omdb, abort it, to avoid overlapping responses + if (omdbRequest != null) { + omdbRequest.abort(); + } + }, + success: function(response) { + if (response.Response === "True" && response.Search.length > 0) { + const results = $("#results"); + //clear suggestion list + results.html(""); + for (let suggestion of response.Search) { + //some titles have no cover and some covers are hosted at imdb, seems like they don't allow external usage of those, we replace those with a default one + if ( + suggestion.Poster === "N/A" || + suggestion.Poster.includes("media-imdb.com") || + suggestion.Poster.includes("images-na") + ) { + suggestion.Poster = "/nocover.png"; + } + //build string for suggestion + let suggestionHtml = renderSuggestion(suggestion); + results.append(suggestionHtml); + results + .children() + .last() + .click(() => onSuggestionSelected(suggestion)); + } + results.show(); + } + }, + error: function(err, errText) { + //aborting a request also calls the error function - crazy people + if (errText != "abort") { + console.log(`ERR: oMDB failed us, here is the reason: ${errText}`); + $("#results").html( + "😰ooops we can't get results from iMDB, please notify us!
" + ); + $("#results").show(); + } + } + }); + } else { + $("#results").hide(0); + } +} + +const renderSuggestion = suggestion => { + const bIsInWatchlist = isInWatchList(suggestion); + return ` +(${suggestion.Year})
+