Last active
March 24, 2020 16:37
-
-
Save rolandcrosby/1c4cbb3166e5bc9052afe24863737e52 to your computer and use it in GitHub Desktop.
Script that adds "sort by" options to Zulip's "add streams" widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.sortBy = function(key, ascending) { | |
const streams = Array.from(document.querySelectorAll(".stream-row")).map( | |
r => ({ | |
row: r, | |
name: r.querySelector(".stream-name").innerText, | |
description: r.querySelector(".description").innerText, | |
messagesPerWeek: parseInt( | |
r.querySelector(".stream-message-count-text").innerText | |
), | |
subscribers: parseInt( | |
r.querySelector(".subscriber-count-text").innerText | |
), | |
subscribed: !!r.querySelector(".sub_unsub_button.checked") | |
}) | |
); | |
streams | |
.sort((x, y) => { | |
if (ascending) { | |
return x[key] < y[key] ? -1 : 1; | |
} else { | |
return y[key] < x[key] ? -1 : 1; | |
} | |
}) | |
.forEach(r => { | |
const p = r.row.parentNode; | |
p.removeChild(r.row); | |
p.appendChild(r.row); | |
}); | |
} | |
const htmlToInject = `<style>.search-container .ind-tab {width: auto !important;}</style> | |
<div class="tab-switcher"><div class="ind-tab first" onClick="sortBy('name', true)">Name</div><div class="ind-tab" onClick="sortBy('subscribers')"><i class="fa fa-user-o" aria-hidden="true"></i></div><div class="ind-tab last" onClick="sortBy('messagesPerWeek')"><i class="fa fa-bar-chart-o" aria-hidden="true"></i></div></div>` | |
document.querySelector(".search-container .tab-switcher").insertAdjacentHTML('afterEnd', htmlToInject); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment