Skip to content

Commit

Permalink
Merge pull request #445 from statikbe/KarelJanVanHaute/issue427
Browse files Browse the repository at this point in the history
Add Ajax modal plugin to baseinstall
  • Loading branch information
emilyberghen authored Feb 12, 2025
2 parents c4ba0f5 + bc97aed commit a1921ee
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
79 changes: 79 additions & 0 deletions tailoff/js/plugins/modal/ajax.plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { ModalComponent } from '../../components/modal.component';
import { A11yUtils } from '../../utils/a11y';
import { Ajax } from '../../utils/ajax';
import { ArrayPrototypes } from '../../utils/prototypes/array.prototypes';
import { ModalPlugin } from './plugin.interface';

ArrayPrototypes.activateFrom();

export class AjaxModalPlugin implements ModalPlugin {
private triggerClass = 'js-modal-ajax';
private modalComponent: ModalComponent;

private options = {};

constructor(modalComponent: ModalComponent, options: Object = {}) {
this.options = { ...this.options, ...options };
this.modalComponent = modalComponent;
}

public initElement() {}

public getPluginName() {
return 'ajax';
}

public afterCreateModal() {
const closeModalButton = this.modalComponent.modalContent.querySelector('.js-close-modal');
if (closeModalButton) {
closeModalButton.addEventListener('click', () => {
this.modalComponent.closeModal();
});
}
}

public getTriggerClass() {
return this.triggerClass;
}

public getOptions() {
return this.options;
}

public openModalClick(trigger: HTMLElement) {
this.openPluginModal({ url: trigger.getAttribute('href'), callback: () => {} });
}

public gotoNextAction() {}

public gotoPrevAction() {}

public closeModal() {}

public openPluginModal({ url, callback }) {
this.modalComponent.createOverlay();
this.modalComponent.createModal('modal__dialog--ajax', 'modal__ajax');

this.modalComponent.modalLoader = document.createElement('div');
this.modalComponent.modalLoader.classList.add('modal__loader-wrapper');
this.modalComponent.modalLoader.insertAdjacentHTML('afterbegin', `<div class="modal__loader"></div>`);
this.modalComponent.modalContent.insertAdjacentElement('afterbegin', this.modalComponent.modalLoader);

const ajaxModelContent = document.createElement('div');
ajaxModelContent.classList.add('modal__ajax__content');

Ajax.call({
url: url,
method: 'GET',
success: (response) => {
ajaxModelContent.innerHTML = response;
this.modalComponent.modalLoader.classList.add('hidden');
this.modalComponent.modalContent.insertAdjacentElement('beforeend', ajaxModelContent);
this.afterCreateModal();
callback();
},
});

A11yUtils.keepFocus(this.modalComponent.modalContent);
}
}
4 changes: 3 additions & 1 deletion tailoff/js/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,12 @@ const components = [
},
{
name: 'modal',
selector: '.js-modal, .js-modal-image, .js-modal-video, .js-modal-confirmation',
className: 'ModalComponent',
selector: '.js-modal, .js-modal-image, .js-modal-video, .js-modal-confirmation, .js-modal-ajax',
plugins: [
{ path: 'modal', file: 'image.plugin', name: 'ImageModalPlugin' },
{ path: 'modal', file: 'video.plugin', name: 'VideoModalPlugin' },
{ path: 'modal', file: 'ajax.plugin', name: 'AjaxModalPlugin' },
{
path: 'modal',
file: 'confirmation.plugin',
Expand Down

0 comments on commit a1921ee

Please sign in to comment.