2015-10-19 11:17:38 +02:00
|
|
|
(function (MemoryGame, EventDispatcher, $) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controls all the operations for each card.
|
|
|
|
*
|
|
|
|
* @class H5P.MemoryGame.Card
|
2017-01-20 11:58:08 +01:00
|
|
|
* @param {Object} image
|
|
|
|
* @param {number} id
|
|
|
|
* @param {string} [description]
|
2015-10-19 11:17:38 +02:00
|
|
|
*/
|
2017-01-20 11:58:08 +01:00
|
|
|
MemoryGame.Card = function (image, id, description) {
|
2015-10-19 11:17:38 +02:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// Initialize event inheritance
|
|
|
|
EventDispatcher.call(self);
|
|
|
|
|
2017-01-20 11:58:08 +01:00
|
|
|
var path = H5P.getPath(image.path, id);
|
2015-10-19 11:17:38 +02:00
|
|
|
var width, height, margin, $card;
|
|
|
|
|
|
|
|
var a = 96;
|
2017-01-20 11:58:08 +01:00
|
|
|
if (image.width !== undefined && image.height !== undefined) {
|
|
|
|
if (image.width > image.height) {
|
2015-10-19 11:17:38 +02:00
|
|
|
width = a;
|
2017-01-20 11:58:08 +01:00
|
|
|
height = image.height * (width / image.width);
|
2015-10-19 11:17:38 +02:00
|
|
|
margin = '' + ((a - height) / 2) + 'px 0 0 0';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
height = a;
|
2017-01-20 11:58:08 +01:00
|
|
|
width = image.width * (height / image.height);
|
2015-10-19 11:17:38 +02:00
|
|
|
margin = '0 0 0 ' + ((a - width) / 2) + 'px';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
width = height = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flip card.
|
|
|
|
*/
|
|
|
|
self.flip = function () {
|
|
|
|
$card.addClass('h5p-flipped');
|
|
|
|
self.trigger('flip');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flip card back.
|
|
|
|
*/
|
|
|
|
self.flipBack = function () {
|
|
|
|
$card.removeClass('h5p-flipped');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove.
|
|
|
|
*/
|
|
|
|
self.remove = function () {
|
|
|
|
$card.addClass('h5p-matched');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get card description.
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
self.getDescription = function () {
|
2017-01-20 11:58:08 +01:00
|
|
|
return description;
|
2015-10-19 11:17:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get image clone.
|
|
|
|
*
|
|
|
|
* @returns {H5P.jQuery}
|
|
|
|
*/
|
|
|
|
self.getImage = function () {
|
|
|
|
return $card.find('img').clone();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append card to the given container.
|
|
|
|
*
|
|
|
|
* @param {H5P.jQuery} $container
|
|
|
|
*/
|
|
|
|
self.appendTo = function ($container) {
|
|
|
|
// TODO: Translate alt attr
|
|
|
|
$card = $('<li class="h5p-memory-card" role="button" tabindex="1">' +
|
|
|
|
'<div class="h5p-front"></div>' +
|
|
|
|
'<div class="h5p-back">' +
|
|
|
|
'<img src="' + path + '" alt="Memory Card" width="' + width + '" height="' + height + '"' + (margin === undefined ? '' : ' style="margin:' + margin + '"') + '/>' +
|
|
|
|
'</div>' +
|
|
|
|
'</li>')
|
|
|
|
.appendTo($container)
|
|
|
|
.children('.h5p-front')
|
|
|
|
.click(function () {
|
|
|
|
self.flip();
|
|
|
|
})
|
|
|
|
.end();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// Extends the event dispatcher
|
|
|
|
MemoryGame.Card.prototype = Object.create(EventDispatcher.prototype);
|
|
|
|
MemoryGame.Card.prototype.constructor = MemoryGame.Card;
|
|
|
|
|
2016-05-19 09:40:22 +02:00
|
|
|
/**
|
|
|
|
* Check to see if the given object corresponds with the semantics for
|
|
|
|
* a memory game card.
|
|
|
|
*
|
|
|
|
* @param {object} params
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
MemoryGame.Card.isValid = function (params) {
|
|
|
|
return (params !== undefined &&
|
|
|
|
params.image !== undefined &&
|
|
|
|
params.image.path !== undefined);
|
|
|
|
};
|
|
|
|
|
2017-01-20 11:58:08 +01:00
|
|
|
/**
|
|
|
|
* Checks to see if the card parameters should create cards with different
|
|
|
|
* images.
|
|
|
|
*
|
|
|
|
* @param {object} params
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
MemoryGame.Card.hasTwoImages = function (params) {
|
|
|
|
return (params !== undefined &&
|
|
|
|
params.match !== undefined &&
|
|
|
|
params.match.path !== undefined);
|
|
|
|
};
|
|
|
|
|
2015-10-19 11:17:38 +02:00
|
|
|
})(H5P.MemoryGame, H5P.EventDispatcher, H5P.jQuery);
|