From 41b105136df2c62e7d9c37dbd8ba227136853191 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 6 Apr 2018 13:54:51 +0200 Subject: [PATCH] Preserve collapsed FilterEditor subtrees across auto-refreshes refs #2964 --- library/Icinga/Web/JavaScript.php | 1 + public/js/icinga/behavior/filtereditor.js | 69 +++++++++++++++++++++++ public/js/icinga/events.js | 2 + 3 files changed, 72 insertions(+) create mode 100644 public/js/icinga/behavior/filtereditor.js diff --git a/library/Icinga/Web/JavaScript.php b/library/Icinga/Web/JavaScript.php index 802eac6e8..a26a863c3 100644 --- a/library/Icinga/Web/JavaScript.php +++ b/library/Icinga/Web/JavaScript.php @@ -34,6 +34,7 @@ class JavaScript 'js/icinga/behavior/actiontable.js', 'js/icinga/behavior/flyover.js', 'js/icinga/behavior/expandable.js', + 'js/icinga/behavior/filtereditor.js', 'js/icinga/behavior/selectable.js' ); diff --git a/public/js/icinga/behavior/filtereditor.js b/public/js/icinga/behavior/filtereditor.js new file mode 100644 index 000000000..5cd40e43b --- /dev/null +++ b/public/js/icinga/behavior/filtereditor.js @@ -0,0 +1,69 @@ +/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */ + +/** + * Icinga.Behavior.FilterEditor + * + * Initially expanded, but collapsable subtrees + */ +(function(Icinga, $) { + + 'use strict'; + + var containerId = /^col(\d+)$/; + var filterEditors = {}; + + function FilterEditor(icinga) { + Icinga.EventListener.call(this, icinga); + + this.on('beforerender', this.beforeRender, this); + this.on('rendered', this.onRendered, this); + } + + FilterEditor.prototype = new Icinga.EventListener(); + + FilterEditor.prototype.beforeRender = function(event) { + var $container = $(event.target); + var match = containerId.exec($container.attr('id')); + + if (match !== null) { + var id = match[1]; + var subTrees = {}; + filterEditors[id] = subTrees; + + $container.find('.tree .handle').each(function () { + var $li = $(this).closest('li'); + + subTrees[$li.find('select').first().attr('name')] = $li.hasClass('collapsed'); + }); + } + }; + + FilterEditor.prototype.onRendered = function(event) { + var $container = $(event.target); + var match = containerId.exec($container.attr('id')); + + if (match !== null) { + var id = match[1]; + + if (typeof filterEditors[id] !== "undefined") { + var subTrees = filterEditors[id]; + delete filterEditors[id]; + + $container.find('.tree .handle').each(function () { + var $li = $(this).closest('li'); + var name = $li.find('select').first().attr('name'); + if (typeof subTrees[name] !== "undefined" && subTrees[name] !== $li.hasClass('collapsed')) { + $li.toggleClass('collapsed'); + } + }); + + icinga.ui.fixControls($container); + } + } + }; + + Icinga.Behaviors = Icinga.Behaviors || {}; + + Icinga.Behaviors.FilterEditor = FilterEditor; + +}) (Icinga, jQuery); diff --git a/public/js/icinga/events.js b/public/js/icinga/events.js index 33dd04784..39456803d 100644 --- a/public/js/icinga/events.js +++ b/public/js/icinga/events.js @@ -163,6 +163,8 @@ } else { $parent.addClass('collapsed'); } + + icinga.ui.fixControls($parent.closest('.container')); }, onLoad: function (event) {