Remoto - VFS: utils Class Reference
Remoto - VFS

Utility functions for javascript clients. More...

#include "remoto!stdlib:js/include/utils.js"

Public Member Functions

 base64Encode (o)
 Encode a JSON object using base64, replacing '/' characters with '_'. More...
 
 bound (min, n, max)
 Return a number where n is bounded by [min,max]. More...
 
 cleanPath (path)
 Clean and normalize a resource path. More...
 
 copyObject (source, dest)
 Create a deep copy of a JSON object, extending the destination object recursively. More...
 
 dirNameFromPath (p)
 Retrieve the directory portion of a path. More...
 
 equalObjects (o1, o2)
 Do a single-level pass on two objects for key->value equality. More...
 
 exitFullScreen ()
 Request an exit from fullscreen mode. More...
 
 extendBBox (b, a)
 Extend bounding box b by an amount a. More...
 
 fileNameFromPath (p, orDirIfNeeded)
 Retrieve the last part of a file path. More...
 
 humanReadableSize (s)
 Calculate a human-readable size from a number of bytes. More...
 
 isArray (a)
 Determine if an object is a javascript array. More...
 
 isFullScreen ()
 Detect if the page is in fullscreen mode. More...
 
 isInstanceOf (o, t)
 Check if an object is of a chosen type. More...
 
 keyCount (o)
 Return the number of keys in an object. More...
 
 keyCountFiltered (o, f)
 Return the number of keys on an object that match a filter. More...
 
 keyFromValue (o, val)
 Given a value, retrieve the key for a potential entry in an object. More...
 
 randInt (min, max)
 Return a pseudo-random value in the range of [min,max]. More...
 
 requestFullScreen ()
 Issue a request to enter fullscreen state. More...
 
 resolveContextValues (o, ctx)
 Recursively resolve any context values in an object. More...
 
 setAttr (e, a)
 A DOM manipulation function to work around a jQuery bug. More...
 
 sortObjectByMemberAttribute (o, attr)
 Sort an object by the value of an attribute of members of the object. More...
 
 toggleFullScreen ()
 Toggle the fullscreen state of the page. More...
 
 uuid ()
 Generate a universally unique identifier. More...
 

Detailed Description

Utility functions for javascript clients.

The library includes tools for manipulating paths, diffs, objects, fullscreen state, and a few math functions.

All of these functions are free-standing and act like static members of namespace 'utils'.

A simple use case:

define( [
"remoto!stdlib:js/include/utils.js"
],
function(utils)
{
console.log( utils.uuid() ); //returns a new uuid
console.log( utils.bound( 0, -1, 10 ) ); //returns 0
}
);
Utility functions for javascript clients.
uuid()
Generate a universally unique identifier.
bound(min, n, max)
Return a number where n is bounded by [min,max].

Member Function Documentation

◆ base64Encode()

base64Encode (   o)

Encode a JSON object using base64, replacing '/' characters with '_'.

Parameters
oThe object to encode

Qt uses RFC 2045, which uses a '/' for value 63. We want to be able to use a base64 string as a path component, which cannot contain '/'.

◆ bound()

bound (   min,
  n,
  max 
)

Return a number where n is bounded by [min,max].

Parameters
minThe minimum value
nThe value
maxThe maximum value
Returns
The bounded value

◆ cleanPath()

cleanPath (   path)

Clean and normalize a resource path.

Parameters
pathThe path to examine
Returns
The cleaned path

Remove all duplicate directory separators '/' and remove them from the beginning and end of a path to produce a path that can be used safely with the VFS as well as a client and maintain uniformity.

Because paths are used as IDs, they need to be well-formed if path math is performed (appending directories, filenames, removing them, etc.).

Note
The VFS does not use the convention of '.' or '..' to reference self or parent, so those values are not resolved by this function.

◆ copyObject()

copyObject (   source,
  dest 
)

Create a deep copy of a JSON object, extending the destination object recursively.

Parameters
sourceCopy from this object
destCopy to this object
Returns
The copied object

If the destination object is missing, a new one is created. This function will also copy getters and setters, and works on classes, functions, and arrays.

The function is recursive; it calls itself to complete the deep copy. This means that if very deep objects are submitted, they may exceed the call stack limit, although this would be an exotic case.

This is one of the basic mechanisms being used to apply diffs and emulate subclasses in javascript. The concept is essentially the same as jQuery's extend().

It can be useful at times to make a deep copy of an object without a destination: copyObject(o), which will return a new object instance matching the source, but as an entirely new object.

◆ dirNameFromPath()

dirNameFromPath (   p)

Retrieve the directory portion of a path.

Parameters
pThe resource path
Returns
The directory portion of a path.

This is mostly useful for creating contexts. If the path provided is not a string "DIRNAME" is returned, and an error printed.

◆ equalObjects()

equalObjects (   o1,
  o2 
)

Do a single-level pass on two objects for key->value equality.

Parameters
o1The first object
o2The second object
Returns
false if inequal, otherwise true

Values are compared using '!==', and only first level keys are compared. If an object has complex members, it will probably fail to prove equal to another object.

◆ exitFullScreen()

exitFullScreen ( )

Request an exit from fullscreen mode.

Note
This is not possible in all contexts and all devices.

◆ extendBBox()

extendBBox (   b,
  a 
)

Extend bounding box b by an amount a.

Parameters
bThe bounding box object, expressed as {x,y,width,height} values
aThe amount to extend
Returns
The extended bounding box

◆ fileNameFromPath()

fileNameFromPath (   p,
  orDirIfNeeded 
)

Retrieve the last part of a file path.

Parameters
pThe resource path
orDirIfNeededOptionally allow the last part to be the containing directory if it ends with a '/'.
Returns
The last compononent of the path.

If the path provided is not a string, "FILENAME" is returned, and an error printed.

◆ humanReadableSize()

humanReadableSize (   s)

Calculate a human-readable size from a number of bytes.

Parameters
sThe size, in bytes

◆ isArray()

isArray (   a)

Determine if an object is a javascript array.

Parameters
aThe object/array to check.
Returns
true if found to be an array, otherwise false.

Detecting if an object is an array is a much debated topic for JavaScript. Newer versions will resolve this. For now, we use the test:

Object.prototype.toString.call(a) == '[object Array]';

...which generally works.

◆ isFullScreen()

isFullScreen ( )

Detect if the page is in fullscreen mode.

Returns
The fullscreen state of the page.

◆ isInstanceOf()

isInstanceOf (   o,
  t 
)

Check if an object is of a chosen type.

Parameters
oThe object to check.
tA single type or an array of types to check.
Returns
true if the object type matches any of the types being checked, otherwise false.

◆ keyCount()

keyCount (   o)

Return the number of keys in an object.

Parameters
oThe object to check.
Returns
The number of keys in the JSON object.

Return a count of 0 if the object is bad, otherwise return Object.keys(o).length.

◆ keyCountFiltered()

keyCountFiltered (   o,
  f 
)

Return the number of keys on an object that match a filter.

Parameters
oThe object to check.
fThe filter to apply. This function should return a boolean value.
Returns
The number of keys that pass the filter.

A missing filter will match all keys.

◆ keyFromValue()

keyFromValue (   o,
  val 
)

Given a value, retrieve the key for a potential entry in an object.

Parameters
oThe object to check.
valThe value to compare to.
Returns
The first key, if any, that contains the value in question. Otherwise null.

The value comparison is done with '===', so complex objects will not be deeply compared.

◆ randInt()

randInt (   min,
  max 
)

Return a pseudo-random value in the range of [min,max].

Parameters
minThe beginning of the range.
maxThe end of the range.
Returns
The random value as a rounded integer.

This value is calculated as: Math.floor(Math.random()*(max-min+1)+min)

◆ requestFullScreen()

requestFullScreen ( )

Issue a request to enter fullscreen state.

Note
This is not possible in all contexts and all devices.

◆ resolveContextValues()

resolveContextValues (   o,
  ctx 
)

Recursively resolve any context values in an object.

Parameters
oThe object to check
ctxThe context to use
Returns
The resolved object

Context values may be provided by the server to prevent using fixed paths for nodes. Those values need to be resolved to arrive at complete paths for VFS_request operations, for instance. Context values will be in the form "%value%" and will be part of string values only.

This function will resolve strings with context values and also recurse into members of the object to resolve those as well, using the same context.

Keys can also contain tokens.

Because a value may resolve to another context value, the maximum replacement depth is fixed to 10. If a context value can't be resolved, it will be left in place.

See also
vfsClient
form
formListEditor
listing

◆ setAttr()

setAttr (   e,
  a 
)

A DOM manipulation function to work around a jQuery bug.

Parameters
eThe element to modify.
aAn object of attributes to set.

This is a workaround for DOM manipulation, namely for SVG objects, where attribute names should not be set to lower case. This function will maintain case when setting attributes.

◆ sortObjectByMemberAttribute()

sortObjectByMemberAttribute (   o,
  attr 
)

Sort an object by the value of an attribute of members of the object.

Parameters
oThe JSON object to sort
attrThe attribute to sort on
Returns
An array containing the sorted list
Note
This function expects an object as input, but returns an array as output.

This is generally used to sort form fields, where serialized JSON will re-alphabetize field names but we want to present them in some other order. A more advanced version of this would allow attr to be a function, however this hasn't proved to be needed yet.

The resulting array will remember keys by attaching a __key field to each entry:

//input:
var i = {
"a": { value: 1,
index: 7
},
"b": { value: 10,
index: 3
},
"c": { value: 3,
index: 1
}
}
var o = utils.sortObjectByMemberAttribute( i, "index" );
//result:
o = [
{ value:3, index: 1, __key:"c" },
{ value:10, index: 3, __key:"b" },
{ value:1, index: 7, __key:"a" }
]
sortObjectByMemberAttribute(o, attr)
Sort an object by the value of an attribute of members of the object.

◆ toggleFullScreen()

toggleFullScreen ( )

Toggle the fullscreen state of the page.

Note
This is not possible in all contexts and all devices.

◆ uuid()

uuid ( )

Generate a universally unique identifier.

Returns
a 16-digit hexadecimal uuid

This is based on an algorithm found here: http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript , but modified for 16 hex digits.


The documentation for this class was generated from the following file: