mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Rework palette state management
This commit is contained in:
parent
bffd1d61b2
commit
f5fd6e3a36
@ -35,7 +35,7 @@ RED.palette = (function() {
|
||||
var categoryContainers = {};
|
||||
var sidebarControls;
|
||||
|
||||
let lastState = { filter: "", collapsed: [] };
|
||||
let paletteState = { filter: "", collapsed: [] };
|
||||
|
||||
function createCategory(originalCategory,rootCategory,category,ns) {
|
||||
if ($("#red-ui-palette-base-category-"+rootCategory).length === 0) {
|
||||
@ -62,29 +62,61 @@ RED.palette = (function() {
|
||||
catDiv.data('label',label);
|
||||
categoryContainers[category] = {
|
||||
container: catDiv,
|
||||
close: function() {
|
||||
hide: function (instant) {
|
||||
if (instant) {
|
||||
catDiv.hide()
|
||||
} else {
|
||||
catDiv.slideUp()
|
||||
}
|
||||
},
|
||||
show: function () {
|
||||
catDiv.show()
|
||||
},
|
||||
isOpen: function () {
|
||||
return !!catDiv.hasClass("red-ui-palette-open")
|
||||
},
|
||||
getNodeCount: function (visibleOnly) {
|
||||
const nodes = catDiv.find(".red-ui-palette-node")
|
||||
if (visibleOnly) {
|
||||
return nodes.filter(function() { return $(this).css('display') !== 'none'}).length
|
||||
} else {
|
||||
return nodes.length
|
||||
}
|
||||
},
|
||||
close: function(instant, skipSaveState) {
|
||||
catDiv.removeClass("red-ui-palette-open");
|
||||
catDiv.addClass("red-ui-palette-closed");
|
||||
$("#red-ui-palette-base-category-"+category).slideUp();
|
||||
if (instant) {
|
||||
$("#red-ui-palette-base-category-"+category).hide();
|
||||
} else {
|
||||
$("#red-ui-palette-base-category-"+category).slideUp();
|
||||
}
|
||||
$("#red-ui-palette-header-"+category+" i").removeClass("expanded");
|
||||
if (!skipSaveState) {
|
||||
if (!paletteState.collapsed.includes(category)) {
|
||||
paletteState.collapsed.push(category);
|
||||
savePaletteState();
|
||||
}
|
||||
}
|
||||
},
|
||||
open: function() {
|
||||
open: function(skipSaveState) {
|
||||
catDiv.addClass("red-ui-palette-open");
|
||||
catDiv.removeClass("red-ui-palette-closed");
|
||||
$("#red-ui-palette-base-category-"+category).slideDown();
|
||||
$("#red-ui-palette-header-"+category+" i").addClass("expanded");
|
||||
if (!skipSaveState) {
|
||||
if (paletteState.collapsed.includes(category)) {
|
||||
paletteState.collapsed.splice(paletteState.collapsed.indexOf(category), 1);
|
||||
savePaletteState();
|
||||
}
|
||||
}
|
||||
},
|
||||
toggle: function() {
|
||||
if (catDiv.hasClass("red-ui-palette-open")) {
|
||||
if (categoryContainers[category].isOpen()) {
|
||||
categoryContainers[category].close();
|
||||
if (!lastState.collapsed.includes(category)) {
|
||||
lastState.collapsed.push(category);
|
||||
}
|
||||
} else {
|
||||
categoryContainers[category].open();
|
||||
lastState.collapsed.splice(lastState.collapsed.indexOf(category), 1);
|
||||
}
|
||||
savePaletteState();
|
||||
}
|
||||
};
|
||||
|
||||
@ -422,7 +454,11 @@ RED.palette = (function() {
|
||||
|
||||
var categoryNode = $("#red-ui-palette-container-"+rootCategory);
|
||||
if (categoryNode.find(".red-ui-palette-node").length === 1) {
|
||||
categoryContainers[rootCategory].open();
|
||||
if (!paletteState?.collapsed?.includes(rootCategory)) {
|
||||
categoryContainers[rootCategory].open();
|
||||
} else {
|
||||
categoryContainers[rootCategory].close(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -535,16 +571,26 @@ RED.palette = (function() {
|
||||
}
|
||||
});
|
||||
|
||||
for (var category in categoryContainers) {
|
||||
for (let category in categoryContainers) {
|
||||
if (categoryContainers.hasOwnProperty(category)) {
|
||||
if (categoryContainers[category].container
|
||||
.find(".red-ui-palette-node")
|
||||
.filter(function() { return $(this).css('display') !== 'none'}).length === 0) {
|
||||
categoryContainers[category].close();
|
||||
categoryContainers[category].container.slideUp();
|
||||
const categorySection = categoryContainers[category]
|
||||
if (categorySection.getNodeCount(true) === 0) {
|
||||
categorySection.hide()
|
||||
} else {
|
||||
categoryContainers[category].open();
|
||||
categoryContainers[category].container.show();
|
||||
categorySection.show()
|
||||
if (val) {
|
||||
// There is a filter being applied and it has matched
|
||||
// something in this category - show the contents
|
||||
categorySection.open(true)
|
||||
} else {
|
||||
// No filter. Only show the category if it isn't in lastState
|
||||
if (!paletteState.collapsed.includes(category)) {
|
||||
categorySection.open(true)
|
||||
} else if (categorySection.isOpen()) {
|
||||
// This section should be collapsed but isn't - so make it so
|
||||
categorySection.close(true, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -607,7 +653,7 @@ RED.palette = (function() {
|
||||
delay: 100,
|
||||
change: function() {
|
||||
filterChange($(this).val());
|
||||
lastState.filter = $(this).val();
|
||||
paletteState.filter = $(this).val();
|
||||
savePaletteState();
|
||||
}
|
||||
});
|
||||
@ -678,27 +724,11 @@ RED.palette = (function() {
|
||||
});
|
||||
|
||||
try {
|
||||
const stateFromStorage = JSON.parse(localStorage.getItem("palette-state") || "{}");
|
||||
|
||||
for (const prop in stateFromStorage) {
|
||||
if (Object.prototype.hasOwnProperty.call(lastState, prop)) {
|
||||
lastState[prop] = stateFromStorage[prop];
|
||||
}
|
||||
}
|
||||
|
||||
// Remove old/unknown category from localStorage
|
||||
lastState.collapsed = lastState.collapsed.filter((c, i, a) => a.indexOf(c) === i && categoryContainers[c]);
|
||||
|
||||
const callback = function () {
|
||||
// Hide categories previously hidded
|
||||
lastState.collapsed.forEach((category) => categoryContainers[category].close());
|
||||
paletteState = JSON.parse(RED.settings.getLocal("palette-state") || '{"filter":"", "collapsed": []}');
|
||||
if (paletteState.filter) {
|
||||
// Apply the category filter
|
||||
$("#red-ui-palette-search input").searchBox("value", lastState.filter);
|
||||
RED.events.off("runtime-state", callback);
|
||||
};
|
||||
|
||||
savePaletteState();
|
||||
RED.events.on("runtime-state", callback);
|
||||
$("#red-ui-palette-search input").searchBox("value", paletteState.filter);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Unexpected error loading palette state from localStorage: ", error);
|
||||
}
|
||||
@ -726,7 +756,7 @@ RED.palette = (function() {
|
||||
|
||||
function savePaletteState() {
|
||||
try {
|
||||
localStorage.setItem("palette-state", JSON.stringify(lastState));
|
||||
RED.settings.setLocal("palette-state", JSON.stringify(paletteState));
|
||||
} catch (error) {
|
||||
console.error("Unexpected error saving palette state to localStorage: ", error);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user