Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-15819: [R] R docs version switcher doesn't work on Safari on MacOS #12819

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 26 additions & 4 deletions r/pkgdown/extra.js
Expand Up @@ -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];
Expand Down Expand Up @@ -85,32 +91,48 @@ function check_page_exists_and_redirect(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);
});
});
Expand Down