- NEW "plugin API" for custom UI modules, closed #913 - NEW live "Killstream" for killboard module, closed #909 - NEW "custom layout" UI settings, closed #470
55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
define([], () => {
|
|
'use strict';
|
|
|
|
/*
|
|
// Example usage --------------------------------------------------------------------------------------------------
|
|
// global accessible DataStore instance
|
|
window.dataStore = new DataStore();
|
|
|
|
// extend HTMLElement class with an interface to set/get data to it
|
|
HTMLElement.prototype.setData = function(key, value){
|
|
window.dataStore.set(this, key, value);
|
|
};
|
|
|
|
HTMLElement.prototype.getData = function(key){
|
|
return window.dataStore.get(this, key);
|
|
};
|
|
*/
|
|
|
|
/**
|
|
* Stores data to an object
|
|
* -> can be used as a replacement for jQuery $.data()
|
|
*/
|
|
return class DataStore {
|
|
constructor() {
|
|
this._store = new WeakMap();
|
|
}
|
|
|
|
set(obj, key, value) {
|
|
if (!this._store.has(obj)) {
|
|
this._store.set(obj, new Map());
|
|
}
|
|
this._store.get(obj).set(key, value);
|
|
return obj;
|
|
}
|
|
|
|
get(obj, key) {
|
|
return this._store.has(obj) && this._store.get(obj).get(key);
|
|
}
|
|
|
|
has(obj, key) {
|
|
return this._store.has(obj) && this._store.get(obj).has(key);
|
|
}
|
|
|
|
remove(obj, key) {
|
|
let ret = false;
|
|
if (this._store.has(obj)) {
|
|
ret = this._store.get(obj).delete(key);
|
|
if (!this._store.get(obj).size) {
|
|
this._store.delete(obj);
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
};
|
|
}); |