001window.onload = function(){
002 showStuff('sudioghibli.json')
003}
004
005function showStuff(whereFrom) {
006 fetch(whereFrom)
007 .then(response =>
008 {
009 if (!response.ok) {
010 throw new Error("Failed with HTTP code " + response.status);
011 }
012 return response;
013 })
014 .then(result => result.json())
015 .then(data =>
016 {
017
018
019
020
021
022 const theElementSelect = generateOptions(data);
023
024
025
026 getDataFromSelection(data, theElementSelect);
027
028
029 });
030}
031
032
033
034function generateOptions(options)
035{
036 /*console.log(options.films.length)*/
037 const theSelectElement = document.querySelector('select');
038 for (let i = 0; i < options.films.length; i++)
039 {
040 const optionElement = document.createElement("option");
041 const optionText = document.createTextNode(options.films[i].title);
042 optionElement.appendChild(optionText);
043 theSelectElement.append(optionElement);
044 }
045 return theSelectElement;
046}
047
048
049function getDataFromSelection(options, theSelectElement)
050{
051
052
053 const titleP = document.querySelector('#title');
054 const descriptionP = document.querySelector('#description');
055 const directorP = document.querySelector('#director');
056 const yearP = document.querySelector('#year');
057 const bannerI = document.querySelector('#banner');
058
059
060 theSelectElement.onchange = function()
061 {
062
063 const theSectedOption = theSelectElement.options[theSelectElement.selectedIndex];
064
065 const selectedValue = theSectedOption.value;
066
067 const filmIndex = theSectedOption.index-1;
068
069 if(filmIndex == -1)
070 {
071
072 titleP.innerHTML = "";
073 descriptionP.innerHTML = "";
074 directorP.innerHTML = "";
075 yearP.innerHTML = ""
076 bannerI.setAttribute('src', 'logo.svg');
077
078 bannerI.setAttribute('width', '100%');
079 }
080 else
081 {
082 titleP.innerHTML = selectedValue;
083 descriptionP.innerHTML = options.films[filmIndex].description;
084 directorP.innerHTML = "Director: " + options.films[filmIndex].director;
085 yearP.innerHTML = "Release year: " + options.films[filmIndex].release_date
086 bannerI.setAttribute('src', options.films[filmIndex].movie_banner);
087
088 bannerI.setAttribute('width', '70%');
089 bannerI.setAttribute('min-width', '300px');
090 }
091 }
092}
093
094
095
096function createDisplayDataFromNonNestedObject(obj)
097{
098 let html = '';
099 for(item in obj)
{
html += `<p>${capitalise(item)}: ${capitalise(obj[item])}</p>`;
}
return html;
}
function insertIntoHTML(html, elem = document.querySelector('main'))
{
elem.insertAdjacentHTML('afterbegin', html);
}
function capitalise(str)
{
return str[0].toUpperCase() + str.slice(1).toLowerCase();
}