FMC Expand Toggle

Fixes expand all button to allow for collapse all

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         FMC Expand Toggle
// @namespace    http://tampermonkey.net/
// @version      1.11
// @description  Fixes expand all button to allow for collapse all
// @author       geesmavi
// @include      https://trans-logistics.amazon.com/fmc/*
// @include      https://trans-logistics-eu.amazon.com/fmc/*
// @icon         https://www.google.com/s2/favicons?domain=amazon.com
// @grant        GM_addStyle
// @run-at document-start
// ==/UserScript==

(function() {
    'use strict';
    GM_addStyle(`
        .comment-visibility-toggle {
        user-select: none;
        }
    `);

    // restarts detailsToggleObserver on url change
    let lastUrl = location.href;
    new MutationObserver(() => {
        const url = location.href;
        if (url !== lastUrl) {
            lastUrl = url;
            detailsToggleObserver.observe(document, {
                childList: true,
                subtree: true
            });
        }
    }).observe(document, {subtree: true, childList: true});

    // watches page for dashboard-details-toggle which loads after page load
    // if element exists starts the detailsToggler
    const detailsToggleObserver = new MutationObserver((mutations, obs) => {
        const detailsToggle = document.querySelector('th.dashboard-details-toggle');
        if (detailsToggle) {
            const fmcShown = document.querySelector('.fmc-shown');
            if (fmcShown) {
                detailsToggle.classList.remove('child-row-expand-trigger');
                detailsToggle.classList.add('child-row-collapse-trigger');
            }
            detailsToggler(detailsToggle);
            obs.disconnect();
            return;
        }
    });

    detailsToggleObserver.observe(document, {
        childList: true,
        subtree: true
    });

    // swaps the expand/collapse classes of given element
    function detailsToggler(element) {
            const removeClass = function(removedClass) {
                element.classList.remove(removedClass);
                return;
            };
            const addClass = function(addedClass) {
                element.classList.add(addedClass);
                return;
            };

            element.addEventListener("click", function() {
                switch(true){
                    case element.classList.contains('child-row-expand-trigger'):
                        removeClass('child-row-expand-trigger');
                        addClass('child-row-collapse-trigger');
                        break;
                    case element.classList.contains('child-row-collapse-trigger'):
                        removeClass('child-row-collapse-trigger');
                        addClass('child-row-expand-trigger');
                        break;
                    default:
                        break;
                }

                return;
            });
        };
})();