clippable/ts/index.ts
shockrah d924c2359a * Fixing broken 'missing thumbnail' default
* Fixing frontend TS to accomodate for new thumbnail uri path's
2021-10-12 22:44:08 -07:00

86 lines
2.1 KiB
TypeScript

class Category {
name: string
thumbnail: string|null
constructor(raw: any) {
this.name = raw['name']
this.thumbnail= raw['thumbnail']
// Setting some defaults
if(!this.name) { this.name = null }
if(!this.thumbnail) { this.thumbnail= null }
}
title_link() : HTMLHeadingElement {
let container = document.createElement('h2')
let link = document.createElement('a')
if(this.name) {
link.href = `/category/${this.name}`
link.text = this.name
} else {
link.href = '#'
link.text = this.name + 'ft. No thumbnail'
}
container.appendChild(link)
return container
}
thumbnail_div() : HTMLImageElement {
let nail = document.createElement('img')
nail.className = 'pure-img'
// Modern browser's should be able to cache this request
if(!(this.thumbnail == null || this.thumbnail.length == 0)) {
nail.setAttribute('src', `${this.thumbnail}`)
} else{
nail.setAttribute('src', '/static/cantfindshit.gif')
}
return nail
}
public as_div() {
let container = document.createElement('div')
container.className = 'video-block'
let title = this.title_link()
let thumbnail = this.thumbnail_div()
container.appendChild(title)
container.appendChild(thumbnail)
// Dump gallery onto the main page
document.getElementById('main-container').appendChild(container)
return container
}
}
function ready_handler(e?: Event) : Event {
// Only let this make a get request once we're ready on the page
if(document.readyState != 'complete') { return e }
// All we do here is basically make a get request to /api/categories
const endpoint = window.location.origin + '/api/categories'
let xml = new XMLHttpRequest()
// TODO: change this to an async request
xml.open('GET', endpoint, false) // sync request
xml.send(null) // nothing required for stuff
if(xml.getResponseHeader('Content-Type') == 'application/json') {
let raw = JSON.parse(xml.responseText)
for(const cat_raw of raw) {
let cat = new Category(cat_raw)
cat.as_div()
}
} else {
console.log(xml.getResponseHeader('Content-Type'))
}
return e
}
document.addEventListener('readystatechange', ready_handler)