const tingle = require('tingle.js/src/tingle.js')

class MeshViewer {
    constructor(w) {
        this.window = w
    }

    init() {
        const openers = this.window.document.querySelectorAll('*[data-modal]')

        for (let i = 0, len = openers.length; i < len; i++) {
            openers[i].addEventListener('click', (e) => {
                e.preventDefault()

                let target = e.target

                if (target.tagName != 'A') {
                    target = target.parentNode
                }

                const modal = new tingle.modal({
                    footer: false,
                    stickyFooter: false,
                    closeMethods: ['overlay', 'button', 'escape'],
                    closeLabel: 'Close',
                    cssClass: ['tingle-modal-box--mesh']
                })

                modal.setContent('<iframe src="' + target.getAttribute('href') + '"></iframe>')
                modal.open()
            })
        }
    }
}

module.exports = MeshViewer
