Untitled

 avatar
unknown
plain_text
2 years ago
5.4 kB
9
Indexable
(function globalFilter() {

    let currentWidgetGuids = []; // Array to store the current widget GUIDs
    let widgetLoadedCount = 0; // Counter for the number of loaded widgets

    (function checkListChanged() {
        // Get all widgets on the page
        let allWidgets = visApi().getWidgets();
        // Get an array of GUIDs for all the widgets
        let allWidgetsGuids = allWidgets.map(function (w) {
            return w.guid();
        });

        // Check if the list of widgets has changed
        if (!deepEqual(allWidgetsGuids, currentWidgetGuids)) {
            widgetLoadedCount = 0; // Reset the loaded widget count
            currentWidgetGuids = allWidgetsGuids; // Update the current widget GUIDs

            // Loop through all the widgets and set selected values or date range if needed
            allWidgets.forEach(function (widget) {
                setSelectedValuesOrDateRangeIfNeed(widget);
            });

        } else {
            // If all the widgets have been loaded, save them to storage
            if (allWidgets.length === widgetLoadedCount) {
                saveWidgetsToStorage();
            }
        }

        // Call this function again after a delay
        callAfterDelay(checkListChanged);
    })();

    // Get a key for storing the widget in local storage
    function getKeyForStorage(widget) {
        let text = widget.widgetState.title.text;
        let dataSettings = widget.widgetState.dataSettings;

        // Conditions for storing the widget:
        // - Not an OLAP or type 6 datasource
        // - Widget type is "Filter" or "DateFilter"
        // - Widget has a title text with a space at the end
        if ((dataSettings.datasourceType !== "olap" && dataSettings.datasourceType !== 6)
            || widget.type() !== "Filter" && widget.type() !== "DateFilter"
            || !text)
            return null;

        // The key is the widget's title text with a space at the end
        return text;
    }

    // Save selected values of a widget to local storage
    function saveSelectedValuesToLocalStorage(widget) {
        let key = getKeyForStorage(widget);
        if (key == null)
            return;

        let obj = {
            selectedValues: widget.selectedValues()
        };

        localStorage.setItem(key, JSON.stringify(obj));
    }

    // Get selected values of a widget from local storage
    function getSelectedValuesFromLocalStorage(widget) {
        let key = getKeyForStorage(widget);
        if (key == null)
            return null;

        let value = localStorage.getItem(key);
        if (!value)
            return null;

        return JSON.parse(value).selectedValues;
    }

    // Check if two objects are deeply equal
    function deepEqual(obj1, obj2) {
        return JSON.stringify(obj1) === JSON.stringify(obj2);
    }

    // Set selected values or date range for a widget if needed
    function setSelectedValuesOrDateRangeIfNeed(widget) {
        let selectedValuesLocalStorage = getSelectedValuesFromLocalStorage(widget);
        let currentSelectedValues = widget.selectedValues();

        // If the widget is not in the current list of widgets, return
        if (currentWidgetGuids.indexOf(widget.guid()) === -1)
            return;

        // Check if the widget state and data container are available
        if (widget.widgetState == null || widget.widgetDataContainer == null) {
            // Call this function again after a delay
            callAfterDelay(setSelectedValuesOrDateRangeIfNeed, widget);
            return;
        }

        // If the widget type is "DateFilter", set the date range
        if (widget.type() === "DateFilter") {
            if (selectedValuesLocalStorage != null && !deepEqual(selectedValuesLocalStorage, currentSelectedValues)) {
                setDateRange(widget.guid(), selectedValuesLocalStorage);
            }
        } else {
            // Otherwise, set the filter
            if (selectedValuesLocalStorage != null && !deepEqual(selectedValuesLocalStorage, currentSelectedValues)) {
                setFilter(widget.guid(), selectedValuesLocalStorage);
            }
        }

        // Increment the loaded widget count
        widgetLoadedCount += 1;
    }

    // Set filter selected values for a widget
    function setFilter(widgetGuid, values) {
        visApi().setFilterSelectedValues(widgetGuid, values);
    }

    // Set date filter selected values for a widget
    function setDateRange(widgetGuid, dateRange) {
        // Convert the date strings to Date objects
        const dateObjects = values[0].map(dateString => new Date(dateString));
        visApi().setDateFilterSelectedValues(widgetGuid, dateObjects);
    }

    // Save selected values of all widgets to storage
    function saveWidgetsToStorage() {
        let allWidgets = visApi().getWidgets();
        allWidgets.forEach(function (widget) {
            if (widget.widgetState == null || widget.widgetDataContainer == null)
                return;

            saveSelectedValuesToLocalStorage(widget);
        });
    };

    // Call a function after a delay
    function callAfterDelay(func, argument) {
        setTimeout(function () {
            func(argument);
        }, 300);
    }
})();
Editor is loading...
Leave a Comment