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
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function ConfirmationDialog(options) {
|
|
|
|
EventDispatcher.call(this);
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// 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-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');
|
|
|
|
popupBackground.appendChild(popup);
|
|
|
|
|
|
|
|
// 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');
|
2016-06-06 14:57:51 +02:00
|
|
|
text.setAttribute('role', 'alert');
|
2016-04-05 18:35:02 +02:00
|
|
|
text.innerHTML = options.dialogText;
|
|
|
|
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-05-23 14:34:56 +02:00
|
|
|
cancelButton.onclick = dialogCanceled;
|
2016-04-07 14:51:56 +02:00
|
|
|
cancelButton.onkeydown = function (e) {
|
|
|
|
if (e.which === 32) { // Space
|
2016-05-23 14:34:56 +02:00
|
|
|
dialogCanceled(e);
|
2016-04-07 14:51:56 +02:00
|
|
|
}
|
2016-04-05 18:35:02 +02:00
|
|
|
};
|
|
|
|
buttons.appendChild(cancelButton);
|
|
|
|
|
|
|
|
// Confirm button
|
2016-05-23 14:34:56 +02:00
|
|
|
var confirmButton = document.createElement('button');
|
2016-04-05 18:35:02 +02:00
|
|
|
confirmButton.classList.add('h5p-core-button',
|
|
|
|
'h5p-confirmation-dialog-confirm-button');
|
|
|
|
confirmButton.textContent = options.confirmText;
|
2016-05-23 14:34:56 +02:00
|
|
|
confirmButton.onclick = dialogConfirmed;
|
2016-04-07 14:51:56 +02:00
|
|
|
confirmButton.onkeydown = function (e) {
|
|
|
|
if (e.which === 32) { // Space
|
2016-05-23 14:34:56 +02:00
|
|
|
dialogConfirmed(e);
|
2016-04-07 14:51:56 +02:00
|
|
|
}
|
2016-04-05 18:35:02 +02:00
|
|
|
};
|
|
|
|
buttons.appendChild(confirmButton);
|
|
|
|
|
|
|
|
// Exit button
|
2016-05-23 14:34:56 +02:00
|
|
|
var exitButton = document.createElement('button');
|
2016-04-05 18:35:02 +02:00
|
|
|
exitButton.classList.add('h5p-confirmation-dialog-exit');
|
2016-06-06 14:57:51 +02:00
|
|
|
exitButton.title = options.cancelText;
|
2016-05-23 14:34:56 +02:00
|
|
|
exitButton.onclick = dialogCanceled;
|
2016-04-07 14:51:56 +02:00
|
|
|
exitButton.onkeydown = function (e) {
|
|
|
|
if (e.which === 32) { // Space
|
2016-05-23 14:34:56 +02:00
|
|
|
dialogCanceled(e);
|
2016-04-07 14:51:56 +02:00
|
|
|
}
|
2016-04-05 18:35:02 +02:00
|
|
|
};
|
|
|
|
popup.appendChild(exitButton);
|
|
|
|
|
|
|
|
// Wrapper element
|
|
|
|
var wrapperElement;
|
|
|
|
|
|
|
|
/**
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2016-05-04 11:53:46 +02:00
|
|
|
if (offsetTop) {
|
|
|
|
popupOffsetTop = offsetTop;
|
|
|
|
}
|
2016-04-05 18:35:02 +02:00
|
|
|
|
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-05-31 10:19:07 +02:00
|
|
|
wrapperElement.appendChild(popupBackground);
|
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);
|
|
|
|
wrapperElement.style.minHeight = minHeight + 'px';
|
|
|
|
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');
|
|
|
|
setTimeout(function () {
|
|
|
|
popupBackground.classList.add('hidden');
|
2016-05-31 10:19:07 +02:00
|
|
|
wrapperElement.removeChild(popupBackground);
|
2016-04-05 18:35:02 +02:00
|
|
|
}, 100);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfirmationDialog.prototype = Object.create(EventDispatcher.prototype);
|
|
|
|
ConfirmationDialog.prototype.constructor = ConfirmationDialog;
|
|
|
|
|
|
|
|
return ConfirmationDialog;
|
|
|
|
|
|
|
|
}(H5P.EventDispatcher));
|