Skip to main content

utils

Utils : d2/sdk/utils/utils

Utility functions.

Extends: Object

Utils.getAppContext() ⇒ string

Get context path name of D2 Smart View deployment

Kind: static method of Utils
Returns: string - the context path name

Utils.getStoreKey(key) ⇒ string

Get a properly namespaced storage name for the given key. Used while creating a boundary within localStorage or sessionStorage. See getLocalStore and getSessionStore below.

Kind: static method of Utils
Returns: string - namespaced key name

ParamTypeDescription
keystring

original key name

Utils.getLocalStore(key) ⇒ NamedStorage

Get a local storage area identified by the given key. A new storage area gets created if doesn't exist already.

Kind: static method of Utils
Returns: NamedStorage - local store instance

ParamTypeDescription
keystring

Storage identifier

Utils.getSessionStore(key) ⇒ NamedStorage

Get a session storage area identified by the given key. A new storage area gets created if doesn't exist already.

Kind: static method of Utils
Returns: NamedStorage - session store instance

ParamTypeDescription
keystring

Storage identifier

Utils.isMobile() ⇒ boolean

Detects whether the D2 Smart View runtime platform is a mobile

Kind: static method of Utils
Returns: boolean - true if it's being run on mobile, false otherwise.

Utils.isAppworks() ⇒ boolean

Detects whether the D2 Smart View runtime platform is appworks.

Kind: static method of Utils
Returns: boolean - true if it's Appworks.

Open the given URL in window.

Kind: static method of Utils

ParamTypeDefaultDescription
urlstring

The URL to open

[wnd]Window

The window where url will be opened. Defaults to current global window object.

[focus]booleanfalse

Whether to focus the window

[keepLink]booleanfalse

Whether to keep the opener reference. true keeps it.

Utils.makeBatchAjaxCall(options) ⇒ jQuery.Promise

Make a single D2-REST AJAX call with a batch of operations.

Kind: static method of Utils
Returns: jQuery.Promise - The returned promise fails on AJAX error. It resolves otherwise invoking BatchDoneCallback as documented below.

ParamTypeDefaultDescription
optionsobject

Holder of different options

options.connectorConnector

Transport for the AJAX call. See Connector.

options.operationsArray.<BatchOperationRequest>

Operations to be perfomred in this batch.

[options.transactional]booleanfalse

Whether this batch is to be executed as a transaction. A transactional batch operates with 'Either all or none' paradigm.

[options.sequential]booleanfalse

For a non-transactional batch, whether the operations are to be run sequentially or parrallely.

[options.continueOnError]booleanfalse

Whether remaining operations are to be run if one fails.

Example

define([
'd2/sdk/utils/contexts/context.utils',
'd2/sdk/utils/utils',
'd2/sdk/utils/contexts/factories/connector'
], function(contextUtil, util, ConnectorFactory){
'use strict';

// Retrieve document menu actions and favorited status for object '0c0015fa8000291e' in a single batch call
util.makeBatchAjaxCall({
connector: contextUtil.getPerspectiveContext().getObject(ConnectorFactory),
continueOnError: true,
operations: [
{
id: 'favorites',
request: {
method: 'GET',
uri: '/repositories/d2repo/favorites-checker?object-ids=0c0015fa8000291e'
}
},
{
id: 'actions',
request: {
method: 'GET',
uri: '/repositories/d2repo/actions?type=MenuContext&widgetType=DoclistWidget&include-subtype=true&object-ids=0c0015fa8000291e'
}
}
]
})
.done(function(){
console.log('batch call success');
})
.fail(function(){
console.log('batch call failed');
});
});

Build complete URL from a D2-REST link

Kind: static method of Utils
Returns: string - the complete URL.

ParamTypeDescription
linkObject

The link as is from a D2-REST response

isTemplateboolean

Is this D2-REST link a templated one or regular.

Utils.parseAjaxError(xhr, [textStatus], [errorThrown], [includeDetails]) ⇒ ParsedErrorType

Parse AJAX error happened as a result of D2-REST API call and return a normalized error structure.

Kind: static method of Utils
Returns: ParsedErrorType - the parsed error

ParamTypeDefaultDescription
xhrexternal:XMLHttpRequest

The XHR transport

[textStatus]string

Textual status associated with the error

[errorThrown]string

Custom error message in case the XHR response could not be parsed.

[includeDetails]booleanfalse

Whether to include a errorDetails field in the prased error structure.

Utils.parseAjaxErrorAsString(xhr, [textStatus], [errorThrown], [includeDetails]) ⇒ string

Parse AJAX error happened as a result of D2-REST API call and return a compact string representing it.

Kind: static method of Utils
Returns: string - the parsed error

ParamTypeDefaultDescription
xhrexternal:XMLHttpRequest

The XHR transport

[textStatus]string

Textual status associated with the error

[errorThrown]string

Custom error message in case the XHR response could not be parsed.

[includeDetails]booleanfalse

Whether to include a errorDetails field in the prased error structure.

Utils.parseError(responseType, responseData) ⇒ ParsedErrorType

Extract error from custom object and format it similar to return format of parseAjaxError.

Kind: static method of Utils
Returns: ParsedErrorType - the parsed error

ParamTypeDescription
responseTypestring

Hint of how to parse responseData. Can be any of 'xml', 'json' or 'object'

responseDataJSON | string

If responseType is 'object' then a JSON object, otherwise a string serilized error structure. Irrespective of its type, every input is expected to have a code, message and details field in it.

Utils.parseErrorAsString(responseType, responseData) ⇒ string

Extract error from custom object and return a compact string representing it.

Kind: static method of Utils
Returns: string - the parsed error

ParamTypeDescription
responseTypestring

Hint of how to parse responseData. Can be any of 'xml', 'json' or 'object'

responseDataJSON | string

If responseType is 'object' then a JSON object, otherwise a string serilized error structure. Irrespective of its type, every input is expected to have a code, message and details field in it.

Utils.copyToClipboard(clipboardText) ⇒ boolean

Set some text content to the browser clipboard

Kind: static method of Utils
Returns: boolean - true indicates the copy is successful.

ParamTypeDescription
clipboardTextstring

The content to be copied.

Utils.isJqueryPromise(value) ⇒ boolean

Checks whether a given value is a jQuery.Promise instance or not

Kind: static method of Utils
Returns: boolean - true if it's a jQuery.Promise, false otherwise.

ParamTypeDescription
value*

The test value

Utils.isNonEmptyString(str) ⇒ boolean

Checks whether a given value is a non-empty string

Kind: static method of Utils
Returns: boolean - true if it's a string

ParamTypeDescription
str*

The test value

Utils.areSameFunctions(fn1, fn2) ⇒ boolean

Compare two functions for equality

Kind: static method of Utils
Returns: boolean - true only if Left hand side is equal to right hand side.

ParamTypeDescription
fn1function

Left hand side

fn2function

Right hand side