2015-10-19 11:17:38 +02:00
|
|
|
(function (MemoryGame, EventDispatcher, $) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controls all the operations for each card.
|
|
|
|
*
|
|
|
|
* @class H5P.MemoryGame.Card
|
2017-02-13 14:42:34 +01:00
|
|
|
* @extends H5P.EventDispatcher
|
2017-01-20 11:58:08 +01:00
|
|
|
* @param {Object} image
|
|
|
|
* @param {number} id
|
|
|
|
* @param {string} [description]
|
2017-05-04 14:20:20 +02:00
|
|
|
* @param {Object} [styles]
|
2015-10-19 11:17:38 +02:00
|
|
|
*/
|
2017-05-04 14:20:20 +02:00
|
|
|
MemoryGame.Card = function (image, id, description, styles) {
|
2017-02-13 14:42:34 +01:00
|
|
|
/** @alias H5P.MemoryGame.Card# */
|
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;
|
|
|
|
|
2017-01-20 11:58:08 +01:00
|
|
|
if (image.width !== undefined && image.height !== undefined) {
|
|
|
|
if (image.width > image.height) {
|
2017-01-23 14:12:33 +01:00
|
|
|
width = '100%';
|
|
|
|
height = 'auto';
|
2015-10-19 11:17:38 +02:00
|
|
|
}
|
|
|
|
else {
|
2017-01-23 14:12:33 +01:00
|
|
|
height = '100%';
|
|
|
|
width = 'auto';
|
2015-10-19 11:17:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2017-01-23 14:12:33 +01:00
|
|
|
width = height = '100%';
|
2015-10-19 11:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
};
|
|
|
|
|
2017-02-13 14:42:34 +01:00
|
|
|
/**
|
|
|
|
* Reset card to natural state
|
|
|
|
*/
|
|
|
|
self.reset = function () {
|
|
|
|
$card[0].classList.remove('h5p-flipped', 'h5p-matched');
|
|
|
|
};
|
|
|
|
|
2015-10-19 11:17:38 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2017-01-23 14:12:33 +01:00
|
|
|
$card = $('<li class="h5p-memory-wrap"><div class="h5p-memory-card" role="button" tabindex="1">' +
|
2017-05-04 14:20:20 +02:00
|
|
|
'<div class="h5p-front"' + (styles && styles.front ? styles.front : '') + '>' + (styles.backImage ? '' : '<span></span>') + '</div>' +
|
|
|
|
'<div class="h5p-back"' + (styles && styles.back ? styles.back : '') + '>' +
|
2017-01-23 14:12:33 +01:00
|
|
|
'<img src="' + path + '" alt="Memory Card" style="width:' + width + ';height:' + height + '"/>' +
|
2015-10-19 11:17:38 +02:00
|
|
|
'</div>' +
|
2017-01-23 14:12:33 +01:00
|
|
|
'</div></li>')
|
2015-10-19 11:17:38 +02:00
|
|
|
.appendTo($container)
|
2017-01-23 14:12:33 +01:00
|
|
|
.children('.h5p-memory-card')
|
|
|
|
.children('.h5p-front')
|
|
|
|
.click(function () {
|
|
|
|
self.flip();
|
|
|
|
})
|
|
|
|
.end();
|
2017-02-13 14:42:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Re-append to parent container
|
|
|
|
*/
|
|
|
|
self.reAppend = function () {
|
|
|
|
var parent = $card[0].parentElement.parentElement;
|
|
|
|
parent.appendChild($card[0].parentElement);
|
|
|
|
};
|
2015-10-19 11:17:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
};
|
|
|
|
|
2017-05-04 14:20:20 +02:00
|
|
|
/**
|
|
|
|
* Determines the theme for how the cards should look
|
|
|
|
*
|
|
|
|
* @param {string} color The base color selected
|
|
|
|
* @param {number} invertShades Factor used to invert shades in case of bad contrast
|
|
|
|
*/
|
|
|
|
MemoryGame.Card.determineStyles = function (color, invertShades, backImage) {
|
|
|
|
var styles = {
|
|
|
|
front: '',
|
|
|
|
back: '',
|
2017-05-08 15:14:18 +02:00
|
|
|
popup: '',
|
2017-05-04 14:20:20 +02:00
|
|
|
backImage: !!backImage
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create color theme
|
|
|
|
if (color) {
|
|
|
|
var frontColor = shade(color, 43.75 * invertShades);
|
|
|
|
var backColor = shade(color, 56.25 * invertShades);
|
|
|
|
|
|
|
|
styles.front += 'color:' + color + ';' +
|
2017-05-08 15:14:18 +02:00
|
|
|
'background-color:' + frontColor + ';' +
|
|
|
|
'border-color:' + frontColor +';';
|
2017-05-04 14:20:20 +02:00
|
|
|
styles.back += 'color:' + color + ';' +
|
2017-05-08 15:14:18 +02:00
|
|
|
'background-color:' + backColor + ';' +
|
|
|
|
'border-color:' + frontColor +';';
|
|
|
|
styles.popup += 'border-color:' + backColor + ';' +
|
|
|
|
'background-color:' + shade(color, 90 * invertShades) + ';';
|
2017-05-04 14:20:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add back image for card
|
|
|
|
if (backImage) {
|
|
|
|
var backgroundImage = 'background-image:url(' + backImage + ')';
|
|
|
|
|
|
|
|
styles.front += backgroundImage;
|
|
|
|
styles.back += backgroundImage;
|
2017-05-08 15:14:18 +02:00
|
|
|
styles.popup += backgroundImage;
|
2017-05-04 14:20:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prep style attribute
|
|
|
|
if (styles.front) {
|
|
|
|
styles.front = ' style="' + styles.front + '"';
|
|
|
|
}
|
|
|
|
if (styles.back) {
|
|
|
|
styles.back = ' style="' + styles.back + '"';
|
|
|
|
}
|
2017-05-08 15:14:18 +02:00
|
|
|
if (styles.popup) {
|
|
|
|
styles.popup = ' style="' + styles.popup + '"';
|
|
|
|
}
|
2017-05-04 14:20:20 +02:00
|
|
|
|
|
|
|
return styles;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert hex color into shade depending on given percent
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {string} color
|
|
|
|
* @param {number} percent
|
|
|
|
* @return {string} new color
|
|
|
|
*/
|
|
|
|
var shade = function (color, percent) {
|
|
|
|
var newColor = '#';
|
|
|
|
|
|
|
|
// Determine if we should lighten or darken
|
|
|
|
var max = (percent < 0 ? 0 : 255);
|
|
|
|
|
|
|
|
// Always stay positive
|
|
|
|
if (percent < 0) {
|
|
|
|
percent *= -1;
|
|
|
|
}
|
|
|
|
percent /= 100;
|
|
|
|
|
|
|
|
for (var i = 1; i < 6; i += 2) {
|
|
|
|
// Grab channel and convert from hex to dec
|
|
|
|
var channel = parseInt(color.substr(i, 2), 16);
|
|
|
|
|
|
|
|
// Calculate new shade and convert back to hex
|
|
|
|
channel = (Math.round((max - channel) * percent) + channel).toString(16);
|
|
|
|
|
|
|
|
// Make sure to always use two digits
|
|
|
|
newColor += (channel.length < 2 ? '0' + channel : channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newColor;
|
|
|
|
};
|
|
|
|
|
2015-10-19 11:17:38 +02:00
|
|
|
})(H5P.MemoryGame, H5P.EventDispatcher, H5P.jQuery);
|