2018-11-15 23:53:52 +01:00
/// <reference path="User.ts" />
2018-11-07 22:00:16 +01:00
/// <reference path="Colour.ts" />
/// <reference path="Support.ts" />
2018-11-15 23:53:52 +01:00
/// <reference path="Permissions.ts" />
2019-06-05 16:56:37 +02:00
/// <reference path="Comments.tsx" />
2018-12-09 04:42:38 +01:00
/// <reference path="Common.ts" />
2018-12-11 00:43:50 +01:00
/// <reference path="FormUtilities.ts" />
2018-12-30 23:07:32 +01:00
/// <reference path="UserRelations.ts" />
2019-01-30 10:19:35 +01:00
/// <reference path="Forum/Posting.ts" />
2019-03-01 00:06:28 +01:00
/// <reference path="UrlRegistry.ts" />
2019-04-18 01:59:33 +02:00
/// <reference path="Forum/Polls.ts" />
2018-12-30 23:07:32 +01:00
2018-11-06 23:55:05 +01:00
declare const timeago : any ;
declare const hljs : any ;
2018-11-07 22:00:16 +01:00
let loginFormAvatarTimeout : number = 0 ;
2019-06-05 16:56:37 +02:00
function mszCreateElement ( type : string , properties : { } = { } , children : any [ ] = [ ] ) : HTMLElement {
const element : HTMLElement = document . createElement ( type ) ;
if ( ! Array . isArray ( children ) )
children = [ children ] ;
if ( arguments . length > 3 )
for ( let i = 3 ; i < arguments . length ; i ++ )
children . push ( arguments [ i ] ) ;
if ( properties )
for ( let prop in properties ) {
switch ( typeof properties [ prop ] ) {
case 'function' :
element . addEventListener (
prop . substring ( 0 , 2 ) === 'on'
? prop . substring ( 2 ) . toLowerCase ( )
: prop ,
properties [ prop ]
) ;
break ;
default :
element . setAttribute ( prop , properties [ prop ] ) ;
break ;
}
}
if ( children )
for ( let child in children as [ ] ) {
switch ( typeof children [ child ] ) {
case 'string' :
element . appendChild ( document . createTextNode ( children [ child ] ) ) ;
break ;
default :
if ( children [ child ] instanceof Element )
element . appendChild ( children [ child ] ) ;
break ;
}
}
return element ;
}
2018-11-06 23:55:05 +01:00
// Initialisation process.
window . addEventListener ( 'load' , ( ) = > {
2019-03-01 00:06:28 +01:00
console . log ( "%c __ ____\n / |/ (_)______ ______ __ __\n / /|_/ / / ___/ / / /_ / / / / /\n / / / / (__ ) /_/ / / /_/ /_/ /\n/_/ /_/_/____/\\__,_/ /___/\\__,_/\nhttps://github.com/flashwave/misuzu" , 'color: #8559a5' ) ;
2019-04-10 20:31:31 +02:00
timeago . render ( document . querySelectorAll ( 'time' ) ) ;
2018-11-06 23:55:05 +01:00
hljs . initHighlighting ( ) ;
2018-12-11 00:43:50 +01:00
initCSRF ( ) ;
2019-03-01 00:06:28 +01:00
urlRegistryInit ( ) ;
2018-11-15 23:53:52 +01:00
userInit ( ) ;
2018-12-30 23:07:32 +01:00
userRelationsInit ( ) ;
2018-11-07 22:00:16 +01:00
const changelogChangeAction : HTMLDivElement = document . querySelector ( '.changelog__change__action' ) as HTMLDivElement ;
2019-06-10 19:04:53 +02:00
if ( changelogChangeAction && ! Support . sidewaysText ) {
2018-11-07 22:00:16 +01:00
changelogChangeAction . title = "This is supposed to be sideways, but your browser doesn't support that." ;
}
2018-11-06 23:55:05 +01:00
2018-11-15 23:02:57 +01:00
const loginForms : HTMLCollectionOf < HTMLFormElement > = document . getElementsByClassName ( 'js-login-form' ) as HTMLCollectionOf < HTMLFormElement > ;
2018-11-06 23:55:05 +01:00
2019-06-10 19:04:53 +02:00
if ( loginForms . length > 0 ) {
for ( let i = 0 ; i < loginForms . length ; i ++ ) {
2018-11-15 23:02:57 +01:00
const loginForm : HTMLFormElement = loginForms [ i ] ,
loginAvatar : HTMLElement = loginForm . getElementsByClassName ( 'js-login-avatar' ) [ 0 ] as HTMLElement ,
loginUsername : HTMLInputElement = loginForm . getElementsByClassName ( 'js-login-username' ) [ 0 ] as HTMLInputElement ;
2018-11-07 22:00:16 +01:00
2018-11-15 23:02:57 +01:00
// Initial bump, in case anything is prefilled.
loginFormUpdateAvatar ( loginAvatar , loginUsername , true ) ;
2018-11-07 22:00:16 +01:00
2018-11-15 23:02:57 +01:00
loginUsername . addEventListener ( 'keyup' , ( ) = > loginFormUpdateAvatar ( loginAvatar , loginUsername ) ) ;
}
2018-11-07 22:00:16 +01:00
}
2018-11-15 23:53:52 +01:00
2019-04-10 20:43:33 +02:00
const ctrlSubmit : HTMLCollectionOf < HTMLInputElement > = document . getElementsByClassName ( 'js-ctrl-enter-submit' ) as HTMLCollectionOf < HTMLInputElement > ;
2019-06-10 19:04:53 +02:00
if ( ctrlSubmit . length > 0 ) {
for ( let i = 0 ; i < ctrlSubmit . length ; i ++ ) {
2019-04-10 20:43:33 +02:00
ctrlSubmit [ i ] . addEventListener ( 'keydown' , ev = > {
2019-06-10 19:04:53 +02:00
if ( ev . code === 'Enter' /* i hate this fucking language so much */
2019-04-10 20:43:33 +02:00
&& ev . ctrlKey && ! ev . altKey && ! ev . shiftKey && ! ev . metaKey ) {
2019-06-05 17:28:02 +02:00
// for a hackjob
forumPostingCloseOK = true ;
2019-04-10 20:43:33 +02:00
ctrlSubmit [ i ] . form . submit ( ) ;
ev . preventDefault ( ) ;
}
} ) ;
}
}
2018-11-15 23:53:52 +01:00
commentsInit ( ) ;
2019-01-30 10:19:35 +01:00
forumPostingInit ( ) ;
2019-04-18 01:59:33 +02:00
forumPollsInit ( ) ;
2019-06-13 13:53:27 +02:00
if ( Math . round ( Math . random ( ) * 1000 ) > 900 )
< img src = "about:logo" onError = { ( ) = > {
let ffxPos : number = - 1000 ;
const ffx : HTMLElement = < a href = "https://firefox.com" title = "This PSA is brought to you by the van de Groep & von Schnitzel Alliance For A Better Tomorrow."
style = { ` position: fixed; bottom: ${ ffxPos } px; left: calc(50% - 234px); ` } >
< img src = "/images/ffbxexy.png" alt = "Get Firefox!" / >
< / a > ;
const ffxInterval : number = setInterval ( ( ) = > {
ffx . style . bottom = ( ffxPos ++ ) . toString ( ) + "px" ;
if ( ffxPos >= 10 )
clearInterval ( ffxInterval ) ;
} , 100 ) ;
document . body . append ( ffx ) ;
} } / >
2018-11-06 23:55:05 +01:00
} ) ;
2018-11-07 22:00:16 +01:00
function loginFormUpdateAvatar ( avatarElement : HTMLElement , usernameElement : HTMLInputElement , force : boolean = false ) : void {
2019-06-10 19:04:53 +02:00
if ( ! force ) {
if ( loginFormAvatarTimeout )
2018-11-07 22:00:16 +01:00
return ;
loginFormAvatarTimeout = setTimeout ( ( ) = > {
loginFormUpdateAvatar ( avatarElement , usernameElement , true ) ;
clearTimeout ( loginFormAvatarTimeout ) ;
loginFormAvatarTimeout = 0 ;
} , 750 ) ;
return ;
}
const xhr : XMLHttpRequest = new XMLHttpRequest ;
xhr . addEventListener ( 'readystatechange' , ( ) = > {
2019-06-10 19:04:53 +02:00
if ( xhr . readyState !== 4 )
2018-11-07 22:00:16 +01:00
return ;
2019-03-25 21:11:31 +01:00
avatarElement . style . backgroundImage = "url('{0}')" . replace ( '{0}' , urlFormat ( 'user-avatar' , [
{ name : 'user' , value : xhr.responseText } ,
{ name : 'res' , value : 100 } ,
] ) ) ;
2018-11-07 22:00:16 +01:00
} ) ;
2019-03-08 01:35:53 +01:00
xhr . open ( 'GET' , urlFormat ( 'auth-resolve-user' , [ { name : 'username' , value : encodeURIComponent ( usernameElement . value ) } ] ) ) ;
2018-11-07 22:00:16 +01:00
xhr . send ( ) ;
}
2019-01-22 17:38:20 +01:00
interface MessageBoxButton {
text : string ;
callback : Function ;
}
2019-02-12 17:38:42 +01:00
function messageBox ( text : string , title : string = null , buttons : MessageBoxButton [ ] = [ ] ) : boolean {
2019-06-10 19:04:53 +02:00
if ( document . querySelector ( '.messagebox' ) ) {
2019-01-22 17:38:20 +01:00
return false ;
}
const element = document . createElement ( 'div' ) ;
element . className = 'messagebox' ;
const container = element . appendChild ( document . createElement ( 'div' ) ) ;
container . className = 'container messagebox__container' ;
const titleElement = container . appendChild ( document . createElement ( 'div' ) ) ,
titleBackground = titleElement . appendChild ( document . createElement ( 'div' ) ) ,
titleText = titleElement . appendChild ( document . createElement ( 'div' ) ) ;
titleElement . className = 'container__title' ;
titleBackground . className = 'container__title__background' ;
titleText . className = 'container__title__text' ;
titleText . textContent = title || 'Information' ;
const textElement = container . appendChild ( document . createElement ( 'div' ) ) ;
textElement . className = 'container__content' ;
textElement . textContent = text ;
const buttonsContainer = container . appendChild ( document . createElement ( 'div' ) ) ;
buttonsContainer . className = 'messagebox__buttons' ;
2019-02-12 17:38:42 +01:00
let firstButton = null ;
2019-06-10 19:04:53 +02:00
if ( buttons . length < 1 ) {
2019-02-12 17:38:42 +01:00
firstButton = buttonsContainer . appendChild ( document . createElement ( 'button' ) ) ;
firstButton . className = 'input__button' ;
firstButton . textContent = 'OK' ;
firstButton . addEventListener ( 'click' , ( ) = > element . remove ( ) ) ;
2019-01-22 17:38:20 +01:00
} else {
2019-06-10 19:04:53 +02:00
for ( let i = 0 ; i < buttons . length ; i ++ ) {
2019-01-22 17:38:20 +01:00
let button = buttonsContainer . appendChild ( document . createElement ( 'button' ) ) ;
button . className = 'input__button' ;
button . textContent = buttons [ i ] . text ;
button . addEventListener ( 'click' , ( ) = > {
element . remove ( ) ;
buttons [ i ] . callback ( ) ;
} ) ;
2019-02-12 17:38:42 +01:00
2019-06-10 19:04:53 +02:00
if ( firstButton === null )
2019-02-12 17:38:42 +01:00
firstButton = button ;
2019-01-22 17:38:20 +01:00
}
}
document . body . appendChild ( element ) ;
2019-02-12 17:38:42 +01:00
firstButton . focus ( ) ;
return true ;
}