From 46d6f4ed85a2f0e6f42c564f61ce0a9397f4af98 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Mon, 17 Jan 2022 18:30:51 +0000 Subject: [PATCH 01/13] Add JS to swap version for dropdown selector --- r/pkgdown/assets/versions.json | 30 ++++++++++++++++++++ r/pkgdown/extra.js | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 r/pkgdown/assets/versions.json diff --git a/r/pkgdown/assets/versions.json b/r/pkgdown/assets/versions.json new file mode 100644 index 00000000000..a53bbee5146 --- /dev/null +++ b/r/pkgdown/assets/versions.json @@ -0,0 +1,30 @@ +[ + { + "name": "7.0 (dev)", + "version": "dev" + }, + { + "name": "6.0 (stable)", + "version": "" + }, + { + "name": "5.0", + "version": "5.0/" + }, + { + "name": "4.0", + "version": "4.0/" + }, + { + "name": "3.0", + "version": "3.0/" + }, + { + "name": "2.0", + "version": "2.0/" + }, + { + "name": "1.0", + "version": "1.0/" + } +] diff --git a/r/pkgdown/extra.js b/r/pkgdown/extra.js index aca15c56625..6fa23ec887a 100644 --- a/r/pkgdown/extra.js +++ b/r/pkgdown/extra.js @@ -59,6 +59,57 @@ var empty_ul = $("#toc").find("ul").filter(":empty"); empty_ul.remove(); }); + + $(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. + */ + + $pathStart = function(){ + return window.location.origin + "/docs/"; + } + + $pathEnd = function(){ + var current_path = window.location.pathname; + return current_path.match("(?<=\/r).*"); + } + + /** + * Load the versions JSON and construct the select items + */ + $.getJSON("./versions.json", function( data ) { + + var items = []; + // get the current page's version number: + var current_version = $('.version').text(); + $.each( data, function( key, val ) { + + //get the two item version number + var version_array = current_version.split("."); + var version_major_minor = version_array[0] + "." + version_array[1]; + + // need an extra slash if it's the dev docs + var dev_path_string = (val.version == "dev" ? "/" : ""); + + var selected_string = (val.name.match("[0-9.*]\.[0-9.*]") == version_major_minor ? "selected" : ""); + + items.push( + "" + ); + }); + + // Replace the version button with a selector with the doc versions + $("span.version").replaceWith( + ' ' + ); + }); + }); + }; document.head.appendChild(script); From df3f7139935cc458eb77c12e1ee506a8b5ebbb21 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Mon, 17 Jan 2022 18:37:36 +0000 Subject: [PATCH 02/13] Update version numbers to match R package names --- r/pkgdown/assets/versions.json | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/r/pkgdown/assets/versions.json b/r/pkgdown/assets/versions.json index a53bbee5146..c3a1b7ce0c2 100644 --- a/r/pkgdown/assets/versions.json +++ b/r/pkgdown/assets/versions.json @@ -1,30 +1,30 @@ [ { - "name": "7.0 (dev)", + "name": "6.0.1.9000 (dev)", "version": "dev" }, { - "name": "6.0 (stable)", + "name": "6.0.1 (release)", "version": "" }, { - "name": "5.0", + "name": "5.0.0", "version": "5.0/" }, { - "name": "4.0", + "name": "4.0.1", "version": "4.0/" }, { - "name": "3.0", + "name": "3.0.0", "version": "3.0/" }, { - "name": "2.0", + "name": "2.0.0", "version": "2.0/" }, { - "name": "1.0", + "name": "1.0.1", "version": "1.0/" } ] From 168d5e25b5a149505134ff3cb631283d81cf31db Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Mon, 17 Jan 2022 19:35:44 +0000 Subject: [PATCH 03/13] Rename a few vars --- r/pkgdown/extra.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/r/pkgdown/extra.js b/r/pkgdown/extra.js index 6fa23ec887a..613ccc06626 100644 --- a/r/pkgdown/extra.js +++ b/r/pkgdown/extra.js @@ -60,7 +60,7 @@ empty_ul.remove(); }); - $(document).ready(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. @@ -75,24 +75,23 @@ return current_path.match("(?<=\/r).*"); } - /** - * Load the versions JSON and construct the select items - */ + // Load the versions JSON and construct the select items + // You can test with $pathStart() + "/_static/versions.json" $.getJSON("./versions.json", function( data ) { var items = []; // get the current page's version number: - var current_version = $('.version').text(); + var displayed_version = $('.version').text(); $.each( data, function( key, val ) { - //get the two item version number - var version_array = current_version.split("."); - var version_major_minor = version_array[0] + "." + version_array[1]; - // need an extra slash if it's the dev docs var dev_path_string = (val.version == "dev" ? "/" : ""); - var selected_string = (val.name.match("[0-9.*]\.[0-9.*]") == version_major_minor ? "selected" : ""); + var selected_string = ( + val.name.match("[0-9.]*")[0] === displayed_version ? + "selected" : + "" + ); items.push( "" - ); - }); - - // Replace the version button with a selector with the doc versions - $("span.version").replaceWith( - ' ' - ); - }); - }); - - }; - - document.head.appendChild(script); + var items = []; + // get the current page's version number: + var displayed_version = $('.version').text(); + $.each( data, function( key, val ) { + + // need an extra slash if it's the dev docs + var dev_path_string = (val.version == "dev" ? "/" : ""); + + var selected_string = ( + val.name.match("[0-9.]*")[0] === displayed_version ? + "selected" : + "" + ); + + items.push( + "" + ); + }); + + // Replace the version button with a selector with the doc versions + $("span.version").replaceWith( + ' ' + ); + }); + }); + + }; + + document.head.appendChild(script); })(); From 47625bcd7cf8a04eb5d834369d8c23d614f0e511 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Mon, 17 Jan 2022 21:10:56 +0000 Subject: [PATCH 05/13] Add slash to dev --- r/pkgdown/assets/versions.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/r/pkgdown/assets/versions.json b/r/pkgdown/assets/versions.json index c3a1b7ce0c2..220fe8401dc 100644 --- a/r/pkgdown/assets/versions.json +++ b/r/pkgdown/assets/versions.json @@ -1,7 +1,7 @@ [ { "name": "6.0.1.9000 (dev)", - "version": "dev" + "version": "dev/" }, { "name": "6.0.1 (release)", From 1109305e6b2470cd9d3593f75fb4066c5c4e4466 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Mon, 17 Jan 2022 21:33:05 +0000 Subject: [PATCH 06/13] Simplify item construction so can test URLs --- r/pkgdown/extra.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/r/pkgdown/extra.js b/r/pkgdown/extra.js index 2f7c7e90e9b..915aeba5fd5 100644 --- a/r/pkgdown/extra.js +++ b/r/pkgdown/extra.js @@ -76,29 +76,25 @@ } // Load the versions JSON and construct the select items - // You can test with $pathStart() + "/_static/versions.json" - $.getJSON("./versions.json", function( data ) { + // Delete below line before merging + // You can test with https://raw.githubusercontent.com/thisisnic/arrow/ARROW-14338_pkgdown_versioning/r/pkgdown/assets/versions.json + //$.getJSON("./versions.json", function( data ) { + $.getJSON("https://raw.githubusercontent.com/thisisnic/arrow/ec8c60d97eb489f0c19297e8d9b3f48e44db5afb/r/pkgdown/assets/versions.json", function( data ) { var items = []; // get the current page's version number: var displayed_version = $('.version').text(); $.each( data, function( key, val ) { - // need an extra slash if it's the dev docs - var dev_path_string = (val.version == "dev" ? "/" : ""); - var selected_string = ( val.name.match("[0-9.]*")[0] === displayed_version ? "selected" : "" ); - items.push( - "" - ); + var item_path = $pathStart() + val.version + "r" + $pathEnd(); + + items.push(""); }); // Replace the version button with a selector with the doc versions From 6f5f197d4793b405ef1d81b6bde9e23149ecedbd Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Tue, 18 Jan 2022 00:08:43 +0000 Subject: [PATCH 07/13] Working redirect --- r/pkgdown/extra.js | 65 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/r/pkgdown/extra.js b/r/pkgdown/extra.js index 915aeba5fd5..2f3dfd59980 100644 --- a/r/pkgdown/extra.js +++ b/r/pkgdown/extra.js @@ -15,6 +15,29 @@ // specific language governing permissions and limitations // under the License. +function checkPageExistsAndRedirect(event) { + + console.log(event.target.getAttribute("value")); + console.log(event.target); + + const path_to_try = event.target.selectedOptions()[0].getAttributes("value"); + //$( "#version-selector" ).val(); + + const base_path = path_to_try.match("(.*\/r\/)?")[0]; + let tryUrl = path_to_try; + $.ajax({ + type: 'HEAD', + url: tryUrl, + // if the page exists, go there + success: function() { + location.href = tryUrl; + } + }).fail(function() { + location.href = base_path; + }); + return false; +} + (function () { // Load the rmarkdown tabset script var script = document.createElement("script"); @@ -84,6 +107,12 @@ var items = []; // get the current page's version number: var displayed_version = $('.version').text(); + const sel = document.createElement("select"); + sel.name = "version-selector"; + sel.id = "version-selector"; + sel.class = "navbar-default"; + sel.onchange = checkPageExistsAndRedirect; + $.each( data, function( key, val ) { var selected_string = ( @@ -94,14 +123,40 @@ var item_path = $pathStart() + val.version + "r" + $pathEnd(); - items.push(""); +/*** + var corrected_path = $.ajax({ + type: 'GET', + url: item_path, + success: function() { + console.log("win") + return item_path; + }, + error: function() { + // everything up to the /r/ + console.log("fail") + return item_path.match("(.*\/r\/)?")[0]; + + } + }); + console.log(corrected_path); + */ + + //root_path = $pathStart() + val.version + "r"; + + const opt = document.createElement("option"); + opt.value = item_path; + opt.selected = selected_string.length > 0; + opt.text = val.name; + // Argh!!!! Options don't have hyperlinks!!!! + // Do them as list items with hyperlinks instead - see the code for e.g. the project docs dropdown and con + sel.append(opt); + //items.push(opt); + + //items.push(""); }); // Replace the version button with a selector with the doc versions - $("span.version").replaceWith( - ' ' - ); + $("span.version").replaceWith(sel); }); }); From 3cf96faa5dae0850c3b70467e22c4f5bf93ac668 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Tue, 18 Jan 2022 00:13:49 +0000 Subject: [PATCH 08/13] scrap all comments --- r/pkgdown/extra.js | 51 +++++----------------------------------------- 1 file changed, 5 insertions(+), 46 deletions(-) diff --git a/r/pkgdown/extra.js b/r/pkgdown/extra.js index 2f3dfd59980..007a8506f10 100644 --- a/r/pkgdown/extra.js +++ b/r/pkgdown/extra.js @@ -15,20 +15,15 @@ // specific language governing permissions and limitations // under the License. -function checkPageExistsAndRedirect(event) { +function check_page_exists_and_redirect(event) { - console.log(event.target.getAttribute("value")); - console.log(event.target); - - const path_to_try = event.target.selectedOptions()[0].getAttributes("value"); - //$( "#version-selector" ).val(); + const path_to_try = event.target.value; const base_path = path_to_try.match("(.*\/r\/)?")[0]; let tryUrl = path_to_try; $.ajax({ type: 'HEAD', url: tryUrl, - // if the page exists, go there success: function() { location.href = tryUrl; } @@ -104,58 +99,22 @@ function checkPageExistsAndRedirect(event) { //$.getJSON("./versions.json", function( data ) { $.getJSON("https://raw.githubusercontent.com/thisisnic/arrow/ec8c60d97eb489f0c19297e8d9b3f48e44db5afb/r/pkgdown/assets/versions.json", function( data ) { - var items = []; // get the current page's version number: var displayed_version = $('.version').text(); const sel = document.createElement("select"); sel.name = "version-selector"; sel.id = "version-selector"; sel.class = "navbar-default"; - sel.onchange = checkPageExistsAndRedirect; + sel.onchange = check_page_exists_and_redirect; $.each( data, function( key, val ) { - - var selected_string = ( - val.name.match("[0-9.]*")[0] === displayed_version ? - "selected" : - "" - ); - - var item_path = $pathStart() + val.version + "r" + $pathEnd(); - -/*** - var corrected_path = $.ajax({ - type: 'GET', - url: item_path, - success: function() { - console.log("win") - return item_path; - }, - error: function() { - // everything up to the /r/ - console.log("fail") - return item_path.match("(.*\/r\/)?")[0]; - - } - }); - console.log(corrected_path); - */ - - //root_path = $pathStart() + val.version + "r"; - const opt = document.createElement("option"); - opt.value = item_path; - opt.selected = selected_string.length > 0; + opt.value = $pathStart() + val.version + "r" + $pathEnd(); + opt.selected = val.name.match("[0-9.]*")[0] === displayed_version; opt.text = val.name; - // Argh!!!! Options don't have hyperlinks!!!! - // Do them as list items with hyperlinks instead - see the code for e.g. the project docs dropdown and con sel.append(opt); - //items.push(opt); - - //items.push(""); }); - // Replace the version button with a selector with the doc versions $("span.version").replaceWith(sel); }); }); From 2956e583480b2a5db8e0df6d9efac430346362d1 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Tue, 18 Jan 2022 10:17:47 +0000 Subject: [PATCH 09/13] Remove comments --- r/pkgdown/extra.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/r/pkgdown/extra.js b/r/pkgdown/extra.js index 007a8506f10..e318ff42ebd 100644 --- a/r/pkgdown/extra.js +++ b/r/pkgdown/extra.js @@ -94,11 +94,7 @@ function check_page_exists_and_redirect(event) { } // Load the versions JSON and construct the select items - // Delete below line before merging - // You can test with https://raw.githubusercontent.com/thisisnic/arrow/ARROW-14338_pkgdown_versioning/r/pkgdown/assets/versions.json - //$.getJSON("./versions.json", function( data ) { - $.getJSON("https://raw.githubusercontent.com/thisisnic/arrow/ec8c60d97eb489f0c19297e8d9b3f48e44db5afb/r/pkgdown/assets/versions.json", function( data ) { - + $.getJSON("./versions.json", function( data ) { // get the current page's version number: var displayed_version = $('.version').text(); const sel = document.createElement("select"); From e219f6def684ede8a7b44c1f37c875d84ebbe31b Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Tue, 18 Jan 2022 10:21:08 +0000 Subject: [PATCH 10/13] Fix spacing --- r/pkgdown/extra.js | 69 +++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/r/pkgdown/extra.js b/r/pkgdown/extra.js index e318ff42ebd..e72ab47e2ad 100644 --- a/r/pkgdown/extra.js +++ b/r/pkgdown/extra.js @@ -78,44 +78,45 @@ function check_page_exists_and_redirect(event) { empty_ul.remove(); }); - $(document).ready(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. */ - $pathStart = function(){ + $pathStart = function(){ return window.location.origin + "/docs/"; - } - - $pathEnd = function(){ - var current_path = window.location.pathname; - return current_path.match("(?<=\/r).*"); - } - - // Load the versions JSON and construct the select items - $.getJSON("./versions.json", function( data ) { - // get the current page's version number: - var displayed_version = $('.version').text(); - const sel = document.createElement("select"); - sel.name = "version-selector"; - sel.id = "version-selector"; - sel.class = "navbar-default"; - sel.onchange = check_page_exists_and_redirect; - - $.each( data, function( key, val ) { - const opt = document.createElement("option"); - opt.value = $pathStart() + val.version + "r" + $pathEnd(); - opt.selected = val.name.match("[0-9.]*")[0] === displayed_version; - opt.text = val.name; - sel.append(opt); - }); - - $("span.version").replaceWith(sel); - }); - }); - - }; - - document.head.appendChild(script); + } + + $pathEnd = function(){ + var current_path = window.location.pathname; + return current_path.match("(?<=\/r).*"); + } + + // Load the versions JSON and construct the select items + $.getJSON("./versions.json", function( data ) { + // get the current page's version number: + var displayed_version = $('.version').text(); + const sel = document.createElement("select"); + sel.name = "version-selector"; + sel.id = "version-selector"; + sel.class = "navbar-default"; + sel.onchange = check_page_exists_and_redirect; + + $.each( data, function( key, val ) { + const opt = document.createElement("option"); + opt.value = $pathStart() + val.version + "r" + $pathEnd(); + opt.selected = val.name.match("[0-9.]*")[0] === displayed_version; + opt.text = val.name; + sel.append(opt); + }); + + $("span.version").replaceWith(sel); + }); + }); + + }; + + document.head.appendChild(script); })(); From ecd951bbf1fb5506135f158db7de4b6e355e41ca Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Wed, 19 Jan 2022 09:59:59 +0000 Subject: [PATCH 11/13] Update r/pkgdown/extra.js --- r/pkgdown/extra.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/r/pkgdown/extra.js b/r/pkgdown/extra.js index e72ab47e2ad..627291dcd62 100644 --- a/r/pkgdown/extra.js +++ b/r/pkgdown/extra.js @@ -101,7 +101,7 @@ function check_page_exists_and_redirect(event) { const sel = document.createElement("select"); sel.name = "version-selector"; sel.id = "version-selector"; - sel.class = "navbar-default"; + sel.classList.add("navbar-default"); sel.onchange = check_page_exists_and_redirect; $.each( data, function( key, val ) { From 195ffeac01d328a9484eadda9c407897d9785a03 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Wed, 19 Jan 2022 11:23:47 +0000 Subject: [PATCH 12/13] Construct versions path --- r/pkgdown/extra.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/r/pkgdown/extra.js b/r/pkgdown/extra.js index 627291dcd62..bf0ef4e1494 100644 --- a/r/pkgdown/extra.js +++ b/r/pkgdown/extra.js @@ -79,7 +79,7 @@ function check_page_exists_and_redirect(event) { }); $(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. @@ -95,7 +95,9 @@ function check_page_exists_and_redirect(event) { } // Load the versions JSON and construct the select items - $.getJSON("./versions.json", function( data ) { + versions_path = window.location.href.match(".+(\/r\/)")[0] + "versions.json"; + + $.getJSON(versions_path, function( data ) { // get the current page's version number: var displayed_version = $('.version').text(); const sel = document.createElement("select"); From b0a1c77994605e637a65c269ee553f72c9f4a798 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Wed, 19 Jan 2022 11:37:29 +0000 Subject: [PATCH 13/13] Absolute URL --- r/pkgdown/extra.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/r/pkgdown/extra.js b/r/pkgdown/extra.js index bf0ef4e1494..4212d27e814 100644 --- a/r/pkgdown/extra.js +++ b/r/pkgdown/extra.js @@ -94,10 +94,7 @@ function check_page_exists_and_redirect(event) { return current_path.match("(?<=\/r).*"); } - // Load the versions JSON and construct the select items - versions_path = window.location.href.match(".+(\/r\/)")[0] + "versions.json"; - - $.getJSON(versions_path, function( data ) { + $.getJSON("https://arrow.apache.org/docs/r/versions.json", function( data ) { // get the current page's version number: var displayed_version = $('.version').text(); const sel = document.createElement("select");