-
Notifications
You must be signed in to change notification settings - Fork 67
/
h5p-accordion.js
292 lines (259 loc) · 7.74 KB
/
h5p-accordion.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* Accordion module
*
* @param {jQuery} $
*/
H5P.Accordion = (function ($) {
var nextIdPrefix = 0;
var nextLooperId = 0;
var allowedLoopers = [];
/**
* Initialize a new Accordion
*
* @class H5P.InteractiveVideo
* @extends H5P.EventDispatcher
* @param {Object} params Behavior settings
* @param {Number} contentId Content identification
* @param {Object} contentData Object containing task specific content data
*/
function Accordion(params, contentId, contentData) {
this.contentId = contentId;
H5P.EventDispatcher.call(this);
// Set default behavior.
this.params = $.extend({}, {
hTag: "h2",
panels: []
}, params);
this.contentData = contentData;
this.instances = [];
for (var i = 0; i < this.params.panels.length; i++) {
this.instances[i] = H5P.newRunnable(this.params.panels[i].content, contentId);
}
this.idPrefix = (nextIdPrefix++) + '-';
}
Accordion.prototype = Object.create(H5P.EventDispatcher.prototype);
Accordion.prototype.constructor = Accordion;
/**
* Append field to wrapper.
* @param {jQuery} container the jQuery object which this module will attach itself to.
*/
Accordion.prototype.attach = function ($container) {
var self = this;
if (self.$content === undefined) {
// Mark as consumed
self.triggerConsumed();
// Create the content
self.elements = [];
for (var i = 0; i < self.params.panels.length; i++) {
self.createPanel(i);
}
self.$content = $(self.elements);
}
// Insert content
$container.html('').addClass('h5p-accordion').append(self.$content);
};
/**
* Create HTML for Panel.
* @param {number} id
*/
Accordion.prototype.createPanel = function (id) {
var self = this;
var titleId = 'h5p-panel-link-' + this.idPrefix + id;
var contentId = 'h5p-panel-content-' + self.idPrefix + id;
var toggleCollapse = function () {
if (self.$expandedTitle === undefined || !self.$expandedTitle.is($title)) {
self.collapseExpandedPanels();
self.expandPanel($title, $titleButton, $content);
}
else {
self.collapsePanel($title, $titleButton, $content);
}
// We're running in an iframe, so we must animate the iframe height
self.animateResize();
};
// Create panel title
var $title = $('<' + this.params.hTag + '/>', {
'id': titleId,
'class': 'h5p-panel-title',
});
// Create panel button
var $titleButton = $('<button/>', {
'class': 'h5p-panel-button',
'tabindex': '0',
'aria-expanded': 'false',
'aria-controls': contentId,
'html': self.params.panels[id].title,
'on': {
'click': toggleCollapse,
'keydown': function (event) {
switch (event.keyCode) {
case 38: // Up
case 37: { // Left
// Try to select previous item
var $prev = $title.prev().prev().children('.h5p-panel-button');
if ($prev.length) {
$prev.focus();
}
return false;
}
case 40: // Down
case 39: { // Right
// Try to select next item
var $next = $content.next().children('.h5p-panel-button');
if ($next.length) {
$next.focus();
}
return false;
}
case 32: // SPACE
case 13: { // ENTER
toggleCollapse();
return false;
}
}
},
// The class needs to be set programmatically as the title
// is not able to detect focus-visible on the button
'focus': function () {
if($titleButton.is(':focus-visible')) {
$title.addClass('h5p-panel-focused');
}
},
'blur': function () {
$title.removeClass('h5p-panel-focused');
}
}
});
$title.append($titleButton);
// Create panel content
var $content = $('<div>', {
'id': contentId,
'class': 'h5p-panel-content',
'role': 'region',
'aria-labelledby': titleId,
'aria-hidden': 'true'
});
// Add the content itself to the content section
self.instances[id].attach($content);
// Gather all content
self.elements.push($title[0]);
self.elements.push($content[0]);
};
/**
* Trigger the 'consumed' xAPI event when this commences
*
* (Will be more sophisticated in future version)
*/
Accordion.prototype.triggerConsumed = function () {
var xAPIEvent = this.createXAPIEventTemplate({
id: 'http://activitystrea.ms/schema/1.0/consume',
display: {
'en-US': 'consumed'
}
}, {
result: {
completion: true
}
});
this.trigger(xAPIEvent);
};
/**
* Collapse all expanded panels
*/
Accordion.prototype.collapseExpandedPanels = function () {
var self = this;
if (this.$expandedTitle !== undefined) {
this.$expandedButton.attr('aria-expanded', false);
this.$expandedTitle.removeClass('h5p-panel-expanded');
}
if (this.$expandedPanel !== undefined) {
this.$expandedPanel
.stop(false, true)
.slideUp(200, function () {
self.stopWorkLoop(self.resizing);
self.trigger('resize');
})
.attr('aria-hidden', true);
}
};
/**
* Expand a panel
*
* @param {jQuery} $title The title of the panel that is to be expanded
* @param {jQuery} $panel The panel that is to be expanded
*/
Accordion.prototype.expandPanel = function($title, $titleButton, $panel) {
var self = this;
$titleButton.attr('aria-expanded', true);
$title.addClass('h5p-panel-expanded');
$panel
.stop(false, true)
.slideDown(200, function () {
self.stopWorkLoop(self.resizing);
self.trigger('resize');
})
.attr('aria-hidden', false);
self.$expandedButton = $titleButton;
self.$expandedTitle = $title;
self.$expandedPanel = $panel;
};
/**
* Collapse a panel
*
* @param {jQuery} $title The title of the panel that is to be collapsed
* @param {jQuery} $panel The panel that is to be collapsed
*/
Accordion.prototype.collapsePanel = function($title, $titleButton, $panel) {
var self = this;
$titleButton.attr('aria-expanded', false)
$title.removeClass('h5p-panel-expanded');
$panel
.stop(false, true)
.slideUp(200, function () {
self.stopWorkLoop(self.resizing);
self.trigger('resize');
})
.attr('aria-hidden', true);
self.$expandedTitle = self.$expandedButton = self.$expandedPanel = undefined;
};
/**
* Makes sure that the heigt of the iframe gets animated
*/
Accordion.prototype.animateResize = function () {
var self = this;
self.stopWorkLoop(this.resizing);
this.resizing = self.startWorkLoop(function () {
self.trigger('resize');
}, 40);
};
Accordion.prototype.startWorkLoop = function (func, wait) {
var myId = nextLooperId++;
var self = this;
allowedLoopers.push(myId);
var looper = function(func, wait, myId) {
return function () {
if (self.allowedToWork(myId)) {
try {
func.call(null);
}
catch (e) {
self.stopWorkLoop(myId);
}
setTimeout(looper, wait, func, wait, myId);
}
};
} (func, wait, myId);
setTimeout(looper, wait);
return myId;
};
Accordion.prototype.stopWorkLoop = function (myId) {
var index;
while ((index = allowedLoopers.indexOf(myId)) !== -1) {
allowedLoopers.splice(index, 1);
}
};
Accordion.prototype.allowedToWork = function (myId) {
return allowedLoopers.indexOf(myId) !== -1;
};
return Accordion;
})(H5P.jQuery);