OSDN Git Service

Correctly handle internal page section references.
[mingw/website.git] / site.js
1 /*
2  * site.js
3  *
4  * General purpose functions for manipulation of page content.
5  *
6  *
7  * $Id$
8  *
9  * Written by Keith Marshall <keith@users.osdn.me>
10  * Copyright (C) 2020, MinGW.org Project
11  *
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice, this permission notice, and the following
21  * disclaimer shall be included in all copies or substantial portions of
22  * the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
27  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF OR OTHER
30  * DEALINGS IN THE SOFTWARE.
31  *
32  */
33 function set_content( item, value )
34 { /* Replace the existing content, if any, of the HTML element with
35    * id attribute named "item", (if such an element exists), with new
36    * content as specified by "value".
37    */
38   var element = document.getElementById( item );
39   if( element ) element.innerHTML = value;
40 }
41
42 function no_break( text )
43 { /* Helper function to replace all occurrences of ASCII hyphen-minus,
44    * within "text", by substitution of HTML non-breaking hyphen.
45    */
46   return text.replace( /-/g, "&#8209;" );
47 }
48
49 function update_page_content_header( tag )
50 { /* Update the "page-title" and "page-subtitle" content-header text,
51    * by substitution into the "as-page-title" and "as-page-subtitle"
52    * place-holder elements, respectively.
53    */
54   var element = document.getElementById( "page-".concat( tag ));
55   if( element )
56   { if( tag == "title" ) document.title = element.innerHTML;
57     set_content( "as-page-".concat( tag ), no_break( element.innerHTML ));
58   }
59 }
60
61 function load_content( container, src )
62 { /* Set the content of the specified HTML "container" element, by
63    * injection of the entire contents of an external file which is
64    * fetched by http server request; (either an http, or an https,
65    * server connection is required; does not work for local files).
66    */
67   var request_handler = new XMLHttpRequest();
68   request_handler.onreadystatechange = function()
69   { if( this.readyState == this.DONE )
70       switch( this.status )
71       { case 200:
72           set_content( container, this.responseText );
73           update_page_content_header( "title" );
74           update_page_content_header( "subtitle" );
75           set_content( "e404-missing-page", document.URL );
76           if( src.includes("#") )
77           { src = src.substring( src.indexOf("#") + 1, src.length );
78             element = document.getElementById( src );
79             if( element ) element.scrollIntoView();
80           }
81           break;
82         case 404:
83           load_content( container, "missing.html" );
84       }
85   }
86   request_handler.open( "GET", src, true );
87   request_handler.send();
88 }
89
90 function load_page( src )
91 { /* Load page content from the HTML fragment file, as determined
92    * from the specified "src" URL; if no alternative fragment name
93    * is specified, fall back to loading "about.html".
94    */
95   const ref = "?page=";
96   const div = "page-content";
97   set_content( div, null );
98   load_content( "header", "header.html" );
99   if( src.includes( ref ) )
100     src = src.substring( src.indexOf( ref ) + ref.length, src.length );
101   else src = "about.html";
102   load_content( div, src );
103 }
104
105 /* $RCSfile$: end of file */