2018-08-13 10:34:00 +00:00
'use strict' ;
var me = { } ;
var phraseBox = { } ;
2019-02-28 07:28:17 +00:00
if ( tmplInits === undefined ) var tmplInits = { } ;
2018-08-13 10:34:00 +00:00
var tmplPhrases = [ ] ; // [key] array of phrases indexed by order of use
2019-03-21 22:59:41 +00:00
var hooks = { // Shorten this list by binding the hooks just in time?
2018-08-13 10:34:00 +00:00
"pre_iffe" : [ ] ,
"pre_init" : [ ] ,
"start_init" : [ ] ,
"end_init" : [ ] ,
2018-11-30 03:02:20 +00:00
"after_phrases" : [ ] ,
2018-09-20 10:45:33 +00:00
"after_add_alert" : [ ] ,
"after_update_alert_list" : [ ] ,
2018-12-27 05:42:41 +00:00
"open_edit" : [ ] ,
"close_edit" : [ ] ,
2019-03-04 07:47:38 +00:00
"edit_item_pre_bind" : [ ] ,
2019-03-21 22:59:41 +00:00
"analytics_loaded" : [ ] ,
2018-08-13 10:34:00 +00:00
} ;
var ranInitHooks = { }
function runHook ( name , ... args ) {
if ( ! ( name in hooks ) ) {
console . log ( "Couldn't find hook '" + name + "'" ) ;
return ;
}
console . log ( "Running hook '" + name + "'" ) ;
let hook = hooks [ name ] ;
for ( const index in hook ) {
hook [ index ] ( ... args ) ;
}
}
function addHook ( name , callback ) {
hooks [ name ] . push ( callback ) ;
}
// 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
2018-11-30 03:02:20 +00:00
function runInitHook ( name , ... args ) {
runHook ( name , ... args ) ;
2018-08-13 10:34:00 +00:00
ranInitHooks [ name ] = true ;
}
function addInitHook ( name , callback ) {
addHook ( name , callback ) ;
if ( name in ranInitHooks ) {
callback ( ) ;
}
}
// Temporary hack for templates
function len ( item ) {
return item . length ;
}
2019-02-28 07:28:17 +00:00
function asyncGetScript ( source ) {
2018-08-13 10:34:00 +00:00
return new Promise ( ( resolve , reject ) => {
let script = document . createElement ( 'script' ) ;
script . async = true ;
2019-02-28 07:28:17 +00:00
const onloadHandler = ( e , isAbort ) => {
2018-08-13 10:34:00 +00:00
if ( isAbort || ! script . readyState || /loaded|complete/ . test ( script . readyState ) ) {
script . onload = null ;
script . onreadystatechange = null ;
script = undefined ;
2019-02-28 07:28:17 +00:00
isAbort ? reject ( e ) : resolve ( ) ;
2018-08-13 10:34:00 +00:00
}
}
2019-02-28 07:28:17 +00:00
script . onerror = ( e ) => {
reject ( e ) ;
2018-08-13 10:34:00 +00:00
} ;
2019-02-28 07:28:17 +00:00
script . onload = onloadHandler ;
script . onreadystatechange = onloadHandler ;
2018-08-13 10:34:00 +00:00
script . src = source ;
const prior = document . getElementsByTagName ( 'script' ) [ 0 ] ;
prior . parentNode . insertBefore ( script , prior ) ;
} ) ;
2019-02-28 07:28:17 +00:00
}
2018-08-13 10:34:00 +00:00
2019-02-28 07:28:17 +00:00
function notifyOnScript ( source ) {
2019-03-16 11:31:10 +00:00
source = "/static/" + source ;
2019-02-28 07:28:17 +00:00
return new Promise ( ( resolve , reject ) => {
2019-03-16 11:31:10 +00:00
let ss = source . replace ( "/static/" , "" ) ;
try {
let ssp = ss . charAt ( 0 ) . toUpperCase ( ) + ss . slice ( 1 )
console . log ( "ssp:" , ssp )
if ( window [ ssp ] ) {
resolve ( ) ;
return ;
}
} catch ( e ) { }
console . log ( "source:" , source )
2019-02-28 07:28:17 +00:00
let script = document . querySelectorAll ( '[src^="' + source + '"]' ) [ 0 ] ;
2019-03-16 11:31:10 +00:00
console . log ( "script:" , script ) ;
2019-02-28 07:28:17 +00:00
if ( script === undefined ) {
reject ( "no script found" ) ;
return ;
}
2019-03-16 11:31:10 +00:00
const onloadHandler = ( e ) => {
script . onload = null ;
script . onreadystatechange = null ;
resolve ( ) ;
2019-02-28 07:28:17 +00:00
}
script . onerror = ( e ) => {
reject ( e ) ;
} ;
script . onload = onloadHandler ;
script . onreadystatechange = onloadHandler ;
} ) ;
}
function notifyOnScriptW ( name , complete , success ) {
notifyOnScript ( name )
. then ( ( ) => {
console . log ( "Loaded " + name + ".js" ) ;
complete ( ) ;
if ( success !== undefined ) success ( ) ;
} ) . catch ( ( e ) => {
console . log ( "Unable to get script name '" + name + "'" ) ;
console . log ( "e: " , e ) ;
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
function loadScript ( name , callback , fail ) {
let fname = name ;
let value = "; " + document . cookie ;
let parts = value . split ( "; current_theme=" ) ;
if ( parts . length == 2 ) fname += "_" + parts . pop ( ) . split ( ";" ) . shift ( ) ;
let url = "/static/" + fname + ".js"
let iurl = "/static/" + name + ".js"
2018-08-13 10:34:00 +00:00
asyncGetScript ( url )
. then ( callback )
2019-02-28 07:28:17 +00:00
. catch ( ( e ) => {
2018-08-13 10:34:00 +00:00
console . log ( "Unable to get script '" + url + "'" ) ;
2019-02-28 07:28:17 +00:00
if ( fname != name ) {
asyncGetScript ( iurl )
. then ( callback )
. catch ( ( e ) => {
console . log ( "Unable to get script '" + iurl + "'" ) ;
console . log ( "e: " , e ) ;
console . trace ( ) ;
} ) ;
}
console . log ( "e: " , e ) ;
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
} ) ;
}
2019-02-10 05:52:26 +00:00
/ *
function loadTmpl ( name , callback ) {
let url = "/static/" + name
let worker = new Worker ( url ) ;
}
* /
2018-08-13 10:34:00 +00:00
function DoNothingButPassBack ( item ) {
return item ;
}
2019-02-10 05:52:26 +00:00
function RelativeTime ( date ) {
return date ;
}
2019-01-21 12:27:59 +00:00
function initPhrases ( ) {
2019-02-28 07:28:17 +00:00
console . log ( "in initPhrases" )
2019-03-16 11:31:10 +00:00
console . log ( "tmlInits:" , tmplInits )
2019-03-03 02:28:17 +00:00
fetchPhrases ( "status,topic_list,alerts,paginator,analytics" )
2019-01-21 12:27:59 +00:00
}
function fetchPhrases ( plist ) {
fetch ( "/api/phrases/?query=" + plist )
2018-08-13 10:34:00 +00:00
. then ( ( resp ) => resp . json ( ) )
. then ( ( data ) => {
console . log ( "loaded phrase endpoint data" ) ;
console . log ( "data:" , data ) ;
Object . keys ( tmplInits ) . forEach ( ( key ) => {
let phrases = [ ] ;
let tmplInit = tmplInits [ key ] ;
2019-01-21 12:27:59 +00:00
for ( let phraseName of tmplInit ) phrases . push ( data [ phraseName ] ) ;
2018-08-13 10:34:00 +00:00
console . log ( "Adding phrases" ) ;
console . log ( "key:" , key ) ;
console . log ( "phrases:" , phrases ) ;
tmplPhrases [ key ] = phrases ;
} ) ;
let prefixes = { } ;
Object . keys ( data ) . forEach ( ( key ) => {
let prefix = key . split ( "." ) [ 0 ] ;
2019-01-21 12:27:59 +00:00
if ( prefixes [ prefix ] === undefined ) prefixes [ prefix ] = { } ;
2018-08-13 10:34:00 +00:00
prefixes [ prefix ] [ key ] = data [ key ] ;
} ) ;
Object . keys ( prefixes ) . forEach ( ( prefix ) => {
console . log ( "adding phrase prefix '" + prefix + "' to box" ) ;
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" ) ;
2019-02-10 05:52:26 +00:00
let toLoad = 2 ;
// TODO: Shunt this into loggedIn if there aren't any search and filter widgets?
2019-03-16 11:31:10 +00:00
notifyOnScriptW ( "template_topics_topic" , ( ) => {
2019-02-10 05:52:26 +00:00
toLoad -- ;
if ( toLoad === 0 ) initPhrases ( ) ;
2019-03-16 12:50:31 +00:00
if ( ! Template _topics _topic ) throw ( "template function not found" ) ;
2019-02-10 05:52:26 +00:00
} ) ;
2019-03-16 11:31:10 +00:00
notifyOnScriptW ( "template_paginator" , ( ) => {
2019-02-10 05:52:26 +00:00
toLoad -- ;
if ( toLoad === 0 ) initPhrases ( ) ;
2019-03-16 12:50:31 +00:00
if ( ! Template _paginator ) throw ( "template function not found" ) ;
2019-02-10 05:52:26 +00:00
} ) ;
2018-08-13 10:34:00 +00:00
let loggedIn = document . head . querySelector ( "[property='x-loggedin']" ) . content ;
2018-12-27 05:42:41 +00:00
if ( loggedIn == "true" ) {
2018-08-13 12:01:27 +00:00
fetch ( "/api/me/" )
2018-08-13 10:34:00 +00:00
. then ( ( resp ) => resp . json ( ) )
. then ( ( data ) => {
console . log ( "loaded me endpoint data" ) ;
console . log ( "data:" , data ) ;
me = data ;
runInitHook ( "pre_init" ) ;
} ) ;
2018-08-13 12:01:27 +00:00
} else {
me = { User : { ID : 0 , Session : "" } , Site : { "MaxRequestSize" : 0 } } ;
runInitHook ( "pre_init" ) ;
2018-08-13 10:34:00 +00:00
}
} ) ( ) ;