storage.js: Just use a factory to create behavior storages

This commit is contained in:
Johannes Meyer 2019-07-04 10:21:50 +02:00
parent 3b7a1a5ab4
commit 95dee43f5b
2 changed files with 21 additions and 36 deletions

View File

@ -23,7 +23,7 @@
this.defaultVisibleHeight = 36;
this.state = new Icinga.Storage.StorageAwareSet.withStorage(
new Icinga.BehaviorStorage('collapsible'),
Icinga.Storage.BehaviorStorage('collapsible'),
'expanded'
)
.on('add', { self: this }, this.onExternalExpansion)

View File

@ -8,15 +8,17 @@
* Icinga.Storage
*
* localStorage access
*
* @param {string} prefix
*/
Icinga.Storage = function() {
Icinga.Storage = function(prefix) {
/**
* Namespace separator being used
* Prefix to use for keys
*
* @type {string}
*/
this.keySeparator = '.';
this.prefix = prefix;
/**
* Callbacks for storage events on particular keys
@ -28,17 +30,30 @@
this.setup();
};
/**
* Create a new storage with `behavior.<name>` as prefix
*
* @param {string} name
*
* @returns {Icinga.Storage}
*/
Icinga.Storage.BehaviorStorage = function(name) {
return new Icinga.Storage('behavior.' + name);
};
Icinga.Storage.prototype = {
/**
* Prefix the given key
*
* Base implementation, noop.
*
* @param {string} key
* @returns {string}
*/
prefixKey: function(key) {
if (typeof this.prefix !== 'undefined') {
return this.prefix + '.' + key;
}
return key;
},
@ -118,36 +133,6 @@
}
};
/**
* Icinga.BehaviorStorage
*
* @param {string} behaviorName
* @constructor
*/
Icinga.BehaviorStorage = function(behaviorName) {
/**
* The behavior's name
*
* @type {string}
*/
this.behaviorName = behaviorName;
Icinga.Storage.call(this);
};
Icinga.BehaviorStorage.prototype = Object.create(Icinga.Storage.prototype);
/**
* Prefix the given key with `behavior.<name>.`
*
* @param {string} key
*
* @returns {string}
*/
Icinga.BehaviorStorage.prototype.prefixKey = function(key) {
return 'behavior' + this.keySeparator + this.behaviorName + this.keySeparator + key;
};
/**
* Icinga.Storage.StorageAwareSet
*