2014-04-13 12:30:47 +00:00
|
|
|
/* global $ */
|
2014-02-18 19:11:27 +00:00
|
|
|
/**
|
|
|
|
* Utility functions.
|
|
|
|
*/
|
|
|
|
var Util = (function (my) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the text width for the given element.
|
|
|
|
*
|
|
|
|
* @param el the element
|
|
|
|
*/
|
2014-04-13 12:30:47 +00:00
|
|
|
my.getTextWidth = function (el) {
|
2014-02-18 19:11:27 +00:00
|
|
|
return (el.clientWidth + 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the text height for the given element.
|
|
|
|
*
|
|
|
|
* @param el the element
|
|
|
|
*/
|
2014-04-13 12:30:47 +00:00
|
|
|
my.getTextHeight = function (el) {
|
2014-02-18 19:11:27 +00:00
|
|
|
return (el.clientHeight + 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Casts the given number to integer.
|
|
|
|
*
|
|
|
|
* @param number the number to cast
|
|
|
|
*/
|
2014-04-13 12:30:47 +00:00
|
|
|
my.toInteger = function (number) {
|
2014-02-18 19:11:27 +00:00
|
|
|
return Math.round(Number(number));
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plays the sound given by id.
|
|
|
|
*
|
|
|
|
* @param id the identifier of the audio element.
|
|
|
|
*/
|
2014-04-13 12:30:47 +00:00
|
|
|
my.playSoundNotification = function (id) {
|
2014-02-18 19:11:27 +00:00
|
|
|
document.getElementById(id).play();
|
|
|
|
};
|
|
|
|
|
2014-02-26 15:19:39 +00:00
|
|
|
/**
|
|
|
|
* Escapes the given text.
|
|
|
|
*/
|
2014-04-13 12:30:47 +00:00
|
|
|
my.escapeHtml = function (unsafeText) {
|
2014-02-26 15:19:39 +00:00
|
|
|
return $('<div/>').text(unsafeText).html();
|
|
|
|
};
|
|
|
|
|
2014-06-18 11:42:31 +00:00
|
|
|
my.imageToGrayScale = function (canvas) {
|
2014-06-12 18:35:42 +00:00
|
|
|
var context = canvas.getContext('2d');
|
|
|
|
var imgData = context.getImageData(0, 0, canvas.width, canvas.height);
|
|
|
|
var pixels = imgData.data;
|
|
|
|
|
|
|
|
for (var i = 0, n = pixels.length; i < n; i += 4) {
|
|
|
|
var grayscale
|
|
|
|
= pixels[i] * .3 + pixels[i+1] * .59 + pixels[i+2] * .11;
|
|
|
|
pixels[i ] = grayscale; // red
|
|
|
|
pixels[i+1] = grayscale; // green
|
|
|
|
pixels[i+2] = grayscale; // blue
|
|
|
|
// pixels[i+3] is alpha
|
|
|
|
}
|
|
|
|
// redraw the image in black & white
|
|
|
|
context.putImageData(imgData, 0, 0);
|
|
|
|
};
|
|
|
|
|
2014-06-18 11:42:31 +00:00
|
|
|
my.setTooltip = function (element, tooltipText, position) {
|
|
|
|
element.setAttribute("data-content", tooltipText);
|
|
|
|
element.setAttribute("data-toggle", "popover");
|
|
|
|
element.setAttribute("data-placement", position);
|
|
|
|
element.setAttribute("data-html", true);
|
2014-09-30 15:22:22 +00:00
|
|
|
element.setAttribute("data-container", "body");
|
2014-06-18 11:42:31 +00:00
|
|
|
};
|
|
|
|
|
2014-11-28 15:18:12 +00:00
|
|
|
my.createExpBackoffTimer = function (step) {
|
|
|
|
var count = 1;
|
|
|
|
return function (reset) {
|
|
|
|
// Reset call
|
|
|
|
if (reset) {
|
|
|
|
count = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Calculate next timeout
|
|
|
|
var timeout = Math.pow(2, count - 1);
|
|
|
|
count += 1;
|
|
|
|
return timeout * step;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-02-18 19:11:27 +00:00
|
|
|
return my;
|
2014-04-13 12:30:47 +00:00
|
|
|
}(Util || {}));
|