utils Class
The grapefruit utility object, used for misc functions used throughout the code base
Item Index
Methods
ajax
-
settings
Performs an ajax request, and manages the callbacks passed in
Parameters:
-
settings
ObjectThe settings of the ajax request, similar to jQuery's ajax function
Returns:
An XHR object
AjaxRequest
()
XMLHttpRequest | ActiveXObject
Wraps XMLHttpRequest in a cross-browser way.
Returns:
error
()
Safe way to error to console, if console.error doesn't exist nothing happens.
extend
()
From jQuery.extend, extends one object into another taken straight from jQuery 2.0.3
getAbsoluteUrl
-
url
Gets the absolute url from a relative one
Parameters:
-
url
StringThe relative url to translate into absolute
Returns:
The absolute url (fully qualified)
getOffset
-
element
Get the DOM offset values of any given element
Parameters:
-
element
HTMLElementThe targeted element that we want to retrieve the offset
Returns:
The offset of the element
isPlainObject
-
obj
From jQuery.isPlainObject, checks if an object is a plain object taken straight from jQuery 2.0.3
Parameters:
-
obj
MixedThe object to test
Returns:
log
()
Safe way to log to console, if console.log doesn't exist nothing happens.
noop
()
An empty function that performs no action
parseHitArea
-
value
Parses an array of numbers that represent a hitArea into the actual shape.
For example: [1, 1, 15]
is a Circle ([x, y, radius]
); [1, 1, 15, 15]
is a Rectangle
([x, y, width, height]
); and any length >= 5 is a polygon in the form [x1, y1, x2, y2, ..., xN, yN]
.
Parameters:
-
value
ArrayThe array to parse
parseTiledProperties
-
value
Parses an object of string properties into potential javascript types. First it attempts to convert to a number, if that fails it checks for the string 'true' or 'false' and changes it to the actual Boolean value, then it attempts to parse a string as JSON.
Parameters:
-
value
ArrayThe array to parse
parseXML
-
xmlStr
Parses an XML string into a Document object. Will use window.DOMParser if available, falling back to Microsoft.XMLDOM ActiveXObject in IE.
Eventually, it would be nice to include a node.js alternative as well for running in that environment.
Parameters:
-
xmlStr
StringThe xml string to parse
Returns:
An XML Document
setValues
-
obj
-
values
This will take values and override the passed obj's properties with those values. The difference from a normal object extend is that this will try to massage the passed value into the same type as the object's property. Also if the key for the value is not in the original object, it is not copied.
Parameters:
-
obj
ObjectThe object to extend the values into
-
values
ObjectThe values to put into the object
Returns:
returns the updated object
Example:
var obj = { vec: new Vector(), arr: [] },
vals = { vec: '2|5', arr: '5|10|11' };
utils.setValues(obj, vals);
//now obj is:
// { vec: Vector(2, 5), arr: [5, 10, 11] }
warn
()
Safe way to warn to console, if console.warn doesn't exist nothing happens.