From 324d06fc74c7543fe1184d21e469a7897123329d Mon Sep 17 00:00:00 2001 From: blankie Date: Thu, 2 Nov 2023 10:02:00 +1100 Subject: [PATCH] Update Compass QoL Enhancer to 1.21.0 - Re-open panel tabs and learning tasks when the URL changes --- Compass QoL Enhancer.user.js | 85 +++++++++++++++++++++++++++++++----- README.md | 1 + 2 files changed, 75 insertions(+), 11 deletions(-) diff --git a/Compass QoL Enhancer.user.js b/Compass QoL Enhancer.user.js index 281ca0a..15fd246 100644 --- a/Compass QoL Enhancer.user.js +++ b/Compass QoL Enhancer.user.js @@ -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-: /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"); diff --git a/README.md b/README.md index 18fee21..dda5c33 100644 --- a/README.md +++ b/README.md @@ -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