iwmlib/lib/index.js

68 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

2019-07-18 12:26:39 +02:00
import { Capabilities } from './capabilities.js'
2019-03-21 09:57:27 +01:00
export default class Index {
2019-07-18 12:26:39 +02:00
constructor(template, pages, notfound = 'thumbnails/notfound.png') {
2019-03-21 09:57:27 +01:00
this.template = template
this.pages = pages
this.notfound = notfound
}
setup() {
2019-07-18 12:26:39 +02:00
for (let pair of this.pages) {
2019-03-21 09:57:27 +01:00
let [title, src] = pair
let id = getId()
pair.push(id)
let t = this.template
let wrapper = t.content.querySelector('.wrapper')
wrapper.id = id
let clone = document.importNode(t.content, true)
container.appendChild(clone)
2019-07-18 12:26:39 +02:00
wrapper = container.querySelector('#' + id)
2019-03-21 09:57:27 +01:00
let icon = wrapper.querySelector('.icon')
2019-07-18 12:26:39 +02:00
icon.onerror = e => {
if (this.notfound) icon.src = this.notfound
2019-03-21 09:57:27 +01:00
}
let iconSrc = src.replace('.html', '.png')
//console.log("iconSrc", iconSrc)
if (iconSrc.endsWith('index.png')) {
icon.src = iconSrc.replace('index.png', 'thumbnail.png')
2019-07-18 12:26:39 +02:00
} else {
2019-03-21 09:57:27 +01:00
icon.src = 'thumbnails/' + iconSrc
}
// icon.src = 'thumbnails/' + iconSrc
// console.log(iconSrc)
2019-03-21 09:57:27 +01:00
wrapper.href = src
let titleDiv = wrapper.querySelector('.title')
titleDiv.innerText = title
}
}
frames() {
2019-07-18 12:26:39 +02:00
if (this.pages.length == 0) return
2019-03-21 09:57:27 +01:00
let [title, src, id] = this.pages.shift()
let iframe = document.createElement('iframe')
iframe.frameborder = 0
let wrapper = document.getElementById(id)
let icon = wrapper.querySelector('.icon')
icon.parentNode.replaceChild(iframe, icon)
2019-07-18 12:26:39 +02:00
iframe.onload = e => {
2019-03-21 09:57:27 +01:00
this.frames()
}
iframe.src = src + window.location.search
}
load() {
this.setup()
2019-07-18 12:26:39 +02:00
if (window.location.search.startsWith('?test')) this.frames()
2019-03-21 09:57:27 +01:00
}
loadAndTest() {
this.setup()
2019-07-18 12:26:39 +02:00
if (!Capabilities.isMobile) this.frames()
2019-03-21 09:57:27 +01:00
}
}