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

Version selector #2072

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
08a1c25
Commit whitespace to create diff for blank PR
thisisnic Apr 15, 2022
69c820b
Add check_page_exists_and_redirect functions to BS3 JS
thisisnic Apr 16, 2022
ab6cafd
Add temp testing site
thisisnic Apr 16, 2022
8cae24d
Add in base_url and versions
thisisnic Apr 16, 2022
57597df
Add the code which copies the version selector code if version select…
thisisnic Apr 16, 2022
4bbd7e6
Add the version selectot assets in its own directory
thisisnic Apr 16, 2022
64dc0bf
Remove added whitespace
thisisnic Apr 16, 2022
6df2cef
Add versions.json to the site root dir instead of trying to do it wit…
thisisnic Apr 16, 2022
913aeb2
Put full path prefix in version field and ditch $pathStart in favour …
thisisnic Apr 16, 2022
8d8a602
Rename "version" to "path"
thisisnic Apr 16, 2022
7c1eb77
Ditch the $pathEnd function
thisisnic Apr 16, 2022
18ec6cb
Add "version" field so that we're not doing weird things with partial…
thisisnic Apr 16, 2022
a2241df
Update comments to reflect new field names
thisisnic Apr 16, 2022
5b87e20
Move logic for generating path/fallback path up to the option selector
thisisnic Apr 16, 2022
f4f3680
Remove the version selector from the yaml and configure based on if i…
thisisnic Apr 16, 2022
6e8e5ff
Move versions script into correct dir
thisisnic Apr 16, 2022
f11f1d6
Look for versions json in correct dir
thisisnic Apr 16, 2022
25ed723
Construct URLs in a better way
thisisnic Apr 16, 2022
6e2fb45
Update the example versions to not just be like the Arrow ones
thisisnic Apr 17, 2022
ca8f298
Unindent and lose outer func
thisisnic Apr 17, 2022
d84ca88
Add the version selector to the BS3 template
thisisnic Apr 17, 2022
2b776a4
Add version selector import to the BS5 template
thisisnic Apr 17, 2022
042543b
Add version span class to BS5 template
thisisnic Apr 17, 2022
eafd581
Move version-selector to own directory not tied to Bootstrap version
thisisnic Apr 17, 2022
46eb70b
Load from updated directory
thisisnic Apr 17, 2022
e5994c4
Add comment
thisisnic Apr 17, 2022
3cb1547
Use bootstrap 5
thisisnic Apr 17, 2022
69a2e53
Use 3 numbered versioning
thisisnic Apr 19, 2022
c02b44d
Use dev/release versions
thisisnic Apr 19, 2022
c454846
Remove redirector func, update examples
thisisnic Apr 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ copy_assets <- function(pkg = ".") {
copy_asset_dir(pkg, assets)
}

if (has_versions(pkg)) {
copy_asset_dir(pkg, path_pkgdown("version-selector"))
}

# extras
copy_asset_dir(pkg, "pkgdown", file_regexp = "^extra")
# site assets
Expand Down Expand Up @@ -157,3 +161,9 @@ is_non_pkgdown_site <- function(dst_path) {

length(top_level) >= 1 && !"pkgdown.yml" %in% path_file(top_level)
}

has_versions <- function(pkg = ".") {
pkg <- as_pkgdown(pkg)

file.exists(path(pkg$src_path, "pkgdown", "assets", "versions.json"))
}
1 change: 1 addition & 0 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ data_template <- function(pkg = ".", depth = 0L) {
css = path_first_existing(pkg$src_path, "pkgdown", "extra.css"),
js = path_first_existing(pkg$src_path, "pkgdown", "extra.js")
)
out$version_selector <- has_versions(pkg)
out$includes <- purrr::pluck(pkg, "meta", "template", "includes", .default = list())
out$yaml <- purrr::pluck(pkg, "meta", "template", "params", .default = list())
# Force inclusion so you can reliably refer to objects inside yaml
Expand Down
4 changes: 4 additions & 0 deletions inst/BS3/templates/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
{{#js}}<script src="{{#site}}{{root}}{{/site}}extra.js"></script>{{/js}}
{{/extra}}

{{#version_selector}}
<script src="{{#site}}{{root}}{{/site}}version-selector.js"></script>
{{/version_selector}}

<meta property="og:title" content="{{{pagetitle}}}" />
{{#opengraph}}
{{#description}}
Expand Down
4 changes: 4 additions & 0 deletions inst/BS5/templates/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
{{#js}}<script src="{{#site}}{{root}}{{/site}}extra.js"></script>{{/js}}
{{/extra}}

{{#version_selector}}
<script src="{{#site}}{{root}}{{/site}}version-selector.js"></script>
{{/version_selector}}

<meta property="og:title" content="{{{pagetitle}}}" />
{{#opengraph}}
{{#description}}
Expand Down
4 changes: 3 additions & 1 deletion inst/BS5/templates/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
{{#includes}}{{{before_title}}}{{/includes}}
<a class="navbar-brand me-2" href="{{#site}}{{root}}{{/site}}index.html">{{#site}}{{title}}{{/site}}</a>

<small class="nav-text {{#development}}text-{{version_label}}{{/development}} me-auto" data-bs-toggle="tooltip" data-bs-placement="bottom" title="{{#development}}{{version_tooltip}}{{/development}}">{{#package}}{{version}}{{/package}}</small>
<span class = "version">
<small class="nav-text {{#development}}text-{{version_label}}{{/development}} me-auto" data-bs-toggle="tooltip" data-bs-placement="bottom" title="{{#development}}{{version_tooltip}}{{/development}}">{{#package}}{{version}}{{/package}}</small>
</span>

{{#includes}}{{{before_navbar}}}{{/includes}}
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="{{#translate}}{{toggle_nav}}{{/translate}}">
Expand Down
51 changes: 51 additions & 0 deletions inst/version-selector/version-selector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* We need to do this so that the version dropdown doesn't used cached HTML
*/
$(window).bind("pageshow", function(event) {
if (event.originalEvent.persisted) {
window.location.reload()
}
});

$(document).ready(function () {

/**
* This replaces the package version number in the docs with a
* dropdown where you can select the version of the docs to view.
*/

// Load JSON file mapping between docs version and R package version
$.getJSON(window.location.origin + "/versions.json", function( data ) {
// get the current page's version number:
var displayed_version = $('.version').text().trim();
// 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 = function(){
if (this.value) window.location.href=this.value;
}

// 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 value to the path and the fallback path
opt.value = window.location.origin + val.path;
// Set the currently selected item based on the major and minor version numbers
opt.selected = val.version === displayed_version;
// Set the displayed text based on the 'label' field
opt.text = val.label;
// Add to the selector
sel.append(opt);
});

// Replace the HTML "version" component with the new selector
$("span.version").replaceWith(sel);
});
});


9 changes: 9 additions & 0 deletions tests/testthat/assets/version-dropdown/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Package: testpackage
Version: 1.0.0
Title: A test package
Description: A longer statement about the package.
Authors@R: c(
person("Hadley", "Wickham", , "[email protected]", role = c("aut", "cre")),
person("RStudio", role = c("cph", "fnd"))
)
RoxygenNote: 6.0.1
32 changes: 32 additions & 0 deletions tests/testthat/assets/version-dropdown/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Start off by building the different versions of the package:

```
devtools::load_all()
# Build dev version of docs
override <- list(development = list(mode = "devel"))
pkg <- as_pkgdown(test_path("assets/version-dropdown/"), override = override)
pkg$version = "1.0.0.9000"
build_site(pkg)

# Build previous versions - release (1.0.0)
pkg <- as_pkgdown(test_path("assets/version-dropdown/"))
build_site(pkg)

# Build previous versions - oldrel (0.9)
override <- list(destination = "docs/0.9")
pkg <- as_pkgdown(test_path("assets/version-dropdown/"), override = override)
pkg$version = "0.9.0"
build_site(pkg)

# Build previous versions - oldest (0.8)
override <- list(destination = "docs/0.8")
pkg <- as_pkgdown(test_path("assets/version-dropdown/"), override = override)
pkg$version = "0.8.1"
build_site(pkg)
```

Navigate to the correct directory and start up a webserver:

```
python -m http.server 8080
```
2 changes: 2 additions & 0 deletions tests/testthat/assets/version-dropdown/_pkgdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
template:
bootstrap: 5
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"label": "1.0.0.9000 (dev)",
"version": "1.0.0.9000",
"path": "/dev"
},
{
"label": "1.0.0 (release)",
"version": "1.0.0",
"path": "/"
},
{
"label": "0.9",
"version": "0.9.0",
"path": "/0.9"
},
{
"label": "0.8 is old",
"version": "0.8.1",
"path": "/0.8"
}
]