2015-10-19 11:17:38 +02:00
|
|
|
|
(function (MemoryGame, $) {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A dialog for reading the description of a card.
|
|
|
|
|
*
|
|
|
|
|
* @class H5P.MemoryGame.Popup
|
|
|
|
|
* @param {H5P.jQuery} $container
|
2017-06-20 11:52:30 +02:00
|
|
|
|
* @param {Object.<string, string>} l10n
|
2015-10-19 11:17:38 +02:00
|
|
|
|
*/
|
2017-06-20 13:28:15 +02:00
|
|
|
|
MemoryGame.Popup = function ($container, l10n) {
|
2017-02-13 14:42:34 +01:00
|
|
|
|
/** @alias H5P.MemoryGame.Popup# */
|
2015-10-19 11:17:38 +02:00
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
var closed;
|
|
|
|
|
|
2017-09-25 14:38:14 +02:00
|
|
|
|
var $popup = $('<div class="h5p-memory-pop" role="dialog"><div class="h5p-memory-top"></div><div class="h5p-memory-desc" tabindex="-1"></div><div class="h5p-memory-close" role="button" tabindex="0" title="' + (l10n.closeLabel || 'Close') + '"></div></div>').appendTo($container);
|
2015-10-19 11:17:38 +02:00
|
|
|
|
var $desc = $popup.find('.h5p-memory-desc');
|
2017-05-08 15:14:18 +02:00
|
|
|
|
var $top = $popup.find('.h5p-memory-top');
|
2015-10-19 11:17:38 +02:00
|
|
|
|
|
2017-06-20 11:52:30 +02:00
|
|
|
|
// Hook up the close button
|
|
|
|
|
$popup.find('.h5p-memory-close').on('click', function () {
|
2017-09-25 14:38:14 +02:00
|
|
|
|
self.close(true);
|
2017-06-20 11:52:30 +02:00
|
|
|
|
}).on('keypress', function (event) {
|
|
|
|
|
if (event.which === 13 || event.which === 32) {
|
2017-09-25 14:38:14 +02:00
|
|
|
|
self.close(true);
|
2017-06-20 11:52:30 +02:00
|
|
|
|
event.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2015-10-19 11:17:38 +02:00
|
|
|
|
/**
|
|
|
|
|
* Show the popup.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} desc
|
2017-05-08 15:14:18 +02:00
|
|
|
|
* @param {H5P.jQuery[]} imgs
|
2015-10-19 11:17:38 +02:00
|
|
|
|
* @param {function} done
|
|
|
|
|
*/
|
2017-05-08 15:14:18 +02:00
|
|
|
|
self.show = function (desc, imgs, styles, done) {
|
2016-03-04 12:42:01 +01:00
|
|
|
|
$desc.html(desc);
|
2017-05-08 15:14:18 +02:00
|
|
|
|
$top.html('').toggleClass('h5p-memory-two-images', imgs.length > 1);
|
|
|
|
|
for (var i = 0; i < imgs.length; i++) {
|
2017-06-20 10:58:13 +02:00
|
|
|
|
$('<div class="h5p-memory-image"' + (styles ? styles : '') + '></div>').append(imgs[i]).appendTo($top);
|
2017-05-08 15:14:18 +02:00
|
|
|
|
}
|
2015-10-19 11:17:38 +02:00
|
|
|
|
$popup.show();
|
2017-09-25 14:38:14 +02:00
|
|
|
|
$desc.focus();
|
2015-10-19 11:17:38 +02:00
|
|
|
|
closed = done;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Close the popup.
|
2017-09-25 14:38:14 +02:00
|
|
|
|
*
|
|
|
|
|
* @param {boolean} refocus Sets focus after closing the dialog
|
2015-10-19 11:17:38 +02:00
|
|
|
|
*/
|
2017-09-25 14:38:14 +02:00
|
|
|
|
self.close = function (refocus) {
|
2015-10-19 11:17:38 +02:00
|
|
|
|
if (closed !== undefined) {
|
|
|
|
|
$popup.hide();
|
2017-09-25 14:38:14 +02:00
|
|
|
|
closed(refocus);
|
2015-10-19 11:17:38 +02:00
|
|
|
|
closed = undefined;
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-02-13 12:35:10 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets popup size relative to the card size
|
2017-09-25 14:38:14 +02:00
|
|
|
|
*
|
2017-02-13 12:35:10 +01:00
|
|
|
|
* @param {number} fontSize
|
|
|
|
|
*/
|
|
|
|
|
self.setSize = function (fontSize) {
|
|
|
|
|
// Set image size
|
2017-05-08 15:14:18 +02:00
|
|
|
|
$top[0].style.fontSize = fontSize + 'px';
|
2017-02-13 12:35:10 +01:00
|
|
|
|
|
|
|
|
|
// Determine card size
|
|
|
|
|
var cardSize = fontSize * 6.25; // From CSS
|
|
|
|
|
|
|
|
|
|
// Set popup size
|
2017-05-08 15:14:18 +02:00
|
|
|
|
$popup[0].style.minWidth = (cardSize * 2.5) + 'px';
|
2017-02-13 12:35:10 +01:00
|
|
|
|
$popup[0].style.minHeight = cardSize + 'px';
|
|
|
|
|
};
|
2015-10-19 11:17:38 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
})(H5P.MemoryGame, H5P.jQuery);
|