glesea Class
GleseaJS : Common utility functions package for KnowRe web client
glesea is not actually a class but the collection of useful utility functions. It includes:
- jQuery
- We all know what this is.
- jsRender
- Client-side view & template framework
- webcomponents
- HTML5 web components polyfill from Polymer
Item Index
Methods
ajax
-
[method='get'] -
[url='/'] -
[port=443] -
[headers={}] -
[input={}] -
[onSuccess=Function] -
[onFailure=Function] -
[https=true] -
[needLogin=true] -
[swintStyle=true] -
[timeOut=0] -
[timeOutPopup=true] -
[errorPopup=true] -
[loadingIndicator={on: Function, off: Function}] -
[sessionError=Function] -
[serverError=Function] -
[aborted=Function]
Calls KnowRe-style AJAX.
glesea.ajax({
method: 'get',
url: '/api/foo',
input: {
key: 'value'
},
// ...
});
Parameters:
-
[method='get']String optionalHTTP method
-
[url='/']String optionalAddress to call
-
[port=443]Number optionalPort to connect
-
[headers={}]Object optionalAdditional headers
-
[input={}]Object optionalPayload
-
[onSuccess=Function]Function optionalCallback on success
-
[onFailure=Function]Function optionalCallback on failure
-
[https=true]Boolean optionalWhether to convert every request to https
-
[needLogin=true]Boolean optionalWhether needs session logged in
-
[swintStyle=true]Boolean optionalWhether to apply Swint-style API(input payload and session-error-output tuple)
-
[timeOut=0]Number optionalTimeout. Void when set to 0. Milliseconds.
-
[timeOutPopup=true]Boolean optionalWhether to show popup window when timeout occurs
-
[errorPopup=true]Boolean optionalWhether to show popup window when server/session error occurs
-
[loadingIndicator={on: Function, off: Function}]Object optionalCallback when loading indicator needs to be turned on/off.
-
[sessionError=Function]Function optionalCallback especially on session error. On default, it redirects to the root(/) instead of executing onFailure.
-
[serverError=Function]Function optionalCallback especially on 500 server error. After closing modaless dialog, it executes onFailure.
-
[aborted=Function]Function optionalCallback especially on timeout. After closing modaless dialog, it executes onFailure.
Returns:
nothing
defaultize
-
defVal -
tgtVal
Filling default value to an JavaScript object.
If tgtVal doesn't have the key -> It is filled from defVal, recursively.
If tgtVal doesn't have enough values at the Array -> It is filed from defVal, recursively.
var def, tgt;
def = {
a: 1,
b: 'aaa',
c: [1, 2, 3]
};
tgt = {
b: 'bbb',
c: [4]
};
glesea.defaultize(def, tgt);
// tgt ==> { a: 1, b: 'bbb', c: [4, 2, 3] }
Parameters:
-
defValObjectThe default format of the object
-
tgtValObjectThe object to be defaultized
Returns:
The defaultized object
setWithQuery
-
struct -
query -
val
Traverses through a structure by following a list of keys/indexs
var s = { a: [10, 19, { b: 'ccc' } ] },
s2 = { a: { b: 'ccc2' } };
glesea.setWithQuery(s, ['a', 1], 'new val');
glesea.setWithQuery(s2, 'a.c', 'new val2');
// s ==> { a: [ 10, 'new val', { b: 'ccc' } ] }
// s2 ==> { a: { b: 'ccc2', c: 'new val2' } }
Parameters:
-
structObjectThe structure to traverse through
-
queryArrayAn array of keys/indexs, which are followed in order
-
valAnyThe value which will be assigned to the structure's query value
Returns:
Nothing
toEm
-
context -
px
Converts a given amount of pixels to the corresponding amount of Ems.
var a = glesea.toEm({px: 42});
// a ==> 2.625
Parameters:
-
contextDOM nodeThe context to measure px/em
-
pxNumber
Returns:
Amount of Ems
toPx
-
context -
em
Converts a given amount of Ems to the corresponding amount of Pixels.
var a = glesea.toPx({em: 42});
// a ==> 672
Parameters:
-
contextDOM nodeThe context to measure px/em
-
emNumber
Returns:
Amount of pixels
toSHA256
-
text
Convert text data to SHA-256 Hex code string.
var hex = glesea.toSHA256('1234');
// hex ==> '03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4'
Parameters:
-
textString
Returns:
hex code string
traverseWithQuery
-
struct -
query
Traverses through a structure by following a list of keys/indexs
var s = { a: [1, 2, { b: 'ccc' } ] },
s2 = { a: { b: 'ccc2' } };
var o = glesea.traverseWithQuery(s, ['a', 2, 'b']),
o2 = glesea.traverseWithQuery(s2, 'a.b');
// o ==> 'ccc'
// o2 ==> 'ccc2'
Parameters:
-
structObjectThe structure to traverse through
-
queryArrayAn array of keys/indexs, which are followed in order
Returns:
Value of the query, or undefined if it wasn't there
validate
-
ruleVal -
query
Recursively looks through an object to see if its values match the rules
Rules:
undefined -> Can be any type
0 -> Has to be a Number
'' -> Has to be a String
'aaa\bbbb\bccc' -> Has to be enum('aaa', 'bbb', 'ccc')
new Date(0) -> Has to be a Date
{} -> Has to be an object
[0] -> Has to be an Array of Numbers
[''] -> Has to be an Array of Strings
[new Date(0)] -> Has to be an Array of Dates
[[0]] -> Has to be an Array of Arrays of Numbers
{ a: 0 } -> Has to be an Object with key of a: Number
Example:
var r = {
a: 0,
b: '',
c: {
d: [0],
e: undefined
}
}
var t = {
a: 100,
b: 'Monday',
c: {
d: [1,2,3],
e: 'Friday'
}
}
var o = glesea.validate(r, t);
// o ==> [ true, [] ]
If the target didn't match the target, it will return false and a path to the failing entry:
var r = {
a: 0,
b: '',
c: {
d: [0],
e: undefined
}
}
var t = {
a: 100,
b: 'Monday',
c: {
d: ['a', 'b', 'c'],
e: 'Friday'
}
}
var o = glesea.validate(r, t);
// o ==> [ false, [ 'c', 'd', 0 ] ]
Parameters:
-
ruleValObjectAn Object representing the rules
-
queryObjectAn Object that will be evaluated against the rules
Returns:
0th entry is a Boolean, 1st entry is Array which represents path of potential failing entry
