2020-10-25 17:43:06 +02:00
const loadedScripts = [ ] ;
2020-10-04 23:56:12 +03:00
2020-10-25 18:34:19 +02:00
let selectedButtonId = 'button-home' ;
2020-10-04 23:56:12 +03:00
const changeSelected = ( buttonId ) => {
2020-10-07 19:15:33 +03:00
A ( ` # ${ selectedButtonId } ` ) . removeClass ( 'button-selected' ) ;
2020-10-04 23:56:12 +03:00
A ( ` # ${ buttonId } ` ) . addClass ( 'button-selected' ) ;
2020-10-07 19:15:33 +03:00
selectedButtonId = buttonId ;
2020-10-04 23:56:12 +03:00
} ;
2020-10-25 16:20:23 +02:00
const stripTrailingSlash = ( str ) => {
return str . endsWith ( '/' ) ? str . slice ( 0 , - 1 ) : str ;
} ;
2020-10-25 18:23:46 +02:00
const loadBlogScripts = async ( ) => {
await loadScript ( 'https://cdn.jsdelivr.net/npm/marked/marked.min.js' ) ;
await loadScript ( '/res/blog.js' ) ;
}
2020-10-25 17:43:06 +02:00
const peformRouteAction = async ( pathname ) => {
2020-10-25 16:20:23 +02:00
const route = stripTrailingSlash ( pathname ) ;
switch ( route ) {
2020-10-25 16:26:58 +02:00
case '' : {
2020-10-04 22:40:10 +03:00
A ( '#content-container' ) . load ( '/pages/home.html' ) ;
2020-10-25 18:50:03 +02:00
A ( 'meta[name="description"]' ) . attr ( 'content' , "Hippoz's homepage on hippoz.xyz with a file server, a git server, an about page, a contact page, and a blog." ) ;
2020-10-04 22:40:10 +03:00
break ;
}
2020-10-25 17:43:06 +02:00
case '/blog' : {
A ( 'meta[name="description"]' ) . attr ( 'content' , "hippoz's blog" ) ;
2020-10-25 18:23:46 +02:00
await loadBlogScripts ( ) ;
await A ( '#content-container' ) . load ( '/pages/blog/blog.html' ) ;
blogPopulateArticleList ( 'articlelist' ) ;
2020-10-25 17:43:06 +02:00
break ;
}
2020-10-25 16:20:23 +02:00
case '/upload' : {
2020-10-25 17:43:06 +02:00
A ( 'meta[name="description"]' ) . attr ( 'content' , "hippoz's upload server" ) ;
2020-10-05 00:00:20 +03:00
A ( '#content-container' ) . load ( '/pages/upload.html' ) ;
2020-10-04 22:40:10 +03:00
break ;
}
2020-10-25 16:20:23 +02:00
case '/git' : {
2020-10-04 22:55:27 +03:00
window . location . href = 'https://git.hippoz.xyz/' ;
2020-10-04 22:40:10 +03:00
break ;
}
2020-10-25 16:20:23 +02:00
case '/about' : {
2020-10-25 17:43:06 +02:00
A ( 'meta[name="description"]' ) . attr ( 'content' , "hippoz's about page. i'm hippoz" ) ;
2020-10-04 22:48:57 +03:00
A ( '#content-container' ) . load ( '/pages/about.html' ) ;
break ;
}
2020-10-25 16:20:23 +02:00
case '/contact' : {
2020-10-25 17:43:06 +02:00
A ( 'meta[name="description"]' ) . attr ( 'content' , "you can contact hippoz" ) ;
2020-10-04 22:48:57 +03:00
A ( '#content-container' ) . load ( '/pages/contact.html' ) ;
break ;
}
2020-11-09 19:45:54 +02:00
case '/punjabfreemovie' : {
A ( 'meta[name="description"]' ) . attr ( 'content' , "" ) ;
A ( '#content-container' ) . html ( 'سوف تموت من أجل خطا يا كwinrarfreetrialفيلم أنبوب مثلي ا لجنسfreemovie 240p camel sex.mp4 😳😳😳' ) ;
break ;
}
2020-10-25 16:20:23 +02:00
case '/404' : { }
2020-10-04 23:56:12 +03:00
default : {
2020-10-24 20:04:40 +03:00
A ( '#content-container' ) . html ( '<h2>page not found</h2><p>i think</p>' ) ;
2020-10-25 16:20:23 +02:00
break ;
2020-10-04 23:56:12 +03:00
}
2020-10-04 22:40:10 +03:00
}
} ;
2020-10-25 16:20:23 +02:00
const buttonMap = {
'button-home' : '/' ,
2020-10-25 17:43:06 +02:00
'button-blog' : '/blog' ,
2020-10-25 16:20:23 +02:00
'button-file-server' : '/upload' ,
'button-git-server' : '/git' ,
'button-about-me' : '/about' ,
'button-contact-me' : '/contact' ,
2020-11-09 19:48:45 +02:00
'button-free-movie' : '/punjabfreemovie' ,
2020-10-25 16:20:23 +02:00
'__NOT_FOUND' : '/404'
}
2020-10-25 18:23:46 +02:00
const dynamicRoutesActions = {
'/article' : async ( arg ) => {
await loadBlogScripts ( ) ;
const desc = blogArticles [ arg ] ;
if ( ! desc ) {
peformRouteAction ( '/404' ) ;
return ;
}
2020-10-25 18:34:19 +02:00
changeSelected ( getButtonFromPathname ( '/blog' ) ) ;
2020-10-25 18:23:46 +02:00
blogVisitArticle ( arg , desc ) ;
}
} ;
2020-10-25 16:20:23 +02:00
const onButtonClick = ( buttonId ) => {
2020-10-25 18:34:19 +02:00
// The condition after && is kinda of a hack, the reason I do this is because I want users to be able to click the blog button and return to the article list to continue reading when they are already on an article.
if ( selectedButtonId === buttonId && buttonId !== 'button-blog' ) {
2020-10-25 16:20:23 +02:00
return ;
}
changeSelected ( buttonId ) ;
let route = buttonMap [ buttonId ] ;
if ( ! route ) {
route = buttonMap [ '__NOT_FOUND' ] ;
}
peformRouteAction ( route ) ;
history . pushState ( { route } , '' , route ) ;
} ;
const getButtonFromPathname = ( pathname ) => {
let button = 'button-home' ;
2020-10-25 16:24:26 +02:00
for ( const [ k , v ] of Object . entries ( buttonMap ) ) {
2020-10-25 16:20:23 +02:00
if ( v === pathname ) {
button = k ;
break ;
}
}
return button ;
}
const updateFromCurrentPathname = ( ) => {
let pathname = window . location . pathname ;
if ( ! pathname || pathname === '' || pathname === ' ' ) {
pathname = '/' ;
}
2020-10-25 18:23:46 +02:00
for ( const [ route , action ] of Object . entries ( dynamicRoutesActions ) ) {
if ( pathname . startsWith ( route ) ) {
2020-10-25 18:24:50 +02:00
const arg = stripTrailingSlash ( pathname ) . split ( '/' ) [ 2 ] ;
2020-10-25 18:23:46 +02:00
action ( arg ) ;
break ;
}
}
2020-10-25 16:20:23 +02:00
const buttonId = getButtonFromPathname ( pathname ) ;
changeSelected ( buttonId ) ;
2020-10-25 16:25:02 +02:00
peformRouteAction ( pathname ) ;
2020-10-25 16:20:23 +02:00
}
2020-10-25 17:43:06 +02:00
const loadScript = ( src ) => {
if ( loadedScripts . includes ( src ) ) {
return ;
}
return new Promise ( ( resolve , reject ) => {
const script = document . createElement ( 'script' ) ;
script . onload = ( ) => {
loadedScripts . push ( src ) ;
resolve ( ) ;
} ;
script . onerror = reject ;
script . src = src ;
document . head . appendChild ( script ) ;
} ) ;
}
2020-10-04 22:40:10 +03:00
A ( ( ) => {
A ( '.navigation-buttons .button-default' ) . each ( ( el ) => {
A ( el ) . on ( 'click' , ( ) => {
onButtonClick ( el . id ) ;
} ) ;
2020-10-25 16:23:23 +02:00
} ) ;
updateFromCurrentPathname ( ) ;
2020-10-04 22:40:10 +03:00
} ) ;