Update Compass QoL Enhancer to 1.21.0

- Re-open panel tabs and learning tasks when the URL changes
This commit is contained in:
blankie 2023-11-02 10:02:00 +11:00
parent 40268aade6
commit 324d06fc74
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
2 changed files with 75 additions and 11 deletions

View File

@ -2,7 +2,7 @@
// @name Compass QoL Enhancer
// @namespace blankie-scripts
// @match http*://*.compass.education/*
// @version 1.20.0
// @version 1.21.0
// @author blankie
// @description A userscript that adds small but useful features for Compass, such as the ability to close windows by clicking on the background
// @inject-into page
@ -13,6 +13,7 @@
let qolTabOpened = false;
let qolLearningTaskOpened = false;
let modifyUrlHash = true;
let shownWindows = 0;
// needed because .toString() adds a trailing = for empty values
@ -139,7 +140,10 @@ if (LearningTasksDetailsWidgetNew) {
return;
}
this.openUrlLearningTask();
qolLearningTaskOpened = true;
};
LearningTasksDetailsWidgetNew.prototype.openUrlLearningTask = function() {
let learningTask = parseInt(getHashSearch().get("qol_learning_task"), 10);
if (isNaN(learningTask)) {
return;
@ -154,11 +158,19 @@ if (LearningTasksDetailsWidgetNew) {
// Automatically add (and remove) the currently open learning task to the URL
function handleLearningTaskShow() {
if (!modifyUrlHash) {
return;
}
let search = getHashSearch();
search.set("qol_learning_task", this.learningTask.data.id);
history.pushState("", "", `#${search.laxToString()}`);
}
function handleLearningTaskClose() {
if (!modifyUrlHash) {
return;
}
let search = getHashSearch();
search.delete("qol_learning_task");
history.pushState("", "", `#${search.laxToString()}`);
@ -342,6 +354,15 @@ function handleNewCalendarEvent(element, calendar) {
element.replaceWith(a);
}
function handlePanel(panel) {
let tabToOpen = window.location.pathname === "/Organise/Activities/Activity.aspx"
? getHashSearch().get("qol_open_tab")
: (window.location.hash.replace(/^#(.+?)(?:&.*)?$/, "$1") || null);
for (let i = 0; i < panel.items.items.length; i++) {
handlePanelItem(panel, panel.items.items[i], i === 0, tabToOpen);
}
}
function handlePanelItem(panel, panelItem, isDefault, tabToOpen) {
let panelId = panelItem.itemId || panelItem.id;
// example of panel-<id>: /Organise/Subjects/Subject.aspx
@ -354,7 +375,7 @@ function handlePanelItem(panel, panelItem, isDefault, tabToOpen) {
if (panelId === tabToOpen) {
// if the sessions tab is automatically opened, the currently selected tab is still dashboard for some reason,
// hence this hook to open it when the dashboard finishes loading
if (panel.setDashboardToLoading) {
if (!qolTabOpened && panel.setDashboardToLoading) {
let original = panel.setDashboardToLoading;
panel.setDashboardToLoading = function(loading) {
original.apply(this, arguments);
@ -380,7 +401,7 @@ function handlePanelItem(panel, panelItem, isDefault, tabToOpen) {
}
// prevent the browser from scrolling to the body
event.preventDefault();
if (window.location.hash !== panelItemHash && (window.location.pathname === "/Organise/Activities/Activity.aspx" || !window.location.hash.startsWith(`${panelItemHash}&`))) {
if (modifyUrlHash && window.location.hash !== panelItemHash && (window.location.pathname === "/Organise/Activities/Activity.aspx" || !window.location.hash.startsWith(`${panelItemHash}&`))) {
// Automatically add a reference to the tab when it is clicked
history.pushState("", "", panelItemHash);
}
@ -471,18 +492,12 @@ function handleNewNode(node, observer) {
handleNewCalendarEvent(element, calendar);
}
} else if (!qolTabOpened && node.classList.contains("x-panel")) {
qolTabOpened = true;
let topMostPanel = node;
while (topMostPanel.parentElement.closest(".x-panel")) {
topMostPanel = topMostPanel.parentElement.closest(".x-panel");
}
let panel = unsafeWindow.Ext.getCmp(topMostPanel.id);
let tabToOpen = window.location.pathname === "/Organise/Activities/Activity.aspx"
? getHashSearch().get("qol_open_tab")
: (window.location.hash.replace(/^#(.+?)(?:&.*)?$/, "$1") || null);
for (let i = 0; i < panel.items.items.length; i++) {
handlePanelItem(panel, panel.items.items[i], i === 0, tabToOpen);
}
handlePanel(unsafeWindow.Ext.getCmp(topMostPanel.id));
qolTabOpened = true;
} else if (node.closest("[id^='wikibrowserpanel-'], #CompassWidgetsWikiBrowserPanel")) {
// Make files and folders in wiki/resources clickable for Link Hints
if (node.localName === "td") {
@ -553,6 +568,54 @@ document.body.addEventListener("keydown", function(event) {
window.down("#saveButton").handler();
}, {passive: true});
// Re-open panel tabs and learning tasks when the URL changes
function getLearningTaskWindow() {
let LearningTasksSubmissionWidget = getExtClass("Compass.widgets.LearningTasksSubmissionWidget");
for (let window of document.querySelectorAll(".x-window")) {
window = unsafeWindow.Ext.getCmp(window.id);
if (unsafeWindow.Ext.getClass(window) === LearningTasksSubmissionWidget) {
return window;
}
}
return null;
}
function getLearningTaskTab(panel) {
return panel.items.items.find(function(tab) {
return tab.itemId === "learningtasks";
});
}
window.addEventListener("hashchange", function(event) {
modifyUrlHash = false;
let isActivityPage = window.location.pathname === "/Organise/Activities/Activity.aspx";
let hashSearch = getHashSearch();
let panel = document.querySelector(".x-panel");
if (panel) {
panel = unsafeWindow.Ext.getCmp(panel.id);
if (isActivityPage && hashSearch.has("openLearningTaskTab")) {
panel.setActiveTab(getLearningTaskTab(panel));
} else if (isActivityPage && hashSearch.size === 1) {
event.stopImmediatePropagation();
panel.setActiveTab(panel.items.items[0]);
} else {
handlePanel(panel);
}
}
let learningTaskWindow = getLearningTaskWindow();
if (learningTaskWindow) {
learningTaskWindow.close();
}
let learningTaskWidget = document.querySelector(".sel-learning-tasks-widget");
if (learningTaskWidget) {
unsafeWindow.Ext.getCmp(learningTaskWidget.id).openUrlLearningTask();
}
modifyUrlHash = true;
});
// Stop the calendar and email buttons from opening in a new tab
for (let element of document.querySelectorAll("#productNavBar a[target='_blank']")) {
element.removeAttribute("target");

View File

@ -57,6 +57,7 @@ A userscript that adds small but useful features for Compass. Features are:
- Workaround a [Link Hints bug](https://github.com/lydell/LinkHints/issues/86)
that prevents it from seeing links inside lesson plans and such
- Files and folders in Resources are now sorted alphabetically
- Reopening panel tabs and learning tasks when the URL changes
- The option to remember logins is unchecked by default
- The dashboard tab in a user's profile no longer points you to #dsh
- Pages can no longer be scrolled if a window is currently open