radio/libjs/utils/globalMethods.js

105 lines
3.2 KiB
JavaScript

"use strict";
global.W = {
extend: function () {
var lTarget = arguments[0] || {};
var lIndex = 1;
var lLength = arguments.length;
var lDeep = false;
var lOptions, lName, lSrc, lCopy, lCopyIsArray, lClone;
// Handle a deep copy situation
if (typeof lTarget === "boolean") {
lDeep = lTarget;
lTarget = arguments[1] || {};
// skip the boolean and the target
lIndex = 2;
}
// Handle case when target is a string or something (possible in deep
// copy)
if (typeof lTarget !== "object" && typeof lTarget != "function") {
lTarget = {};
}
if (lLength === lIndex) {
lTarget = this;
--lIndex;
}
for (; lIndex < lLength; lIndex++) {
// Only deal with non-null/undefined values
if ((lOptions = arguments[lIndex]) != undefined) {
// Extend the base object
for (lName in lOptions) {
lSrc = lTarget[lName];
lCopy = lOptions[lName];
// Prevent never-ending loop
if (lTarget === lCopy) {
continue;
}
// Recurse if we're merging plain objects or arrays
if (lDeep && lCopy && (Object.isObject(lCopy) || (lCopyIsArray = Array.isArray(lCopy)))) {
if (lCopyIsArray) {
lCopyIsArray = false;
lClone = lSrc && Array.isArray(lSrc) ? lSrc : [];
} else {
lClone = lSrc && Object.isObject(lSrc) ? lSrc : {};
}
// Never move original objects, clone them
lTarget[lName] = Object.extend(lDeep, lClone, lCopy);
// Don't bring in undefined values
} else {
if (lCopy !== undefined) {
lTarget[lName] = lCopy;
}
}
}
}
}
// Return the modified object
return lTarget;
},
touchToMouse: function (e) {
e.preventDefault();
if (e.touches.length > 1 || (e.type == "touchend" && e.touches.length > 0))
return;
var type = null;
var touch = null;
var src = e.currentTarget;
switch (e.type) {
case "touchstart":
type = "mousedown";
touch = e.changedTouches[0];
break;
case "touchmove":
type = "mousemove";
touch = e.changedTouches[0];
src = window;
break;
case "touchend":
type = "mouseup";
touch = e.changedTouches[0];
src = window;
break;
}
var event = new MouseEvent(type, {
button: 0,
screenX: touch.screenX,
screenY: touch.screenY,
clientX: touch.clientX,
clientY: touch.clientY
});
src.dispatchEvent(event);
}
};