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
- _wrapFileCallInPromise
- adjustWidthForSrollbar static
- afterAll static
- arrayToQuery static
- b64EncodeUnicode
- checkConsole static
- compareGraphics static
- containsInDom static
- createGraphic static
- createLazyVariable static
- detectDelimiter static
- endsWith static
- escapeHtml static
- executeOnDone static
- executeOnLoad static
- getWhereClause static
- guid static
- hexToRgb static
- isNumber static
- isSpatialRefEqual static
- isUndefined static
- keyboardSortable static
- mergeRecursive static
- once static
- parseBool static
- queryToArray static
- readFileAsArrayBuffer static
- readFileAsBinary static
- readFileAsText static
- resetTimelines static
- rgbToHex static
- scrollbarWidth static
- stripHtml static
- subscribe static
- subscribeAll static
- subscribeOnce static
- subscribeOnceAny static
- transformXML static
Methods
_wrapFileCallInPromise
-
readMethod
Helper function for wrapping File API calls in Promise objects. Used for building a series of helpers which call different file read methods.
Parameters:
-
readMethod
Stringa string indicating the FileReader method to call
Returns:
a function which accepts a {File} object and returns a Promise
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 accommodate for the scrollbar assuming target's width is set to "100%".
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
b64EncodeUnicode
-
str
Base64 encoding for unicode text.
Parameters:
-
str
Stringa string to encode
Returns:
encoded string
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
containsInDom
-
el
Checks if the given dom node is present in the dom.
Parameters:
-
el
ObjectDOM node to check
Returns:
true if the given node is in the dom
createGraphic
-
extent
Create boudingbox graphic for a bounding box extent
Parameters:
-
extent
Esri/geometry/Extentof a bounding box
Returns:
An ESRI graphic object represents a bouding box
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 variable
detectDelimiter
-
data
-
[type]
Detects either cell or line delimiter in a given csv data.
Parameters:
Returns:
Detected delimiter
endsWith
-
str
-
suffix
Checks if the string ends with the supplied suffix.
Returns:
True if suffix matches
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
-
target
-
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
hexToRgb
-
hex
Converts HEX colour to RGB. http://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb/5624139#5624139
Parameters:
-
hex
Stringhex colour code
Returns:
object containing r, g, and b components of the supplied colour
isNumber
-
input
Returns true if the given String is a number
Parameters:
-
input
StringThe string to check
Returns:
True if number
isSpatialRefEqual
-
sr1
-
sr2
Checks if two spatial reference objects are equivalent. Handles both wkid and wkt definitions
Parameters:
-
sr1
Esri/SpatialReferenceFirst Esri/SpatialReference to compare
-
sr2
Esri/SpatialReferenceSecond Esri/SpatialReference to compare
Returns:
true if the two spatial references are equivalent. False otherwise.
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
keyboardSortable
-
ulNodes
-
[settings]
Augments lists of items to be sortable using keyboard.
Parameters:
-
ulNodes
ArrayAn array of
- tags containing a number of
- tags to be made keyboard sortable.
-
[settings]
Object optionalAdditional settings
-
[linkLists]
Object optionalIndicates if the supplied lists (if more than one) should be linked - items could be moved from one to another; Update: this is wrong - items can't be moved from list to list right now, but the keyboard focus can be moved between lists - need to fix.
-
[onStart]
Object optionalA callback function to be called when the user initiates sorting process
-
[onUpdate]
Object optionalA callback function to be called when the user moves the item around
-
[onStop]
Object optionalA callback function to be called when the user commits the item to its new place ending the sorting process
-
mergeRecursive
()
static
Recursively merge JSON objects into a target object. The merge will also merge array elements.
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
readFileAsArrayBuffer
-
file
Parses a file using the FileReader API. Wraps readAsArrayBuffer and returns a promise.
Parameters:
-
file
Filea dom file object to be read
Returns:
a promise which sends an ArrayBuffer containing the file output if successful
readFileAsBinary
-
file
Parses a file using the FileReader API. Wraps readAsBinaryString and returns a promise.
Parameters:
-
file
Filea dom file object to be read
Returns:
a promise which sends a string containing the file output if successful
readFileAsText
-
file
Parses a file using the FileReader API. Wraps readAsText and returns a promise.
Parameters:
-
file
Filea dom file object to be read
Returns:
a promise which sends a string containing the file output if successful
resetTimelines
-
tls
-
keepPosition
Takes an array of timelines and their generator functions, clear and recreates timelines optionally preserving the play position.
Example of tls parameter
[
{
timeline: {timeline},
generator: {function}
}
]
rgbToHex
-
r
-
g
-
b
Converts RGB colour to HEX. http://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb/5624139#5624139
Returns:
hex colour code
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
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
Subscribes 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.