Better auto initialization method for forms and resource displays

This commit is contained in:
Patrick Jentsch 2023-10-09 14:21:31 +02:00
parent 1b974f0bbc
commit e20dd01710
10 changed files with 42 additions and 26 deletions

View File

@ -1,10 +1,5 @@
Forms.CreateContributionForm = class CreateContributionForm extends Forms.BaseForm { Forms.CreateContributionForm = class CreateContributionForm extends Forms.BaseForm {
static autoInit() { static htmlClass = 'create-contribution-form';
let createContributionFormElements = document.querySelectorAll('.create-contribution-form');
for (let createContributionFormElement of createContributionFormElements) {
new Forms.CreateContributionForm(createContributionFormElement);
}
}
constructor(formElement) { constructor(formElement) {
super(formElement); super(formElement);

View File

@ -1,10 +1,5 @@
Forms.CreateCorpusFileForm = class CreateCorpusFileForm extends Forms.BaseForm { Forms.CreateCorpusFileForm = class CreateCorpusFileForm extends Forms.BaseForm {
static autoInit() { static htmlClass = 'create-corpus-file-form';
let createCorpusFileFormElements = document.querySelectorAll('.create-corpus-file-form');
for (let createCorpusFileFormElement of createCorpusFileFormElements) {
new Forms.CreateCorpusFileForm(createCorpusFileFormElement);
}
}
constructor(formElement) { constructor(formElement) {
super(formElement); super(formElement);

View File

@ -1,10 +1,5 @@
Forms.CreateJobForm = class CreateJobForm extends Forms.BaseForm { Forms.CreateJobForm = class CreateJobForm extends Forms.BaseForm {
static autoInit() { static htmlClass = 'create-job-form';
let createJobFormElements = document.querySelectorAll('.create-job-form');
for (let createJobFormElement of createJobFormElements) {
new Forms.CreateJobForm(createJobFormElement);
}
}
constructor(formElement) { constructor(formElement) {
super(formElement); super(formElement);

View File

@ -3,15 +3,22 @@ var Forms = {};
Forms.autoInit = () => { Forms.autoInit = () => {
for (let propertyName in Forms) { for (let propertyName in Forms) {
let property = Forms[propertyName]; let property = Forms[propertyName];
// Call the autoInit method of all properties that are subclasses of Forms.BaseForm // Call autoInit of all properties that are subclasses of Forms.BaseForm.
// This does not include Forms.BaseForm itself.
if (property.prototype instanceof Forms.BaseForm) { if (property.prototype instanceof Forms.BaseForm) {
property.autoInit(); // 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 formElements = document.querySelectorAll(`.${property.htmlClass}:not(.no-autoinit)`);
// Create an instance of this class for each form element.
for (let formElement of formElements) {new property(formElement);}
} }
} }
}; };
Forms.BaseForm = class BaseForm { Forms.BaseForm = class BaseForm {
static autoInit() {throw 'Not implemented';} static htmlClass;
constructor(formElement) { constructor(formElement) {
this.formElement = formElement; this.formElement = formElement;

View File

@ -1,4 +1,6 @@
ResourceDisplays.CorpusDisplay = class CorpusDisplay extends ResourceDisplays.BaseDisplay { ResourceDisplays.CorpusDisplay = class CorpusDisplay extends ResourceDisplays.BaseDisplay {
static htmlClass = 'corpus-display';
constructor(displayElement) { constructor(displayElement) {
super(displayElement); super(displayElement);
this.corpusId = displayElement.dataset.corpusId; this.corpusId = displayElement.dataset.corpusId;

View File

@ -1,6 +1,28 @@
var ResourceDisplays = {}; var ResourceDisplays = {};
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);}
}
}
}
ResourceDisplays.BaseDisplay = class BaseDisplay { ResourceDisplays.BaseDisplay = class BaseDisplay {
static htmlClass;
constructor(displayElement) { constructor(displayElement) {
this.displayElement = displayElement; this.displayElement = displayElement;
this.userId = this.displayElement.dataset.userId; this.userId = this.displayElement.dataset.userId;

View File

@ -1,4 +1,6 @@
ResourceDisplays.JobDisplay = class JobDisplay extends ResourceDisplays.BaseDisplay { ResourceDisplays.JobDisplay = class JobDisplay extends ResourceDisplays.BaseDisplay {
static htmlClass = 'job-display';
constructor(displayElement) { constructor(displayElement) {
super(displayElement); super(displayElement);
this.jobId = this.displayElement.dataset.jobId; this.jobId = this.displayElement.dataset.jobId;

View File

@ -140,6 +140,7 @@
document.querySelectorAll('#nav-more-dropdown-trigger'), document.querySelectorAll('#nav-more-dropdown-trigger'),
{alignment: 'right', constrainWidth: false, coverTrigger: false} {alignment: 'right', constrainWidth: false, coverTrigger: false}
); );
ResourceDisplays.autoInit();
ResourceList.autoInit(); ResourceList.autoInit();
Forms.autoInit(); Forms.autoInit();

View File

@ -5,7 +5,7 @@
{% block page_content %} {% block page_content %}
<div class="container"> <div class="container">
<div class="row" data-corpus-id="{{ corpus.hashid }}" data-user-id="{{ corpus.user.hashid }}" id="corpus-display"> <div class="row corpus-display" data-corpus-id="{{ corpus.hashid }}" data-user-id="{{ corpus.user.hashid }}">
<div class="col s12"> <div class="col s12">
<h1>{{ corpus.title }}</h1> <h1>{{ corpus.title }}</h1>
</div> </div>
@ -237,8 +237,6 @@
{% block scripts %} {% block scripts %}
{{ super() }} {{ super() }}
<script> <script>
let corpusDisplay = new ResourceDisplays.CorpusDisplay(document.querySelector('#corpus-display'));
{# {% if current_user.is_following_corpus(corpus) %} {# {% if current_user.is_following_corpus(corpus) %}
let unfollowRequestElement = document.querySelector('.action-button[data-action="unfollow-request"]'); let unfollowRequestElement = document.querySelector('.action-button[data-action="unfollow-request"]');
unfollowRequestElement.addEventListener('click', () => { unfollowRequestElement.addEventListener('click', () => {

View File

@ -5,7 +5,7 @@
{% block page_content %} {% block page_content %}
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<div class="col s12" data-job-id="{{ job.hashid }}" data-user-id="{{ job.user.hashid }}" id="job-display"> <div class="col s12 job-display" data-job-id="{{ job.hashid }}" data-user-id="{{ job.user.hashid }}">
<div class="row"> <div class="row">
<div class="col s8 m9 l10"> <div class="col s8 m9 l10">
<h1 id="title"><i style="font-size: inherit;" class="nopaque-icons service-icons" data-service="{{ job.service }}"></i> <span class="job-title"></span></h1> <h1 id="title"><i style="font-size: inherit;" class="nopaque-icons service-icons" data-service="{{ job.service }}"></i> <span class="job-title"></span></h1>
@ -150,7 +150,6 @@
{% block scripts %} {% block scripts %}
{{ super() }} {{ super() }}
<script> <script>
let jobDisplay = new ResourceDisplays.JobDisplay(document.querySelector('#job-display'));
let deleteJobRequestElement = document.querySelector('#delete-job-request'); let deleteJobRequestElement = document.querySelector('#delete-job-request');
let restartJobRequestElement = document.querySelector('#restart-job-request'); let restartJobRequestElement = document.querySelector('#restart-job-request');
deleteJobRequestElement.addEventListener('click', (event) => { deleteJobRequestElement.addEventListener('click', (event) => {