From 233156b19ab2b3d490a8bb450bbb22fdff4caaec Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Mon, 16 Sep 2024 12:34:32 +0200 Subject: js: consolidate sidebar collapse persistence logic Consolidate all the sidebar collapse persistence logic. Implement a single function to handle all the views. --- ogcp/static/js/ogcp.js | 51 +++++----------------------------------------- ogcp/templates/base.html | 2 +- ogcp/templates/images.html | 2 +- ogcp/templates/macros.html | 2 +- ogcp/templates/repos.html | 2 +- 5 files changed, 9 insertions(+), 50 deletions(-) diff --git a/ogcp/static/js/ogcp.js b/ogcp/static/js/ogcp.js index 362aed1..658748f 100644 --- a/ogcp/static/js/ogcp.js +++ b/ogcp/static/js/ogcp.js @@ -152,60 +152,19 @@ function keepSelectedClients(context) { }); } -function keepImagesTreeState() { - const images_tree = $('#servers .collapse') - images_tree.on('hidden.bs.collapse', function (event) { - event.stopPropagation(); - localStorage.removeItem(this.id); - }); - images_tree.on('shown.bs.collapse', function (event) { - event.stopPropagation(); - localStorage.setItem(this.id, 'show'); - }); - - images_tree.each(function () { - if (localStorage.getItem(this.id) == 'show') { - $(this).collapse('show'); - } else { - $(this).siblings('a').addClass('collapsed'); - } - }); -} - -function keepReposTreeState() { - const repos_tree = $('#repos-list .collapse') - - repos_tree.on('hidden.bs.collapse', function (event) { - event.stopPropagation(); - localStorage.removeItem(this.id); - }); - repos_tree.on('shown.bs.collapse', function (event) { - event.stopPropagation(); - localStorage.setItem(this.id, 'show'); - }); - - repos_tree.each(function () { - if (localStorage.getItem(this.id) == 'show') { - $(this).collapse('show'); - } else { - $(this).siblings('a').addClass('collapsed'); - } - }); -} - -function keepScopesTreeState() { - const scopes_tree = $('#scopes .collapse') +function keepTreeState(selector) { + const tree = $(selector + ' .collapse'); - scopes_tree.on('hidden.bs.collapse', function (event) { + tree.on('hidden.bs.collapse', function (event) { event.stopPropagation(); localStorage.removeItem(this.id); }); - scopes_tree.on('shown.bs.collapse', function (event) { + tree.on('shown.bs.collapse', function (event) { event.stopPropagation(); localStorage.setItem(this.id, 'show'); }); - scopes_tree.each(function () { + tree.each(function () { if (localStorage.getItem(this.id) == 'show') { $(this).collapse('show'); } else { diff --git a/ogcp/templates/base.html b/ogcp/templates/base.html index b9042fd..c9b2d63 100644 --- a/ogcp/templates/base.html +++ b/ogcp/templates/base.html @@ -111,7 +111,7 @@ - +