/*jshint supernew:true */
/** @namespace */
var Utils = {
name: 'Utils'
};
/**
* Extends the properties and methods of a superClass onto a subClass.
*
* @function extend
* @memberof Utils
* @param {Object} subClass The subClass.
* @param {Object} superClass The superClass.
*/
Utils.extend = function(subClass, superClass) {
function F() {}
F.prototype = superClass.prototype;
subClass.prototype = new F;
subClass.prototype.constructor = subClass;
subClass._superClass = superClass.prototype;
};
/**
* Generates a psuedo-random number within an inclusive range.
*
* @function getRandomNumber
* @memberof Utils
* @param {number} low The low end of the range.
* @param {number} high The high end of the range.
* @param {boolean} [flt] Set to true to return a float or when passing floats as a range.
* @returns {number} A number.
*/
Utils.getRandomNumber = function(low, high, flt) {
if (flt) {
return (Math.random() * (high - low)) + low;
}
high++;
return Math.floor((Math.random() * (high - low))) + low;
};
/**
* Determines the size of the browser window.
*
* @function extend
* @memberof System
* @returns {Object} The current browser window width and height.
*/
Utils.getWindowSize = function() {
var d = {
'width' : false,
'height' : false
};
if (typeof(window.innerWidth) !== 'undefined') {
d.width = window.innerWidth;
d.height = window.innerHeight;
} else if (typeof(document.documentElement) !== 'undefined' &&
typeof(document.documentElement.clientWidth) !== 'undefined') {
d.width = document.documentElement.clientWidth;
d.height = document.documentElement.clientHeight;
} else if (typeof(document.body) !== 'undefined') {
d.width = document.body.clientWidth;
d.height = document.body.clientHeight;
}
return d;
};
/**
* Re-maps a number from one range to another.
*
* @function map
* @memberof Utils
* @param {number} value The value to be converted.
* @param {number} min1 Lower bound of the value's current range.
* @param {number} max1 Upper bound of the value's current range.
* @param {number} min2 Lower bound of the value's target range.
* @param {number} max2 Upper bound of the value's target range.
* @returns {number} A number.
*/
Utils.map = function(value, min1, max1, min2, max2) { // returns a new value relative to a new range
var unitratio = (value - min1) / (max1 - min1);
return (unitratio * (max2 - min2)) + min2;
};
/**
* Adds an event listener to a DOM element.
*
* @function _addEvent
* @memberof System
* @private
* @param {Object} target The element to receive the event listener.
* @param {string} eventType The event type.
* @param {function} The function to run when the event is triggered.
*/
Utils.addEvent = function(target, eventType, handler) {
if (target.addEventListener) { // W3C
target.addEventListener(eventType, handler, false);
} else if (target.attachEvent) { // IE
target.attachEvent('on' + eventType, handler);
}
};
/**
* Converts degrees to radians.
*
* @function degreesToRadians
* @memberof Utils
* @param {number} degrees The degrees value to be converted.
* @returns {number} A number in radians.
*/
Utils.degreesToRadians = function(degrees) {
if (typeof degrees !== 'undefined') {
return 2 * Math.PI * (degrees/360);
} else {
if (typeof console !== 'undefined') {
throw new Error('Error: Utils.degreesToRadians is missing degrees param.');
}
}
};
/**
* Converts radians to degrees.
*
* @function radiansToDegrees
* @memberof Utils
* @param {number} radians The radians value to be converted.
* @returns {number} A number in degrees.
*/
Utils.radiansToDegrees = function(radians) {
if (typeof radians !== 'undefined') {
return radians * (180/Math.PI);
} else {
if (typeof console !== 'undefined') {
throw new Error('Error: Utils.radiansToDegrees is missing radians param.');
}
}
};
/**
* Constrain a value within a range.
*
* @function constrain
* @memberof Utils
* @param {number} val The value to constrain.
* @param {number} low The lower bound of the range.
* @param {number} high The upper bound of the range.
* @returns {number} A number.
*/
Utils.constrain = function(val, low, high) {
if (val > high) {
return high;
} else if (val < low) {
return low;
}
return val;
};
/**
* Determines if one object is inside another.
*
* @function isInside
* @memberof Utils
* @param {Object} obj The object.
* @param {Object} container The containing object.
* @returns {boolean} Returns true if the object is inside the container.
*/
Utils.isInside = function(obj, container) {
if (!obj || !container) {
throw new Error('isInside() requires both an object and a container.');
}
obj.width = obj.width || 0;
obj.height = obj.height || 0;
container.width = container.width || 0;
container.height = container.height || 0;
if (obj.location.x + obj.width / 2 > container.location.x - container.width / 2 &&
obj.location.x - obj.width / 2 < container.location.x + container.width / 2 &&
obj.location.y + obj.height / 2 > container.location.y - container.height / 2 &&
obj.location.y - obj.height / 2 < container.location.y + container.height / 2) {
return true;
}
return false;
};
/**
* Capitalizes the first character in a string.
*
* @function capitalizeFirstLetter
* @memberof Utils
* @param {string} string The string to capitalize.
* @returns {string} The string with the first character capitalized.
*/
Utils.capitalizeFirstLetter = function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};
module.exports = Utils;