Skip to content
Snippets Groups Projects
code.js 8.94 KiB
Newer Older
AA2875's avatar
AA2875 committed
function init() {
    let infoText = document.getElementById('infoText')
    infoText.innerHTML = 'Ladataan hyppyjä palvelimelta, odota...'
    jumpOrder = 'desc'
AA2875's avatar
AA2875 committed
    loadJumps()

    // Send form with enter
    window.addEventListener("keypress", function(event) {
        if (event.key === "Enter") {
            event.preventDefault();
            document.getElementById("addJumpBtn").click();
        }});
AA2875's avatar
AA2875 committed
}

async function loadJumps() {
AA2875's avatar
AA2875 committed
    let response = await fetch('http://localhost:3000/jumps')
    jumpsJSON = await response.json()
    showJumps(jumpsJSON)
    let jumpOrderButton = document.getElementById('sortJumpsButton')
    jumpOrderButton.style = 'display: block;'
AA2875's avatar
AA2875 committed
}

AA2875's avatar
AA2875 committed
function createJumpListItem(jump) {
AA2875's avatar
AA2875 committed
    let li = document.createElement('li')
AA2875's avatar
AA2875 committed
    let li_attr_id = document.createAttribute('id')
    let li_attr_class = document.createAttribute('class')
    li_attr_class.value = 'jumpItem'
    li.setAttributeNode(li_attr_class)
    li_attr_id.value = jump._id
    li.setAttributeNode(li_attr_id)
AA2875's avatar
AA2875 committed

AA2875's avatar
AA2875 committed
    let spanDate = document.createElement('span')
    let spanClassDate = document.createAttribute('class')
    spanClassDate.value = 'headerJumpDate'
    spanDate.setAttributeNode(spanClassDate)
AA2875's avatar
AA2875 committed
    let jumpDate = document.createTextNode(jump.date)
AA2875's avatar
AA2875 committed
    spanDate.appendChild(jumpDate)
    let jumpHeader = document.createTextNode('#' + jump.number)
    li.appendChild(jumpHeader)
    li.appendChild(spanDate)
AA2875's avatar
AA2875 committed

AA2875's avatar
AA2875 committed
    let spanMore = document.createElement('span')
    let spanClassMore = document.createAttribute('class')
    spanClassMore.value = 'headerMore'
    spanMore.setAttributeNode(spanClassMore)
    let moreButton = document.createTextNode('')
    spanMore.appendChild(moreButton)
    li.appendChild(spanMore)
AA2875's avatar
AA2875 committed

AA2875's avatar
AA2875 committed
    li.appendChild(jumpItemFullInfo(jump))
AA2875's avatar
AA2875 committed

    // Shows and hides rest of the jump info
    li.onclick = function() {
        let arrow = document.getElementById(jump._id).getElementsByClassName('headerMore')[0]
AA2875's avatar
AA2875 committed
        let clickedItem = document.getElementById(jump._id).getElementsByClassName('fullJumpInfo')[0]
AA2875's avatar
AA2875 committed
            if (clickedItem.style.display != 'none') {
                clickedItem.style.display = 'none'
                arrow.innerHTML = ''
            }
            else {
                clickedItem.style.display = 'list-item'
                arrow.innerHTML = ''
            }
AA2875's avatar
AA2875 committed
        }
AA2875's avatar
AA2875 committed
    return li
}
var totalFalltime = 0
function jumpItemFullInfo(jump) {
    totalFalltime += parseInt(jump.falltime)
AA2875's avatar
AA2875 committed
    const specs = [
        'Paikka: ' + jump.place,
        'Lentokone: ' + jump.plane,
        'Päävarjo: ' + jump.canopy,
        'Korkeus: ' + jump.height + ' m',
        'Vapaapudotusaika: ' + jump.falltime + ' s ' + `(yht. ${totalFalltime} s)`,
        jump.comments,
        jump.link
AA2875's avatar
AA2875 committed
    ]
    let ul = document.createElement('ul')
    let ulClass = document.createAttribute('class')
    ulClass.value = 'fullJumpInfo'
    ul.setAttributeNode(ulClass)
AA2875's avatar
AA2875 committed
    let ulStyle = document.createAttribute('style')
    ulStyle.value = 'display: none;'
    ul.setAttributeNode(ulStyle)    

    specs.forEach((spec, index, specs) => {
        let li = document.createElement('li')
        let jumpSpec = document.createTextNode(spec)
        if (index === specs.length - 1 && spec != '' && spec != undefined) {
            li.appendChild(createHyperlink(spec))
            ul.appendChild(li)
        }
        else if (spec != '') {
AA2875's avatar
AA2875 committed
            li.appendChild(jumpSpec)
            ul.appendChild(li)
        }
AA2875's avatar
AA2875 committed
    ul.appendChild(createDeleteButton(jump))
AA2875's avatar
AA2875 committed
    return ul
}
AA2875's avatar
AA2875 committed

function createHyperlink(link) {
    if (link.slice(0,4) != 'http') {
        link = `https://${link}`
    }
    let a = document.createElement('a')
    let href = document.createAttribute('href')
    href.value = link
    a.setAttributeNode(href)
    let target = document.createAttribute('target')
    target.value = '_blank'
    a.setAttributeNode(target)
    let linkText = document.createTextNode(link)
    a.appendChild(linkText)
    return a
}

AA2875's avatar
AA2875 committed
function createDeleteButton(jump) {
    let deleteDiv = document.createElement('div')
    let delDivClass = document.createAttribute('class')
    delDivClass.value = 'jumpItemDeleteButton'
    deleteDiv.setAttributeNode(delDivClass)
AA2875's avatar
AA2875 committed
    let deleteButton = document.createElement('img')
    deleteButton.src = "../img/trash.png"
AA2875's avatar
AA2875 committed
    deleteDiv.appendChild(deleteButton)
AA2875's avatar
AA2875 committed
    deleteButton.onclick = function() { cancelDelete(jump._id) }
AA2875's avatar
AA2875 committed
    return deleteDiv
}

AA2875's avatar
AA2875 committed
function cancelDelete(id) {
    let jumpItem = document.getElementById(id)
    let jumpIndex = jumpsJSON.findIndex(jump => jump._id === id)
    let jumpNumber = jumpsJSON[jumpIndex].number
    jumpItem.innerHTML = `#${jumpNumber} Poistetaan...`
AA2875's avatar
AA2875 committed
    let cancelButtonSpan = document.createElement('span')
    let cancelButtonSpanClass = document.createAttribute('class')
    cancelButtonSpanClass.value = 'cancelDeleteButton'
    cancelButtonSpan.setAttributeNode(cancelButtonSpanClass)
    let cancelButton = document.createTextNode('Peruuta')
    cancelButtonSpan.appendChild(cancelButton)
    jumpItem.appendChild(cancelButtonSpan)
    
    let cancel = false
    cancelButtonSpan.onclick = () => {
        cancel = true
        document.getElementById('jumpsList').innerHTML = ''
        showJumps(jumpsJSON)
AA2875's avatar
AA2875 committed
    }

    setTimeout(() => {
        if (!cancel) {
AA2875's avatar
AA2875 committed
            removeJump(id)
AA2875's avatar
AA2875 committed
        }
    }, 2500);
}

function showJumps(jumps) {
AA2875's avatar
AA2875 committed
    let jumpsList = document.getElementById('jumpsList')
    let infoText = document.getElementById('infoText')
AA2875's avatar
AA2875 committed
    if (jumps.length === 0) {
AA2875's avatar
AA2875 committed
        infoText.innerHTML = 'Ei hyppyjä'
AA2875's avatar
AA2875 committed
    } else {
        if (jumpOrder == 'desc') {
            jumps.sort((a, b) => a.number - b.number)
        } else {
            jumps.sort((a, b) => b.number - a.number)
        jumps.reverse().forEach(jump => {
AA2875's avatar
AA2875 committed
            let li = createJumpListItem(jump)        
AA2875's avatar
AA2875 committed
            jumpsList.appendChild(li)
        })
AA2875's avatar
AA2875 committed
        infoText.innerHTML = ''
AA2875's avatar
AA2875 committed
    }
}

AA2875's avatar
AA2875 committed
function changeJumpOrder() {
    document.getElementById('jumpsList').innerHTML = ''
AA2875's avatar
AA2875 committed
    let button = document.getElementById('sortJumpsButton')
AA2875's avatar
AA2875 committed
    if (button.innerHTML == 'Järjestys ▼') {
        button.innerHTML = 'Järjestys ▲'
        jumpOrder = 'desc'
        showJumps(jumpsJSON)
AA2875's avatar
AA2875 committed
    } else {
        button.innerHTML = 'Järjestys ▼'
        jumpOrder = 'asc'
        showJumps(jumpsJSON)
AA2875's avatar
AA2875 committed
    }
AA2875's avatar
AA2875 committed
}

AA2875's avatar
AA2875 committed
async function addJump() {
    const keys = ['Number','Date','Place','Plane','Canopy',
                  'Height','Falltime','Comments','Link']
AA2875's avatar
AA2875 committed
    let data = {}

    keys.forEach(key => {
        let variableName = 'newJump' + key
        let element = document.getElementById(variableName)
        let dataKey = key.toLowerCase()
        data[`${dataKey}`] = element.value
        element.value = ''
    })

    try {
        const response = await fetch('http://localhost:3000/jumps', {
            method: 'POST',
            headers: {
            'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        })
        let jump = await response.json()
        let jumpsList = document.getElementById('jumpsList')
        let li = createJumpListItem(jump)
        jumpsList.prepend(li)
    } catch (error) {
        console.log(error)
    }
AA2875's avatar
AA2875 committed
      
AA2875's avatar
AA2875 committed
    let infoText = document.getElementById('infoText')
    infoText.innerHTML = ''
AA2875's avatar
AA2875 committed
    loadJumps()
AA2875's avatar
AA2875 committed
}

AA2875's avatar
AA2875 committed
function editJump(id){
AA2875's avatar
AA2875 committed
    let li = document.getElementById(id)
AA2875's avatar
AA2875 committed
    let number = document.getElementById("newJumpNumber")
AA2875's avatar
AA2875 committed
    let jump_content = li.innerHTML.slice(0, -63)
AA2875's avatar
AA2875 committed
    number.value = jump_content
    let button = document.getElementById("addJumpBtn")
AA2875's avatar
AA2875 committed
    button.style.backgroundColor = "yellow"
    button.innerHTML = "Tallenna"
AA2875's avatar
AA2875 committed
    button.onclick = function() { saveJump(id) }
AA2875's avatar
AA2875 committed
}

AA2875's avatar
AA2875 committed
async function saveJump(id){  // Erheitä esim text newJump.value
AA2875's avatar
AA2875 committed
    let newJump = document.getElementById('newJump')
    const data = { 'text': newJump.value }
AA2875's avatar
AA2875 committed
    const response = await fetch('http://localhost:3000/jumps/'+id, {
        method: 'PUT',
        headers: {
        'Content-Type': 'application/json'
        },
    body: JSON.stringify(data)
    })
    let jump = await response.json()
    document.getElementById(jump._id).firstChild.nodeValue = jump.text
    let addButton = document.getElementById('btn')
    addButton.setAttribute('onclick', "addJump()")
    addButton.innerHTML = "Lissee"
    addButton.style.backgroundColor = ""
    let infoText = document.getElementById('infoText')
    infoText.innerHTML = ''
AA2875's avatar
AA2875 committed
    newJump.value = ''
AA2875's avatar
AA2875 committed
}

AA2875's avatar
AA2875 committed
async function removeJump(id) {
AA2875's avatar
AA2875 committed
    const response = await fetch('http://localhost:3000/jumps/'+id, {
        method: 'DELETE'
    })
    let responseJson = await response.json()
    let li = document.getElementById(id)
    li.parentNode.removeChild(li)
AA2875's avatar
AA2875 committed
    removeLocally(id)
AA2875's avatar
AA2875 committed
  
    let jumpsList = document.getElementById('jumpsList')
    if (!jumpsList.hasChildNodes()) {
        let infoText = document.getElementById('infoText')
AA2875's avatar
AA2875 committed
        infoText.innerHTML = 'Ei hyppyjä'
AA2875's avatar
AA2875 committed
    }
AA2875's avatar
AA2875 committed
}
AA2875's avatar
AA2875 committed

function removeLocally(id) {
    var index = jumpsJSON.findIndex(jump => jump._id === id)
AA2875's avatar
AA2875 committed
    if (index > -1) {
        jumpsJSON.splice(index, 1)
    }
}