2023-09-01 00:31:21 +01:00
let newLink = "" ;
let newVersion = "" ;
let currentVersion = "" ;
let fileRequested = "" ;
2025-05-06 11:10:16 +01:00
// Get the base URL dynamically
function getBaseUrl ( ) {
2025-05-06 19:19:17 +01:00
const path = window . location . pathname ;
const pathParts = path . split ( '/' ) . filter ( part => part . length > 0 ) ;
2025-05-06 11:10:16 +01:00
2025-05-06 19:19:17 +01:00
// Check if we're in the openshift-docs subdirectory for github pages
if ( pathParts . length > 0 && pathParts [ 0 ] === 'openshift-docs' ) {
return ` ${ window . location . protocol } // ${ window . location . host } /openshift-docs/ ` ;
}
// If not in the openshift-docs subdirectory, use the root
2025-05-06 11:10:16 +01:00
return ` ${ window . location . protocol } // ${ window . location . host } / ` ;
}
const baseUrl = getBaseUrl ( ) ;
// Use relative paths in the mappings
2023-09-01 00:31:21 +01:00
const urlMappings = {
2025-05-06 11:10:16 +01:00
"openshift-acs" : "acs/" ,
"openshift-builds" : "builds/" ,
"openshift-enterprise" : "container-platform/" ,
"openshift-gitops" : "gitops/" ,
"openshift-lightspeed" : "lightspeed/" ,
"openshift-origin" : "" ,
"openshift-pipelines" : "pipelines/" ,
"openshift-serverless" : "serverless/" ,
"openshift-telco" : "container-platform-telco/" ,
"openshift-logging" : "logging/" ,
2023-09-01 00:31:21 +01:00
} ;
2023-05-24 12:46:46 +01:00
2019-05-17 14:05:15 +10:00
function versionSelector ( list ) {
2023-09-01 00:31:21 +01:00
"use strict" ;
2019-05-17 14:05:15 +10:00
newVersion = list [ list . selectedIndex ] . value ;
2024-05-21 22:11:49 +01:00
if ( dk === "openshift-origin" ) {
2024-01-09 15:44:08 +01:00
currentVersion = window . location . pathname . split ( "/" ) [ 1 ] ;
2024-05-21 22:11:49 +01:00
} else {
2025-05-06 22:48:10 +01:00
const path = window . location . pathname ;
const pathParts = path . split ( '/' ) . filter ( part => part . length > 0 ) ;
// Check if we're in the openshift-docs subdirectory for github pages
if ( pathParts . length > 0 && pathParts [ 0 ] === 'openshift-docs' ) {
currentVersion = pathParts [ 2 ] ;
} else {
// If not in the openshift-docs subdirectory
currentVersion = pathParts [ 1 ] ;
}
2024-01-09 15:44:08 +01:00
}
2019-05-17 14:05:15 +10:00
2025-05-06 11:10:16 +01:00
// Get the correct URL path from the urlMappings and prepend the base URL
let productPath = urlMappings [ dk ] ;
let fullBaseUrl = baseUrl + productPath ;
2019-05-17 14:05:15 +10:00
2023-09-01 00:31:21 +01:00
//Handle special OCP case
if ( [ "3.0" , "3.1" , "3.2" ] . includes ( newVersion ) && dk === "openshift-enterprise" ) {
2025-05-06 11:10:16 +01:00
fullBaseUrl = "https://docs.openshift.com/enterprise/" ;
2019-05-17 15:04:58 +10:00
}
2023-11-30 14:07:38 +01:00
if ( ( dk === "openshift-enterprise" || dk === "openshift-origin" ) && currentVersion . charAt ( 0 ) !== newVersion . charAt ( 0 ) ) {
2023-09-01 00:31:21 +01:00
fileRequested = "/welcome/index.html" ;
2019-05-17 14:05:15 +10:00
} else {
2023-09-01 00:31:21 +01:00
const versionIndex = window . location . pathname . lastIndexOf ( currentVersion ) + currentVersion . length ;
fileRequested = window . location . pathname . substring ( versionIndex ) ;
2019-05-17 14:05:15 +10:00
}
2025-05-06 11:10:16 +01:00
newLink = ` ${ fullBaseUrl } ${ newVersion } ${ fileRequested } ` ;
2023-09-01 00:31:21 +01:00
2019-05-17 14:05:15 +10:00
// without doing async loads, there is no way to know if the path actually
// exists - so we will just have to load
2021-12-09 12:49:33 +10:00
// window.location = newLink;
// testing async validations
$ . ajax ( {
type : 'HEAD' ,
url : newLink ,
success : function ( ) {
2023-09-01 00:31:21 +01:00
window . location . href = newLink ;
2021-12-09 12:49:33 +10:00
} ,
error : function ( jqXHR , exception ) {
if ( jqXHR . status == 404 ) {
list . value = currentVersion ;
2023-09-01 00:31:21 +01:00
const confirmMessage = ` This page doesn't exist in version ${ newVersion } . Click OK to search the ${ newVersion } docs OR Cancel to stay on this page. ` ;
if ( confirm ( confirmMessage ) ) {
let searchUrl ;
if ( [ "3.0" , "3.1" , "3.2" ] . includes ( newVersion ) && dk === "openshift-enterprise" ) {
searchUrl = ` https://google.com/search?q=site: ${ baseUrl } ${ newVersion } ${ document . title } ` ;
} else {
searchUrl = ` https://google.com/search?q=site: ${ urlMappings [ dk ] } ${ newVersion } ${ document . title } ` ;
2023-05-24 12:46:46 +01:00
}
2023-09-01 00:31:21 +01:00
window . location . href = searchUrl ;
2021-12-09 12:49:33 +10:00
} else {
// do nothing, user doesn't want to search
}
} else {
2023-09-01 00:31:21 +01:00
window . location . href = newLink ; // assumption here is that we can follow through with a redirect
2021-12-09 12:49:33 +10:00
}
}
} ) ;
2019-05-17 14:05:15 +10:00
}
2021-07-01 16:44:35 +10:00
// checks what language was selected and then sends the user to the portal for their localized version
function selectLang ( langList ) {
var lang = langList [ langList . selectedIndex ] . value ;
var winPath = window . location . pathname ;
console . log ( "Lang: " + lang ) ;
console . log ( "Win Path: " + winPath ) ;
var currentVersion = document . getElementById ( "version-selector" ) . value ;
console . log ( "CurrentVersion: " + currentVersion ) ;
// path for the file to reference on portal (the last bit removes .html)
2021-12-09 12:49:33 +10:00
var path = winPath . substring ( winPath . lastIndexOf ( currentVersion ) + ( currentVersion . length + 1 ) , winPath . length - 5 ) ;
var parts = path . split ( "/" ) ;
console . log ( parts ) ;
// map things to html-single. While plain HTML is preferred, it is harder to map and get all anchors right. html-single ensures there is no 404 and the user at least lands on the right book
console . log ( parts [ parts . length - 1 ] ) ;
var anchorid = parts [ parts . length - 1 ] ;
var book = parts [ 0 ] ;
// add changed book names here
if ( book == "updating" ) book = "updating_clusters" ;
if ( book == "virt" ) book = "openshift_virtualization" ;
if ( book == "post_installation_configuration" ) book = "post-installation_configuration" ;
// var section = parts[1].replace(/\_/g, "-"); // replace underscore with dash
// var section = subGroup.toLowerCase().replace(" ", "-");
// console.log(section);
// var subsection = parts[2].replace(/\_/g, "-");
// console.log(subsection);
// path = book + "/" + section + "#" + subsection;
path = book + "#" + anchorid ;
2021-07-01 16:44:35 +10:00
console . log ( "Path: " + path ) ;
var portalBaseURL = "https://access.redhat.com/documentation" ;
2021-12-09 12:49:33 +10:00
var finalURL = portalBaseURL + "/" + lang + "/openshift_container_platform/" + currentVersion + "/html-single/" + path ;
2021-07-01 16:44:35 +10:00
console . log ( "Final URL: " + finalURL ) ;
window . location . href = finalURL ;
}
2020-06-22 09:01:05 +10:00
// sets the current version in the drop down and sets up suggest an edit options
2019-05-17 14:05:15 +10:00
function selectVersion ( currentVersion ) {
2020-06-22 09:01:05 +10:00
// currentVersion = "3.11"; // for testing
// set the version selector to what the current version is
2019-05-17 14:05:15 +10:00
var el = document . getElementById ( "version-selector" ) ;
if ( el ) {
el . value = currentVersion ;
}
2020-06-22 09:01:05 +10:00
2021-03-24 16:37:00 +10:00
// check the docs referrer to add warning box based on whether we are coming from rosa docs or elsewhere
addReferrer ( ) ;
2020-06-22 09:01:05 +10:00
// the rest creates an suggest an edit element for h1 and h2 elements
// only enabled at the moment on the 3.11 docs
if ( currentVersion != "3.11" ) return ;
var is3 = ( currentVersion . charAt ( 0 ) == 3 ) ;
var is4 = ( currentVersion . charAt ( 0 ) == 4 ) ;
// in version 4 and version 3 books are put together differently. In 3,
// the WYSIWYG (mostly) and there are not many includes. In 4, everything
// (mostly) is an include and the wrapper is just an assembly.
// in version 3, there are generally no modules, and the page you are on, is
// the page you will edit, so the logic is a bit different.
// there is always just one h1 whether you are on version 4 or 3.
// In 4, this is the main assembly, in 3, this is the file to edit.
// in version 4 it assumes that the h2 section's id is correctly named as per the file that it resides in. This is the convention.
// we start with adding suggest an edit to the main assembly/file
var h1s = document . getElementsByTagName ( 'h1' ) ;
var h1 = h1s [ 0 ] ; // there is only one ever
// main file to edit is the file path after the version to the html at
// the end.
2021-12-07 14:58:59 -05:00
// Example: https://docs.openshift.com/container-platform/4.4/updating/updating-cluster-within-minor.html
// file path is updating/updating-cluster-within-minor.adoc
2020-06-22 09:01:05 +10:00
mainFileToEdit =
window . location . pathname . substring (
window . location . pathname . lastIndexOf ( currentVersion ) +
currentVersion . length , window . location . pathname . length - 4 ) ;
// rest api is put together automatically, so ignore
if ( mainFileToEdit . includes ( "rest_api" ) ) return ;
var fn = mainFileToEdit + "adoc" ; // add adoc to file name
var message = "message=[Suggested Edit] for " + fn + "' target='_new' id='" + fn + "' style='font-size: x-small; display: inline; visibility: hidden'>Suggest an edit</a>" ;
// in 4, edit the file in master, so it can cped to the right places. In 3,
// edit in the branch
h1 . innerHTML += " <a href='https://github.com/openshift/openshift-docs/edit/" + ( is4 ? "master" : ( "enterprise-" + currentVersion ) ) + "/" + fn + "?" + message ;
2020-06-22 10:53:01 +10:00
// add mouseover and out to the h1 tag to show or hide the link
// unlike the links added to h2, here it is at [0], the only 'a' tag
// added
h1 . addEventListener ( "mouseover" , function ( ) {
this . getElementsByTagName ( 'a' ) [ 0 ] . style . visibility = "visible" ;
} ) ;
2020-06-22 09:01:05 +10:00
2020-06-22 10:53:01 +10:00
h1 . addEventListener ( "mouseout" , function ( ) {
this . getElementsByTagName ( 'a' ) [ 0 ] . style . visibility = "hidden" ;
} ) ;
2020-06-22 09:01:05 +10:00
if ( is4 ) { // in version 4 also allow to edit subsections which are modules
// all h2 elements map to modules
var h2s = document . getElementsByTagName ( 'h2' ) ;
var i ;
for ( i = 0 ; i < h2s . length ; i ++ ) {
var h2 = h2s [ i ] ;
var splitFile = h2 . id . split ( "_" , 1 ) [ 0 ] ; // file ids are of the format: filename_context
if ( splitFile === h2 . id ) { continue ; } // nothing was found, don't create an edit file link
// this overwrites the global fn for h1 from earlier
var fn = splitFile + ".adoc" ; // add adoc to file name
// we are going to allow the PR to open against the master branch
h2 . innerHTML += " <a href='https://github.com/openshift/openshift-docs/edit/master/modules/" + fn + "?" + "message=[Suggested Edit] for " + fn + "' target='_new' id='" + fn + "' style='font-size: x-small; display: inline; visibility: hidden'>Suggest an edit</a>" ;
// add mouseover and out to the h2 tag to show or hide the link
// in 4, the h2 also has an 'a' tag already, so the tag we are looking for
// here is the second one ([1] and not [0])
h2 . addEventListener ( "mouseover" , function ( ) {
this . getElementsByTagName ( 'a' ) [ 1 ] . style . visibility = "visible" ;
} ) ;
h2 . addEventListener ( "mouseout" , function ( ) {
this . getElementsByTagName ( 'a' ) [ 1 ] . style . visibility = "hidden" ;
} ) ;
}
}
2019-05-17 14:05:15 +10:00
}
2021-03-24 16:37:00 +10:00
function addReferrer ( ) {
// grab target element reference
// we want to add a notice to the top of the OCP docs page if the reader is coming from ROSA docs
// check the referrer
// alert(document.referrer);
// var ref = "http://127.0.0.1/addreferrer";
// var ref = "http://127.0.0.1/addreferrer/authentication/understanding-authentication.html";
var ref = "https://docs.openshift.com/rosa" ;
if ( document . referrer && document . referrer . startsWith ( ref ) && ! document . location . href . startsWith ( ref ) ) {
// get the first section/header
var elements = document . getElementsByClassName ( 'sect1' ) ;
var requiredElement = elements [ 0 ] ;
// the warning text
var text = '<div class="admonitionblock important"><table><tbody><tr><td class="icon"><i class="fa icon-important" title="Important"></i></td><td class="content"><div class="paragraph"><p>This is the <b>OpenShift Container Platform</b> documentation. There may be some sections that don\'t apply to ROSA docs.</p><p>Click <a href="' + document . referrer + '">here</a> to go back to the page you came from or browse the full <a href="https://docs.openshift.com/rosa/welcome/index.html">ROSA documentation</a>.</p></div></td></tr></tbody></table></div>' ;
// insert the element before target element
requiredElement . insertAdjacentHTML ( "beforebegin" , text ) ;
}
2023-09-01 00:31:21 +01:00
}