2020-04-23 17:13:56 +08:00
|
|
|
export function getDom(element) {
|
2020-06-03 23:11:02 +08:00
|
|
|
if (!element) {
|
|
|
|
element = document.body;
|
|
|
|
} else if (typeof element === 'string') {
|
2020-07-23 22:51:02 +08:00
|
|
|
if (element === 'document') {
|
|
|
|
return document;
|
|
|
|
}
|
2020-06-03 23:11:02 +08:00
|
|
|
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-06-03 23:11:02 +08:00
|
|
|
var result = {};
|
2020-03-10 15:25:29 +08:00
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
var dom = getDom(element);
|
2020-03-11 16:49:25 +08:00
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
result["offsetTop"] = dom.offsetTop || 0;
|
|
|
|
result["offsetLeft"] = dom.offsetLeft || 0;
|
|
|
|
result["offsetWidth"] = dom.offsetWidth || 0;
|
|
|
|
result["offsetHeight"] = dom.offsetHeight || 0;
|
|
|
|
result["scrollHeight"] = dom.scrollHeight || 0;
|
|
|
|
result["scrollWidth"] = dom.scrollWidth || 0;
|
|
|
|
result["scrollLeft"] = dom.scrollLeft || 0;
|
|
|
|
result["scrollTop"] = dom.scrollTop || 0;
|
|
|
|
result["clientTop"] = dom.clientTop || 0;
|
|
|
|
result["clientLeft"] = dom.clientLeft || 0;
|
|
|
|
result["clientHeight"] = dom.clientHeight || 0;
|
|
|
|
result["clientWidth"] = dom.clientWidth || 0;
|
2021-04-18 16:06:06 +08:00
|
|
|
result["selectionStart"] = dom.selectionStart || 0;
|
2020-11-12 16:53:14 +08:00
|
|
|
var absolutePosition = getElementAbsolutePos(dom);
|
|
|
|
result["absoluteTop"] = Math.round(absolutePosition.y);
|
|
|
|
result["absoluteLeft"] = Math.round(absolutePosition.x);
|
2020-05-03 17:22:33 +08:00
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
return result;
|
2020-03-10 15:25:29 +08:00
|
|
|
}
|
|
|
|
|
2020-11-12 16:53:14 +08:00
|
|
|
function getElementAbsolutePos(element) {
|
2021-01-09 23:57:31 +08:00
|
|
|
var res: any = new Object();
|
|
|
|
res.x = 0; res.y = 0;
|
|
|
|
if (element !== null) {
|
|
|
|
if (element.getBoundingClientRect) {
|
|
|
|
var viewportElement = document.documentElement;
|
|
|
|
var box = element.getBoundingClientRect();
|
|
|
|
var scrollLeft = viewportElement.scrollLeft;
|
|
|
|
var scrollTop = viewportElement.scrollTop;
|
|
|
|
|
|
|
|
res.x = box.left + scrollLeft;
|
|
|
|
res.y = box.top + scrollTop;
|
2020-11-12 16:53:14 +08:00
|
|
|
}
|
2021-01-09 23:57:31 +08:00
|
|
|
}
|
|
|
|
return res;
|
2020-11-12 16:53:14 +08:00
|
|
|
}
|
|
|
|
|
2020-07-02 14:08:51 +08:00
|
|
|
export function addFileClickEventListener(btn) {
|
2020-09-02 15:15:59 +08:00
|
|
|
if ((btn as HTMLElement).addEventListener) {
|
|
|
|
(btn as HTMLElement).addEventListener("click", fileClickEvent);
|
|
|
|
}
|
2020-07-02 14:08:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function removeFileClickEventListener(btn) {
|
2020-09-02 15:15:59 +08:00
|
|
|
(btn as HTMLElement).removeEventListener("click", fileClickEvent);
|
2020-07-02 14:08:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function fileClickEvent() {
|
|
|
|
var fileId = this.attributes["data-fileid"].nodeValue;
|
|
|
|
var element = document.getElementById(fileId);
|
|
|
|
(element as HTMLInputElement).click();
|
|
|
|
}
|
|
|
|
|
2020-07-01 06:49:51 +08:00
|
|
|
export function clearFile(element) {
|
|
|
|
element.setAttribute("type", "input");
|
|
|
|
element.value = '';
|
|
|
|
element.setAttribute("type", "file");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getFileInfo(element) {
|
|
|
|
if (element.files && element.files.length > 0) {
|
2020-07-21 00:16:02 +08:00
|
|
|
var fileInfo = [];
|
|
|
|
for (var i = 0; i < element.files.length; i++) {
|
|
|
|
var file = element.files[i];
|
2021-04-03 00:23:00 +08:00
|
|
|
var objectUrl = getObjectURL(file);
|
2020-07-21 00:16:02 +08:00
|
|
|
fileInfo.push({
|
|
|
|
fileName: file.name,
|
|
|
|
size: file.size,
|
|
|
|
objectURL: objectUrl,
|
|
|
|
type: file.type
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-01 06:49:51 +08:00
|
|
|
return fileInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 00:23:00 +08:00
|
|
|
export function getObjectURL(file: File) {
|
2020-07-01 06:49:51 +08:00
|
|
|
var url = null;
|
|
|
|
if (window.URL != undefined) {
|
|
|
|
url = window.URL.createObjectURL(file);
|
|
|
|
} else if (window.webkitURL != undefined) {
|
|
|
|
url = window.webkitURL.createObjectURL(file);
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2020-07-21 15:50:38 +08:00
|
|
|
export function uploadFile(element, index, data, headers, fileId, url, name, instance, percentMethod, successMethod, errorMethod) {
|
2020-07-01 06:49:51 +08:00
|
|
|
let formData = new FormData();
|
2020-07-21 00:16:02 +08:00
|
|
|
var file = element.files[index];
|
2020-07-01 06:49:51 +08:00
|
|
|
var size = file.size;
|
2020-07-21 15:50:38 +08:00
|
|
|
formData.append(name, file);
|
|
|
|
if (data != null) {
|
|
|
|
for (var key in data) {
|
|
|
|
formData.append(key, data[key]);
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 06:49:51 +08:00
|
|
|
const req = new XMLHttpRequest()
|
|
|
|
req.onreadystatechange = function () {
|
|
|
|
if (req.readyState === 4) {
|
2020-07-13 11:54:53 +08:00
|
|
|
if (req.status != 200) {
|
|
|
|
instance.invokeMethodAsync(errorMethod, fileId, `{"status": ${req.status}}`);
|
2020-07-01 06:49:51 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
instance.invokeMethodAsync(successMethod, fileId, req.responseText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
req.upload.onprogress = function (event) {
|
|
|
|
var percent = Math.floor(event.loaded / size * 100);
|
|
|
|
instance.invokeMethodAsync(percentMethod, fileId, percent);
|
|
|
|
}
|
|
|
|
req.onerror = function (e) {
|
|
|
|
instance.invokeMethodAsync(errorMethod, fileId, "error");
|
|
|
|
}
|
|
|
|
req.open('post', url, true)
|
|
|
|
if (headers != null) {
|
|
|
|
for (var header in headers) {
|
|
|
|
req.setRequestHeader(header, headers[header]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
req.send(formData)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function triggerEvent(element, eventType, eventName) {
|
2020-07-02 14:08:51 +08:00
|
|
|
var dom = element as HTMLInputElement;
|
2020-07-01 06:49:51 +08:00
|
|
|
var evt = document.createEvent(eventType);
|
|
|
|
evt.initEvent(eventName);
|
|
|
|
return dom.dispatchEvent(evt);
|
|
|
|
}
|
|
|
|
|
2020-03-10 15:25:29 +08:00
|
|
|
export function getBoundingClientRect(element) {
|
2020-06-03 23:11:02 +08:00
|
|
|
let dom = getDom(element);
|
2020-10-14 13:56:59 +08:00
|
|
|
if (dom && dom.getBoundingClientRect) {
|
2020-07-21 00:16:02 +08:00
|
|
|
return dom.getBoundingClientRect();
|
|
|
|
}
|
|
|
|
return null;
|
2020-03-10 15:25:29 +08:00
|
|
|
}
|
|
|
|
|
2021-01-21 17:20:10 +08:00
|
|
|
export function addDomEventListener(element, eventName, preventDefault, invoker) {
|
2020-06-03 23:11:02 +08:00
|
|
|
let callback = args => {
|
|
|
|
const obj = {};
|
2021-02-22 11:29:56 +08:00
|
|
|
for (let k in args) {
|
2021-02-14 17:42:44 +08:00
|
|
|
if (k !== 'originalTarget') { //firefox occasionally raises Permission Denied when this property is being stringified
|
|
|
|
obj[k] = args[k];
|
|
|
|
}
|
2021-02-22 11:29:56 +08:00
|
|
|
}
|
|
|
|
let json = JSON.stringify(obj, (k, v) => {
|
2020-06-03 23:11:02 +08:00
|
|
|
if (v instanceof Node) return 'Node';
|
|
|
|
if (v instanceof Window) return 'Window';
|
|
|
|
return v;
|
2021-04-15 14:19:26 +08:00
|
|
|
}, ' ');
|
|
|
|
setTimeout(function () { invoker.invokeMethodAsync('Invoke', json) }, 0);
|
2021-01-21 17:20:10 +08:00
|
|
|
if (preventDefault === true) {
|
|
|
|
args.preventDefault();
|
|
|
|
}
|
2020-06-03 23:11:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (element == 'window') {
|
|
|
|
if (eventName == 'resize') {
|
|
|
|
window.addEventListener(eventName, debounce(() => callback({ innerWidth: window.innerWidth, innerHeight: window.innerHeight }), 200, false));
|
2020-05-10 22:43:58 +08:00
|
|
|
} else {
|
2020-06-03 23:11:02 +08:00
|
|
|
window.addEventListener(eventName, callback);
|
2020-04-22 19:37:06 +08:00
|
|
|
}
|
2020-06-03 23:11:02 +08:00
|
|
|
} else {
|
2021-02-22 11:29:56 +08:00
|
|
|
let dom = getDom(element);
|
2020-06-03 23:11:02 +08:00
|
|
|
(dom as HTMLElement).addEventListener(eventName, callback);
|
|
|
|
}
|
2020-03-10 15:25:29 +08:00
|
|
|
}
|
|
|
|
|
2020-03-15 23:54:48 +08:00
|
|
|
export function matchMedia(query) {
|
2020-06-03 23:11:02 +08:00
|
|
|
return window.matchMedia(query).matches;
|
2020-03-10 15:25:29 +08:00
|
|
|
}
|
|
|
|
|
2020-03-12 16:35:23 +08:00
|
|
|
function fallbackCopyTextToClipboard(text) {
|
2020-06-03 23:11:02 +08:00
|
|
|
var textArea = document.createElement("textarea");
|
|
|
|
textArea.value = text;
|
|
|
|
|
|
|
|
// Avoid scrolling to bottom
|
|
|
|
textArea.style.top = "0";
|
|
|
|
textArea.style.left = "0";
|
|
|
|
textArea.style.position = "fixed";
|
|
|
|
|
|
|
|
document.body.appendChild(textArea);
|
|
|
|
textArea.focus();
|
|
|
|
textArea.select();
|
2020-03-12 16:35:23 +08:00
|
|
|
|
2020-06-03 23:11:02 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.body.removeChild(textArea);
|
2020-03-12 16:35:23 +08:00
|
|
|
}
|
|
|
|
export function copy(text) {
|
2020-06-03 23:11:02 +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
|
|
|
}
|
2020-03-12 13:00:54 +08:00
|
|
|
|
2021-04-13 13:12:18 +08:00
|
|
|
export function focus(selector, noScroll: boolean=false) {
|
|
|
|
let dom = getDom(selector);
|
|
|
|
if (!(dom instanceof HTMLElement))
|
2021-04-15 14:19:26 +08:00
|
|
|
throw new Error("Unable to focus an invalid element.");
|
2021-04-13 13:12:18 +08:00
|
|
|
dom.focus({
|
2021-04-15 14:19:26 +08:00
|
|
|
preventScroll: noScroll
|
2021-04-13 13:12:18 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hasFocus(selector) {
|
2020-06-03 23:11:02 +08:00
|
|
|
let dom = getDom(selector);
|
2021-04-13 13:12:18 +08:00
|
|
|
return (document.activeElement === dom);
|
2020-03-25 00:32:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function blur(selector) {
|
2020-06-03 23:11:02 +08:00
|
|
|
let dom = getDom(selector);
|
|
|
|
dom.blur();
|
2020-03-25 00:32:26 +08:00
|
|
|
}
|
|
|
|
|
2020-03-12 13:00:54 +08:00
|
|
|
export function log(text) {
|
2020-06-03 23:11:02 +08:00
|
|
|
console.log(text);
|
2020-03-27 17:24:16 +08:00
|
|
|
}
|
|
|
|
|
2021-01-20 15:53:32 +08:00
|
|
|
export function backTop(target: string) {
|
2020-10-09 10:59:52 +08:00
|
|
|
let dom = getDom(target);
|
|
|
|
if (dom) {
|
|
|
|
slideTo(dom.scrollTop);
|
|
|
|
} else {
|
|
|
|
slideTo(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function slideTo(targetPageY) {
|
|
|
|
var timer = setInterval(function () {
|
|
|
|
var currentY = document.documentElement.scrollTop || document.body.scrollTop;
|
|
|
|
var distance = targetPageY > currentY ? targetPageY - currentY : currentY - targetPageY;
|
|
|
|
var speed = Math.ceil(distance / 10);
|
|
|
|
if (currentY == targetPageY) {
|
|
|
|
clearInterval(timer);
|
|
|
|
} else {
|
|
|
|
window.scrollTo(0, targetPageY > currentY ? currentY + speed : currentY - speed);
|
|
|
|
}
|
|
|
|
}, 10);
|
2020-05-03 17:22:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-20 15:53:32 +08:00
|
|
|
export function scrollTo(target) {
|
|
|
|
let dom = getDom(target);
|
|
|
|
if (dom instanceof HTMLElement) {
|
|
|
|
dom.scrollIntoView({
|
|
|
|
behavior: "smooth",
|
|
|
|
block: "start",
|
|
|
|
inline: "nearest"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 17:22:33 +08:00
|
|
|
export function getFirstChildDomInfo(element) {
|
2020-06-03 23:11:02 +08:00
|
|
|
var dom = getDom(element);
|
2021-01-17 15:41:28 +08:00
|
|
|
if (dom.firstElementChild)
|
|
|
|
return getDomInfo(dom.firstElementChild);
|
|
|
|
return getDomInfo(dom);
|
2020-05-03 17:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function addClsToFirstChild(element, className) {
|
2020-06-03 23:11:02 +08:00
|
|
|
var dom = getDom(element);
|
|
|
|
if (dom.firstElementChild) {
|
|
|
|
dom.firstElementChild.classList.add(className);
|
|
|
|
}
|
2020-05-03 17:22:33 +08:00
|
|
|
}
|
|
|
|
|
2020-12-03 12:28:02 +08:00
|
|
|
export function removeClsFromFirstChild(element, className) {
|
2021-01-09 23:57:31 +08:00
|
|
|
var dom = getDom(element);
|
|
|
|
if (dom.firstElementChild) {
|
|
|
|
dom.firstElementChild.classList.remove(className);
|
|
|
|
}
|
2020-12-03 12:28:02 +08:00
|
|
|
}
|
|
|
|
|
2021-01-21 17:20:10 +08:00
|
|
|
export function addDomEventListenerToFirstChild(element, eventName, preventDefault, invoker) {
|
2020-06-03 23:11:02 +08:00
|
|
|
var dom = getDom(element);
|
2020-05-03 17:22:33 +08:00
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
if (dom.firstElementChild) {
|
2021-01-21 17:20:10 +08:00
|
|
|
addDomEventListener(dom.firstElementChild, eventName, preventDefault, invoker);
|
2020-06-03 23:11:02 +08:00
|
|
|
}
|
2020-05-03 17:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getAbsoluteTop(e) {
|
2020-06-03 23:11:02 +08:00
|
|
|
var offset = e.offsetTop;
|
|
|
|
if (e.offsetParent != null) {
|
|
|
|
offset += getAbsoluteTop(e.offsetParent);
|
|
|
|
}
|
|
|
|
return offset;
|
2020-05-03 17:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getAbsoluteLeft(e) {
|
2020-06-03 23:11:02 +08:00
|
|
|
var offset = e.offsetLeft;
|
|
|
|
if (e.offsetParent != null) {
|
|
|
|
offset += getAbsoluteLeft(e.offsetParent);
|
|
|
|
}
|
|
|
|
return offset;
|
2020-05-10 22:43:58 +08:00
|
|
|
}
|
2020-05-03 17:22:33 +08:00
|
|
|
|
|
|
|
export function addElementToBody(element) {
|
2020-06-03 23:11:02 +08:00
|
|
|
document.body.appendChild(element);
|
2020-05-03 17:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function delElementFromBody(element) {
|
2020-06-03 23:11:02 +08:00
|
|
|
document.body.removeChild(element);
|
2020-04-23 17:13:56 +08:00
|
|
|
}
|
2020-05-09 11:09:07 +08:00
|
|
|
|
|
|
|
export function addElementTo(addElement, elementSelector) {
|
2020-06-03 23:11:02 +08:00
|
|
|
let parent = getDom(elementSelector);
|
|
|
|
if (parent && addElement) {
|
|
|
|
parent.appendChild(addElement);
|
|
|
|
}
|
2020-05-09 11:09:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function delElementFrom(delElement, elementSelector) {
|
2020-06-03 23:11:02 +08:00
|
|
|
let parent = getDom(elementSelector);
|
|
|
|
if (parent && delElement) {
|
|
|
|
parent.removeChild(delElement);
|
|
|
|
}
|
2020-06-02 14:25:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getActiveElement() {
|
2020-06-03 23:11:02 +08:00
|
|
|
let element = document.activeElement;
|
|
|
|
let id = element.getAttribute("id") || "";
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function focusDialog(selector: string, count: number = 0) {
|
|
|
|
let ele = <HTMLElement>document.querySelector(selector);
|
|
|
|
if (ele && !ele.hasAttribute("disabled")) {
|
|
|
|
setTimeout(() => {
|
|
|
|
ele.focus();
|
|
|
|
let curId = "#" + getActiveElement();
|
|
|
|
if (curId !== selector) {
|
|
|
|
if (count < 10) {
|
|
|
|
focusDialog(selector, count + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getWindow() {
|
|
|
|
return {
|
|
|
|
innerWidth: window.innerWidth,
|
|
|
|
innerHeight: window.innerHeight
|
|
|
|
};
|
2020-06-02 14:25:44 +08:00
|
|
|
}
|
2020-06-03 23:11:02 +08:00
|
|
|
|
|
|
|
function debounce(func, wait, immediate) {
|
|
|
|
var timeout;
|
|
|
|
return () => {
|
|
|
|
const context = this, args = arguments;
|
|
|
|
const later = () => {
|
|
|
|
timeout = null;
|
|
|
|
if (!immediate) func.apply(this, args);
|
|
|
|
};
|
|
|
|
const callNow = immediate && !timeout;
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(later, wait);
|
|
|
|
if (callNow) func.apply(context, args);
|
|
|
|
};
|
2020-06-04 17:03:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export function css(element: HTMLElement, name: string | object, value: string | null = null) {
|
|
|
|
if (typeof name === 'string') {
|
|
|
|
element.style[name] = value;
|
|
|
|
} else {
|
|
|
|
for (let key in name) {
|
|
|
|
if (name.hasOwnProperty(key)) {
|
|
|
|
element.style[key] = name[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addCls(selector: Element | string, clsName: string | Array<string>) {
|
|
|
|
let element = getDom(selector);
|
|
|
|
|
|
|
|
if (typeof clsName === "string") {
|
|
|
|
element.classList.add(clsName);
|
|
|
|
} else {
|
|
|
|
element.classList.add(...clsName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeCls(selector: Element | string, clsName: string | Array<string>) {
|
|
|
|
let element = getDom(selector);
|
|
|
|
|
|
|
|
if (typeof clsName === "string") {
|
|
|
|
element.classList.remove(clsName);
|
|
|
|
} else {
|
|
|
|
element.classList.remove(...clsName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 13:13:26 +08:00
|
|
|
export function elementScrollIntoView(selector: Element | string) {
|
2021-01-09 23:57:31 +08:00
|
|
|
let element = getDom(selector);
|
2020-11-27 13:13:26 +08:00
|
|
|
|
2021-01-09 23:57:31 +08:00
|
|
|
if (!element)
|
|
|
|
return;
|
2020-11-27 13:13:26 +08:00
|
|
|
|
2021-01-09 23:57:31 +08:00
|
|
|
element.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'start' });
|
2020-11-27 13:13:26 +08:00
|
|
|
}
|
|
|
|
|
2020-07-21 22:32:54 +08:00
|
|
|
const oldBodyCacheStack = [];
|
2020-07-11 23:41:27 +08:00
|
|
|
|
2020-07-22 22:24:58 +08:00
|
|
|
const hasScrollbar = () => {
|
|
|
|
let overflow = document.body.style.overflow;
|
|
|
|
if (overflow && overflow === "hidden") return false;
|
|
|
|
return document.body.scrollHeight > (window.innerHeight || document.documentElement.clientHeight);
|
|
|
|
}
|
|
|
|
|
2020-06-04 17:03:13 +08:00
|
|
|
export function disableBodyScroll() {
|
2020-07-11 23:41:27 +08:00
|
|
|
let body = document.body;
|
2020-07-21 22:32:54 +08:00
|
|
|
const oldBodyCache = {};
|
2020-07-11 23:41:27 +08:00
|
|
|
["position", "width", "overflow"].forEach((key) => {
|
|
|
|
oldBodyCache[key] = body.style[key];
|
|
|
|
});
|
2020-07-21 22:32:54 +08:00
|
|
|
oldBodyCacheStack.push(oldBodyCache);
|
2020-07-11 23:41:27 +08:00
|
|
|
css(body,
|
2020-06-04 17:03:13 +08:00
|
|
|
{
|
|
|
|
"position": "relative",
|
2020-07-22 22:24:58 +08:00
|
|
|
"width": hasScrollbar() ? "calc(100% - 17px)" : null,
|
2020-06-04 17:03:13 +08:00
|
|
|
"overflow": "hidden"
|
|
|
|
});
|
|
|
|
addCls(document.body, "ant-scrolling-effect");
|
|
|
|
}
|
|
|
|
|
2020-09-05 16:09:58 +08:00
|
|
|
export function enableBodyScroll() {
|
|
|
|
let oldBodyCache = oldBodyCacheStack.length > 0 ? oldBodyCacheStack.pop() : {};
|
2020-06-04 17:03:13 +08:00
|
|
|
|
2020-09-05 16:09:58 +08:00
|
|
|
css(document.body,
|
|
|
|
{
|
|
|
|
"position": oldBodyCache["position"] ?? null,
|
|
|
|
"width": oldBodyCache["width"] ?? null,
|
|
|
|
"overflow": oldBodyCache["overflow"] ?? null
|
|
|
|
});
|
|
|
|
removeCls(document.body, "ant-scrolling-effect");
|
2020-06-10 21:44:21 +08:00
|
|
|
}
|
|
|
|
|
2020-11-18 16:01:31 +08:00
|
|
|
export function destroyAllDialog() {
|
2021-01-09 23:57:31 +08:00
|
|
|
document.querySelectorAll('.ant-modal-root')
|
|
|
|
.forEach(e => document.body.removeChild(e.parentNode));
|
2020-11-18 16:01:31 +08:00
|
|
|
}
|
|
|
|
|
2020-06-10 21:44:21 +08:00
|
|
|
export function createIconFromfontCN(scriptUrl) {
|
|
|
|
if (document.querySelector(`[data-namespace="${scriptUrl}"]`)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.setAttribute('src', scriptUrl);
|
|
|
|
script.setAttribute('data-namespace', scriptUrl);
|
|
|
|
document.body.appendChild(script);
|
2020-06-14 18:54:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getScroll() {
|
2020-07-01 06:49:51 +08:00
|
|
|
return { x: window.pageXOffset, y: window.pageYOffset };
|
2020-06-14 18:54:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getInnerText(element) {
|
2020-07-01 06:49:51 +08:00
|
|
|
let dom = getDom(element);
|
|
|
|
return dom.innerText;
|
2020-06-14 18:54:14 +08:00
|
|
|
}
|
2020-07-11 01:46:35 +08:00
|
|
|
|
2020-10-14 17:24:26 +08:00
|
|
|
export function getMaxZIndex() {
|
|
|
|
return [...document.all].reduce((r, e) => Math.max(r, +window.getComputedStyle(e).zIndex || 0), 0)
|
|
|
|
}
|
|
|
|
|
2021-03-12 17:02:11 +08:00
|
|
|
export function getStyle(element, styleProp) {
|
2021-03-01 22:50:22 +08:00
|
|
|
if (element.currentStyle)
|
|
|
|
return element.currentStyle[styleProp];
|
|
|
|
else if (window.getComputedStyle)
|
|
|
|
return document.defaultView.getComputedStyle(element, null).getPropertyValue(styleProp);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getTextAreaInfo(element) {
|
2021-03-12 17:02:11 +08:00
|
|
|
var result = {};
|
|
|
|
var dom = getDom(element);
|
|
|
|
result["scrollHeight"] = dom.scrollHeight || 0;
|
2021-03-01 22:50:22 +08:00
|
|
|
|
|
|
|
if (element.currentStyle) {
|
|
|
|
result["lineHeight"] = parseFloat(element.currentStyle["line-height"]);
|
|
|
|
result["paddingTop"] = parseFloat(element.currentStyle["padding-top"]);
|
|
|
|
result["paddingBottom"] = parseFloat(element.currentStyle["padding-bottom"]);
|
|
|
|
result["borderBottom"] = parseFloat(element.currentStyle["border-bottom"]);
|
|
|
|
result["borderTop"] = parseFloat(element.currentStyle["border-top"]);
|
|
|
|
}
|
|
|
|
else if (window.getComputedStyle) {
|
|
|
|
result["lineHeight"] = parseFloat(document.defaultView.getComputedStyle(element, null).getPropertyValue("line-height"));
|
|
|
|
result["paddingTop"] = parseFloat(document.defaultView.getComputedStyle(element, null).getPropertyValue("padding-top"));
|
|
|
|
result["paddingBottom"] = parseFloat(document.defaultView.getComputedStyle(element, null).getPropertyValue("padding-bottom"));
|
|
|
|
result["borderBottom"] = parseFloat(document.defaultView.getComputedStyle(element, null).getPropertyValue("border-bottom"));
|
|
|
|
result["borderTop"] = parseFloat(document.defaultView.getComputedStyle(element, null).getPropertyValue("border-top"));
|
|
|
|
}
|
2021-04-03 15:22:11 +08:00
|
|
|
//Firefox can return this as NaN, so it has to be handled here like that.
|
|
|
|
if (Object.is(NaN, result["borderTop"]))
|
2021-04-08 22:32:48 +08:00
|
|
|
result["borderTop"] = 1;
|
2021-04-03 15:22:11 +08:00
|
|
|
if (Object.is(NaN, result["borderBottom"]))
|
2021-04-08 22:32:48 +08:00
|
|
|
result["borderBottom"] = 1;
|
2021-03-01 22:50:22 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const funcDict = {};
|
|
|
|
|
|
|
|
export function registerResizeTextArea(element, minRows, maxRows, objReference) {
|
2021-04-03 15:22:11 +08:00
|
|
|
if (!objReference) {
|
|
|
|
disposeResizeTextArea(element);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
objReferenceDict[element.id] = objReference;
|
|
|
|
funcDict[element.id + "input"] = function () { resizeTextArea(element, minRows, maxRows); }
|
|
|
|
element.addEventListener("input", funcDict[element.id + "input"]);
|
|
|
|
return getTextAreaInfo(element);
|
|
|
|
}
|
2021-03-01 22:50:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function disposeResizeTextArea(element) {
|
2021-04-03 15:22:11 +08:00
|
|
|
element.removeEventListener("input", funcDict[element.id + "input"]);
|
|
|
|
objReferenceDict[element.id] = null;
|
|
|
|
funcDict[element.id + "input"] = null;
|
2021-03-01 22:50:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function resizeTextArea(element, minRows, maxRows) {
|
2021-04-03 15:22:11 +08:00
|
|
|
var dims = getTextAreaInfo(element);
|
|
|
|
var rowHeight = dims["lineHeight"];
|
|
|
|
var offsetHeight = dims["paddingTop"] + dims["paddingBottom"] + dims["borderTop"] + dims["borderBottom"];
|
|
|
|
var oldHeight = parseFloat(element.style.height);
|
|
|
|
element.style.height = 'auto';
|
|
|
|
|
|
|
|
var rows = Math.trunc(element.scrollHeight / rowHeight);
|
|
|
|
rows = Math.max(minRows, rows);
|
|
|
|
|
|
|
|
var newHeight = 0;
|
|
|
|
if (rows > maxRows) {
|
|
|
|
rows = maxRows;
|
|
|
|
|
|
|
|
newHeight = (rows * rowHeight + offsetHeight);
|
|
|
|
element.style.height = newHeight + "px";
|
|
|
|
element.style.overflowY = "visible";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newHeight = rows * rowHeight + offsetHeight;
|
|
|
|
element.style.height = newHeight + "px";
|
|
|
|
element.style.overflowY = "hidden";
|
|
|
|
}
|
|
|
|
if (oldHeight !== newHeight) {
|
|
|
|
let textAreaObj = objReferenceDict[element.id];
|
|
|
|
textAreaObj.invokeMethodAsync("ChangeSizeAsyncJs", parseFloat(element.scrollWidth), newHeight);
|
|
|
|
}
|
2021-03-01 22:50:22 +08:00
|
|
|
}
|
|
|
|
|
2020-07-11 01:46:35 +08:00
|
|
|
const objReferenceDict = {};
|
|
|
|
export function disposeObj(objReferenceName) {
|
2020-07-21 00:16:02 +08:00
|
|
|
delete objReferenceDict[objReferenceName];
|
2020-07-11 01:46:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//#region mentions
|
|
|
|
|
2020-08-26 07:28:44 +08:00
|
|
|
import getOffset from "./modules/Caret";
|
2020-07-11 01:46:35 +08:00
|
|
|
|
|
|
|
export function getCursorXY(element, objReference) {
|
2020-07-21 00:16:02 +08:00
|
|
|
objReferenceDict["mentions"] = objReference;
|
|
|
|
window.addEventListener("click", mentionsOnWindowClick);
|
2020-07-11 01:46:35 +08:00
|
|
|
|
2020-07-21 00:16:02 +08:00
|
|
|
var offset = getOffset(element);
|
2020-07-11 01:46:35 +08:00
|
|
|
|
2020-07-21 00:16:02 +08:00
|
|
|
return [offset.left, offset.top + offset.height + 14];
|
2020-07-11 01:46:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function mentionsOnWindowClick(e) {
|
2020-07-21 00:16:02 +08:00
|
|
|
let mentionsObj = objReferenceDict["mentions"];
|
|
|
|
if (mentionsObj) {
|
|
|
|
mentionsObj.invokeMethodAsync("CloseMentionsDropDown");
|
|
|
|
} else {
|
|
|
|
window.removeEventListener("click", mentionsOnWindowClick);
|
|
|
|
}
|
2020-07-11 01:46:35 +08:00
|
|
|
}
|
|
|
|
|
2020-08-26 07:28:44 +08:00
|
|
|
//#endregion
|
|
|
|
|
2021-01-09 23:57:31 +08:00
|
|
|
export { enableDraggable, disableDraggable, resetModalPosition } from "./modules/dragHelper";
|
|
|
|
|
|
|
|
export function bindTableHeaderAndBodyScroll(bodyRef, headerRef) {
|
|
|
|
bodyRef.bindScrollLeftToHeader = () => {
|
|
|
|
headerRef.scrollLeft = bodyRef.scrollLeft;
|
|
|
|
}
|
|
|
|
bodyRef.addEventListener('scroll', bodyRef.bindScrollLeftToHeader);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function unbindTableHeaderAndBodyScroll(bodyRef) {
|
2021-02-22 11:29:56 +08:00
|
|
|
if (bodyRef) {
|
|
|
|
bodyRef.removeEventListener('scroll', bodyRef.bindScrollLeftToHeader);
|
|
|
|
}
|
2021-02-04 23:40:47 +08:00
|
|
|
}
|
|
|
|
|
2021-03-04 23:16:24 +08:00
|
|
|
function preventKeys(e, keys: string[]) {
|
|
|
|
if (keys.indexOf(e.key.toUpperCase()) !== -1) {
|
2021-02-22 11:29:56 +08:00
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-04 23:40:47 +08:00
|
|
|
}
|
|
|
|
|
2021-03-04 23:16:24 +08:00
|
|
|
export function addPreventKeys(inputElement, keys: string[]) {
|
2021-04-03 15:22:11 +08:00
|
|
|
if (inputElement) {
|
|
|
|
let dom = getDom(inputElement);
|
|
|
|
keys = keys.map(function (x) { return x.toUpperCase(); })
|
|
|
|
funcDict[inputElement.id + "keydown"] = (e) => preventKeys(e, keys);
|
2021-03-04 23:16:24 +08:00
|
|
|
(dom as HTMLElement).addEventListener("keydown", funcDict[inputElement.id + "keydown"], false);
|
2021-04-03 15:22:11 +08:00
|
|
|
}
|
2021-02-04 23:40:47 +08:00
|
|
|
}
|
|
|
|
|
2021-03-04 23:16:24 +08:00
|
|
|
export function removePreventKeys(inputElement) {
|
2021-04-03 15:22:11 +08:00
|
|
|
if (inputElement) {
|
|
|
|
let dom = getDom(inputElement);
|
|
|
|
(dom as HTMLElement).removeEventListener("keydown", funcDict[inputElement.id + "keydown"]);
|
|
|
|
funcDict[inputElement.id + "keydown"] = null;
|
|
|
|
}
|
2021-03-04 23:16:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function preventKeyOnCondition(e, key: string, check: () => boolean) {
|
|
|
|
if (e.key.toUpperCase() === key.toUpperCase() && check()) {
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addPreventEnterOnOverlayVisible(element, overlayElement) {
|
2021-04-03 15:22:11 +08:00
|
|
|
if (element && overlayElement) {
|
|
|
|
let dom = getDom(element);
|
|
|
|
funcDict[element.id + "keydown:Enter"] = (e) => preventKeyOnCondition(e, "enter", () => overlayElement.offsetParent !== null);
|
|
|
|
(dom as HTMLElement).addEventListener("keydown", funcDict[element.id + "keydown:Enter"], false);
|
|
|
|
}
|
2021-03-04 23:16:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function removePreventEnterOnOverlayVisible(element) {
|
2021-04-03 15:22:11 +08:00
|
|
|
if (element) {
|
|
|
|
let dom = getDom(element);
|
|
|
|
(dom as HTMLElement).removeEventListener("keydown", funcDict[element.id + "keydown:Enter"]);
|
|
|
|
funcDict[element.id + "keydown:Enter"] = null;
|
|
|
|
}
|
2021-03-12 17:02:11 +08:00
|
|
|
}
|
|
|
|
|
2021-03-31 15:56:06 +08:00
|
|
|
export function setDomAttribute(element, attributes) {
|
2021-03-12 17:02:11 +08:00
|
|
|
let dom = getDom(element);
|
2021-03-31 15:56:06 +08:00
|
|
|
for (var key in attributes) {
|
|
|
|
(dom as HTMLElement).setAttribute(key, attributes[key]);
|
|
|
|
}
|
2021-04-18 16:06:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setSelectionStart(element, position) {
|
|
|
|
if (position >= 0) {
|
|
|
|
let dom = getDom(element);
|
|
|
|
if (position <= dom.value.length) {
|
|
|
|
dom.selectionStart = position;
|
|
|
|
dom.selectionEnd = position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|