Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Scene Page Remember States plugin #261

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions plugins/scenesPageRememberStates/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Scene Page Remember States

This plugin uses local storage to rememebr what is the current active nav tab of the scenes' detail panel, and upon any page load activate the last remembered active nav tab.

It also rembers the active collapsed state of the divider button and upon page load if it's true, it will automatically collapse the divider.
101 changes: 101 additions & 0 deletions plugins/scenesPageRememberStates/scenePageRememberStates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
(async function waitForStash() {
while (!window.stash) {
Copy link
Contributor

Choose a reason for hiding this comment

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

window.stash is never exposed and being a requirement should ensure it's loaded beforehand

await new Promise((resolve) => setTimeout(resolve, 100));
}

function handlePageDivider() {
waitForElementClass("scene-tabs order-xl-first order-last", function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

see other waitForElement comment

const pageDivider = document
.querySelector(".scene-divider.d-none.d-xl-block")
.querySelector("button");

let isActive = localStorage.getItem("scene-page-divider");
if (isActive === null) {
localStorage.setItem("scene-page-divider", "false");
isActive = false;
} else {
isActive = isActive === "true";
}

if (isActive === true) {
pageDivider.click();
}

pageDivider.addEventListener("click", function () {
isActive = !isActive;
localStorage.setItem("scene-page-divider", isActive.toString());
});
});
}

function nav() {
waitForElementClass("mr-auto nav nav-tabs", function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

waitForElement(".mr-auto.nav.nav-tabs", function() {

const navMenuItems = [
{ name: "Details", key: "scene-details-panel" },
{ name: "Queue", key: "scene-queue-panel" },
{ name: "Markers", key: "scene-markers-panel" },
{ name: "Filters", key: "scene-filters-panel" },
{ name: "File Info", key: "scene-file-info-panel" },
{ name: "Edit", key: "scene-edit-panel" },
];

const detailsNav = document.getElementsByClassName(
Copy link
Contributor

Choose a reason for hiding this comment

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

see other WaitForElement comment

"mr-auto nav nav-tabs"
)[0];
const hrefs = detailsNav.getElementsByTagName("a");
Copy link
Contributor

Choose a reason for hiding this comment

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

see other waitForElement comment


// Check local storage for entries
let activeKey = localStorage.getItem("detailsNavActive");

// If no entry found, create default entry
if (!activeKey) {
navMenuItems.forEach((item) => {
if (item.name === "Details") {
localStorage.setItem("detailsNavActive", item.key);
}
});
activeKey = "scene-details-panel";
}

// Remove active class from all hrefs
Array.from(hrefs).forEach((href) => {
href.classList.remove("active");
});

// Add active class to the one that matches activeKey
Array.from(hrefs).forEach((href) => {
if (href.dataset.rbEventKey === activeKey) {
href.classList.add("active");
}
});

// Simulate click on active tab
const activeTab = detailsNav.querySelector(
`a[data-rb-event-key="${activeKey}"]`
);
activeTab.click();

// Add event listeners
Array.from(hrefs).forEach((href) => {
href.addEventListener("click", function () {
// Remove active class from all hrefs
Array.from(hrefs).forEach((href) => {
href.classList.remove("active");
});

// Add active class to the clicked href
this.classList.add("active");

// Store active key in local storage
const newActiveKey = this.dataset.rbEventKey;
localStorage.setItem("detailsNavActive", newActiveKey);
});
});
});
}

function main() {
nav(), handlePageDivider();
}
stash.addEventListener("stash:page:scene", main());
Copy link
Contributor

Choose a reason for hiding this comment

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

replaced by PathElementListener

csLib.PathElementListener("/scene/", "", main))

})();
9 changes: 9 additions & 0 deletions plugins/scenesPageRememberStates/scenePageRememberStates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: Scene Page Remember States
description: Uses local storage to remember the state of the scene page detail panel nav bar and activate it on page load. Remembers collapse state of the divider.
url:
version: 0.1
ui:
requires:
- stashUserscriptLibrary
Copy link
Contributor

Choose a reason for hiding this comment

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

stashUserscriptLibrary is no longer part of the CommunityScripts repository. It was replaced with https://github.com/stashapp/CommunityScripts/tree/main/plugins/CommunityScriptsUILibrary, but it might be lacking some functionality.

Needs a review.

javascript:
- scenePageRememberStates.js