cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
150 lines (149 loc) • 2.87 kB
JavaScript
function isObject(obj) {
return obj !== null && typeof obj === "object" && "constructor" in obj && obj.constructor === Object;
}
function extend(target, src) {
if (target === void 0) {
target = {};
}
if (src === void 0) {
src = {};
}
Object.keys(src).forEach(function(key) {
if (typeof target[key] === "undefined")
target[key] = src[key];
else if (isObject(src[key]) && isObject(target[key]) && Object.keys(src[key]).length > 0) {
extend(target[key], src[key]);
}
});
}
var ssrDocument = {
body: {},
addEventListener: function() {
},
removeEventListener: function() {
},
activeElement: {
blur: function() {
},
nodeName: ""
},
querySelector: function() {
return null;
},
querySelectorAll: function() {
return [];
},
getElementById: function() {
return null;
},
createEvent: function() {
return {
initEvent: function() {
}
};
},
createElement: function() {
return {
children: [],
childNodes: [],
style: {},
setAttribute: function() {
},
getElementsByTagName: function() {
return [];
}
};
},
createElementNS: function() {
return {};
},
importNode: function() {
return null;
},
location: {
hash: "",
host: "",
hostname: "",
href: "",
origin: "",
pathname: "",
protocol: "",
search: ""
}
};
function getDocument() {
var doc = typeof document !== "undefined" ? document : {};
extend(doc, ssrDocument);
return doc;
}
var ssrWindow = {
document: ssrDocument,
navigator: {
userAgent: ""
},
location: {
hash: "",
host: "",
hostname: "",
href: "",
origin: "",
pathname: "",
protocol: "",
search: ""
},
history: {
replaceState: function() {
},
pushState: function() {
},
go: function() {
},
back: function() {
}
},
CustomEvent: function CustomEvent() {
return this;
},
addEventListener: function() {
},
removeEventListener: function() {
},
getComputedStyle: function() {
return {
getPropertyValue: function() {
return "";
}
};
},
Image: function() {
},
Date: function() {
},
screen: {},
setTimeout: function() {
},
clearTimeout: function() {
},
matchMedia: function() {
return {};
},
requestAnimationFrame: function(callback) {
if (typeof setTimeout === "undefined") {
callback();
return null;
}
return setTimeout(callback, 0);
},
cancelAnimationFrame: function(id) {
if (typeof setTimeout === "undefined") {
return;
}
clearTimeout(id);
}
};
function getWindow() {
var win = typeof window !== "undefined" ? window : {};
extend(win, ssrWindow);
return win;
}
export { extend, getDocument, getWindow, ssrDocument, ssrWindow };