storage.js: Drop StorageAwareSet and replace it with StorageAwareMap

This commit is contained in:
Johannes Meyer 2019-07-09 11:35:41 +02:00
parent 8377a2d096
commit cbd1e1bb92
2 changed files with 109 additions and 53 deletions

View File

@ -22,7 +22,7 @@
this.defaultVisibleRows = 2; this.defaultVisibleRows = 2;
this.defaultVisibleHeight = 36; this.defaultVisibleHeight = 36;
this.state = new Icinga.Storage.StorageAwareSet.withStorage( this.state = new Icinga.Storage.StorageAwareMap.withStorage(
Icinga.Storage.BehaviorStorage('collapsible'), Icinga.Storage.BehaviorStorage('collapsible'),
'expanded' 'expanded'
) )
@ -132,7 +132,7 @@
_this.state.delete(collapsiblePath); _this.state.delete(collapsiblePath);
_this.collapse($collapsible); _this.collapse($collapsible);
} else { } else {
_this.state.add(collapsiblePath); _this.state.set(collapsiblePath, 1);
_this.expand($collapsible); _this.expand($collapsible);
} }
} }

View File

@ -47,6 +47,7 @@
* Prefix the given key * Prefix the given key
* *
* @param {string} key * @param {string} key
*
* @returns {string} * @returns {string}
*/ */
prefixKey: function(key) { prefixKey: function(key) {
@ -136,14 +137,14 @@
}; };
/** /**
* Icinga.Storage.StorageAwareSet * Icinga.Storage.StorageAwareMap
* *
* Emits events `StorageAwareSetDelete` and `StorageAwareSetAdd` in case an update occurs in the storage. * Emits events `StorageAwareMapDelete` and `StorageAwareMapAdd` in case an update occurs in the storage.
* *
* @param {Array} values * @param {object} items
* @constructor * @constructor
*/ */
Icinga.Storage.StorageAwareSet = function(values) { Icinga.Storage.StorageAwareMap = function(items) {
/** /**
* Storage object * Storage object
@ -160,36 +161,36 @@
this.key = undefined; this.key = undefined;
/** /**
* The internal (real) set * The internal (real) map
* *
* @type {Set<*>} * @type {Map<*>}
*/ */
this.data = new Set(); this.data = new Map();
// items is not passed directly because IE11 doesn't support constructor arguments // items is not passed directly because IE11 doesn't support constructor arguments
if (typeof values !== 'undefined' && !! values && values.length) { if (typeof items !== 'undefined' && !! items) {
values.forEach(function(value) { Object.keys(items).forEach(function(key) {
this.data.add(value); this.data.set(key, items[key]);
}, this); }, this);
} }
}; };
/** /**
* Create a new StorageAwareSet for the given storage and key * Create a new StorageAwareMap for the given storage and key
* *
* @param {Icinga.Storage} storage * @param {Icinga.Storage} storage
* @param {string} key * @param {string} key
* *
* @returns {Icinga.Storage.StorageAwareSet} * @returns {Icinga.Storage.StorageAwareMap}
*/ */
Icinga.Storage.StorageAwareSet.withStorage = function(storage, key) { Icinga.Storage.StorageAwareMap.withStorage = function(storage, key) {
return (new Icinga.Storage.StorageAwareSet(storage.get(key)).setStorage(storage, key)); return (new Icinga.Storage.StorageAwareMap(storage.get(key)).setStorage(storage, key));
}; };
Icinga.Storage.StorageAwareSet.prototype = { Icinga.Storage.StorageAwareMap.prototype = {
/** /**
* Bind this set to the given storage and key * Bind this map to the given storage and key
* *
* @param {Icinga.Storage} storage * @param {Icinga.Storage} storage
* @param {string} key * @param {string} key
@ -205,7 +206,7 @@
}, },
/** /**
* Return a boolean indicating this set got a storage * Return a boolean indicating this map got a storage
* *
* @returns {boolean} * @returns {boolean}
*/ */
@ -214,24 +215,24 @@
}, },
/** /**
* Update the set * Update the map
* *
* @param {Array} newValue * @param {object} newValue
*/ */
onChange: function(newValue) { onChange: function(newValue) {
// Check for deletions first // Check for deletions first
this.values().forEach(function (value) { this.keys().forEach(function (key) {
if (newValue.indexOf(value) < 0) { if (typeof newValue[key] === 'undefined') {
this.data.delete(value); this.data.delete(key);
$(window).trigger('StorageAwareSetDelete', value); $(window).trigger('StorageAwareMapDelete', key);
} }
}, this); }, this);
// Now check for new entries // Now check for new entries
newValue.forEach(function(value) { Object.keys(newValue).forEach(function(key) {
if (! this.data.has(value)) { if (! this.data.has(key)) {
this.data.add(value); this.data.set(key, newValue[key]);
$(window).trigger('StorageAwareSetAdd', value); $(window).trigger('StorageAwareMapAdd', key);
} }
}, this); }, this);
}, },
@ -249,7 +250,7 @@
*/ */
on: function(event, data, handler) { on: function(event, data, handler) {
$(window).on( $(window).on(
'StorageAwareSet' + event.charAt(0).toUpperCase() + event.slice(1), 'StorageAwareMap' + event.charAt(0).toUpperCase() + event.slice(1),
data, data,
handler handler
); );
@ -258,7 +259,7 @@
}, },
/** /**
* Return the number of (unique) elements in the set * Return the number of key/value pairs in the map
* *
* @returns {number} * @returns {number}
*/ */
@ -267,24 +268,25 @@
}, },
/** /**
* Append the given value to the end of the set * Set the value for the key in the map
* *
* @param value * @param {string} key
* @param {*} value
* *
* @returns {this} * @returns {this}
*/ */
add: function(value) { set: function(key, value) {
this.data.add(value); this.data.set(key, value);
if (this.hasStorage()) { if (this.hasStorage()) {
this.storage.set(this.key, this.values()); this.storage.set(this.key, this.toObject());
} }
return this; return this;
}, },
/** /**
* Remove all elements from the set * Remove all key/value pairs from the map
* *
* @returns {void} * @returns {void}
*/ */
@ -297,35 +299,43 @@
}, },
/** /**
* Remove the given value from the set * Remove the given key from the map
* *
* @param value * @param {string} key
* *
* @returns {boolean} * @returns {boolean}
*/ */
delete: function(value) { delete: function(key) {
var retVal = this.data.delete(value); var retVal = this.data.delete(key);
if (this.hasStorage()) { if (this.hasStorage()) {
this.storage.set(this.key, this.values()); this.storage.set(this.key, this.toObject());
} }
return retVal; return retVal;
}, },
/** /**
* Returns an iterable of [v,v] pairs for every value v in the set. * Return a list of [key, value] pairs for every item in the map
* *
* @returns {IterableIterator<[*, *]>} * @returns {Array}
*/ */
entries: function() { entries: function() {
return this.data.entries(); var list = [];
if (this.size > 0) {
this.forEach(function (value, key) {
list.push([key, value]);
});
}
return list;
}, },
/** /**
* Execute a provided function once for each value in the Set object, in insertion order. * Execute a provided function once for each item in the map, in insertion order
* *
* @param callback * @param {function} callback
* *
* @returns {void} * @returns {void}
*/ */
@ -334,18 +344,47 @@
}, },
/** /**
* Return a boolean indicating whether an element with the specified value exists in a Set object or not. * Return the value associated to the key, or undefined if there is none
* *
* @param value * @param {string} key
* *
* @returns {boolean} * @returns {*}
*/ */
has: function(value) { get: function(key) {
return this.data.has(value); return this.data.get(key);
}, },
/** /**
* Returns an array of values in the set. * Return a boolean asserting whether a value has been associated to the key in the map
*
* @param {string} key
*
* @returns {boolean}
*/
has: function(key) {
return this.data.has(key);
},
/**
* Return an array of keys in the map
*
* @returns {Array}
*/
keys: function() {
var list = [];
if (this.size > 0) {
// .forEach() is used because IE11 doesn't support .keys()
this.forEach(function(_, key) {
list.push(key);
});
}
return list;
},
/**
* Return an array of values in the map
* *
* @returns {Array} * @returns {Array}
*/ */
@ -360,6 +399,23 @@
} }
return list; return list;
},
/**
* Return this map as simple object
*
* @returns {object}
*/
toObject: function() {
var obj = {};
if (this.size > 0) {
this.forEach(function (value, key) {
obj[key] = value;
});
}
return obj;
} }
}; };