Util Class
A set of functions used by at least one module in this project. The functions are generic enough that they may become useful for other modules later or functions that are shared amongst multiple modules should be added here.
NOTE: None of these functions require the global configuration object. (i.e. they are not exclusive to RAMP). For functions that depend on the global configuration object, place them in ramp.js.
Item Index
Methods
- adjustWidthForSrollbar static
- afterAll static
- arrayToQuery static
- checkConsole static
- compareGraphics static
- createLazyVariable static
- escapeHtml static
- executeOnDone static
- executeOnLoad static
- getWhereClause static
- guid static
- isNumber static
- isUndefined static
- once static
- parseBool static
- queryToArray static
- scrollbarWidth static
- stripHtml static
- styleCheckboxes static
- subscribe static
- subscribeAll static
- subscribeOnce static
- subscribeOnceAny static
- transformXML static
Methods
adjustWidthForSrollbar
-
body
-
targets
Checks if the height of the scrollable content of the body is taller than its height; if so, offset the content horizontally to accomodate for the scrollbar.
Parameters:
-
body
JObjectA DOM node with a scrollbar (or not)
-
targets
JObjectAn array of jObjects to add the offset to
afterAll
-
deferredList
-
callback
Executes the callback function only after all the deferred Objects in the given deferredList has resolved.
arrayToQuery
-
array
Converts an array into a '+' separated String that can be used as the query parameter of a URL.
arrayToQuery(["abc", 123, "efg"]) -> "abc+123+efg"
NOTE: the array should only contain primitives, objects will not be serialized properly.
Parameters:
-
array
ArrayAn array of primitives to be serialized
Returns:
A serialized representation of the given array
checkConsole
()
static
Checks if the console exists, if not, redefine the console and all console methods to a function that does nothing. Useful for IE which does not have the console until the debugger is opened.
compareGraphics
-
one
-
two
Compares two graphic objects.
Returns:
True if the objects represent the same feature
createLazyVariable
-
initFunc
Creates an object that acts like a lazy variable (i.e. a variable whose value is only resolved the first time it is retrieved, not when it is assigned). The value given to the lazy variable should be the return value of the given initFunc. The returned object has two methods:
- get - returns the value of the variable, if it is the first time get is called, the the initFunc will be called to resolve the value of the variable.
- reset - forces the variable to call the initFunc again the next time get is called
Parameters:
-
initFunc
FunctionA function to call to resolve the variable value
Returns:
The lazy varialbe
escapeHtml
-
html
Returns an String that has it's angle brackets ('<' and '>') escaped (replaced by '<' and '>'). This will effectively cause the String to be displayed in plain text when embedded in an html page.
Parameters:
-
html
StringString to escape
Returns:
escapeHtml Escaped string
executeOnDone
-
o
-
func
-
d
Loops through all object properties and applies a given function to each. Resolves the given deferred when done.
executeOnLoad
-
func
-
callback
Waits until a given function is available and executes a callback function.
getWhereClause
-
varName
-
query
Returns an appropriate where clause depending on whether the query is a String (returns a where clause with CASE INSENSITIVE comparison) or an integer.
Returns:
The generated "where" clause
guid
()
String
static
Generates an rfc4122 version 4 compliant guid. Taken from here: http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
Returns:
The generated guid string
isNumber
-
input
Returns true if the given String is a number
Parameters:
-
input
StringThe string to check
Returns:
True if number
isUndefined
-
obj
Returns true if the given obj is undefined, false otherwise.
Parameters:
-
obj
ObjectObject to be checked
Returns:
True if the given object is undefined, false otherwise
once
-
func
Returns a function that has the same functionality as the given function, but can only be executed once (subsequent execution does nothing).
Parameters:
-
func
FunctionFunction to be decorated
Returns:
Decorated function that can be executed once
parseBool
-
str
Parse the given String into a boolean. Returns true if the String is the word "true" (case insensitive). False otherwise.
Parameters:
-
str
StringThe string to check
Returns:
True if true
queryToArray
-
query
Converts a query String generated by arrayToQuery into an array object. The array object will only contain Strings.
queryToArray("abc+123+efg") -> ["abc", "123", "efg"]
Parameters:
-
query
StringA query string to be converted
Returns:
A resulting array of strings
scrollbarWidth
()
Int
static
Returns the width of the scrollbar in pixels. Since different browsers render scrollbars differently, the width may vary.
Returns:
The width of the scrollbar in pixels
stripHtml
-
html
Converts html into text by replacing all html tags with their appropriate special characters
Parameters:
-
html
StringHTML to be converted to text
Returns:
The HTML in text form
styleCheckboxes
-
nodes
-
checkedClass
-
focusedClass
-
labels
Wraps the specified checkboxes to provide an alternative rendering of checkboxes without compromising their functionality. Handles synchronisation of the checkbox's state with its new rendering. Also adds highlight/unhighlight on focus/unfocus, update label when checked/unchecked
Parameters:
Returns:
A control objects allowing to toggle checkboxes supplying a state, and retrieve original checkbox nodes
subscribe
-
name
-
callback
-
scope
A convenience method that wraps around Dojo's subscribe method to allow a scope to hitched to the given callback function.
subscribeAll
-
nameArray
-
callback
Given an array of event names published by topic.publish, call the given callback function after ALL of the given events have occurred. An array of arguments is passed to the callback function, the arguments are those returned by the events (in the order that the events appear in the array).
Example
Assume somewhere a module publishes a "click" event:
topic.publish("click", { mouseX: 10, mouseY: 50 });
and somewhere else another module publishes a "finishLoading" event:
topic.publish("finishLoading", { loadedPictures: [pic1, pic2] });
Then if one wants to do something (e.g. display pictures) only after the pictures have been loaded AND the user clicked somewhere, then:
- args[0] will be the object returned by the "click" event
- which in this case will be: { mouseX: 10, mouseY: 50 }
- args[1] will be the object returned by the "finishLoading" event
- which in this case will be: { loadedPictures: [pic1, pic2] }
subscribe(["click", "finishLoading"], function(args) {
doSomething();
});
NOTE: If one of the events fires multiple times before the other event, the object passed by this function to the callback will be the object returned when the event FIRST fired (subsequent firings of the same event are ignored). Also, if some event do not return an object, it will also be excluded in the arguments to the callback function. So be careful! For example, say you subscribed to the events: "evt1", "evt2", "evt3". "evt1" returns an object (call it "evt1Obj"), "evt2" does not, "evt3" returns two objects (call it "evt3Obj-1" and "evt3Obj-2" respectively). Then the array passed to the callback will be: ["evt1Obj", "evt3Obj-1", "evt3Obj-2"].
subscribeOnce
-
name
-
callback
Subscribes to an event, after the event has occurred, the handle is removed.
subscribeOnceAny
-
names
-
callback
Subscrives to a set of events, executes the callback when any of the events fire, then removes the handle.
transformXML
-
xmlurl
-
xslurl
-
callback
-
returnFragment
Applies supplied xslt to supplied xml. IE always returns a String; others may return a documentFragment or a jObject.