Skip to content

Instantly share code, notes, and snippets.

@aleph-naught2tog
Last active April 29, 2018 18:41
Show Gist options
  • Save aleph-naught2tog/8dde16243e7bb7686f264e5ef2305b43 to your computer and use it in GitHub Desktop.
Save aleph-naught2tog/8dde16243e7bb7686f264e5ef2305b43 to your computer and use it in GitHub Desktop.
Java API Sorting Bookmarklet
/*
For sorting the method tables by return type in the Java API.
To use: make a bookmarklet whose URL is exactly the following:
javascript:(function(){const%20TABLE_CLASS=".memberSummary";const%20ROWS_OTHER_THAN_FIRST_SELECTOR="tr:not(:first-child)";const%20methodTable=document.querySelector(TABLE_CLASS);const%20tbody=methodTable.querySelector("tbody");const%20allRows=tbody.querySelectorAll(ROWS_OTHER_THAN_FIRST_SELECTOR);const%20sortAlphabetically=(firstString,secondString)=>{if(firstString===secondString){return%200;}if(firstString<secondString){return-1;}if(firstString>secondString){return%201;}};const%20sortByInnerText=(firstHtmlElement,secondHtmlElement)=>{const%20firstText=firstHtmlElement.innerText;const%20secondText=secondHtmlElement.innerText;return%20sortAlphabetically(firstText,secondText);};const%20sortRowByFirstColumn=(firstRow,secondRow)=>{return%20sortByInnerText(firstRow.querySelector("td"),secondRow.querySelector("td"));};const%20sortByJdocRowId=(firstJavadocRow,secondJavadocRow)=>{const%20firstId=firstJavadocRow.id;const%20secondId=secondJavadocRow.id;return%20sortAlphabetically(firstId,secondId);};const%20removeTableRows=()=>{allRows.forEach(element=>element.remove());};const%20replaceRowsWith=(collectionToReplaceWith)=>{removeTableRows();collectionToReplaceWith.forEach(element=>tbody.appendChild(element));};const%20rowsSortedByJdocRowId=Array.from(allRows).sort(sortByJdocRowId);const%20rowsSortedByType=Array.from(allRows).sort(sortRowByFirstColumn);if(methodTable.classList.contains("bookmarkletSortedAsc")){replaceRowsWith(rowsSortedByType.reverse());methodTable.classList.add("bookmarkletSortedDesc");methodTable.classList.remove("bookmarkletSortedAsc");}else%20if(methodTable.classList.contains("bookmarkletSortedDesc")){replaceRowsWith(rowsSortedByType);methodTable.classList.remove("bookmarkletSortedDesc");}else{replaceRowsWith(rowsSortedByJdocRowId);methodTable.classList.add("bookmarkletSortedAsc");}})();
(The above is just `javascript:` plus everything below, but with spaces and linebreaks removed.)
*/
(function() {
const TABLE_CLASS = ".memberSummary";
const ROWS_OTHER_THAN_FIRST_SELECTOR = "tr:not(:first-child)";
const methodTable = document.querySelector(TABLE_CLASS);
const tbody = methodTable.querySelector("tbody");
const allRows = tbody.querySelectorAll(ROWS_OTHER_THAN_FIRST_SELECTOR);
const sortAlphabetically = (firstString, secondString) => {
if (firstString === secondString) {
return 0;
}
if (firstString < secondString) {
return -1;
}
if (firstString > secondString) {
return 1;
}
};
const sortByInnerText = (firstHtmlElement, secondHtmlElement) => {
const firstText = firstHtmlElement.innerText;
const secondText = secondHtmlElement.innerText;
return sortAlphabetically(firstText, secondText);
};
const sortRowByFirstColumn = (firstRow, secondRow) => {
return sortByInnerText(firstRow.querySelector("td"), secondRow.querySelector("td"));
};
const sortByJdocRowId = (firstJavadocRow, secondJavadocRow) => {
const firstId = firstJavadocRow.id;
const secondId = secondJavadocRow.id;
return sortAlphabetically(firstId, secondId);
};
const removeTableRows = () => {
// in place side effects on a global, oh javascript
allRows.forEach(element => element.remove());
};
const replaceRowsWith = (collectionToReplaceWith) => {
removeTableRows();
collectionToReplaceWith.forEach(element => tbody.appendChild(element));
};
const rowsSortedByJdocRowId = Array.from(allRows).sort(sortByJdocRowId);
const rowsSortedByType = Array.from(allRows).sort(sortRowByFirstColumn);
if (methodTable.classList.contains("bookmarkletSortedAsc")) {
replaceRowsWith(rowsSortedByType.reverse());
methodTable.classList.add("bookmarkletSortedDesc");
methodTable.classList.remove("bookmarkletSortedAsc");
} else if (methodTable.classList.contains("bookmarkletSortedDesc")) {
replaceRowsWith(rowsSortedByType);
methodTable.classList.remove("bookmarkletSortedDesc");
} else {
replaceRowsWith(rowsSortedByJdocRowId);
methodTable.classList.add("bookmarkletSortedAsc");
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment