Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ajax modal plugin to baseinstall #445

Merged
merged 2 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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