{"size": 4394, "ext": "js", "max_stars_repo_path": "portfolio/src/main/webapp/script.js", "max_stars_repo_name": "Alexjoshua14/software-product-sprint", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "portfolio/src/main/webapp/script.js", "max_issues_repo_name": "Alexjoshua14/software-product-sprint", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "portfolio/src/main/webapp/script.js", "max_forks_repo_name": "Alexjoshua14/software-product-sprint", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 32.7910447761, "max_line_length": 81, "alphanum_fraction": 0.7077833409}
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * Adds a random greeting to the page.
 */
function addRandomGreeting() {
  const greetings =
      ['Hello world!', '¡Hola Mundo!', '你好，世界！', 'Bonjour le monde!'];

  // Pick a random greeting.
  const greeting = greetings[Math.floor(Math.random() * greetings.length)];

  // Add it to the page.
  const greetingContainer = document.getElementById('greeting-container');
  greetingContainer.innerText = greeting;
}

/** 
 * Cycles through pictures
*/

// I need to figure out how to seperate the
// different picture sections. 
let slideIndex = [0, 0];
let slideId = ["slides-1", "slides-2"];

// Next/previous controls
function changeSlides(n, sectionNumber) {
    //Adjust for zero indexing
    sectionNumber -= 1;
    showSlides(slideIndex[sectionNumber] += n, sectionNumber);
}

/**
 * 
 * @param {number} index The index of the slide to display
 * @param {string} slides The id name of the group of slides
 */
function showSlides(index, sectionNumber) {
    let slides = document.getElementsByClassName(slideId[sectionNumber]);

    if (index >= slides.length) {
        slideIndex[sectionNumber] = 0;
    } else if (index < 0) {
        slideIndex[sectionNumber] = slides.length - 1;
    }

    for (let i = 0; i < slides.length; i++) {
        slides[i].style.display = "none";
    }

    slides[slideIndex[sectionNumber]].style.display = "flex";
}

async function sayHello() {
    // Fetch Json object from servlet
    const responseFromServer = await fetch('/hello');
    const jsonObj = await responseFromServer.json();

    // Parse the Json object for a random phrase
    const responseLength = jsonObj.length;
    const randomIndex = Math.floor(Math.random() * Math.floor(responseLength));
    const textResponse = jsonObj[randomIndex];

    // Send this phrase to the helloContainer in index.js
    const helloContainer = document.getElementById('hello-container');
    helloContainer.innerText = textResponse;
}

async function randomFact() {
    // Fetch Json object from servlet
    const responseFromServer = await fetch('/facts');
    const jsonObj = await responseFromServer.json();

    // Parse the Json object for a random phrase
    const responseLength = jsonObj.length;
    const randomIndex = Math.floor(Math.random() * Math.floor(responseLength));
    const textResponse = jsonObj[randomIndex];

    // Send this phrase to the helloContainer in index.js
    const factContainer = document.getElementById('fact-container');
    factContainer.innerText = textResponse;
}

/** Fetches messages from the server and adds them to the DOM. */
function loadMessages() {
  fetch('/list-messages').then(response => response.json()).then((tasks) => {
    const taskListElement = document.getElementById('message-list');
    tasks.forEach((message) => {
      taskListElement.appendChild(createTaskElement(message));
    })
  });
}

/** Creates an element that represents a message, including its delete button. */
function createMessageElement(message) {
  const messageElement = document.createElement('li');
  messageElement.className = 'message';

  const detailsElement = document.createElement('span');
  detailsElement.innerText = task.email + ": " + task.message;

  const deleteButtonElement = document.createElement('button');
  deleteButtonElement.innerText = 'Delete';
  deleteButtonElement.addEventListener('click', () => {
    deleteMessage(message);

    // Remove the message from the DOM.
    messageElement.remove();
  });

  messageElement.appendChild(detailsElement);
  messageElement.appendChild(deleteButtonElement);
  return messageElement;
}

/** Tells the server to delete the message. */
function deleteMessage(message) {
  const params = new URLSearchParams();
  params.append('id', message.id);
  fetch('/delete-message', {method: 'POST', body: params});
}