forked from jithin-space/h5p-image-pair
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathh5p-image-pair-card.js
More file actions
executable file
·319 lines (273 loc) · 8.38 KB
/
h5p-image-pair-card.js
File metadata and controls
executable file
·319 lines (273 loc) · 8.38 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
(function(ImagePair, EventDispatcher, $) {
/**
* Controls all the operations for each card.
*
* @class H5P.ImagePair.Card
* @extends H5P.EventDispatcher
* @param {Object} image
* @param {number} id
* @param {string} alt
*/
ImagePair.Card = function(image, id, alt) {
// @alias H5P.ImagePair.Card#
var self = this;
// Initialize event inheritance
EventDispatcher.call(self);
var path = H5P.getPath(image.path, id);
alt = alt || 'Missing description';
self.isPaired = false;
var width, height;
if (image.width !== undefined && image.height !== undefined) {
if (image.width > image.height) {
width = '100%';
height = 'auto';
} else {
height = '100%';
width = 'auto';
}
} else {
width = height = '100%';
}
/**
* get the image element of the current card
* @public
*/
self.getImage = function() {
return self.$card.find('img').clone();
};
/**
* set a card to correct state
* @public
*/
self.setCorrect = function() {
self.$pairingMark.addClass('correct-mark');
self.$front.addClass('pair-item-correct');
self.$rear.addClass('pair-item-correct');
};
/**
* set a card to incorrect state
* @public
*/
self.setIncorrect = function() {
self.$pairingMark.addClass('incorrect-mark');
self.$front.addClass('pair-item-incorrect');
self.$rear.addClass('pair-item-incorrect');
};
/**
* set card to solved state
* @public
*/
self.setSolved = function() {
self.$pairingMark.addClass('solved-mark');
self.$front.addClass('pair-item-solved');
self.$rear.addClass('pair-item-solved');
};
/**
* set card to selected state
* @public
*/
self.setSelected = function() {
//for keyboard navigation
self.isSelected = true;
self.$card.addClass('h5p-image-pair-item-selected');
};
/**
*remove card from selected state
* @public
*/
self.removeSelected = function() {
//for keyboard navigation
self.isSelected = false;
self.$card.removeClass('h5p-image-pair-item-selected');
};
/**
* makTabbable - Make the card accessible when tabbing
*/
self.makeTabbable = function() {
if (self.$card) {
self.$card.attr('tabindex', '0');
}
};
/**
* Prevent tabbing to the card
*/
self.makeUntabbable = function() {
if (self.$card) {
self.$card.attr('tabindex', '-1');
}
};
/**
* Make card tabbable and move focus to it
*/
self.setFocus = function() {
self.makeTabbable();
self.$card.focus();
}
/**
* triggerd on mate when it is paired. make its droppable propery disabled
* @public
*/
self.transform = function() {
// remove droppable property
self.$card.removeClass('h5p-image-pair-item-hover').removeClass(
'droppable').droppable("option", "disabled", true);
};
/**
* triggered on card when it is paired with a mate
* @public
*/
self.disable = function() {
//for keyboard navigation
self.isPaired = true;
self.$card.removeClass('h5p-image-pair-item-selected').addClass(
'h5p-image-pair-item-disabled');
};
/**
* triggered on mate when pairing happens
* @public
* @param {H5P.ImagePair.Card} pair
*/
self.pair = function(pair) {
self.srcImage = (self.srcImage) ? self.srcImage : self.getImage();
self.$top = self.$card;
self.$top.html('').toggleClass('h5p-image-pair-images-paired', true);
self.$pairingMark = $('<span class="pairing-mark"></span>').appendTo(
self.$top);
self.$front = $(
'<div class="card-paired front"><div class="h5p-image-pair-overlay"></div></div>'
).append(pair.getImage()).appendTo(self.$top);
self.$rear = $(
'<div class="card-paired"><div class="h5p-image-pair-overlay"></div></div>'
).append(self.srcImage).appendTo(self.$top);
self.$card.replaceWith(self.$top);
//while clicking on either of the paired cards, trigger detach
self.$top.children('.card-paired').on('click', function() {
pair.$card.removeClass('h5p-image-pair-item-disabled');
self.detach();
});
self.$top.children('.card-paired').hover(function() {
$(this).addClass('h5p-image-pair-item-hover');
$(this).siblings('div').addClass('h5p-image-pair-item-hover');
}, function() {
$(this).removeClass('h5p-image-pair-item-hover');
$(this).siblings('div').removeClass(
'h5p-image-pair-item-hover');
});
self.isPaired = true;
};
/**
* triggerd user clicks on either of the card that is currently paired
* @public
*/
self.detach = function() {
self.isPaired = false;
self.$card.removeClass('h5p-image-pair-images-paired').empty();
$('<div class="image-container"></div>').append(self.srcImage).appendTo(
self.$card);
self.$card.removeClass('h5p-image-pair-item-selected').addClass(
'droppable').removeClass('h5p-image-pair-item-hover').droppable(
"option", "disabled", false);
self.trigger('unpair');
};
/**
* Append card to the given container.
*
* @param {H5P.jQuery} $container
*/
self.appendTo = function($container) {
self.$card = $('<li class="h5p-image-pair-item">' +
'<div class="image-container">' +
'<img src="' + path + '" alt="' + alt + '" style="width:' +
width + ';height:' + height + '"/>' +
'<div class="h5p-image-pair-overlay"></div>' +
'</div>' +
'</li>').appendTo($container);
self.$card.on('keydown', function(event) {
switch (event.which) {
case 13: //enter
self.trigger('makeSelection');
event.preventDefault();
return;
case 32: //space
//toggle between card container & mate container
self.trigger('shiftContainer');
event.preventDefault();
return;
case 39: //Right
case 40: //Down
if (self.isPaired) {
// move to next pair
self.trigger('nextPair');
} else {
// move to the next card/mate
self.trigger('next');
}
return;
case 37: //Left
case 38: //Up
if (self.isPaired) {
//move to the previous pair
self.trigger('prevPair');
} else {
// move to the previous card/mate
self.trigger('prev');
}
return;
case 35: //End
if (self.isPaired) {
// move to the last pair
self.trigger('lastPair');
} else {
// move to the last card/mate
self.trigger('last');
}
return;
case 36: //Home
if (self.isPaired) {
// move to the first pair
self.trigger('firstPair');
} else {
// move to the first card/mate
self.trigger('first');
}
return;
}
});
self.$card.click(function() {
self.trigger('selected');
}).end();
self.$card.hover(function() {
$(this).addClass('h5p-image-pair-item-hover');
}, function() {
$(this).removeClass('h5p-image-pair-item-hover');
});
};
};
// Extends the event dispatcher
ImagePair.Card.prototype = Object.create(EventDispatcher.prototype);
ImagePair.Card.prototype.constructor = ImagePair.Card;
/**
* Check to see if the given object corresponds with the semantics for
* a image pair game card.
*
* @param {object} params
* @returns {boolean}
*/
ImagePair.Card.isValid = function(params) {
return (params !== undefined &&
params.image !== undefined &&
params.image.path !== undefined);
};
/**
* Checks to see if the card parameters should create cards with different
* images.
*
* @param {object} params
* @returns {boolean}
*/
ImagePair.Card.hasTwoImages = function(params) {
return (params !== undefined &&
params.match !== undefined &&
params.match.path !== undefined);
};
})(H5P.ImagePair, H5P.EventDispatcher, H5P.jQuery);