Common utility methods
- Source:
Methods
(static) addClass(htmlEle, className)
Adds class to HTML DOM Element if not already applied.
Parameters:
Name | Type | Description |
---|---|---|
htmlEle |
DOMElement | The target element. |
className |
string | The CSS class name. |
- Source:
(static) alignmentStringToXY(alignmentDescription, defineSingleAxisModeopt) → {vector}
Given a 1-2 character description of a horizontal and/or vertical alignment, will return the alignment as a vector representation.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
alignmentDescription |
string | The alignment description as initials. Eg. `CT` for center / top. Case independent. | ||
defineSingleAxisMode |
boolean |
<optional> |
false | If true: `C` means centered on x axis,`M` means centered on y axis and any unset axes will return `null`. If false: Both x and y will resolve, `C` applies to both x and y, `M` means centered on y axis, will default to 0 (centered). |
- Source:
Returns:
alignment - The alignment as a vector. A value of -1 means left/top, a value of 0 means centered and a value of 1 means right/bottom.
- Type
- vector
(static) canPageScrollVertically() → {boolean}
Detects if the page can scroll vertically.
- Source:
Returns:
scrollsVertically
- Type
- boolean
(static) cloneObj(source) → {Object}
Performs a shallow clone of a given object.
Parameters:
Name | Type | Description |
---|---|---|
source |
Object | The object to clone. |
- Source:
Returns:
clone - The duplicate object.
- Type
- Object
(static) copyTextToClipboard(text)
Copies text to clipboard with fallback for legacy browsers.
Parameters:
Name | Type | Description |
---|---|---|
text |
string | The text to copy. |
- Source:
(static) darkenCol(color, brightness) → {int}
Darkens color by set percentage.
Parameters:
Name | Type | Description |
---|---|---|
color |
int | Color value, eg. 0xff3300. |
brightness |
number | Amount to darken in the range of 0.0 to 1.0. |
- Source:
Returns:
colorResult - Darkened color value.
- Type
- int
Example
utils.darkenCol(0xff3300, 0.5); // 50% darken
(static) debounce(callback, wait)
Enforces a minimum wait time between multiple calls to a given function.
Parameters:
Name | Type | Description |
---|---|---|
callback |
function | Function to call. |
wait |
number | Minimum time in milliseconds between calls. |
- Source:
Example
onResize(){
utils.debounce(this.onResizeDebounced.bind(this), 1000);
}
(static) deepFreeze(targetObject)
Apply `Object.freeze` to an object and its child objects recursively.
Parameters:
Name | Type | Description |
---|---|---|
targetObject |
Object | The object to freeze. |
- Source:
(static) e(id) → {DOMElement}
Returns the HTML DOM element with the given id.
Parameters:
Name | Type | Description |
---|---|---|
id |
string | The id of the DOM element to retrieve. |
- Source:
Returns:
[element=null] - The DOM element, if found.
- Type
- DOMElement
(static) escapeRegExp(subject) → {string}
Escapes any regex reserved tokens from a string.
Parameters:
Name | Type | Description |
---|---|---|
subject |
string | The subject string. |
- Source:
Returns:
escapedSubject - The escaped string, ready to be used in a regex expression.
- Type
- string
(static) extend(baseObject, extendObject)
Extend an object with any properties defined in a second object.
Parameters:
Name | Type | Description |
---|---|---|
baseObject |
Object | The base object. |
extendObject |
Object | The object with properties to be applied to the base object. |
- Source:
Example
constructor(options) {
let defaults = {
color: 0xff3300,
isLabel: true
};
options = utils.extend(defaults, options);
}
(static) fontWeightStrToNum(fontWeightStrToNum) → {number}
Given a font weight string description, will return the weight as a number compatible with the PIXI text system.
- 100 - Thin
- 200 - Extra Light (Ultra Light)
- 300 - Light
- 400 - Normal
- 500 - Medium
- 600 - Semi Bold (Demi Bold)
- 700 - Bold
- 800 - Extra Bold (Ultra Bold)
- 900 - Black (Heavy)
- 100 - Thin
- 200 - Extra Light (Ultra Light)
- 300 - Light
- 400 - Normal
- 500 - Medium
- 600 - Semi Bold (Demi Bold)
- 700 - Bold
- 800 - Extra Bold (Ultra Bold)
- 900 - Black (Heavy)
Parameters:
Name | Type | Description |
---|---|---|
fontWeightStrToNum |
string | The font weight description. |
- Source:
Returns:
fontWeight - The numeric font weight. If weight is undetermined will return 400 as default.
- Type
- number
Example
utils.fontWeightStrToNum('extra bold'); // Returns 800
(static) getObjPath(object, path) → {Object}
Returns the value of an object at a given path.
Supports array indexes in the path. Eg. `path.to.arr[7]`.
Supports array indexes in the path. Eg. `path.to.arr[7]`.
Parameters:
Name | Type | Description |
---|---|---|
object |
Object | The target object. |
path |
string | The path to retrieve, as a single string with dot syntax. |
- Source:
Returns:
value - The value retrieved.
- Type
- Object
Example
let obj = {foo:{bar:['a','b','c']}}
getObjPath(obj, 'foo.bar[1]'); // Returns `b`
(static) getProp(element, property) → {integer}
Gets the computed style of the given HTML DOM element as a number value.
Parameters:
Name | Type | Description |
---|---|---|
element |
DOMElement | The DOM element to target. |
property |
string | The property to retrieve. Eg. `width`. |
- Source:
Returns:
value - The property value.
- Type
- integer
(static) getQueryVar() → {string}
Retrieves a query variable from the current url.
- Source:
Returns:
value
- Type
- string
(static) globMatch(subject, glob) → {boolean}
Returns if a string matches a glob pattern.
Supports patterns such as '!pattern','pattern*','!dingo*','*dingo'.
Supports patterns such as '!pattern','pattern*','!dingo*','*dingo'.
Parameters:
Name | Type | Description |
---|---|---|
subject |
string | The subject string. |
glob |
string | The glob pattern. |
- Source:
Returns:
match - Whether the subject matched the glob pattern.
- Type
- boolean
Example
utils.globMatch('kitten', '*ten'); // Returns true.
(static) hasClass(htmlEle, className) → {boolean}
Determines if the given HTML DOM Element has the class applied.
Parameters:
Name | Type | Description |
---|---|---|
htmlEle |
DOMElement | The target element. |
className |
string | The CSS class name. |
- Source:
Returns:
classExists - Whether the class is applied.
- Type
- boolean
(static) isImgLoaded(image) → {boolean}
Returns wheather the given image is loaded.
Parameters:
Name | Type | Description |
---|---|---|
image |
string | DOMElement | The id of the DOM element or the DOM element itself. |
- Source:
Returns:
loaded - The load state.
- Type
- boolean
(static) isTouchDevice() → {boolean}
Determines if currently running on a touch device.
- Source:
Returns:
isTouch
- Type
- boolean
(static) killWait(delayedCall)
Cancels a queued wait call.
Parameters:
Name | Type | Description |
---|---|---|
delayedCall |
gsap.delayedCall | The result of `utils.wait`. |
- Source:
Example
let delayedCall = utils.wait(this, 2.0, this.delayCall);
utils.killWait(delayedCall); // Cancels delay call
(static) killWaitsFor(callback)
Enforces a minimum wait time between multiple calls to a function.
Parameters:
Name | Type | Description |
---|---|---|
callback |
function | Function associated with `utils.wait` call. |
- Source:
Example
utils.wait(this, 2.0, this.delayCall);
utils.killWaitsFor(this.delayCall); // Cancels delay call
(static) lightenCol(color, brightness) → {int}
Lightens color by set percentage.
Parameters:
Name | Type | Description |
---|---|---|
color |
int | Color value, eg. 0xff3300. |
brightness |
number | Amount to brighten in the range of 0.0 to 1.0. |
- Source:
Returns:
colorResult - Lightened color value.
- Type
- int
Example
utils.lightenCol(0xff3300, 0.5); // 50% lighter
(static) loadScript(jsFilePath, loadCallback)
Loads a js document at runtime.
Parameters:
Name | Type | Description |
---|---|---|
jsFilePath |
string | The path to js file. |
loadCallback |
function | The function to call after script is loaded. |
- Source:
Example
loadScript('js/my-script.js', this.onScriptLoaded.bind(this))
(static) pad(subject, targetLength, padCharopt, padBeforeopt) → {string}
Pads string to a given length with supplied character.
Supports array indexes in the path. Eg. `path.to.arr[7]`.
Supports array indexes in the path. Eg. `path.to.arr[7]`.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
subject |
string | number | The target to pad. | ||
targetLength |
integer | The desired string length. | ||
padChar |
string |
<optional> |
'0' | The pad character to use. |
padBefore |
boolean |
<optional> |
true | If true: pad chars will be added to the start of the subject, otherwise will be added to the end. |
- Source:
Returns:
padded - The padded string.
- Type
- string
Example
pad(777, 5); // Returns `00777`
(static) removeClass(htmlEle, className)
Removes class from HTML DOM Element.
Parameters:
Name | Type | Description |
---|---|---|
htmlEle |
DOMElement | The target element. |
className |
string | The CSS class name. |
- Source:
(static) requireAll(requireContext) → {Array.<*>}
Requires all files with given context and returns an array of document content.
>Note: Webpack may need to be configured to process the required file type.
>Note: Webpack may need to be configured to process the required file type.
Parameters:
Name | Type | Description |
---|---|---|
requireContext |
RequireContext | A directory to search, a flag indicating whether subdirectories should be searched too, and a regular expression to match files against. |
- Source:
Returns:
documentContent - Each entry in the array represents the content of a matched document.
- Type
- Array.<*>
Example
const _psdInfo = requireAll(require.context('./ui', false, /.json$/));
(static) requireAllLookup(requireContext) → {Object.<string>}
Requires all files with given context and returns an object with the original filename as the key.
Note: Webpack may need to be configured to process the required file type. See: https://webpack.js.org/guides/asset-modules/
Note: Webpack may need to be configured to process the required file type. See: https://webpack.js.org/guides/asset-modules/
Parameters:
Name | Type | Description |
---|---|---|
requireContext |
RequireContext | A directory to search, a flag indicating whether subdirectories should be searched too, and a regular expression to match files against. |
- Source:
Returns:
documentContent - An object with document filenames set at the key, populated with its content.
- Type
- Object.<string>
(static) setObjPathVal(object, path, value)
Set the path of an object with supplied value.
If the path doesn't exist then it will be created.
If the existing value is numeric and the value is a string prefixed with `-=` or `+=`, then the value will be updated relative to its existing value.
If the path doesn't exist then it will be created.
If the existing value is numeric and the value is a string prefixed with `-=` or `+=`, then the value will be updated relative to its existing value.
Parameters:
Name | Type | Description |
---|---|---|
object |
Object | The target object. |
path |
string | The path to set, as a single string with dot syntax. |
value |
* | The value to apply. |
- Source:
Example
let obj = {foo:{bar:123}}
setObjPathVal(obj, 'foo.bar', 321);
(static) shuffle(array)
Shuffles the element order of an array.
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | Array to shuffle. |
- Source:
Example
let tmp = ['one','two','three'];
utils.shuffle(tmp);
(static) wait(thisScopeopt, delay, callback, callpackParamsopt) → {gsap.delayedCall}
Calls function after set delay.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
thisScope |
Object |
<optional> |
null | Optionally supply a scope in which to call the function. |
delay |
number | The delay in seconds. | ||
callback |
function | The function to call. | ||
callpackParams |
Array |
<optional> |
null | An array of parameters to supply to the callback function. |
- Source:
Returns:
delayedCall - The queued GSAP delayed call instance.
- Type
- gsap.delayedCall
Example
utils.wait(this, 2.0, this.delayCall);