window.onload = function () {
    new mdc.topAppBar.MDCTopAppBar(document.querySelector('.mdc-top-app-bar'))
    var menu = new mdc.menu.MDCMenu(document.querySelector('.mdc-menu'))
    menu.setAnchorCorner(mdc.menuSurface.Corner.BOTTOM_LEFT)
    document.getElementById('menu-button').addEventListener('click', function (event) {
        menu.open = !menu.open
    })
    document.querySelectorAll('.mdc-text-field').forEach(function (field) {
        mdc.textField.MDCTextField.attachTo(field)
    })
    document.querySelectorAll('.mdc-icon-button').forEach(function (field) {
        new mdc.iconButton.MDCIconButtonToggle(field)
    })
    if (document.querySelector('.collapsible')) {
        document.querySelectorAll('.collapsible').forEach(function (col) {
            col.addEventListener('click', function () {
                this.classList.toggle('active')
                var content = this.parentNode.parentNode.nextElementSibling.nextElementSibling
                if (content.style.maxHeight) {
                    content.style.maxHeight = null
                } else {
                    content.style.maxHeight = content.scrollHeight + "px"
                }
            })
        })
    }
    if (document.querySelector('.mdc-dialog')) {
        var dialog = new mdc.dialog.MDCDialog(document.querySelector('.mdc-dialog'))
        if (document.querySelector('.mdc-dialog #delete')) {
            document.querySelectorAll('.delete').forEach(function (d) {
                d.addEventListener('click', function (event) {
                    document.body.setAttribute('data-selected', this.getAttribute('data-filename'))
                    dialog.open()
                })
            })
            document.querySelector('.opened').addEventListener('click', function (event) {
                document.body.getAttribute('data-selected') ? window.location.href = '/user/deletefile?file=' + document.body.getAttribute('data-selected') : ''
            })
            document.querySelector('.closed').addEventListener('click', function (event) {
                dialog.close()
            })
        } else if (document.querySelector('.mdc-dialog #error')) {
            dialog.open()
        }
    }
}