API Docs for: v0.1.0
Show:

utils Class

Extends Object
Defined in: src\utils\utils.js:6

The grapefruit utility object, used for misc functions used throughout the code base

Methods

ajax

(
  • settings
)
XMLHttpRequest | ActiveXObject

Performs an ajax request, and manages the callbacks passed in

Parameters:

  • settings Object

    The settings of the ajax request, similar to jQuery's ajax function

Returns:

XMLHttpRequest | ActiveXObject:

An XHR object

AjaxRequest

() XMLHttpRequest | ActiveXObject

Wraps XMLHttpRequest in a cross-browser way.

Returns:

XMLHttpRequest | ActiveXObject:

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
)
String

Gets the absolute url from a relative one

Parameters:

  • url String

    The relative url to translate into absolute

Returns:

String:

The absolute url (fully qualified)

getOffset

(
  • element
)
Vector

Get the DOM offset values of any given element

Parameters:

  • element HTMLElement

    The targeted element that we want to retrieve the offset

Returns:

Vector:

The offset of the element

isPlainObject

(
  • obj
)
Boolean

From jQuery.isPlainObject, checks if an object is a plain object taken straight from jQuery 2.0.3

Parameters:

  • obj Mixed

    The object to test

Returns:

Boolean:

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
)
Circle | Rectangle | Polygon

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 Array

    The array to parse

Returns:

Circle | Rectangle | Polygon:

The parsed out shape

parseTiledProperties

(
  • value
)
Circle | Rectangle | Polygon

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 Array

    The array to parse

Returns:

Circle | Rectangle | Polygon:

The parsed out shape

parseXML

(
  • xmlStr
)
Document

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 String

    The xml string to parse

Returns:

Document:

An XML Document

setValues

(
  • obj
  • values
)
Object

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 Object

    The object to extend the values into

  • values Object

    The values to put into the object

Returns:

Object:

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.