ffr-components
Version:
Fiori styled UI components
136 lines (122 loc) • 3.13 kB
JavaScript
import _typeof from "@babel/runtime/helpers/esm/typeof";
export var isMobile = function isMobile() {
var userAgent = window.navigator.userAgent;
return /Mobile/.test(userAgent);
};
export var KeyCode = {
ENTER: 13,
CTRL: 17,
ALT: 18,
ESC: 27,
SPACE: 32,
DOWN: 40,
LEFT: 37,
UP: 38,
RIGHT: 39,
TAB: 9
};
export var isClickedKey = function isClickedKey(keyCode) {
return keyCode === KeyCode.SPACE || keyCode === KeyCode.ENTER;
};
var __isType = function __isType(type) {
return function (oTarget) {
return Object.prototype.toString.call(oTarget).replace(/^.*\s(.*)]$/, '$1') === type;
};
};
/**
* export functions:
* isString
* isObject
* isNumber
* isUndefined
* isFunction
* isArray
* isNull
*/
var oType = {};
['String', 'Object', 'Number', 'Undefined', 'Function', 'Array', 'Null', 'Boolean'].forEach(function (_type) {
oType["is".concat(_type)] = __isType(_type);
});
/**
* function: check an object is Promise or not
* @param obj
* @returns {string}
*/
oType.isPromise = function (obj) {
return __isType('Promise')(obj) || (oType.isObject(obj) || oType.isFunction(obj)) && oType.isFunction(obj.then);
};
export var hasClass = function hasClass(obj, cls) {
cls = String(cls).trim();
var classArray = String(obj.className).split(' ');
classArray.map(function (x) {
return String(x).trim();
});
return classArray.indexOf(cls) !== -1;
};
export var addClass = function addClass(obj, cls) {
if (!hasClass(obj, cls)) obj.className += " ".concat(cls);
};
export var removeClass = function removeClass(obj, cls) {
if (hasClass(obj, cls)) {
obj.className = String(obj.className).split(cls).map(function (x) {
return String(x).trim();
}).join(' ');
}
};
/**
* function: get the type of the given parameter
* @param o
* @returns {string}
*/
export function getType(o) {
var _t = null;
return ((_t = _typeof(o)) === 'object' ? o == null && 'null' || Object.prototype.toString.call(o).slice(8, -1) : _t).toLowerCase();
}
/**
* function: deep clone
* @param data
* @returns {*}
*/
export var deepClone = function deepClone(data) {
try {
data = JSON.parse(JSON.stringify(data));
} catch (e) {
console.error(e);
}
return data;
};
export default oType;
export var getRandom = function getRandom() {
return Math.floor(Math.random() * 10000);
};
export var throttle = function throttle(fn, delay) {
var timer = null;
var previous = new Date();
var tFn = function tFn() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var now = new Date(),
remaining = now - previous;
if (remaining >= delay) {
if (timer) {
clearTimeout(timer);
}
fn.apply(null, args);
previous = now;
} else {
if (!timer) {
timer = setTimeout(function () {
fn.apply(null, args);
previous = new Date();
}, delay - remaining);
}
}
};
tFn.cancel = function () {
if (timer !== null) {
clearTimeout(timer);
}
};
return tFn;
};