11document . addEventListener ( "DOMContentLoaded" , function ( ) {
2- const markedOptions = { gfm : true } ;
3- const loginDialog = document . getElementById ( "loginDialog" ) ;
42 let timeout = 0 ;
53
64 // Modal handling
75 function showModal ( element ) {
8- element . style . display = "block" ;
6+ if ( element ) element . style . display = "block" ;
97 }
108
119 function hideModal ( element ) {
12- element . style . display = "none" ;
10+ if ( element ) element . style . display = "none" ;
1311 }
1412
15- hideModal ( loginDialog ) ;
13+ const loginDialog = document . getElementById ( "loginDialog" ) ;
14+ if ( loginDialog ) hideModal ( loginDialog ) ;
1615
1716 function search ( ) {
18- const formData = new FormData ( document . getElementById ( "fun-query-form" ) ) ;
17+ const form = document . getElementById ( "fun-query-form" ) ;
18+ if ( ! form ) return ;
19+ const formData = new FormData ( form ) ;
1920 formData . append ( "action" , "search" ) ;
2021
2122 fetch ( "ajax.html" , {
@@ -25,9 +26,11 @@ document.addEventListener("DOMContentLoaded", function () {
2526 . then ( ( response ) => response . text ( ) )
2627 . then ( ( data ) => {
2728 const results = document . getElementById ( "results" ) ;
28- results . style . display = "none" ;
29- results . innerHTML = data ;
30- results . style . display = "block" ;
29+ if ( results ) {
30+ results . style . display = "none" ;
31+ results . innerHTML = data ;
32+ results . style . display = "block" ;
33+ }
3134 timeout = null ;
3235 } ) ;
3336 }
@@ -47,59 +50,78 @@ document.addEventListener("DOMContentLoaded", function () {
4750 function reindex ( ) {
4851 const messages = document . getElementById ( "messages" ) ;
4952 const loadIndicator = document . getElementById ( "f-load-indicator" ) ;
50- messages . innerHTML = "" ;
51- loadIndicator . style . display = "block" ;
53+ if ( messages ) messages . innerHTML = "" ;
54+ if ( loadIndicator ) loadIndicator . style . display = "block" ;
5255
5356 fetch ( "modules/reindex.xql" , {
5457 method : "POST" ,
5558 headers : { Accept : "application/json" } ,
5659 } )
5760 . then ( ( response ) => response . json ( ) )
5861 . then ( ( data ) => {
59- loadIndicator . style . display = "none" ;
62+ if ( loadIndicator ) loadIndicator . style . display = "none" ;
6063 if ( data . status === "failed" ) {
61- messages . textContent = data . message ;
64+ if ( messages ) messages . textContent = data . message ;
6265 } else {
6366 window . location . reload ( ) ;
6467 }
6568 } ) ;
6669 }
6770
68- loginDialog . querySelector ( "form" ) . addEventListener ( "submit" , function ( event ) {
69- event . preventDefault ( ) ;
70- const formData = new FormData ( this ) ;
71+ if ( loginDialog ) {
72+ const form = loginDialog . querySelector ( "form" ) ;
73+ if ( form ) {
74+ form . addEventListener ( "submit" , function ( event ) {
75+ event . preventDefault ( ) ;
76+ const formData = new FormData ( this ) ;
7177
72- fetch ( "login" , {
73- method : "POST" ,
74- body : new URLSearchParams ( formData ) ,
75- headers : { Accept : "application/json" } ,
76- } )
77- . then ( ( response ) => {
78- if ( ! response . ok ) throw new Error ( ) ;
79- return response . json ( ) ;
80- } )
81- . then ( ( ) => {
82- hideModal ( loginDialog ) ;
83- reindex ( ) ;
84- } )
85- . catch ( ( ) => {
86- loginDialog . querySelector ( ".login-message" ) . style . display = "block" ;
87- loginDialog . querySelector ( ".login-message" ) . textContent = "Login failed!" ;
78+ fetch ( "login" , {
79+ method : "POST" ,
80+ body : new URLSearchParams ( formData ) ,
81+ headers : { Accept : "application/json" } ,
82+ } )
83+ . then ( ( response ) => {
84+ if ( ! response . ok ) throw new Error ( ) ;
85+ return response . json ( ) ;
86+ } )
87+ . then ( ( ) => {
88+ hideModal ( loginDialog ) ;
89+ reindex ( ) ;
90+ } )
91+ . catch ( ( ) => {
92+ const loginMessage = loginDialog . querySelector ( ".login-message" ) ;
93+ if ( loginMessage ) {
94+ loginMessage . style . display = "block" ;
95+ loginMessage . textContent = "Login failed!" ;
96+ }
97+ } ) ;
8898 } ) ;
89- } ) ;
99+ }
100+ }
90101
91- document . getElementById ( "f-load-indicator" ) . style . display = "none" ;
102+ const loadIndicator = document . getElementById ( "f-load-indicator" ) ;
103+ if ( loadIndicator ) loadIndicator . style . display = "none" ;
92104
93- document . getElementById ( "query-field" ) . addEventListener ( "keyup" , function ( ) {
94- const val = this . value ;
95- if ( val . length > 3 ) {
96- if ( timeout ) clearTimeout ( timeout ) ;
97- timeout = setTimeout ( search , 300 ) ;
98- }
99- } ) ;
105+ const queryField = document . getElementById ( "query-field" ) ;
106+ if ( queryField ) {
107+ queryField . addEventListener ( "keyup" , function ( ) {
108+ const val = this . value ;
109+ if ( val . length > 3 ) {
110+ if ( timeout ) clearTimeout ( timeout ) ;
111+ timeout = setTimeout ( search , 300 ) ;
112+ }
113+ } ) ;
114+ }
100115
101- document . getElementById ( "f-btn-reindex" ) . addEventListener ( "click" , reindexIfLoggedIn ) ;
102- document . getElementById ( "f-btn-reindex-regen" ) . addEventListener ( "click" , reindexIfLoggedIn ) ;
116+ const btnReindex = document . getElementById ( "f-btn-reindex" ) ;
117+ if ( btnReindex ) {
118+ btnReindex . addEventListener ( "click" , reindexIfLoggedIn ) ;
119+ }
120+
121+ const btnReindexRegen = document . getElementById ( "f-btn-reindex-regen" ) ;
122+ if ( btnReindexRegen ) {
123+ btnReindexRegen . addEventListener ( "click" , reindexIfLoggedIn ) ;
124+ }
103125
104126 const tooltips = document . querySelectorAll ( "#fun-query-form [data-toggle='tooltip']" ) ;
105127 tooltips . forEach ( ( tooltip ) => {
0 commit comments