ant-design-blazor/components/core/JsInterop/interop.ts

155 lines
3.6 KiB
TypeScript
Raw Normal View History

export function getDom(element) {
2020-03-15 23:54:48 +08:00
if (!element) {
element = document.body;
} else if (typeof element === 'string') {
element = document.querySelector(element);
}
return element;
2020-03-11 16:49:25 +08:00
}
2020-03-10 15:25:29 +08:00
2020-03-11 16:49:25 +08:00
export function getDomInfo(element) {
2020-03-15 23:54:48 +08:00
var result = {};
2020-03-10 15:25:29 +08:00
2020-03-15 23:54:48 +08:00
var dom = getDom(element);
2020-03-11 16:49:25 +08:00
2020-03-15 23:54:48 +08:00
for (var key in dom) {
var item = dom[key];
if (!item) continue;
if (typeof item === 'string' || (typeof item === 'number' && !isNaN(item))) result[key] = item;
}
2020-03-10 15:25:29 +08:00
result["absoluteTop"] = getAbsoluteTop(dom);
result["absoluteLeft"] = getAbsoluteLeft(dom);
2020-03-15 23:54:48 +08:00
return result;
2020-03-10 15:25:29 +08:00
}
export function getBoundingClientRect(element) {
2020-03-15 23:54:48 +08:00
let dom = getDom(element);
return dom.getBoundingClientRect();
2020-03-10 15:25:29 +08:00
}
2020-03-11 16:49:25 +08:00
export function addDomEventListener(element, eventName, invoker) {
2020-03-15 23:54:48 +08:00
let callback = args => {
const obj = {};
for (let k in args) {
obj[k] = args[k];
}
let json = JSON.stringify(obj, (k, v) => {
if (v instanceof Node) return 'Node';
if (v instanceof Window) return 'Window';
return v;
}, ' ');
invoker.invokeMethodAsync('Invoke', json);
2020-03-15 23:54:48 +08:00
};
2020-03-11 16:40:07 +08:00
2020-03-15 23:54:48 +08:00
if (element == 'window') {
window.addEventListener(eventName, callback);
} else {
let dom = getDom(element);
(dom as HTMLElement).addEventListener(eventName, callback);
2020-03-15 23:54:48 +08:00
}
2020-03-10 15:25:29 +08:00
}
2020-03-15 23:54:48 +08:00
export function matchMedia(query) {
return window.matchMedia(query).matches;
2020-03-10 15:25:29 +08:00
}
function fallbackCopyTextToClipboard(text) {
2020-03-15 23:54:48 +08:00
var textArea = document.createElement("textarea");
textArea.value = text;
2020-03-10 15:25:29 +08:00
2020-03-15 23:54:48 +08:00
// Avoid scrolling to bottom
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";
2020-03-15 23:54:48 +08:00
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
2020-03-15 23:54:48 +08:00
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Fallback: Copying text command was ' + msg);
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
}
2020-03-15 23:54:48 +08:00
document.body.removeChild(textArea);
}
export function copy(text) {
2020-03-15 23:54:48 +08:00
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text).then(function () {
console.log('Async: Copying to clipboard was successful!');
}, function (err) {
console.error('Async: Could not copy text: ', err);
});
2020-03-11 16:49:25 +08:00
}
export function focus(selector) {
let dom = getDom(selector);
dom.focus();
}
export function blur(selector) {
let dom = getDom(selector);
dom.blur();
}
export function log(text) {
2020-03-15 23:54:48 +08:00
console.log(text);
}
export function BackTop(element) {
let dom = document.getElementById("BodyContainer");
dom.scrollTo(0, 0);
}
export function getFirstChildDomInfo(element) {
var dom = getDom(element);
return getDomInfo(dom.firstElementChild);
}
export function addClsToFirstChild(element, className) {
var dom = getDom(element);
if (dom.firstElementChild) {
dom.firstElementChild.classList.add(className);
}
}
export function addDomEventListenerToFirstChild(element, eventName, invoker) {
var dom = getDom(element);
if (dom.firstElementChild) {
addDomEventListener(dom.firstElementChild, eventName, invoker);
}
}
export function getAbsoluteTop(e) {
var offset = e.offsetTop;
if (e.offsetParent != null) {
offset += getAbsoluteTop(e.offsetParent);
}
return offset;
}
export function getAbsoluteLeft(e) {
var offset = e.offsetLeft;
if (e.offsetParent != null) {
offset += getAbsoluteLeft(e.offsetParent);
}
return offset;
}
export function addElementToBody(element) {
document.body.appendChild(element);
}
export function delElementFromBody(element) {
document.body.removeChild(element);
}