diff --git a/ts/category.ts b/ts/category.ts index 93f7a76..0061cd5 100644 --- a/ts/category.ts +++ b/ts/category.ts @@ -1,4 +1,4 @@ -class VideoMeta { +export class VideoMeta { name: string|null thumbnail: string|null category: string @@ -79,26 +79,4 @@ class VideoMeta { } } -function category_ready_handler(e?: Event) : Event { - if(document.readyState != 'complete') { return e } - - let xml = new XMLHttpRequest() - - xml.onload = function() { - if(this.getResponseHeader('Content-Type') == 'application/json') { - let raw = JSON.parse(this.responseText) - for(const cat_raw of raw) { - let cat = new VideoMeta(cat_raw) - cat.as_div() - } - } - } - - const endpoint = window.location.origin + '/api' + window.location.pathname - xml.open('GET', endpoint, true) - xml.send() - - return e -} -document.addEventListener('readystatechange', category_ready_handler) diff --git a/ts/index.ts b/ts/index.ts index 2f63b3d..31794a4 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1,4 +1,4 @@ -class Category { +export class Category { name: string thumbnail: string|null constructor(raw: any) { @@ -70,29 +70,3 @@ class Category { } -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() - - xml.onload = function() { - if(this.getResponseHeader('Content-Type') == 'application/json') { - let raw = JSON.parse(this.responseText) - for(const cat_raw of raw) { - let cat = new Category(cat_raw) - cat.as_div() - } - } - } - - // Fire request - xml.open('GET', endpoint, true) // sync request - xml.send(null) // nothing required for stuff - - return e -} - -document.addEventListener('readystatechange', ready_handler) diff --git a/ts/main.ts b/ts/main.ts new file mode 100644 index 0000000..4933cfe --- /dev/null +++ b/ts/main.ts @@ -0,0 +1,33 @@ +import * as ready from './ready' + +enum PageType { + Index, + Category, + Admin, +} + +function get_page_type(uri: string) : PageType { + if(uri == '/') { + return PageType.Index + } + if (uri.startsWith('/category')) { + return PageType.Category + } + if (uri.startsWith('/admin')) { + return PageType.Admin + } + return PageType.Index +} + +document.addEventListener('readystatechange', (e?: Event) : Event => { + /* + * This dispatch method basically figures out what kind of + * page we're sitting on before delegating control to the + * appropriate place. + */ + switch (get_page_type(document.location.pathname)) { + case PageType.Index: return ready.index_ready_handler(e) + case PageType.Category: return ready.category_ready_handler(e) + case PageType.Admin: return e + } +}) diff --git a/ts/ready.ts b/ts/ready.ts new file mode 100644 index 0000000..7036e28 --- /dev/null +++ b/ts/ready.ts @@ -0,0 +1,60 @@ +/* + * This module defines the hooks required + * to initialize a page's JS environment + * when the document.readystate is changed + * */ +import { Category } from './index' +import { VideoMeta } from './category' + +export function category_ready_handler(e?: Event) : Event { + /* + * On page load: + * This function takes care of fetching the links/thumbnails for that + * category + */ + if(document.readyState != 'complete') { return e } + + let xml = new XMLHttpRequest() + + xml.onload = function() { + if(this.getResponseHeader('Content-Type') == 'application/json') { + let raw = JSON.parse(this.responseText) + for(const cat_raw of raw) { + let cat = new VideoMeta(cat_raw) + cat.as_div() + } + } + } + + const endpoint = window.location.origin + '/api' + window.location.pathname + xml.open('GET', endpoint, true) + xml.send() + + return e +} + +export function index_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() + + xml.onload = function() { + if(this.getResponseHeader('Content-Type') == 'application/json') { + let raw = JSON.parse(this.responseText) + for(const cat_raw of raw) { + let cat = new Category(cat_raw) + cat.as_div() + } + } + } + + // Fire request + xml.open('GET', endpoint, true) // sync request + xml.send(null) // nothing required for stuff + + return e +} +