Untitled
unknown
plain_text
2 years ago
7.4 kB
14
Indexable
//Snippet Explanation
//code snippet is a self-executing anonymous function that serves as a global filter for widgets on a web page.
//It keeps track of the current widget GUIDs and the number of loaded widgets.
//The checkListChanged function is called initially and then recursively after a delay.
//It retrieves all the widgets on the page and creates an array of their GUIDs.
//If the list of widget GUIDs has changed since the last check, the loaded widget count is reset,
//and the current widget GUIDs are updated. Then, it loops through all the widgets and calls
//the setSelectedValuesOrDateRangeIfNeed function for each widget.
//The setSelectedValuesOrDateRangeIfNeed function checks if the widget is in the current
//list of widgets and if the widget state and data container are available.
//If the widget is a "DateFilter" type, it sets the date range based on the selected values stored in local storage.
//Otherwise, it sets the filter based on the selected values. The loaded widget count is incremented after processing each widget.
//The saveSelectedValuesToLocalStorage function saves the selected values of a widget to local storage,
//and the getSelectedValuesFromLocalStorage function retrieves the selected values from local storage.
//The deepEqual function checks if two objects are deeply equal by comparing their JSON representations.
//The setFilter and setDateRange functions are utility functions that use the visApi() to set the selected
//values or date range for a widget.
//The saveWidgetsToStorage function saves the selected values of all widgets to local storage.
//The callAfterDelay function is a utility function that calls a function after a delay of 300 milliseconds.
//Overall, this code snippet provides a mechanism to keep track of widget changes, store and retrieve selected values from local storage, and set the selected values or date range for widgets.
(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