diff --git a/r/pkgdown/extra.js b/r/pkgdown/extra.js index d9e3b32ee9d06..ec7a9b2a9f75f 100644 --- a/r/pkgdown/extra.js +++ b/r/pkgdown/extra.js @@ -17,6 +17,12 @@ function check_page_exists_and_redirect(event) { + /** + * When a user uses the version dropdown in the docs, check if the page + * they are currently browsing exists in that version of the docs. + * If yes, take them there; if no, take them to the main docs page. + */ + const path_to_try = event.target.value; const base_path = path_to_try.match("(.*\/r\/)?")[0]; @@ -95,32 +101,48 @@ $(window).bind("pageshow", function(event) { * dropdown where you can select the version of the docs to view. */ + // Get the start of the path which includes the version number or "dev" + // where applicable and add the "/docs/" suffix $pathStart = function(){ return window.location.origin + "/docs/"; } + + // Get the end of the path after the version number or "dev" if present $pathEnd = function(){ var current_path = window.location.pathname; - return current_path.match("(?<=\/r).*"); + // Can't do this via a positive look-behind or we lose Safari compatibility + return current_path.match("\/r.*")[0].substr(2); } + // Load JSON file mapping between docs version and R package version $.getJSON("https://arrow.apache.org/docs/r/versions.json", function( data ) { // get the current page's version number: - var displayed_version = $('.version').text(); + var displayed_version = $('.version').text(); + // Create a dropdown selector and add the appropriate attributes const sel = document.createElement("select"); sel.name = "version-selector"; sel.id = "version-selector"; sel.classList.add("navbar-default"); + // When the selected value is changed, take the user to the version + // of the page they are browsing in the selected version sel.onchange = check_page_exists_and_redirect; - $.each( data, function( key, val ) { + // For each of the items in the JSON object (name/version pairs) + $.each( data, function( key, val ) { + // Add a new option to the dropdown selector const opt = document.createElement("option"); + // Set the path based on the 'version' field opt.value = $pathStart() + val.version + "r" + $pathEnd(); + // Set the currently selected item based on the major and minor version numbers opt.selected = val.name.match("[0-9.]*")[0] === displayed_version; + // Set the displayed text based on the 'name' field opt.text = val.name; + // Add to the selector sel.append(opt); - }); + }); + // Replace the HTML "version" component with the new selector $("span.version").replaceWith(sel); }); });