collapsible.js: Make storage working with multiple tabs

This commit is contained in:
Johannes Meyer 2019-06-25 14:36:03 +02:00
parent a642117c8a
commit 754f45566a
1 changed files with 36 additions and 20 deletions

View File

@ -18,11 +18,9 @@
this.on('click', '.collapsible + .collapsible-control', this.onControlClicked, this); this.on('click', '.collapsible + .collapsible-control', this.onControlClicked, this);
this.icinga = icinga; this.icinga = icinga;
this.expanded = new Set(); this.state = new StateStorage();
this.defaultVisibleRows = 2; this.defaultVisibleRows = 2;
this.defaultVisibleHeight = 36; this.defaultVisibleHeight = 36;
this.loadStorage();
}; };
Collapsible.prototype = new Icinga.EventListener(); Collapsible.prototype = new Icinga.EventListener();
@ -43,7 +41,7 @@
$collapsible.after($('#collapsible-control-ghost').clone().removeAttr('id')); $collapsible.after($('#collapsible-control-ghost').clone().removeAttr('id'));
$collapsible.addClass('can-collapse'); $collapsible.addClass('can-collapse');
if (! _this.expanded.has(collapsiblePath)) { if (! _this.state.isExpanded(collapsiblePath)) {
_this.collapse($collapsible); _this.collapse($collapsible);
} }
} }
@ -64,11 +62,11 @@
_this.icinga.logger.error('[Collapsible] Collapsible control has no associated .collapsible: ', $target); _this.icinga.logger.error('[Collapsible] Collapsible control has no associated .collapsible: ', $target);
} else { } else {
var collapsiblePath = _this.icinga.utils.getCSSPath($collapsible); var collapsiblePath = _this.icinga.utils.getCSSPath($collapsible);
if (_this.expanded.has(collapsiblePath)) { if (_this.state.isExpanded(collapsiblePath)) {
_this.expanded.delete(collapsiblePath); _this.state.collapse(collapsiblePath);
_this.collapse($collapsible); _this.collapse($collapsible);
} else { } else {
_this.expanded.add(collapsiblePath); _this.state.expand(collapsiblePath);
_this.expand($collapsible); _this.expand($collapsible);
} }
} }
@ -140,27 +138,47 @@
$collapsible.css({display: '', height: ''}); $collapsible.css({display: '', height: ''});
}; };
/** Icinga.Behaviors.Collapsible = Collapsible;
* Load state from storage
*/ // State-Storage abstraction, not for use externally until we've had time to think this properly through
Collapsible.prototype.loadStorage = function() {
var StateStorage = function() {};
StateStorage.prototype.isExpanded = function(selector) {
return this.load().has(selector);
};
StateStorage.prototype.expand = function(selector) {
var set = this.load();
set.add(selector);
this.save(set);
};
StateStorage.prototype.collapse = function(selector) {
var set = this.load();
set.delete(selector);
this.save(set);
};
StateStorage.prototype.load = function () {
var set = new Set();
var expanded = localStorage.getItem('behavior.collapsible.expanded'); var expanded = localStorage.getItem('behavior.collapsible.expanded');
if (!! expanded) { if (!! expanded) {
// .forEach() is used because IE11 doesn't support constructor arguments // .forEach() is used because IE11 doesn't support constructor arguments
JSON.parse(expanded).forEach(function(value) { JSON.parse(expanded).forEach(function(value) {
this.expanded.add(value); set.add(value);
}, this); }, this);
} }
return set;
}; };
/** StateStorage.prototype.save = function(set) {
* Save state to storage if (set.size > 0) {
*/
Collapsible.prototype.destroy = function() {
if (this.expanded.size > 0) {
var expanded = []; var expanded = [];
// .forEach() is used because IE11 doesn't support .values() // .forEach() is used because IE11 doesn't support .values()
this.expanded.forEach(function(value) { set.forEach(function(value) {
expanded.push(value); expanded.push(value);
}); });
@ -170,6 +188,4 @@
} }
}; };
Icinga.Behaviors.Collapsible = Collapsible;
})(Icinga, jQuery); })(Icinga, jQuery);