From 08a1c25134586e1ed9feec5827f6e8aa8e60c154 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Fri, 15 Apr 2022 16:42:11 +0100 Subject: [PATCH 01/30] Commit whitespace to create diff for blank PR --- R/render.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/render.R b/R/render.R index 5b861a2db..bbc8469db 100644 --- a/R/render.R +++ b/R/render.R @@ -117,6 +117,7 @@ data_template <- function(pkg = ".", depth = 0L) { } # Development settings; tooltip needs to be generated at render time + out$development <- pkg$development out$development$version_tooltip <- version_tooltip(pkg$development$mode) From 69c820b39e1405e5367344164f407b4757371893 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 16 Apr 2022 20:02:24 +0100 Subject: [PATCH 02/30] Add check_page_exists_and_redirect functions to BS3 JS --- inst/BS3/assets/pkgdown.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/inst/BS3/assets/pkgdown.js b/inst/BS3/assets/pkgdown.js index 6f0eee40b..0d5ddf60b 100644 --- a/inst/BS3/assets/pkgdown.js +++ b/inst/BS3/assets/pkgdown.js @@ -76,6 +76,24 @@ element.setAttribute('data-original-title', tooltipOriginalTitle); } + function check_page_exists_and_redirect(event) { + + const path_to_try = event.target.value; + + const base_path = path_to_try.match("(.*\/r\/)?")[0]; + let try_url = path_to_try; + $.ajax({ + type: 'HEAD', + url: try_url, + success: function() { + location.href = try_url; + } + }).fail(function() { + location.href = base_path; + }); + return false; + } + if(ClipboardJS.isSupported()) { $(document).ready(function() { var copyButton = ""; From ab6cafd8b8ccc305ef19638f8113b5ac34e46c78 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 16 Apr 2022 20:39:06 +0100 Subject: [PATCH 03/30] Add temp testing site --- tests/testthat/assets/version-dropdown/DESCRIPTION | 9 +++++++++ tests/testthat/assets/version-dropdown/_pkgdown.yml | 6 ++++++ 2 files changed, 15 insertions(+) create mode 100644 tests/testthat/assets/version-dropdown/DESCRIPTION create mode 100644 tests/testthat/assets/version-dropdown/_pkgdown.yml diff --git a/tests/testthat/assets/version-dropdown/DESCRIPTION b/tests/testthat/assets/version-dropdown/DESCRIPTION new file mode 100644 index 000000000..f7d39ece5 --- /dev/null +++ b/tests/testthat/assets/version-dropdown/DESCRIPTION @@ -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", , "hadley@rstudio.com", role = c("aut", "cre")), + person("RStudio", role = c("cph", "fnd")) + ) +RoxygenNote: 6.0.1 diff --git a/tests/testthat/assets/version-dropdown/_pkgdown.yml b/tests/testthat/assets/version-dropdown/_pkgdown.yml new file mode 100644 index 000000000..b1fef3f82 --- /dev/null +++ b/tests/testthat/assets/version-dropdown/_pkgdown.yml @@ -0,0 +1,6 @@ +version_selector: + base_url: https://arrow.apache.org/docs/ + - ["7.0.0.9000 (dev)", "dev/r"] + - ["7.0.0 (release)", "r"] + - ["6.0.1", "6.0/r"] + - ["5.0.0", "5.0/r"] From 8cae24d15bda42a81dae262460f76ecf88309b62 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 16 Apr 2022 20:40:26 +0100 Subject: [PATCH 04/30] Add in base_url and versions --- tests/testthat/assets/version-dropdown/_pkgdown.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/testthat/assets/version-dropdown/_pkgdown.yml b/tests/testthat/assets/version-dropdown/_pkgdown.yml index b1fef3f82..8a776732a 100644 --- a/tests/testthat/assets/version-dropdown/_pkgdown.yml +++ b/tests/testthat/assets/version-dropdown/_pkgdown.yml @@ -1,6 +1,7 @@ version_selector: base_url: https://arrow.apache.org/docs/ - - ["7.0.0.9000 (dev)", "dev/r"] - - ["7.0.0 (release)", "r"] - - ["6.0.1", "6.0/r"] - - ["5.0.0", "5.0/r"] + versions: + - ["7.0.0.9000 (dev)", "dev/r"] + - ["7.0.0 (release)", "r"] + - ["6.0.1", "6.0/r"] + - ["5.0.0", "5.0/r"] From 57597dff1f8986569fb7ba04d233196cf7490cbb Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 16 Apr 2022 21:12:50 +0100 Subject: [PATCH 05/30] Add the code which copies the version selector code if version selector is configured --- R/init.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/init.R b/R/init.R index bff37086a..154d36497 100644 --- a/R/init.R +++ b/R/init.R @@ -72,6 +72,10 @@ copy_assets <- function(pkg = ".") { copy_asset_dir(pkg, assets) } + if (!is.null(pkg$meta$version_selector)) { + copy_asset_dir(pkg, path_pkgdown(paste0("BS", pkg$bs_version), "version-selector")) + } + # extras copy_asset_dir(pkg, "pkgdown", file_regexp = "^extra") # site assets From 4bbd7e685973edd63471a1f2fb720c12c9e86859 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 16 Apr 2022 21:13:13 +0100 Subject: [PATCH 06/30] Add the version selectot assets in its own directory --- inst/BS3/assets/pkgdown.js | 18 ---- inst/BS3/version-selector/version-selector.js | 86 +++++++++++++++++++ 2 files changed, 86 insertions(+), 18 deletions(-) create mode 100644 inst/BS3/version-selector/version-selector.js diff --git a/inst/BS3/assets/pkgdown.js b/inst/BS3/assets/pkgdown.js index 0d5ddf60b..6f0eee40b 100644 --- a/inst/BS3/assets/pkgdown.js +++ b/inst/BS3/assets/pkgdown.js @@ -76,24 +76,6 @@ element.setAttribute('data-original-title', tooltipOriginalTitle); } - function check_page_exists_and_redirect(event) { - - const path_to_try = event.target.value; - - const base_path = path_to_try.match("(.*\/r\/)?")[0]; - let try_url = path_to_try; - $.ajax({ - type: 'HEAD', - url: try_url, - success: function() { - location.href = try_url; - } - }).fail(function() { - location.href = base_path; - }); - return false; - } - if(ClipboardJS.isSupported()) { $(document).ready(function() { var copyButton = ""; diff --git a/inst/BS3/version-selector/version-selector.js b/inst/BS3/version-selector/version-selector.js new file mode 100644 index 000000000..938f293d5 --- /dev/null +++ b/inst/BS3/version-selector/version-selector.js @@ -0,0 +1,86 @@ +/** + * 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() + } +}); + +/** + * Check if the page to be browsed to exists and if not, redirect to base path + */ +function check_page_exists_and_redirect(event) { + + const path_to_try = event.target.value; + + const base_path = path_to_try.match("(.*\/r\/)?")[0]; + let try_url = path_to_try; + $.ajax({ + type: 'HEAD', + url: try_url, + success: function() { + location.href = try_url; + } + }).fail(function() { + location.href = base_path; + }); + return false; +} + +(function () { + + $(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. + */ + + // 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).*"); + } + + // 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(); + // 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; + + // 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); + }); +}); + + +})(); + From 64dc0bfb3efb3b28acd3721166eab8307f47ce76 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 16 Apr 2022 21:17:14 +0100 Subject: [PATCH 07/30] Remove added whitespace --- R/render.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/render.R b/R/render.R index bbc8469db..5b861a2db 100644 --- a/R/render.R +++ b/R/render.R @@ -117,7 +117,6 @@ data_template <- function(pkg = ".", depth = 0L) { } # Development settings; tooltip needs to be generated at render time - out$development <- pkg$development out$development$version_tooltip <- version_tooltip(pkg$development$mode) From 6df2cefe6625136e0c48cf68594034cdf8d2874f Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 16 Apr 2022 22:31:39 +0100 Subject: [PATCH 08/30] Add versions.json to the site root dir instead of trying to do it with yaml --- inst/BS3/version-selector/version-selector.js | 2 +- .../assets/version-dropdown/_pkgdown.yml | 5 --- .../version-dropdown/pkgdown/versions.json | 34 +++++++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 tests/testthat/assets/version-dropdown/pkgdown/versions.json diff --git a/inst/BS3/version-selector/version-selector.js b/inst/BS3/version-selector/version-selector.js index 938f293d5..9d0290c1c 100644 --- a/inst/BS3/version-selector/version-selector.js +++ b/inst/BS3/version-selector/version-selector.js @@ -50,7 +50,7 @@ function check_page_exists_and_redirect(event) { } // Load JSON file mapping between docs version and R package version - $.getJSON("https://arrow.apache.org/docs/r/versions.json", function( data ) { + $.getJSON("./versions.json", function( data ) { // get the current page's version number: var displayed_version = $('.version').text(); // Create a dropdown selector and add the appropriate attributes diff --git a/tests/testthat/assets/version-dropdown/_pkgdown.yml b/tests/testthat/assets/version-dropdown/_pkgdown.yml index 8a776732a..f1cc028e1 100644 --- a/tests/testthat/assets/version-dropdown/_pkgdown.yml +++ b/tests/testthat/assets/version-dropdown/_pkgdown.yml @@ -1,7 +1,2 @@ version_selector: base_url: https://arrow.apache.org/docs/ - versions: - - ["7.0.0.9000 (dev)", "dev/r"] - - ["7.0.0 (release)", "r"] - - ["6.0.1", "6.0/r"] - - ["5.0.0", "5.0/r"] diff --git a/tests/testthat/assets/version-dropdown/pkgdown/versions.json b/tests/testthat/assets/version-dropdown/pkgdown/versions.json new file mode 100644 index 000000000..677f7903a --- /dev/null +++ b/tests/testthat/assets/version-dropdown/pkgdown/versions.json @@ -0,0 +1,34 @@ +[ + { + "name": "7.0.0.9000 (dev)", + "version": "dev/" + }, + { + "name": "7.0.0 (release)", + "version": "" + }, + { + "name": "6.0.1", + "version": "6.0/" + }, + { + "name": "5.0.0", + "version": "5.0/" + }, + { + "name": "4.0.1", + "version": "4.0/" + }, + { + "name": "3.0.0", + "version": "3.0/" + }, + { + "name": "2.0.0", + "version": "2.0/" + }, + { + "name": "1.0.1", + "version": "1.0/" + } +] From 913aeb23032cba77e0d275edc9c1bbb0fea12833 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 16 Apr 2022 22:40:58 +0100 Subject: [PATCH 09/30] Put full path prefix in version field and ditch $pathStart in favour of window.location.origin --- inst/BS3/version-selector/version-selector.js | 10 ++-------- .../version-dropdown/pkgdown/versions.json | 16 ++++++++-------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/inst/BS3/version-selector/version-selector.js b/inst/BS3/version-selector/version-selector.js index 9d0290c1c..7aedc2872 100644 --- a/inst/BS3/version-selector/version-selector.js +++ b/inst/BS3/version-selector/version-selector.js @@ -37,16 +37,10 @@ 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).*"); + return current_path.match("\/r.*")[0].substr(2); } // Load JSON file mapping between docs version and R package version @@ -67,7 +61,7 @@ function check_page_exists_and_redirect(event) { // 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(); + opt.value = window.location.origin + val.version + $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 diff --git a/tests/testthat/assets/version-dropdown/pkgdown/versions.json b/tests/testthat/assets/version-dropdown/pkgdown/versions.json index 677f7903a..199963eb0 100644 --- a/tests/testthat/assets/version-dropdown/pkgdown/versions.json +++ b/tests/testthat/assets/version-dropdown/pkgdown/versions.json @@ -1,34 +1,34 @@ [ { "name": "7.0.0.9000 (dev)", - "version": "dev/" + "version": "docs/dev/r/" }, { "name": "7.0.0 (release)", - "version": "" + "version": "docs/r/" }, { "name": "6.0.1", - "version": "6.0/" + "version": "docs/6.0/r/" }, { "name": "5.0.0", - "version": "5.0/" + "version": "docs/5.0/r/" }, { "name": "4.0.1", - "version": "4.0/" + "version": "docs/4.0/r/" }, { "name": "3.0.0", - "version": "3.0/" + "version": "docs/3.0/r/" }, { "name": "2.0.0", - "version": "2.0/" + "version": "docs/2.0/r/" }, { "name": "1.0.1", - "version": "1.0/" + "version": "docs/1.0/r/" } ] From 8d8a602370b919cb8387ef7a8b49c19f8a5108b8 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 16 Apr 2022 22:42:41 +0100 Subject: [PATCH 10/30] Rename "version" to "path" --- inst/BS3/version-selector/version-selector.js | 2 +- .../version-dropdown/pkgdown/versions.json | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/inst/BS3/version-selector/version-selector.js b/inst/BS3/version-selector/version-selector.js index 7aedc2872..ecba057c7 100644 --- a/inst/BS3/version-selector/version-selector.js +++ b/inst/BS3/version-selector/version-selector.js @@ -61,7 +61,7 @@ function check_page_exists_and_redirect(event) { // Add a new option to the dropdown selector const opt = document.createElement("option"); // Set the path based on the 'version' field - opt.value = window.location.origin + val.version + $pathEnd(); + opt.value = window.location.origin + val.path + $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 diff --git a/tests/testthat/assets/version-dropdown/pkgdown/versions.json b/tests/testthat/assets/version-dropdown/pkgdown/versions.json index 199963eb0..3e1feb5de 100644 --- a/tests/testthat/assets/version-dropdown/pkgdown/versions.json +++ b/tests/testthat/assets/version-dropdown/pkgdown/versions.json @@ -1,34 +1,34 @@ [ { "name": "7.0.0.9000 (dev)", - "version": "docs/dev/r/" + "path": "docs/dev/r/" }, { "name": "7.0.0 (release)", - "version": "docs/r/" + "path": "docs/r/" }, { "name": "6.0.1", - "version": "docs/6.0/r/" + "path": "docs/6.0/r/" }, { "name": "5.0.0", - "version": "docs/5.0/r/" + "path": "docs/5.0/r/" }, { "name": "4.0.1", - "version": "docs/4.0/r/" + "path": "docs/4.0/r/" }, { "name": "3.0.0", - "version": "docs/3.0/r/" + "path": "docs/3.0/r/" }, { "name": "2.0.0", - "version": "docs/2.0/r/" + "path": "docs/2.0/r/" }, { "name": "1.0.1", - "version": "docs/1.0/r/" + "path": "docs/1.0/r/" } ] From 7c1eb77af5fb3a254ade87ce59106a8b748f7517 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 16 Apr 2022 22:48:40 +0100 Subject: [PATCH 11/30] Ditch the $pathEnd function --- inst/BS3/version-selector/version-selector.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/inst/BS3/version-selector/version-selector.js b/inst/BS3/version-selector/version-selector.js index ecba057c7..435798cd0 100644 --- a/inst/BS3/version-selector/version-selector.js +++ b/inst/BS3/version-selector/version-selector.js @@ -37,12 +37,6 @@ function check_page_exists_and_redirect(event) { * dropdown where you can select the version of the docs to view. */ - // 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.*")[0].substr(2); - } - // Load JSON file mapping between docs version and R package version $.getJSON("./versions.json", function( data ) { // get the current page's version number: @@ -60,8 +54,10 @@ function check_page_exists_and_redirect(event) { $.each( data, function( key, val ) { // Add a new option to the dropdown selector const opt = document.createElement("option"); + // Get the final component of the path + const pathEnd = window.location.pathname.replace(val.path, ""); // Set the path based on the 'version' field - opt.value = window.location.origin + val.path + $pathEnd(); + opt.value = window.location.origin + val.path + 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 From 18ec6cb36d780846c24cbcbcfe56ed52f9102f4a Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 16 Apr 2022 22:54:12 +0100 Subject: [PATCH 12/30] Add "version" field so that we're not doing weird things with partial matching regex --- inst/BS3/version-selector/version-selector.js | 4 ++-- .../version-dropdown/pkgdown/versions.json | 24 ++++++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/inst/BS3/version-selector/version-selector.js b/inst/BS3/version-selector/version-selector.js index 435798cd0..d6e4b619f 100644 --- a/inst/BS3/version-selector/version-selector.js +++ b/inst/BS3/version-selector/version-selector.js @@ -59,9 +59,9 @@ function check_page_exists_and_redirect(event) { // Set the path based on the 'version' field opt.value = window.location.origin + val.path + pathEnd; // Set the currently selected item based on the major and minor version numbers - opt.selected = val.name.match("[0-9.]*")[0] === displayed_version; + opt.selected = val.version === displayed_version; // Set the displayed text based on the 'name' field - opt.text = val.name; + opt.text = val.label; // Add to the selector sel.append(opt); }); diff --git a/tests/testthat/assets/version-dropdown/pkgdown/versions.json b/tests/testthat/assets/version-dropdown/pkgdown/versions.json index 3e1feb5de..f2fac8f9a 100644 --- a/tests/testthat/assets/version-dropdown/pkgdown/versions.json +++ b/tests/testthat/assets/version-dropdown/pkgdown/versions.json @@ -1,34 +1,42 @@ [ { - "name": "7.0.0.9000 (dev)", + "label": "7.0.0.9000 (dev)", + "version": "7.0.0.9000", "path": "docs/dev/r/" }, { - "name": "7.0.0 (release)", + "label": "7.0.0 (release)", + "version": "7.0.0", "path": "docs/r/" }, { - "name": "6.0.1", + "label": "6.0.1", + "version": "6.0.1", "path": "docs/6.0/r/" }, { - "name": "5.0.0", + "label": "5.0.0", + "version": "5.0.0", "path": "docs/5.0/r/" }, { - "name": "4.0.1", + "label": "4.0.1", + "version": "4.0.1", "path": "docs/4.0/r/" }, { - "name": "3.0.0", + "label": "3.0.0", + "version": "3.0.0", "path": "docs/3.0/r/" }, { - "name": "2.0.0", + "label": "2.0.0", + "version": "2.0.0", "path": "docs/2.0/r/" }, { - "name": "1.0.1", + "label": "1.0.1", + "version": "1.0.1", "path": "docs/1.0/r/" } ] From a2241df39e4617a92c0a3eba1dbb79069f8dfed3 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 16 Apr 2022 23:19:18 +0100 Subject: [PATCH 13/30] Update comments to reflect new field names --- inst/BS3/version-selector/version-selector.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inst/BS3/version-selector/version-selector.js b/inst/BS3/version-selector/version-selector.js index d6e4b619f..2c1f187f7 100644 --- a/inst/BS3/version-selector/version-selector.js +++ b/inst/BS3/version-selector/version-selector.js @@ -56,11 +56,11 @@ function check_page_exists_and_redirect(event) { const opt = document.createElement("option"); // Get the final component of the path const pathEnd = window.location.pathname.replace(val.path, ""); - // Set the path based on the 'version' field + // Set the path based on the 'path' field opt.value = window.location.origin + val.path + pathEnd; // 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 'name' field + // Set the displayed text based on the 'label' field opt.text = val.label; // Add to the selector sel.append(opt); From 5b87e207ac3c37e2c341f15f0a59c975ab07df2a Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 16 Apr 2022 23:44:01 +0100 Subject: [PATCH 14/30] Move logic for generating path/fallback path up to the option selector --- inst/BS3/version-selector/version-selector.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/inst/BS3/version-selector/version-selector.js b/inst/BS3/version-selector/version-selector.js index 2c1f187f7..9c9347d62 100644 --- a/inst/BS3/version-selector/version-selector.js +++ b/inst/BS3/version-selector/version-selector.js @@ -12,18 +12,17 @@ $(window).bind("pageshow", function(event) { */ function check_page_exists_and_redirect(event) { - const path_to_try = event.target.value; + const path_to_try = event.target.value[0]; + const fallback_path = event.target.value[1] - const base_path = path_to_try.match("(.*\/r\/)?")[0]; - let try_url = path_to_try; $.ajax({ type: 'HEAD', - url: try_url, + url: path_to_try, success: function() { - location.href = try_url; + location.href = path_to_try; } }).fail(function() { - location.href = base_path; + location.href = fallback_path; }); return false; } @@ -57,7 +56,8 @@ function check_page_exists_and_redirect(event) { // Get the final component of the path const pathEnd = window.location.pathname.replace(val.path, ""); // Set the path based on the 'path' field - opt.value = window.location.origin + val.path + pathEnd; + const base_path = window.location.origin + val.path; + opt.value = [base_path + pathEnd, base_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 From f4f36800cb1ed7715e356e12892338292768b14f Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 16 Apr 2022 23:53:28 +0100 Subject: [PATCH 15/30] Remove the version selector from the yaml and configure based on if it's in the pkgdown dir --- R/init.R | 8 +++++++- tests/testthat/assets/version-dropdown/_pkgdown.yml | 2 -- 2 files changed, 7 insertions(+), 3 deletions(-) delete mode 100644 tests/testthat/assets/version-dropdown/_pkgdown.yml diff --git a/R/init.R b/R/init.R index 154d36497..0ab98150d 100644 --- a/R/init.R +++ b/R/init.R @@ -72,7 +72,7 @@ copy_assets <- function(pkg = ".") { copy_asset_dir(pkg, assets) } - if (!is.null(pkg$meta$version_selector)) { + if (has_versions(pkg)) { copy_asset_dir(pkg, path_pkgdown(paste0("BS", pkg$bs_version), "version-selector")) } @@ -161,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", "versions.json")) +} diff --git a/tests/testthat/assets/version-dropdown/_pkgdown.yml b/tests/testthat/assets/version-dropdown/_pkgdown.yml deleted file mode 100644 index f1cc028e1..000000000 --- a/tests/testthat/assets/version-dropdown/_pkgdown.yml +++ /dev/null @@ -1,2 +0,0 @@ -version_selector: - base_url: https://arrow.apache.org/docs/ From 6e8e5ff34c5ec4966014e30e96da6b77cf41144a Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sun, 17 Apr 2022 00:33:23 +0100 Subject: [PATCH 16/30] Move versions script into correct dir --- .../pkgdown/{ => assets}/versions.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) rename tests/testthat/assets/version-dropdown/pkgdown/{ => assets}/versions.json (70%) diff --git a/tests/testthat/assets/version-dropdown/pkgdown/versions.json b/tests/testthat/assets/version-dropdown/pkgdown/assets/versions.json similarity index 70% rename from tests/testthat/assets/version-dropdown/pkgdown/versions.json rename to tests/testthat/assets/version-dropdown/pkgdown/assets/versions.json index f2fac8f9a..f163b7e43 100644 --- a/tests/testthat/assets/version-dropdown/pkgdown/versions.json +++ b/tests/testthat/assets/version-dropdown/pkgdown/assets/versions.json @@ -2,41 +2,41 @@ { "label": "7.0.0.9000 (dev)", "version": "7.0.0.9000", - "path": "docs/dev/r/" + "path": "/docs/dev/r" }, { "label": "7.0.0 (release)", "version": "7.0.0", - "path": "docs/r/" + "path": "/docs/r" }, { "label": "6.0.1", "version": "6.0.1", - "path": "docs/6.0/r/" + "path": "/docs/6.0/r" }, { "label": "5.0.0", "version": "5.0.0", - "path": "docs/5.0/r/" + "path": "/docs/5.0/r" }, { "label": "4.0.1", "version": "4.0.1", - "path": "docs/4.0/r/" + "path": "/docs/4.0/r" }, { "label": "3.0.0", "version": "3.0.0", - "path": "docs/3.0/r/" + "path": "/docs/3.0/r" }, { "label": "2.0.0", "version": "2.0.0", - "path": "docs/2.0/r/" + "path": "/docs/2.0/r" }, { "label": "1.0.1", "version": "1.0.1", - "path": "docs/1.0/r/" + "path": "/docs/1.0/r" } ] From f11f1d6d99f06293df8c626b5d3165ed7a81b891 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sun, 17 Apr 2022 00:33:35 +0100 Subject: [PATCH 17/30] Look for versions json in correct dir --- R/init.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/init.R b/R/init.R index 0ab98150d..23a0fec67 100644 --- a/R/init.R +++ b/R/init.R @@ -165,5 +165,5 @@ is_non_pkgdown_site <- function(dst_path) { has_versions <- function(pkg = ".") { pkg <- as_pkgdown(pkg) - file.exists(path(pkg$src_path, "pkgdown", "versions.json")) + file.exists(path(pkg$src_path, "pkgdown", "assets", "versions.json")) } From 25ed723a28965b7bbf18c31b94675cc649977a5f Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sun, 17 Apr 2022 00:33:53 +0100 Subject: [PATCH 18/30] Construct URLs in a better way --- inst/BS3/version-selector/version-selector.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/inst/BS3/version-selector/version-selector.js b/inst/BS3/version-selector/version-selector.js index 9c9347d62..18c7cd4ca 100644 --- a/inst/BS3/version-selector/version-selector.js +++ b/inst/BS3/version-selector/version-selector.js @@ -12,8 +12,9 @@ $(window).bind("pageshow", function(event) { */ function check_page_exists_and_redirect(event) { - const path_to_try = event.target.value[0]; - const fallback_path = event.target.value[1] + const urls = event.target.value.split(",") + const path_to_try = urls[0]; + const fallback_path = urls[1] $.ajax({ type: 'HEAD', @@ -37,7 +38,7 @@ function check_page_exists_and_redirect(event) { */ // Load JSON file mapping between docs version and R package version - $.getJSON("./versions.json", function( data ) { + $.getJSON(window.location.origin + "/versions.json", function( data ) { // get the current page's version number: var displayed_version = $('.version').text(); // Create a dropdown selector and add the appropriate attributes @@ -69,8 +70,5 @@ function check_page_exists_and_redirect(event) { // Replace the HTML "version" component with the new selector $("span.version").replaceWith(sel); }); -}); - - + }); })(); - From 6e2fb450d4225b1a64e0ca0f9373dbbf1f38c3e5 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sun, 17 Apr 2022 01:08:12 +0100 Subject: [PATCH 19/30] Update the example versions to not just be like the Arrow ones --- .../pkgdown/assets/versions.json | 44 +++++-------------- 1 file changed, 12 insertions(+), 32 deletions(-) diff --git a/tests/testthat/assets/version-dropdown/pkgdown/assets/versions.json b/tests/testthat/assets/version-dropdown/pkgdown/assets/versions.json index f163b7e43..1ffa6039c 100644 --- a/tests/testthat/assets/version-dropdown/pkgdown/assets/versions.json +++ b/tests/testthat/assets/version-dropdown/pkgdown/assets/versions.json @@ -1,42 +1,22 @@ [ { - "label": "7.0.0.9000 (dev)", - "version": "7.0.0.9000", - "path": "/docs/dev/r" + "label": "the latest, the greatest, it's 1.0.0 (dev)", + "version": "1.0.0", + "path": "" }, { - "label": "7.0.0 (release)", - "version": "7.0.0", - "path": "/docs/r" + "label": "last one was 0.8", + "version": "0.9", + "path": "/0.9" }, { - "label": "6.0.1", - "version": "6.0.1", - "path": "/docs/6.0/r" + "label": "0.8 is old", + "version": "0.8", + "path": "/0.8" }, { - "label": "5.0.0", - "version": "5.0.0", - "path": "/docs/5.0/r" - }, - { - "label": "4.0.1", - "version": "4.0.1", - "path": "/docs/4.0/r" - }, - { - "label": "3.0.0", - "version": "3.0.0", - "path": "/docs/3.0/r" - }, - { - "label": "2.0.0", - "version": "2.0.0", - "path": "/docs/2.0/r" - }, - { - "label": "1.0.1", - "version": "1.0.1", - "path": "/docs/1.0/r" + "label": "0.7 is the oldest tho", + "version": "0.7", + "path": "/0.7" } ] From ca8f2981f699b09a195207321cd0078c915a8101 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sun, 17 Apr 2022 01:08:46 +0100 Subject: [PATCH 20/30] Unindent and lose outer func --- inst/BS3/version-selector/version-selector.js | 75 ++++++++++--------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/inst/BS3/version-selector/version-selector.js b/inst/BS3/version-selector/version-selector.js index 18c7cd4ca..892d99c79 100644 --- a/inst/BS3/version-selector/version-selector.js +++ b/inst/BS3/version-selector/version-selector.js @@ -28,47 +28,48 @@ function check_page_exists_and_redirect(event) { return false; } -(function () { - $(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. - */ +$(document).ready(function () { - // 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(); - // 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; +/** + * This replaces the package version number in the docs with a + * dropdown where you can select the version of the docs to view. + */ - // 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"); - // Get the final component of the path - const pathEnd = window.location.pathname.replace(val.path, ""); - // Set the path based on the 'path' field - const base_path = window.location.origin + val.path; - opt.value = [base_path + pathEnd, base_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); - }); + // 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(); + // 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; - // Replace the HTML "version" component with the new selector - $("span.version").replaceWith(sel); + // 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"); + // Get the final component of the path + const pathEnd = window.location.pathname.replace(val.path, ""); + // Set the path based on the 'path' field + const base_path = window.location.origin + val.path; + opt.value = [base_path + pathEnd, base_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); }); -})(); +}); + + From d84ca88daf28f5607c2fc8b2d4d1a4fcadf059f9 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sun, 17 Apr 2022 02:09:19 +0100 Subject: [PATCH 21/30] Add the version selector to the BS3 template --- R/render.R | 1 + inst/BS3/templates/head.html | 4 ++++ .../assets/version-dropdown/pkgdown/assets/versions.json | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/R/render.R b/R/render.R index 5b861a2db..abbc29505 100644 --- a/R/render.R +++ b/R/render.R @@ -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 diff --git a/inst/BS3/templates/head.html b/inst/BS3/templates/head.html index 540c5531d..5bdcb5835 100644 --- a/inst/BS3/templates/head.html +++ b/inst/BS3/templates/head.html @@ -56,6 +56,10 @@ {{#js}}{{/js}} {{/extra}} +{{#version_selector}} + +{{/version_selector}} + {{#opengraph}} {{#description}} diff --git a/tests/testthat/assets/version-dropdown/pkgdown/assets/versions.json b/tests/testthat/assets/version-dropdown/pkgdown/assets/versions.json index 1ffa6039c..dda6a2d3f 100644 --- a/tests/testthat/assets/version-dropdown/pkgdown/assets/versions.json +++ b/tests/testthat/assets/version-dropdown/pkgdown/assets/versions.json @@ -5,7 +5,7 @@ "path": "" }, { - "label": "last one was 0.8", + "label": "last one - 0.9", "version": "0.9", "path": "/0.9" }, From 2b776a4e785b8b0c2bfd796a8f58aa7c2db84d1e Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sun, 17 Apr 2022 09:29:56 +0100 Subject: [PATCH 22/30] Add version selector import to the BS5 template --- inst/BS5/templates/head.html | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/inst/BS5/templates/head.html b/inst/BS5/templates/head.html index 56d7259cb..dde030991 100644 --- a/inst/BS5/templates/head.html +++ b/inst/BS5/templates/head.html @@ -46,6 +46,10 @@ {{#js}}{{/js}} {{/extra}} +{{#version_selector}} + +{{/version_selector}} + {{#opengraph}} {{#description}} From 042543b2930f579bed7f750b21e7ae216bd960a9 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sun, 17 Apr 2022 10:16:51 +0100 Subject: [PATCH 23/30] Add version span class to BS5 template --- inst/BS5/templates/navbar.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/inst/BS5/templates/navbar.html b/inst/BS5/templates/navbar.html index b3959c708..bab3649f4 100644 --- a/inst/BS5/templates/navbar.html +++ b/inst/BS5/templates/navbar.html @@ -4,7 +4,9 @@ {{#includes}}{{{before_title}}}{{/includes}} {{#site}}{{title}}{{/site}} - {{#package}}{{version}}{{/package}} + + {{#package}}{{version}}{{/package}} + {{#includes}}{{{before_navbar}}}{{/includes}}