2016-04-05 18:35:02 +02:00
|
|
|
/*global H5P*/
|
|
|
|
H5P.ConfirmationDialog = (function (EventDispatcher) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a confirmation dialog
|
|
|
|
*
|
|
|
|
* @param [options] Options for confirmation dialog
|
2016-04-25 13:17:24 +02:00
|
|
|
* @param [options.instance] Instance that uses confirmation dialog
|
2016-04-05 18:35:02 +02:00
|
|
|
* @param [options.headerText] Header text
|
|
|
|
* @param [options.dialogText] Dialog text
|
|
|
|
* @param [options.cancelText] Cancel dialog button text
|
|
|
|
* @param [options.confirmText] Confirm dialog button text
|
2019-04-03 09:43:09 +02:00
|
|
|
* @param [options.hideCancel] Hide cancel button
|
|
|
|
* @param [options.hideExit] Hide exit button
|
2019-04-05 16:11:19 +02:00
|
|
|
* @param [options.skipRestoreFocus] Skip restoring focus when hiding the dialog
|
2019-04-03 09:43:09 +02:00
|
|
|
* @param [options.classes] Extra classes for popup
|
2016-04-05 18:35:02 +02:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function ConfirmationDialog(options) {
|
|
|
|
EventDispatcher.call(this);
|
|
|
|
var self = this;
|
2016-06-09 15:26:15 +02:00
|
|
|
|
2016-06-09 13:18:09 +02:00
|
|
|
// Make sure confirmation dialogs have unique id
|
|
|
|
H5P.ConfirmationDialog.uniqueId += 1;
|
|
|
|
var uniqueId = H5P.ConfirmationDialog.uniqueId;
|
2016-04-05 18:35:02 +02:00
|
|
|
|
|
|
|
// Default options
|
|
|
|
options = options || {};
|
2016-04-07 14:51:56 +02:00
|
|
|
options.headerText = options.headerText || H5P.t('confirmDialogHeader');
|
|
|
|
options.dialogText = options.dialogText || H5P.t('confirmDialogBody');
|
|
|
|
options.cancelText = options.cancelText || H5P.t('cancelLabel');
|
|
|
|
options.confirmText = options.confirmText || H5P.t('confirmLabel');
|
|
|
|
|
2016-05-23 14:34:56 +02:00
|
|
|
/**
|
|
|
|
* Handle confirming event
|
|
|
|
* @param {Event} e
|
|
|
|
*/
|
|
|
|
function dialogConfirmed(e) {
|
|
|
|
self.hide();
|
|
|
|
self.trigger('confirmed');
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle dialog canceled
|
|
|
|
* @param {Event} e
|
|
|
|
*/
|
|
|
|
function dialogCanceled(e) {
|
|
|
|
self.hide();
|
|
|
|
self.trigger('canceled');
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
|
2016-06-07 11:04:21 +02:00
|
|
|
/**
|
|
|
|
* Flow focus to element
|
|
|
|
* @param {HTMLElement} element Next element to be focused
|
|
|
|
* @param {Event} e Original tab event
|
|
|
|
*/
|
|
|
|
function flowTo(element, e) {
|
|
|
|
element.focus();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
|
2016-04-07 14:51:56 +02:00
|
|
|
// Offset of exit button
|
|
|
|
var exitButtonOffset = 2 * 16;
|
|
|
|
var shadowOffset = 8;
|
2016-04-05 18:35:02 +02:00
|
|
|
|
2016-04-25 13:17:24 +02:00
|
|
|
// Determine if we are too large for our container and must resize
|
|
|
|
var resizeIFrame = false;
|
|
|
|
|
2016-04-05 18:35:02 +02:00
|
|
|
// Create background
|
|
|
|
var popupBackground = document.createElement('div');
|
|
|
|
popupBackground.classList
|
|
|
|
.add('h5p-confirmation-dialog-background', 'hidden', 'hiding');
|
|
|
|
|
|
|
|
// Create outer popup
|
|
|
|
var popup = document.createElement('div');
|
|
|
|
popup.classList.add('h5p-confirmation-dialog-popup', 'hidden');
|
2019-04-03 09:43:09 +02:00
|
|
|
if (options.classes) {
|
|
|
|
options.classes.forEach(function (popupClass) {
|
|
|
|
popup.classList.add(popupClass);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-07 11:04:21 +02:00
|
|
|
popup.setAttribute('role', 'dialog');
|
2016-06-09 13:18:09 +02:00
|
|
|
popup.setAttribute('aria-labelledby', 'h5p-confirmation-dialog-dialog-text-' + uniqueId);
|
2016-04-05 18:35:02 +02:00
|
|
|
popupBackground.appendChild(popup);
|
2016-06-07 15:09:59 +02:00
|
|
|
popup.addEventListener('keydown', function (e) {
|
2016-06-07 11:04:21 +02:00
|
|
|
if (e.which === 27) {// Esc key
|
|
|
|
// Exit dialog
|
|
|
|
dialogCanceled(e);
|
|
|
|
}
|
2016-06-07 15:09:59 +02:00
|
|
|
});
|
2016-04-05 18:35:02 +02:00
|
|
|
|
|
|
|
// Popup header
|
|
|
|
var header = document.createElement('div');
|
|
|
|
header.classList.add('h5p-confirmation-dialog-header');
|
|
|
|
popup.appendChild(header);
|
|
|
|
|
|
|
|
// Header text
|
|
|
|
var headerText = document.createElement('div');
|
|
|
|
headerText.classList.add('h5p-confirmation-dialog-header-text');
|
|
|
|
headerText.innerHTML = options.headerText;
|
|
|
|
header.appendChild(headerText);
|
|
|
|
|
|
|
|
// Popup body
|
|
|
|
var body = document.createElement('div');
|
|
|
|
body.classList.add('h5p-confirmation-dialog-body');
|
|
|
|
popup.appendChild(body);
|
|
|
|
|
|
|
|
// Popup text
|
|
|
|
var text = document.createElement('div');
|
|
|
|
text.classList.add('h5p-confirmation-dialog-text');
|
|
|
|
text.innerHTML = options.dialogText;
|
2016-06-09 13:18:09 +02:00
|
|
|
text.id = 'h5p-confirmation-dialog-dialog-text-' + uniqueId;
|
2016-04-05 18:35:02 +02:00
|
|
|
body.appendChild(text);
|
|
|
|
|
|
|
|
// Popup buttons
|
|
|
|
var buttons = document.createElement('div');
|
|
|
|
buttons.classList.add('h5p-confirmation-dialog-buttons');
|
|
|
|
body.appendChild(buttons);
|
|
|
|
|
|
|
|
// Cancel button
|
2016-05-23 14:34:56 +02:00
|
|
|
var cancelButton = document.createElement('button');
|
2016-04-05 18:35:02 +02:00
|
|
|
cancelButton.classList.add('h5p-core-cancel-button');
|
|
|
|
cancelButton.textContent = options.cancelText;
|
2016-06-07 11:04:21 +02:00
|
|
|
|
|
|
|
// Confirm button
|
|
|
|
var confirmButton = document.createElement('button');
|
2017-04-07 10:28:17 +02:00
|
|
|
confirmButton.classList.add('h5p-core-button');
|
|
|
|
confirmButton.classList.add('h5p-confirmation-dialog-confirm-button');
|
2016-06-07 11:04:21 +02:00
|
|
|
confirmButton.textContent = options.confirmText;
|
|
|
|
|
|
|
|
// Exit button
|
|
|
|
var exitButton = document.createElement('button');
|
|
|
|
exitButton.classList.add('h5p-confirmation-dialog-exit');
|
2016-06-09 11:26:11 +02:00
|
|
|
exitButton.setAttribute('aria-hidden', 'true');
|
2016-06-09 15:26:15 +02:00
|
|
|
exitButton.tabIndex = -1;
|
2016-06-07 11:04:21 +02:00
|
|
|
exitButton.title = options.cancelText;
|
|
|
|
|
|
|
|
// Cancel handler
|
2016-06-07 15:09:59 +02:00
|
|
|
cancelButton.addEventListener('click', dialogCanceled);
|
|
|
|
cancelButton.addEventListener('keydown', function (e) {
|
2016-04-07 14:51:56 +02:00
|
|
|
if (e.which === 32) { // Space
|
2016-05-23 14:34:56 +02:00
|
|
|
dialogCanceled(e);
|
2016-04-07 14:51:56 +02:00
|
|
|
}
|
2016-06-07 11:04:21 +02:00
|
|
|
else if (e.which === 9 && e.shiftKey) { // Shift-tab
|
2016-06-09 15:26:15 +02:00
|
|
|
flowTo(confirmButton, e);
|
2016-06-07 11:04:21 +02:00
|
|
|
}
|
2016-06-07 15:09:59 +02:00
|
|
|
});
|
2019-04-03 09:43:09 +02:00
|
|
|
|
|
|
|
if (!options.hideCancel) {
|
|
|
|
buttons.appendChild(cancelButton);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Center buttons
|
|
|
|
buttons.classList.add('center');
|
|
|
|
}
|
2016-04-05 18:35:02 +02:00
|
|
|
|
2016-06-07 11:04:21 +02:00
|
|
|
// Confirm handler
|
2016-06-07 15:09:59 +02:00
|
|
|
confirmButton.addEventListener('click', dialogConfirmed);
|
|
|
|
confirmButton.addEventListener('keydown', function (e) {
|
2016-04-07 14:51:56 +02:00
|
|
|
if (e.which === 32) { // Space
|
2016-05-23 14:34:56 +02:00
|
|
|
dialogConfirmed(e);
|
2016-04-07 14:51:56 +02:00
|
|
|
}
|
2016-06-09 15:26:15 +02:00
|
|
|
else if (e.which === 9 && !e.shiftKey) { // Tab
|
2019-04-03 09:43:09 +02:00
|
|
|
const nextButton = !options.hideCancel ? cancelButton : confirmButton;
|
|
|
|
flowTo(nextButton, e);
|
2016-06-09 15:26:15 +02:00
|
|
|
}
|
2016-06-07 15:09:59 +02:00
|
|
|
});
|
2016-04-05 18:35:02 +02:00
|
|
|
buttons.appendChild(confirmButton);
|
|
|
|
|
2016-06-07 11:04:21 +02:00
|
|
|
// Exit handler
|
2016-06-07 15:09:59 +02:00
|
|
|
exitButton.addEventListener('click', dialogCanceled);
|
|
|
|
exitButton.addEventListener('keydown', function (e) {
|
2016-04-07 14:51:56 +02:00
|
|
|
if (e.which === 32) { // Space
|
2016-05-23 14:34:56 +02:00
|
|
|
dialogCanceled(e);
|
2016-04-07 14:51:56 +02:00
|
|
|
}
|
2016-06-07 15:09:59 +02:00
|
|
|
});
|
2019-04-03 09:43:09 +02:00
|
|
|
if (!options.hideExit) {
|
|
|
|
popup.appendChild(exitButton);
|
|
|
|
}
|
2016-04-05 18:35:02 +02:00
|
|
|
|
|
|
|
// Wrapper element
|
|
|
|
var wrapperElement;
|
|
|
|
|
2016-06-07 14:31:41 +02:00
|
|
|
// Focus capturing
|
|
|
|
var focusPredator;
|
|
|
|
|
2016-06-08 11:06:03 +02:00
|
|
|
// Maintains hidden state of elements
|
|
|
|
var wrapperSiblingsHidden = [];
|
|
|
|
var popupSiblingsHidden = [];
|
|
|
|
|
|
|
|
// Element with focus before dialog
|
|
|
|
var previouslyFocused;
|
2016-06-09 11:26:11 +02:00
|
|
|
|
2016-04-05 18:35:02 +02:00
|
|
|
/**
|
2016-05-31 10:19:07 +02:00
|
|
|
* Set parent of confirmation dialog
|
2016-04-05 18:35:02 +02:00
|
|
|
* @param {HTMLElement} wrapper
|
|
|
|
* @returns {H5P.ConfirmationDialog}
|
|
|
|
*/
|
|
|
|
this.appendTo = function (wrapper) {
|
|
|
|
wrapperElement = wrapper;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2016-06-07 14:31:41 +02:00
|
|
|
/**
|
|
|
|
* Capture the focus element, send it to confirmation button
|
|
|
|
* @param {Event} e Original focus event
|
|
|
|
*/
|
|
|
|
var captureFocus = function (e) {
|
|
|
|
if (!popupBackground.contains(e.target)) {
|
|
|
|
e.preventDefault();
|
|
|
|
confirmButton.focus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-08 11:06:03 +02:00
|
|
|
/**
|
|
|
|
* Hide siblings of element from assistive technology
|
2016-06-09 11:26:11 +02:00
|
|
|
*
|
2016-06-08 11:06:03 +02:00
|
|
|
* @param {HTMLElement} element
|
|
|
|
* @returns {Array} The previous hidden state of all siblings
|
|
|
|
*/
|
|
|
|
var hideSiblings = function (element) {
|
|
|
|
var hiddenSiblings = [];
|
|
|
|
var siblings = element.parentNode.children;
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < siblings.length; i += 1) {
|
|
|
|
// Preserve hidden state
|
|
|
|
hiddenSiblings[i] = siblings[i].getAttribute('aria-hidden') ?
|
|
|
|
true : false;
|
|
|
|
|
|
|
|
if (siblings[i] !== element) {
|
|
|
|
siblings[i].setAttribute('aria-hidden', true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hiddenSiblings;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restores assistive technology state of element's siblings
|
2016-06-09 11:26:11 +02:00
|
|
|
*
|
2016-06-08 11:06:03 +02:00
|
|
|
* @param {HTMLElement} element
|
|
|
|
* @param {Array} hiddenSiblings Hidden state of all siblings
|
|
|
|
*/
|
|
|
|
var restoreSiblings = function (element, hiddenSiblings) {
|
|
|
|
var siblings = element.parentNode.children;
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < siblings.length; i += 1) {
|
|
|
|
if (siblings[i] !== element && !hiddenSiblings[i]) {
|
|
|
|
siblings[i].removeAttribute('aria-hidden');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-07 14:31:41 +02:00
|
|
|
/**
|
|
|
|
* Start capturing focus of parent and send it to dialog
|
|
|
|
*/
|
|
|
|
var startCapturingFocus = function () {
|
|
|
|
focusPredator = wrapperElement.parentNode || wrapperElement;
|
|
|
|
focusPredator.addEventListener('focus', captureFocus, true);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up event listener for capturing focus
|
|
|
|
*/
|
|
|
|
var stopCapturingFocus = function () {
|
2016-06-08 11:06:03 +02:00
|
|
|
focusPredator.removeAttribute('aria-hidden');
|
2016-06-07 14:31:41 +02:00
|
|
|
focusPredator.removeEventListener('focus', captureFocus, true);
|
|
|
|
};
|
|
|
|
|
2016-06-08 11:06:03 +02:00
|
|
|
/**
|
|
|
|
* Hide siblings in underlay from assistive technologies
|
|
|
|
*/
|
|
|
|
var disableUnderlay = function () {
|
|
|
|
wrapperSiblingsHidden = hideSiblings(wrapperElement);
|
|
|
|
popupSiblingsHidden = hideSiblings(popupBackground);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore state of underlay for assistive technologies
|
|
|
|
*/
|
|
|
|
var restoreUnderlay = function () {
|
|
|
|
restoreSiblings(wrapperElement, wrapperSiblingsHidden);
|
|
|
|
restoreSiblings(popupBackground, popupSiblingsHidden);
|
|
|
|
};
|
|
|
|
|
2016-04-05 18:35:02 +02:00
|
|
|
/**
|
|
|
|
* Fit popup to container. Makes sure it doesn't overflow.
|
2016-05-04 11:53:46 +02:00
|
|
|
* @params {number} [offsetTop] Offset of popup
|
2016-04-05 18:35:02 +02:00
|
|
|
*/
|
2016-05-04 11:53:46 +02:00
|
|
|
var fitToContainer = function (offsetTop) {
|
2016-04-07 14:51:56 +02:00
|
|
|
var popupOffsetTop = parseInt(popup.style.top, 10);
|
2019-04-03 16:09:53 +02:00
|
|
|
if (offsetTop !== undefined) {
|
2016-05-04 11:53:46 +02:00
|
|
|
popupOffsetTop = offsetTop;
|
|
|
|
}
|
2016-04-05 18:35:02 +02:00
|
|
|
|
2019-04-03 16:09:53 +02:00
|
|
|
if (!popupOffsetTop) {
|
|
|
|
popupOffsetTop = 0;
|
|
|
|
}
|
|
|
|
|
2016-04-07 14:51:56 +02:00
|
|
|
// Overflows height
|
|
|
|
if (popupOffsetTop + popup.offsetHeight > wrapperElement.offsetHeight) {
|
2016-04-25 13:17:24 +02:00
|
|
|
popupOffsetTop = wrapperElement.offsetHeight - popup.offsetHeight - shadowOffset;
|
2016-04-05 18:35:02 +02:00
|
|
|
}
|
2016-04-07 14:51:56 +02:00
|
|
|
|
|
|
|
if (popupOffsetTop - exitButtonOffset <= 0) {
|
2016-04-25 13:17:24 +02:00
|
|
|
popupOffsetTop = exitButtonOffset + shadowOffset;
|
|
|
|
|
|
|
|
// We are too big and must resize
|
|
|
|
resizeIFrame = true;
|
2016-04-07 14:51:56 +02:00
|
|
|
}
|
|
|
|
popup.style.top = popupOffsetTop + 'px';
|
2016-04-05 18:35:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show confirmation dialog
|
|
|
|
* @params {number} offsetTop Offset top
|
|
|
|
* @returns {H5P.ConfirmationDialog}
|
|
|
|
*/
|
2016-04-07 15:22:19 +02:00
|
|
|
this.show = function (offsetTop) {
|
2016-06-08 11:06:03 +02:00
|
|
|
// Capture focused item
|
|
|
|
previouslyFocused = document.activeElement;
|
2016-05-31 10:19:07 +02:00
|
|
|
wrapperElement.appendChild(popupBackground);
|
2016-06-07 14:31:41 +02:00
|
|
|
startCapturingFocus();
|
2016-06-08 11:06:03 +02:00
|
|
|
disableUnderlay();
|
2016-04-05 18:35:02 +02:00
|
|
|
popupBackground.classList.remove('hidden');
|
2016-05-04 11:53:46 +02:00
|
|
|
fitToContainer(offsetTop);
|
2016-04-21 14:58:21 +02:00
|
|
|
setTimeout(function () {
|
|
|
|
popup.classList.remove('hidden');
|
|
|
|
popupBackground.classList.remove('hiding');
|
2016-04-25 13:17:24 +02:00
|
|
|
|
2016-05-04 12:37:59 +02:00
|
|
|
setTimeout(function () {
|
|
|
|
// Focus confirm button
|
|
|
|
confirmButton.focus();
|
2016-05-03 11:15:21 +02:00
|
|
|
|
2016-05-04 12:37:59 +02:00
|
|
|
// Resize iFrame if necessary
|
|
|
|
if (resizeIFrame && options.instance) {
|
2016-04-25 13:17:24 +02:00
|
|
|
var minHeight = parseInt(popup.offsetHeight, 10) +
|
|
|
|
exitButtonOffset + (2 * shadowOffset);
|
2017-05-08 14:17:29 +02:00
|
|
|
self.setViewPortMinimumHeight(minHeight);
|
2016-04-25 13:17:24 +02:00
|
|
|
options.instance.trigger('resize');
|
|
|
|
resizeIFrame = false;
|
2016-05-04 12:37:59 +02:00
|
|
|
}
|
|
|
|
}, 100);
|
2016-04-21 14:58:21 +02:00
|
|
|
}, 0);
|
2016-04-08 10:23:55 +02:00
|
|
|
|
2016-04-05 18:35:02 +02:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hide confirmation dialog
|
|
|
|
* @returns {H5P.ConfirmationDialog}
|
|
|
|
*/
|
|
|
|
this.hide = function () {
|
|
|
|
popupBackground.classList.add('hiding');
|
|
|
|
popup.classList.add('hidden');
|
2016-06-08 11:06:03 +02:00
|
|
|
|
|
|
|
// Restore focus
|
|
|
|
stopCapturingFocus();
|
2019-04-05 16:11:19 +02:00
|
|
|
if (!options.skipRestoreFocus) {
|
|
|
|
previouslyFocused.focus();
|
|
|
|
}
|
2016-06-08 11:06:03 +02:00
|
|
|
restoreUnderlay();
|
2016-04-05 18:35:02 +02:00
|
|
|
setTimeout(function () {
|
|
|
|
popupBackground.classList.add('hidden');
|
2016-05-31 10:19:07 +02:00
|
|
|
wrapperElement.removeChild(popupBackground);
|
2017-05-08 14:17:29 +02:00
|
|
|
self.setViewPortMinimumHeight(null);
|
2016-04-05 18:35:02 +02:00
|
|
|
}, 100);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
2017-05-08 14:17:29 +02:00
|
|
|
|
2019-04-03 09:43:09 +02:00
|
|
|
/**
|
|
|
|
* Retrieve element
|
|
|
|
*
|
|
|
|
* @return {HTMLElement}
|
|
|
|
*/
|
|
|
|
this.getElement = function () {
|
|
|
|
return popup;
|
|
|
|
};
|
|
|
|
|
2019-04-05 14:49:55 +02:00
|
|
|
/**
|
|
|
|
* Get previously focused element
|
|
|
|
* @return {HTMLElement}
|
|
|
|
*/
|
|
|
|
this.getPreviouslyFocused = function () {
|
|
|
|
return previouslyFocused;
|
|
|
|
};
|
|
|
|
|
2017-05-08 14:17:29 +02:00
|
|
|
/**
|
|
|
|
* Sets the minimum height of the view port
|
|
|
|
*
|
|
|
|
* @param {number|null} minHeight
|
|
|
|
*/
|
2018-10-23 11:25:46 +02:00
|
|
|
this.setViewPortMinimumHeight = function (minHeight) {
|
2017-05-11 17:09:44 +02:00
|
|
|
var container = document.querySelector('.h5p-container') || document.body;
|
|
|
|
container.style.minHeight = (typeof minHeight === 'number') ? (minHeight + 'px') : minHeight;
|
2017-05-08 14:17:29 +02:00
|
|
|
};
|
2016-04-05 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ConfirmationDialog.prototype = Object.create(EventDispatcher.prototype);
|
|
|
|
ConfirmationDialog.prototype.constructor = ConfirmationDialog;
|
|
|
|
|
|
|
|
return ConfirmationDialog;
|
|
|
|
|
|
|
|
}(H5P.EventDispatcher));
|
2016-06-09 13:18:09 +02:00
|
|
|
|
|
|
|
H5P.ConfirmationDialog.uniqueId = -1;
|