Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Move API docs functionality from search.js to api-docs.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Scandurra committed Jul 15, 2019
1 parent ccef5c1 commit b3e6add
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
44 changes: 31 additions & 13 deletions assets/javascripts/api-docs.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
(function() {
$(document).on("click", ".js-api-name", function(e) {
e.preventDefault();
toggleApiEntry($(this).attr("href"));
return false;
});
if (isApiDocsPage()) {
$(document).on("click", ".js-api-name", function(e) {
e.preventDefault();
toggleApiEntry($(this).attr("href"));
return false;
});

$(document).ready(function() {
if (window.location.hash) {
toggleApiEntry(window.location.hash);
}
});
$(document).ready(function() {
if (window.location.hash) {
toggleApiEntry(window.location.hash);
}

$(window).on("hashchange", function() {
toggleApiEntry(window.location.hash);
});
$("#documents-search-versions").change(function() {
const components = window.location.pathname.split("/");
if (!components[components.length - 1]) {
// Pop trailing slash off
components.pop();
}
const page = components.pop();
const version = this.value;
const { hash } = window.location;
window.location.replace(`/api/v${version}/${page}${hash}`);
});
});

$(window).on("hashchange", function() {
toggleApiEntry(window.location.hash);
});
}

function toggleApiEntry(apiEntryId) {
const entry = $(apiEntryId);
Expand All @@ -29,4 +43,8 @@
window.history.replaceState("", "", apiEntryId); // update url hash w/o adding history
}
}

function isApiDocsPage() {
return window.location.pathname.startsWith("/api/");
}
})();
12 changes: 0 additions & 12 deletions assets/javascripts/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ $(document).ready(function() {
if (isSearchPage()) {
params.set("v", version);
window.history.pushState(null, null, "?" + params.toString());
} else if (isApiDocsPage()) {
const components = window.location.pathname.split('/')
if (!components[components.length - 1]) {
// Pop trailing slash off
components.pop()
}
const page = components.pop()
window.location.replace(`/api/v${version}/${page}`)
}

search.reindex(version, "/search/v" + version + ".json");
Expand Down Expand Up @@ -224,7 +216,3 @@ class LunrSearch {
function isSearchPage() {
return window.location.pathname.match(/\/search(?:\/|$)/);
}

function isApiDocsPage() {
return window.location.pathname.startsWith('/api/')
}

1 comment on commit b3e6add

@ellis9983
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try it like this

Please sign in to comment.