-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathpopup.js
126 lines (109 loc) · 3.61 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
(function (MemoryGame, EventDispatcher, $) {
/**
* A dialog for reading the description of a card.
* @see https://www.w3.org/WAI/ARIA/apg/patterns/dialogmodal/
*
* @class H5P.MemoryGame.Popup
* @extends H5P.EventDispatcher
* @param {Object.<string, string>} l10n
*/
MemoryGame.Popup = function (l10n) {
// Initialize event inheritance
EventDispatcher.call(this);
/** @alias H5P.MemoryGame.Popup# */
var self = this;
var closed;
const $popupContainer = $(
'<div class="h5p-memory-obscure-content"><div class="h5p-memory-pop" role="dialog" aria-modal="true"><div class="h5p-memory-top"></div><div class="h5p-memory-desc h5p-programatically-focusable" tabindex="-1"></div><div class="h5p-memory-close" role="button" tabindex="0" title="' + (l10n.closeLabel || 'Close') + '" aria-label="' + (l10n.closeLabel || 'Close') + '"></div></div></div>'
)
.on('keydown', function (event) {
if (event.code === 'Escape') {
self.close(true);
event.preventDefault();
}
})
.hide();
const $popup = $popupContainer.find('.h5p-memory-pop');
const $top = $popupContainer.find('.h5p-memory-top');
// Hook up the close button
const $closeButton = $popupContainer
.find('.h5p-memory-close')
.on('click', function () {
self.close(true);
})
.on('keydown', function (event) {
if (event.code === 'Enter' || event.code === 'Space') {
self.close(true);
event.preventDefault();
}
else if (event.code === 'Tab') {
event.preventDefault(); // Lock focus
}
});
const $desc = $popupContainer
.find('.h5p-memory-desc')
.on('keydown', function (event) {
if (event.code === 'Tab') {
// Keep focus inside dialog
$closeButton.focus();
event.preventDefault();
}
});
/**
* Append the popup to a container.
* @param {H5P.jQuery} $container Container to append to.
*/
this.appendTo = ($container) => {
$container.append($popupContainer);
};
/**
* Show the popup.
*
* @param {string} desc
* @param {H5P.jQuery[]} imgs
* @param {function} done
*/
self.show = function (desc, imgs, styles, done) {
const announcement = '<span class="h5p-memory-screen-reader">' +
l10n.cardMatchedA11y + '</span>' + desc;
$desc.html(announcement);
$top.html('').toggleClass('h5p-memory-two-images', imgs.length > 1);
for (var i = 0; i < imgs.length; i++) {
$('<div class="h5p-memory-image"' + (styles ? styles : '') + '></div>').append(imgs[i]).appendTo($top);
}
$popupContainer.show();
$desc.focus();
closed = done;
};
/**
* Close the popup.
*
* @param {boolean} refocus Sets focus after closing the dialog
*/
self.close = function (refocus) {
if (closed !== undefined) {
$popupContainer.hide();
closed(refocus);
closed = undefined;
self.trigger('closed');
}
};
/**
* Sets popup size relative to the card size
*
* @param {number} fontSize
*/
self.setSize = function (fontSize) {
// Set image size
$top[0].style.fontSize = fontSize + 'px';
// Determine card size
var cardSize = fontSize * 6.25; // From CSS
// Set popup size
$popupContainer[0].style.minWidth = (cardSize * 2.5) + 'px';
$popupContainer[0].style.minHeight = cardSize + 'px';
};
this.getElement = () => {
return $popup[0];
}
};
})(H5P.MemoryGame, H5P.EventDispatcher, H5P.jQuery);