API Docs for: 2.5.3
Show:

glesea Class

Defined in: src/Intro/Intro.js:1

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

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

Defined in src/Ajax/Ajax.js:1

Calls KnowRe-style AJAX.

glesea.ajax({
    method: 'get',
    url: '/api/foo',
    input: {
        key: 'value'
    },
    // ...
});

Parameters:

  • [method='get'] String optional

    HTTP method

  • [url='/'] String optional

    Address to call

  • [port=443] Number optional

    Port to connect

  • [headers={}] Object optional

    Additional headers

  • [input={}] Object optional

    Payload

  • [onSuccess=Function] Function optional

    Callback on success

  • [onFailure=Function] Function optional

    Callback on failure

  • [https=true] Boolean optional

    Whether to convert every request to https

  • [needLogin=true] Boolean optional

    Whether needs session logged in

  • [swintStyle=true] Boolean optional

    Whether to apply Swint-style API(input payload and session-error-output tuple)

  • [timeOut=0] Number optional

    Timeout. Void when set to 0. Milliseconds.

  • [timeOutPopup=true] Boolean optional

    Whether to show popup window when timeout occurs

  • [errorPopup=true] Boolean optional

    Whether to show popup window when server/session error occurs

  • [loadingIndicator={on: Function, off: Function}] Object optional

    Callback when loading indicator needs to be turned on/off.

  • [sessionError=Function] Function optional

    Callback especially on session error. On default, it redirects to the root(/) instead of executing onFailure.

  • [serverError=Function] Function optional

    Callback especially on 500 server error. After closing modaless dialog, it executes onFailure.

  • [aborted=Function] Function optional

    Callback especially on timeout. After closing modaless dialog, it executes onFailure.

Returns:

nothing

defaultize

(
  • defVal
  • tgtVal
)
Object

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:

  • defVal Object

    The default format of the object

  • tgtVal Object

    The object to be defaultized

Returns:

Object:

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:

  • struct Object

    The structure to traverse through

  • query Array

    An array of keys/indexs, which are followed in order

  • val Any

    The value which will be assigned to the structure's query value

Returns:

Nothing

toEm

(
  • context
  • px
)
Number

Defined in src/EmPx/empx.js:1

Converts a given amount of pixels to the corresponding amount of Ems.

var a = glesea.toEm({px: 42});

// a ==> 2.625

Parameters:

  • context DOM node

    The context to measure px/em

  • px Number

Returns:

Number:

Amount of Ems

toPx

(
  • context
  • em
)
Number

Defined in src/EmPx/empx.js:46

Converts a given amount of Ems to the corresponding amount of Pixels.

var a = glesea.toPx({em: 42});

// a ==> 672

Parameters:

  • context DOM node

    The context to measure px/em

  • em Number

Returns:

Number:

Amount of pixels

toSHA256

(
  • text
)
String

Defined in src/Hash/hash.js:1

Convert text data to SHA-256 Hex code string.

var hex = glesea.toSHA256('1234');

// hex ==> '03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4'

Parameters:

  • text String

Returns:

String:

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:

  • struct Object

    The structure to traverse through

  • query Array

    An array of keys/indexs, which are followed in order

Returns:

Value of the query, or undefined if it wasn't there

validate

(
  • ruleVal
  • query
)
Array

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:

  • ruleVal Object

    An Object representing the rules

  • query Object

    An Object that will be evaluated against the rules

Returns:

Array:

0th entry is a Boolean, 1st entry is Array which represents path of potential failing entry