dom.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.browse = exports.DomTools = void 0;
  6. exports.getOffsetHeight = getOffsetHeight;
  7. exports.getPaddingTopBottomSize = getPaddingTopBottomSize;
  8. exports.setScrollLeft = setScrollLeft;
  9. exports.setScrollTop = setScrollTop;
  10. var _xeUtils = _interopRequireDefault(require("xe-utils"));
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. var browse = _xeUtils.default.browse();
  13. exports.browse = browse;
  14. var reClsMap = {};
  15. function getClsRE(cls) {
  16. if (!reClsMap[cls]) {
  17. reClsMap[cls] = new RegExp("(?:^|\\s)".concat(cls, "(?!\\S)"), 'g');
  18. }
  19. return reClsMap[cls];
  20. }
  21. function getNodeOffset(elem, container, rest) {
  22. if (elem) {
  23. var parentElem = elem.parentNode;
  24. rest.top += elem.offsetTop;
  25. rest.left += elem.offsetLeft;
  26. if (parentElem && parentElem !== document.documentElement && parentElem !== document.body) {
  27. rest.top -= parentElem.scrollTop;
  28. rest.left -= parentElem.scrollLeft;
  29. }
  30. if (container && (elem === container || elem.offsetParent === container) ? 0 : elem.offsetParent) {
  31. return getNodeOffset(elem.offsetParent, container, rest);
  32. }
  33. }
  34. return rest;
  35. }
  36. function isScale(val) {
  37. return val && /^\d+%$/.test(val);
  38. }
  39. function hasClass(elem, cls) {
  40. return elem && elem.className && elem.className.match && elem.className.match(getClsRE(cls));
  41. }
  42. function removeClass(elem, cls) {
  43. if (elem && hasClass(elem, cls)) {
  44. elem.className = elem.className.replace(getClsRE(cls), '');
  45. }
  46. }
  47. function getDomNode() {
  48. var documentElement = document.documentElement;
  49. var bodyElem = document.body;
  50. return {
  51. scrollTop: documentElement.scrollTop || bodyElem.scrollTop,
  52. scrollLeft: documentElement.scrollLeft || bodyElem.scrollLeft,
  53. visibleHeight: documentElement.clientHeight || bodyElem.clientHeight,
  54. visibleWidth: documentElement.clientWidth || bodyElem.clientWidth
  55. };
  56. }
  57. function getOffsetHeight(elem) {
  58. return elem ? elem.offsetHeight : 0;
  59. }
  60. function getPaddingTopBottomSize(elem) {
  61. if (elem) {
  62. var computedStyle = getComputedStyle(elem);
  63. var paddingTop = _xeUtils.default.toNumber(computedStyle.paddingTop);
  64. var paddingBottom = _xeUtils.default.toNumber(computedStyle.paddingBottom);
  65. return paddingTop + paddingBottom;
  66. }
  67. return 0;
  68. }
  69. function setScrollTop(elem, scrollTop) {
  70. if (elem) {
  71. elem.scrollTop = scrollTop;
  72. }
  73. }
  74. function setScrollLeft(elem, scrollLeft) {
  75. if (elem) {
  76. elem.scrollLeft = scrollLeft;
  77. }
  78. } // export function setScrollLeftAndTop (elem, scrollLeft, scrollTop) {
  79. // if (elem) {
  80. // elem.scrollLeft = scrollLeft
  81. // elem.scrollTop = scrollTop
  82. // }
  83. // }
  84. function isNodeElement(elem) {
  85. return elem && elem.nodeType === 1;
  86. }
  87. var DomTools = {
  88. browse: browse,
  89. isPx: function isPx(val) {
  90. return val && /^\d+(px)?$/.test(val);
  91. },
  92. isScale: isScale,
  93. hasClass: hasClass,
  94. removeClass: removeClass,
  95. addClass: function addClass(elem, cls) {
  96. if (elem && !hasClass(elem, cls)) {
  97. removeClass(elem, cls);
  98. elem.className = "".concat(elem.className, " ").concat(cls);
  99. }
  100. },
  101. updateCellTitle: function updateCellTitle(overflowElem, column) {
  102. var content = column.type === 'html' ? overflowElem.innerText : overflowElem.textContent;
  103. if (overflowElem.getAttribute('title') !== content) {
  104. overflowElem.setAttribute('title', content);
  105. }
  106. },
  107. getDomNode: getDomNode,
  108. /**
  109. * 检查触发源是否属于目标节点
  110. */
  111. getEventTargetNode: function getEventTargetNode(evnt, container, queryCls, queryMethod) {
  112. var targetElem;
  113. var target = evnt.target;
  114. while (target && target.nodeType && target !== document) {
  115. if (queryCls && hasClass(target, queryCls) && (!queryMethod || queryMethod(target))) {
  116. targetElem = target;
  117. } else if (target === container) {
  118. return {
  119. flag: queryCls ? !!targetElem : true,
  120. container: container,
  121. targetElem: targetElem
  122. };
  123. }
  124. target = target.parentNode;
  125. }
  126. return {
  127. flag: false
  128. };
  129. },
  130. /**
  131. * 获取元素相对于 document 的位置
  132. */
  133. getOffsetPos: function getOffsetPos(elem, container) {
  134. return getNodeOffset(elem, container, {
  135. left: 0,
  136. top: 0
  137. });
  138. },
  139. getAbsolutePos: function getAbsolutePos(elem) {
  140. var bounding = elem.getBoundingClientRect();
  141. var boundingTop = bounding.top;
  142. var boundingLeft = bounding.left;
  143. var _getDomNode = getDomNode(),
  144. scrollTop = _getDomNode.scrollTop,
  145. scrollLeft = _getDomNode.scrollLeft,
  146. visibleHeight = _getDomNode.visibleHeight,
  147. visibleWidth = _getDomNode.visibleWidth;
  148. return {
  149. boundingTop: boundingTop,
  150. top: scrollTop + boundingTop,
  151. boundingLeft: boundingLeft,
  152. left: scrollLeft + boundingLeft,
  153. visibleHeight: visibleHeight,
  154. visibleWidth: visibleWidth
  155. };
  156. },
  157. scrollToView: function scrollToView(elem) {
  158. var scrollIntoViewIfNeeded = 'scrollIntoViewIfNeeded';
  159. var scrollIntoView = 'scrollIntoView';
  160. if (elem) {
  161. if (elem[scrollIntoViewIfNeeded]) {
  162. elem[scrollIntoViewIfNeeded]();
  163. } else if (elem[scrollIntoView]) {
  164. elem[scrollIntoView]();
  165. }
  166. }
  167. },
  168. triggerEvent: function triggerEvent(targetElem, type) {
  169. if (targetElem) {
  170. targetElem.dispatchEvent(new Event(type));
  171. }
  172. },
  173. calcHeight: function calcHeight($xetable, key) {
  174. var val = $xetable[key];
  175. var num = 0;
  176. if (val) {
  177. if (val === 'auto') {
  178. num = $xetable.parentHeight;
  179. } else {
  180. var excludeHeight = $xetable.getExcludeHeight();
  181. if (isScale(val)) {
  182. num = Math.floor((_xeUtils.default.toInteger(val) || 1) / 100 * $xetable.parentHeight);
  183. } else {
  184. num = _xeUtils.default.toNumber(val);
  185. }
  186. num = Math.max(40, num - excludeHeight);
  187. }
  188. }
  189. return num;
  190. },
  191. isNodeElement: isNodeElement
  192. };
  193. exports.DomTools = DomTools;
  194. var _default = DomTools;
  195. exports.default = _default;