2023-10-05 12:11:17 +00:00
|
|
|
var ResourceDisplays = {};
|
|
|
|
|
2023-10-09 12:21:31 +00:00
|
|
|
ResourceDisplays.autoInit = () => {
|
|
|
|
console.log('ResourceDisplays.autoInit');
|
|
|
|
for (let propertyName in ResourceDisplays) {
|
|
|
|
let property = ResourceDisplays[propertyName];
|
|
|
|
// Call autoInit of all properties that are subclasses of `ResourceDisplays.BaseDisplay`.
|
|
|
|
// This does not include `ResourceDisplays.BaseDisplay` itself.
|
|
|
|
if (property.prototype instanceof ResourceDisplays.BaseDisplay) {
|
|
|
|
console.log(property);
|
|
|
|
// Check if the static `htmlClass` property is defined.
|
|
|
|
if (property.htmlClass === undefined) {return;}
|
|
|
|
// Gather all HTML elements that have the `this.htmlClass` class
|
|
|
|
// and do not have the `no-autoinit` class.
|
|
|
|
let displayElements = document.querySelectorAll(`.${property.htmlClass}:not(.no-autoinit)`);
|
|
|
|
console.log(displayElements);
|
|
|
|
// Create an instance of this class for each display element.
|
|
|
|
for (let displayElement of displayElements) {new property(displayElement);}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-05 12:11:17 +00:00
|
|
|
ResourceDisplays.BaseDisplay = class BaseDisplay {
|
2023-10-09 12:21:31 +00:00
|
|
|
static htmlClass;
|
|
|
|
|
2021-01-11 12:36:45 +00:00
|
|
|
constructor(displayElement) {
|
|
|
|
this.displayElement = displayElement;
|
2021-11-30 15:22:16 +00:00
|
|
|
this.userId = this.displayElement.dataset.userId;
|
2022-07-08 09:46:47 +00:00
|
|
|
this.isInitialized = false;
|
|
|
|
if (this.userId) {
|
2022-09-02 11:07:30 +00:00
|
|
|
app.subscribeUser(this.userId)
|
|
|
|
.then((response) => {
|
|
|
|
app.socket.on('PATCH', (patch) => {
|
|
|
|
if (this.isInitialized) {this.onPatch(patch);}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
app.getUser(this.userId)
|
|
|
|
.then((user) => {
|
|
|
|
this.init(user);
|
|
|
|
this.isInitialized = true;
|
|
|
|
});
|
2022-07-08 09:46:47 +00:00
|
|
|
}
|
2021-01-11 12:36:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 15:22:16 +00:00
|
|
|
init(user) {throw 'Not implemented';}
|
2021-01-11 12:36:45 +00:00
|
|
|
|
2022-09-02 11:07:30 +00:00
|
|
|
onPatch(patch) {throw 'Not implemented';}
|
2021-01-11 12:36:45 +00:00
|
|
|
|
|
|
|
setElement(element, value) {
|
|
|
|
switch (element.tagName) {
|
2022-09-02 11:07:30 +00:00
|
|
|
case 'INPUT': {
|
2021-01-11 12:36:45 +00:00
|
|
|
element.value = value;
|
|
|
|
M.updateTextFields();
|
|
|
|
break;
|
2022-09-02 11:07:30 +00:00
|
|
|
}
|
|
|
|
default: {
|
2021-01-11 12:36:45 +00:00
|
|
|
element.innerText = value;
|
|
|
|
break;
|
2022-09-02 11:07:30 +00:00
|
|
|
}
|
2021-01-11 12:36:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-01 13:15:20 +00:00
|
|
|
|
|
|
|
setElements(elements, value) {
|
2022-09-02 11:07:30 +00:00
|
|
|
for (let element of elements) {
|
2021-12-01 13:15:20 +00:00
|
|
|
this.setElement(element, value);
|
|
|
|
}
|
|
|
|
}
|
2023-10-05 12:11:17 +00:00
|
|
|
};
|