2018-08-13 10:34:00 +00:00
'use strict' ;
2020-03-06 03:07:28 +00:00
var me = { } ;
var phraseBox = { } ;
if ( tmplInits === undefined ) var tmplInits = { } ;
var tmplPhrases = [ ] ; // [key] array of phrases indexed by order of use
2020-03-09 21:41:17 +00:00
var hooks = { } ;
2020-03-06 03:07:28 +00:00
var ranInitHooks = { }
2020-07-30 23:40:14 +00:00
var log = console . log ;
var pre = "/s/" ;
2018-08-13 10:34:00 +00:00
2020-03-09 21:41:17 +00:00
function runHook ( name , ... args ) {
2018-08-13 10:34:00 +00:00
if ( ! ( name in hooks ) ) {
2020-07-23 04:02:16 +00:00
log ( "Couldn't find hook " + name ) ;
2018-08-13 10:34:00 +00:00
return ;
}
2020-07-23 04:02:16 +00:00
log ( "Running hook " + name ) ;
2018-08-13 10:34:00 +00:00
let hook = hooks [ name ] ;
2020-07-23 04:02:16 +00:00
let o ;
2020-07-30 23:40:14 +00:00
for ( const index in hook ) o = hook [ index ] ( ... args ) ;
2020-07-23 04:02:16 +00:00
return o ;
2018-08-13 10:34:00 +00:00
}
2020-03-09 21:41:17 +00:00
function addHook ( name , h ) {
2020-07-23 04:02:16 +00:00
log ( "Add hook " + name ) ;
2020-03-09 21:41:17 +00:00
if ( hooks [ name ] === undefined ) hooks [ name ] = [ ] ;
2020-03-06 03:07:28 +00:00
hooks [ name ] . push ( h ) ;
2018-08-13 10:34:00 +00:00
}
// InitHooks are slightly special, as if they are run, then any adds after the initial run will run immediately, this is to deal with the async nature of script loads
2020-03-09 21:41:17 +00:00
function runInitHook ( name , ... args ) {
2020-07-23 04:02:16 +00:00
ranInitHooks [ name ] = true ;
return runHook ( name , ... args ) ;
2018-08-13 10:34:00 +00:00
}
2020-03-09 21:41:17 +00:00
function addInitHook ( name , h ) {
2020-07-23 04:02:16 +00:00
addHook ( name , h ) ;
2020-07-30 23:40:14 +00:00
if ( name in ranInitHooks ) {
log ( "Delay running " + name ) ;
h ( ) ;
}
2018-08-13 10:34:00 +00:00
}
// Temporary hack for templates
2020-07-23 04:02:16 +00:00
function len ( d ) { return d . length }
2018-08-13 10:34:00 +00:00
2020-03-06 03:07:28 +00:00
function asyncGetScript ( src ) {
return new Promise ( ( resolve , reject ) => {
2018-08-13 10:34:00 +00:00
let script = document . createElement ( 'script' ) ;
script . async = true ;
2020-03-06 03:07:28 +00:00
const onloadHandler = ( e , isAbort ) => {
2020-07-23 04:02:16 +00:00
if ( isAbort || ! script . readyState || /loaded|complete/ . test ( script . readyState ) ) {
script . onload = null ;
script . onreadystatechange = null ;
script = undefined ;
2018-08-13 10:34:00 +00:00
2019-02-28 07:28:17 +00:00
isAbort ? reject ( e ) : resolve ( ) ;
2018-08-13 10:34:00 +00:00
}
}
2020-07-23 04:02:16 +00:00
script . onerror = e => {
2019-02-28 07:28:17 +00:00
reject ( e ) ;
2018-08-13 10:34:00 +00:00
} ;
2019-02-28 07:28:17 +00:00
script . onload = onloadHandler ;
script . onreadystatechange = onloadHandler ;
2020-03-06 03:07:28 +00:00
script . src = src ;
2018-08-13 10:34:00 +00:00
const prior = document . getElementsByTagName ( 'script' ) [ 0 ] ;
2020-03-09 21:41:17 +00:00
prior . parentNode . insertBefore ( script , prior ) ;
2018-08-13 10:34:00 +00:00
} ) ;
2019-02-28 07:28:17 +00:00
}
2018-08-13 10:34:00 +00:00
2020-03-06 03:07:28 +00:00
function notifyOnScript ( src ) {
2020-07-30 23:40:14 +00:00
src = pre + src ;
2020-03-09 21:41:17 +00:00
return new Promise ( ( resolve , reject ) => {
2020-07-30 23:40:14 +00:00
let ss = src . replace ( pre , "" ) ;
2019-03-16 11:31:10 +00:00
try {
let ssp = ss . charAt ( 0 ) . toUpperCase ( ) + ss . slice ( 1 )
2020-07-23 04:02:16 +00:00
log ( "ssp" , ssp )
2019-03-16 11:31:10 +00:00
if ( window [ ssp ] ) {
resolve ( ) ;
return ;
}
} catch ( e ) { }
2020-07-23 04:02:16 +00:00
log ( "src" , src )
let script = document . querySelectorAll ( ` [src^=" ${ src } "] ` ) [ 0 ] ;
log ( "script" , script ) ;
2019-02-28 07:28:17 +00:00
if ( script === undefined ) {
reject ( "no script found" ) ;
return ;
}
2020-07-23 04:02:16 +00:00
const onloadHandler = e => {
script . onload = null ;
script . onreadystatechange = null ;
2019-03-16 11:31:10 +00:00
resolve ( ) ;
2019-02-28 07:28:17 +00:00
}
2020-07-23 04:02:16 +00:00
script . onerror = e => {
2019-02-28 07:28:17 +00:00
reject ( e ) ;
} ;
script . onload = onloadHandler ;
script . onreadystatechange = onloadHandler ;
} ) ;
}
2020-03-06 03:07:28 +00:00
function notifyOnScriptW ( name , complete , success ) {
2019-02-28 07:28:17 +00:00
notifyOnScript ( name )
. then ( ( ) => {
2020-07-23 04:02:16 +00:00
log ( ` Loaded ${ name } .js ` ) ;
2019-02-28 07:28:17 +00:00
complete ( ) ;
if ( success !== undefined ) success ( ) ;
2020-03-09 21:41:17 +00:00
} ) . catch ( e => {
2020-07-23 04:02:16 +00:00
log ( "Unable to get " + name , e ) ;
2019-02-28 07:28:17 +00:00
console . trace ( ) ;
2019-03-16 11:31:10 +00:00
complete ( e ) ;
2019-02-28 07:28:17 +00:00
} ) ;
}
// TODO: Send data at load time so we don't have to rely on a fallback template here
2020-07-23 04:02:16 +00:00
function loadScript ( name , h , fail ) {
2019-02-28 07:28:17 +00:00
let fname = name ;
2020-03-13 09:02:28 +00:00
let value = "; " + document . cookie ;
let parts = value . split ( "; current_theme=" ) ;
if ( parts . length == 2 ) fname += "_" + parts . pop ( ) . split ( ";" ) . shift ( ) ;
2019-02-28 07:28:17 +00:00
2020-07-30 23:40:14 +00:00
let url = pre + fname + ".js"
let iurl = pre + name + ".js"
2018-08-13 10:34:00 +00:00
asyncGetScript ( url )
2020-07-23 04:02:16 +00:00
. then ( h ) . catch ( e => {
log ( "Unable to get " + url , e ) ;
2019-02-28 07:28:17 +00:00
if ( fname != name ) {
asyncGetScript ( iurl )
2020-07-23 04:02:16 +00:00
. then ( h ) . catch ( e => {
log ( "Unable to get " + iurl , e ) ;
2019-02-28 07:28:17 +00:00
console . trace ( ) ;
} ) ;
}
2018-08-13 10:34:00 +00:00
console . trace ( ) ;
2019-02-28 07:28:17 +00:00
fail ( e ) ;
2018-08-13 10:34:00 +00:00
} ) ;
}
2020-03-23 01:18:10 +00:00
function RelativeTime ( date ) { return date }
2019-02-10 05:52:26 +00:00
2020-03-23 01:37:35 +00:00
function initPhrases ( member , acp = false ) {
2020-07-23 04:02:16 +00:00
log ( "initPhrases" )
log ( "tmlInits" , tmplInits )
2019-04-15 01:54:13 +00:00
let e = "" ;
2020-03-23 01:37:35 +00:00
if ( member && ! acp ) e = ",status,topic_list,topic" ;
else if ( acp ) e = ",analytics,panel" ; // TODO: Request phrases for just one section of the acp?
else e = ",status,topic_list" ;
2019-04-28 02:57:25 +00:00
fetchPhrases ( "alerts,paginator" + e ) // TODO: Break this up?
2019-01-21 12:27:59 +00:00
}
function fetchPhrases ( plist ) {
2020-03-23 01:18:10 +00:00
fetch ( "/api/phrases/?q=" + plist , { cache : "no-cache" } )
2020-07-23 04:02:16 +00:00
. then ( r => r . json ( ) )
. then ( d => {
log ( "loaded phrase endpoint data" , d ) ;
2020-03-09 21:41:17 +00:00
Object . keys ( tmplInits ) . forEach ( key => {
2020-03-13 09:02:28 +00:00
let phrases = [ ] ;
2018-08-13 10:34:00 +00:00
let tmplInit = tmplInits [ key ] ;
2020-07-23 04:02:16 +00:00
for ( let phraseName of tmplInit ) phrases . push ( d [ phraseName ] ) ;
log ( "Adding phrases for " + key , phrases ) ;
2018-08-13 10:34:00 +00:00
tmplPhrases [ key ] = phrases ;
} ) ;
2020-07-23 04:02:16 +00:00
let prefixes = { } ;
Object . keys ( d ) . forEach ( key => {
2018-08-13 10:34:00 +00:00
let prefix = key . split ( "." ) [ 0 ] ;
2020-07-23 04:02:16 +00:00
if ( prefixes [ prefix ] === undefined ) prefixes [ prefix ] = { } ;
prefixes [ prefix ] [ key ] = d [ key ] ;
2018-08-13 10:34:00 +00:00
} ) ;
2020-03-09 21:41:17 +00:00
Object . keys ( prefixes ) . forEach ( prefix => {
2020-07-23 04:02:16 +00:00
log ( ` adding phrase prefix ${ prefix } to box ` ) ;
2018-08-13 10:34:00 +00:00
phraseBox [ prefix ] = prefixes [ prefix ] ;
} ) ;
2018-11-30 03:02:20 +00:00
runInitHook ( "after_phrases" ) ;
2018-08-13 10:34:00 +00:00
} ) ;
}
( ( ) => {
runInitHook ( "pre_iife" ) ;
2020-03-23 01:18:10 +00:00
let member = document . head . querySelector ( "[property='x-mem']" ) != null ;
let acp = window . location . pathname . startsWith ( "/panel/" ) ;
2019-04-15 01:54:13 +00:00
2019-04-27 06:32:26 +00:00
let toLoad = 1 ;
2020-03-23 01:18:10 +00:00
// TODO: Shunt this into member if there aren't any search and filter widgets?
2020-07-23 04:02:16 +00:00
let q = f => {
2019-04-27 06:32:26 +00:00
toLoad -- ;
2020-03-23 01:18:10 +00:00
if ( toLoad === 0 ) initPhrases ( member , acp ) ;
if ( f ) throw ( "tmpl func not found" ) ;
2019-04-27 06:32:26 +00:00
} ;
2020-07-23 04:02:16 +00:00
let l = ( n , h ) => notifyOnScriptW ( "tmpl_" + n , h ) ;
2019-04-27 06:32:26 +00:00
2020-03-23 01:18:10 +00:00
if ( ! acp ) {
2019-04-27 06:32:26 +00:00
toLoad += 2 ;
2020-03-23 01:18:10 +00:00
if ( member ) {
2019-06-16 07:26:31 +00:00
toLoad += 3 ;
2020-07-23 04:02:16 +00:00
l ( "topic_c_edit_post" , ( ) => q ( ! Tmpl _topic _c _edit _post ) ) ;
l ( "topic_c_attach_item" , ( ) => q ( ! Tmpl _topic _c _attach _item ) ) ;
l ( "topic_c_poll_input" , ( ) => q ( ! Tmpl _topic _c _poll _input ) ) ;
2019-04-20 02:22:57 +00:00
}
2020-07-23 04:02:16 +00:00
l ( "topics_topic" , ( ) => q ( ! Tmpl _topics _topic ) ) ;
l ( "paginator" , ( ) => q ( ! Tmpl _paginator ) ) ;
2019-04-15 01:54:13 +00:00
}
2020-07-23 04:02:16 +00:00
l ( "notice" , ( ) => q ( ! Tmpl _notice ) ) ;
2019-02-10 05:52:26 +00:00
2020-03-23 01:18:10 +00:00
if ( member ) {
2018-08-13 12:01:27 +00:00
fetch ( "/api/me/" )
2020-07-23 04:02:16 +00:00
. then ( r => r . json ( ) )
. then ( d => {
log ( "me" , d ) ;
me = d ;
2020-07-30 23:40:14 +00:00
pre = d . StaticPrefix ;
2018-08-13 10:34:00 +00:00
runInitHook ( "pre_init" ) ;
} ) ;
2018-08-13 12:01:27 +00:00
} else {
2020-07-30 23:40:14 +00:00
me = { User : { ID : 0 , S : "" } , Site : { "MaxReqSize" : 0 } } ;
2018-08-13 12:01:27 +00:00
runInitHook ( "pre_init" ) ;
2018-08-13 10:34:00 +00:00
}
2020-03-06 03:07:28 +00:00
} ) ( )