ribs-popup
Version:
This module is used to display popup box with ajax or inline content
130 lines (106 loc) • 3.55 kB
JavaScript
import RibsCore from 'ribs-core';
class RibsPopup {
/**
* constructor add event listener on all element wich have
* data-ribspopup
*/
constructor() {
const links = document.querySelectorAll('[data-ribspopup]');
Array.from(links).forEach((element) => {
element.addEventListener('click', (event) => this.openPopup(event));
});
}
/**
* @param event
* method to open a popup with link clicked in event
*/
openPopup(event) {
event.preventDefault();
const link = event.currentTarget;
const popup = document.getElementById(event.currentTarget.dataset.popup);
if (event.currentTarget.dataset.ajax !== undefined) {
this.setContent(popup, event.currentTarget.dataset.ajax)
}
popup.classList.add('ribs-displayed');
document.body.classList.add('ribs-popup-body');
const dataClose = popup.querySelectorAll('.ribs-popup [data-close]');
const dataValidate = popup.querySelectorAll('.ribs-popup [data-validate]');
if (dataClose.length > 0) {
Array.from(dataClose).forEach((element) => {
element.addEventListener('click', (event) => this.closePopup(event));
});
}
if (dataValidate.length > 0) {
Array.from(dataValidate).forEach((element) => {
element.addEventListener('click', (event) => this.setActionValidate(event, link));
});
}
}
/**
* method to open a popup by a js call function
* @param popupId
*/
openJsPopup(popupId) {
const popup = document.getElementById(popupId);
popup.classList.add('ribs-displayed');
document.body.classList.add('ribs-popup-body');
const dataClose = popup.querySelectorAll('.ribs-popup [data-close]');
const dataValidate = popup.querySelectorAll('.ribs-popup [data-validate]');
if (dataClose.length > 0) {
Array.from(dataClose).forEach((element) => {
element.addEventListener('click', (event) => this.closePopup(event));
});
}
if (dataValidate.length > 0) {
Array.from(dataValidate).forEach((element) => {
element.addEventListener('click', (event) => this.setActionValidate(event, null));
});
}
}
/**
* @param event
* @param link
* method that trigger action to do on click on data-validate button
* or send back on an url with data-href or submit a form with data-form
*/
setActionValidate(event, link) {
if (link.dataset.href !== null && link.dataset.href !== undefined) {
event.currentTarget.href = link.dataset.href;
} else if (link.dataset.form !== null && link.dataset.form !== undefined) {
document.getElementById(link.dataset.form).submit();
} else {
this.closePopup(event);
}
}
/**
* @param event
* method to close a popup
*/
closePopup(event) {
event.preventDefault();
const popup = RibsCore.parents(event.currentTarget, '.ribs-popup');
if (popup !== null) {
popup.classList.remove('ribs-displayed');
document.body.classList.remove('ribs-popup-body');
}
}
/**
* @param popup
* @param ajaxUrl
* mathod that add the content of ajax request to the popup
*/
setContent(popup, ajaxUrl) {
const popupContent = popup.querySelector('#set-content');
const request = new Request(ajaxUrl, {
method: 'POST',
credentials: 'same-origin',
});
fetch (request)
.then(response => response.text())
.then((result) => {
popupContent.innerHTML = result;
});
}
}
export default (RibsPopup);
const popup = new RibsPopup();