@@ -11,78 +11,21 @@ const initPHPSearch = async (language) => {
1111 const MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000 ;
1212 const CACHE_DAYS = 14 ;
1313
14- /**
15- * Converts the structure from search-index.php into an array of objects,
16- * mapping the index entries to their respective types.
17- *
18- * @param {object } index
19- * @returns {Array }
20- */
21- const processIndex = ( index ) => {
22- return Object . entries ( index )
23- . map ( ( [ id , [ name , description , tag ] ] ) => {
24- if ( ! name ) return null ;
25-
26- let type = "General" ;
27- switch ( tag ) {
28- case "phpdoc:varentry" :
29- type = "Variable" ;
30- break ;
31-
32- case "refentry" :
33- type = "Function" ;
34- break ;
35-
36- case "phpdoc:exceptionref" :
37- type = "Exception" ;
38- break ;
39-
40- case "phpdoc:classref" :
41- type = "Class" ;
42- break ;
43-
44- case "set" :
45- case "book" :
46- case "reference" :
47- type = "Extension" ;
48- break ;
49- }
50-
51- return {
52- id,
53- name,
54- description,
55- tag,
56- type,
57- methodName : name . split ( "::" ) . pop ( ) ,
58- } ;
59- } )
60- . filter ( Boolean ) ;
61- } ;
62-
6314 /**
6415 * Looks up the search index cached in localStorage.
6516 *
6617 * @returns {Array|null }
6718 */
6819 const lookupIndexCache = ( ) => {
69- const key = `search -${ language } ` ;
20+ const key = `search2 -${ language } ` ;
7021 const cache = window . localStorage . getItem ( key ) ;
7122
72- if ( ! cache ) {
23+ if ( ( ! cache ) || ( language === 'local' ) ) {
7324 return null ;
7425 }
7526
7627 const { data, time : cachedDate } = JSON . parse ( cache ) ;
7728
78- // Invalidate old search cache format (previously an object)
79- // TODO: Remove this check once the new search index (a single array)
80- // has been in use for a while.
81- if ( ! Array . isArray ( data ) ) {
82- console . log ( "Invalidating old search cache format" ) ;
83- return null ;
84- }
85-
8629 const expireDate = cachedDate + CACHE_DAYS * MILLISECONDS_PER_DAY ;
8730
8831 if ( Date . now ( ) > expireDate ) {
@@ -98,23 +41,27 @@ const initPHPSearch = async (language) => {
9841 * @returns {Promise<Array> } The search index.
9942 */
10043 const fetchIndex = async ( ) => {
101- const key = `search-${ language } ` ;
102- const response = await fetch ( `/js/search-index.php?lang=${ language } ` ) ;
103- const data = await response . json ( ) ;
104- const items = processIndex ( data ) ;
105-
106- try {
107- localStorage . setItem (
108- key ,
109- JSON . stringify ( {
110- data : items ,
111- time : Date . now ( ) ,
112- } ) ,
113- ) ;
114- } catch ( e ) {
115- // Local storage might be full, or other error.
116- // Just continue without caching.
117- console . error ( "Failed to cache search index" , e ) ;
44+ const key = `search2-${ language } ` ;
45+ let items ;
46+ if ( language === 'local' ) {
47+ items = localSearchIndexes ;
48+ } else {
49+ const response = await fetch ( `/js/search-index.php?lang=${ language } ` ) ;
50+ items = await response . json ( ) ;
51+
52+ try {
53+ localStorage . setItem (
54+ key ,
55+ JSON . stringify ( {
56+ data : items ,
57+ time : Date . now ( ) ,
58+ } ) ,
59+ ) ;
60+ } catch ( e ) {
61+ // Local storage might be full, or other error.
62+ // Just continue without caching.
63+ console . error ( "Failed to cache search index" , e ) ;
64+ }
11865 }
11966
12067 return items ;
@@ -139,7 +86,7 @@ const initPHPSearch = async (language) => {
13986 try {
14087 return await loadIndex ( ) ;
14188 } catch ( error ) {
142- if ( language !== "en" ) {
89+ if ( ( language !== "en" ) && ( language !== "local" ) ) {
14390 language = "en" ;
14491 return loadIndexWithFallback ( ) ;
14592 }
@@ -200,7 +147,7 @@ const initSearchModal = () => {
200147 const inputElement = document . getElementById ( "search-modal__input" ) ;
201148
202149 const focusTrapHandler = ( event ) => {
203- if ( event . key != "Tab" ) {
150+ if ( event . key !== "Tab" ) {
204151 return ;
205152 }
206153
@@ -276,19 +223,25 @@ const initSearchModal = () => {
276223 "navbar__search-button-mobile" ,
277224 ) ;
278225 const searchButton = document . getElementById ( "navbar__search-button" ) ;
226+ let buttons = [ searchButton ] ;
279227
280228 // Enhance mobile search
281- searchLink . setAttribute ( "hidden" , "true" ) ;
282- searchButtonMobile . removeAttribute ( "hidden" ) ;
229+ if ( searchLink !== null ) {
230+ searchLink . setAttribute ( "hidden" , "true" ) ;
231+ searchButtonMobile . removeAttribute ( "hidden" ) ;
232+ buttons . push ( searchButtonMobile ) ;
233+ }
283234
284235 // Enhance desktop search
285- document
286- . querySelector ( ".navbar__search-form" )
287- . setAttribute ( "hidden" , "true" ) ;
236+ const searchForm = document
237+ . querySelector ( ".navbar__search-form" ) ;
238+ if ( searchForm !== null ) {
239+ searchForm . setAttribute ( "hidden" , "true" ) ;
240+ }
288241 searchButton . removeAttribute ( "hidden" ) ;
289242
290243 // Open when the search button is clicked
291- [ searchButton , searchButtonMobile ] . forEach ( ( button ) =>
244+ buttons . forEach ( ( button ) =>
292245 button . addEventListener ( "click" , show ) ,
293246 ) ;
294247
@@ -390,7 +343,10 @@ const initSearchUI = ({ searchCallback, language, limit = 30 }) => {
390343 const icon = [ "General" , "Extension" ] . includes ( item . type )
391344 ? DOCUMENT_ICON
392345 : BRACES_ICON ;
393- const link = `/manual/${ encodeURIComponent ( language ) } /${ encodeURIComponent ( item . id ) } .php` ;
346+ let link = `/manual/${ encodeURIComponent ( language ) } /${ encodeURIComponent ( item . id ) } .php` ;
347+ if ( language === 'local' ) {
348+ link = encodeURIComponent ( item . id ) + '.html' ;
349+ }
394350
395351 const description =
396352 item . type !== "General"
@@ -459,7 +415,7 @@ const initSearchUI = ({ searchCallback, language, limit = 30 }) => {
459415 if ( selectedIndex !== - 1 ) {
460416 event . preventDefault ( ) ;
461417 resultsElements [ selectedIndex ] . click ( ) ;
462- } else {
418+ } else if ( language !== 'local' ) {
463419 window . location . href = `/search.php?lang=${ language } &q=${ encodeURIComponent ( inputElement . value ) } ` ;
464420 }
465421 break ;
0 commit comments