dom.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.isInContainer = exports.getScrollContainer = exports.isScroll = exports.getStyle = exports.once = exports.off = exports.on = undefined;
  4. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; /* istanbul ignore next */
  5. exports.hasClass = hasClass;
  6. exports.addClass = addClass;
  7. exports.removeClass = removeClass;
  8. exports.setStyle = setStyle;
  9. var _vue = require('vue');
  10. var _vue2 = _interopRequireDefault(_vue);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. var isServer = _vue2.default.prototype.$isServer;
  13. var SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g;
  14. var MOZ_HACK_REGEXP = /^moz([A-Z])/;
  15. var ieVersion = isServer ? 0 : Number(document.documentMode);
  16. /* istanbul ignore next */
  17. var trim = function trim(string) {
  18. return (string || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, '');
  19. };
  20. /* istanbul ignore next */
  21. var camelCase = function camelCase(name) {
  22. return name.replace(SPECIAL_CHARS_REGEXP, function (_, separator, letter, offset) {
  23. return offset ? letter.toUpperCase() : letter;
  24. }).replace(MOZ_HACK_REGEXP, 'Moz$1');
  25. };
  26. /* istanbul ignore next */
  27. var on = exports.on = function () {
  28. if (!isServer && document.addEventListener) {
  29. return function (element, event, handler) {
  30. if (element && event && handler) {
  31. element.addEventListener(event, handler, false);
  32. }
  33. };
  34. } else {
  35. return function (element, event, handler) {
  36. if (element && event && handler) {
  37. element.attachEvent('on' + event, handler);
  38. }
  39. };
  40. }
  41. }();
  42. /* istanbul ignore next */
  43. var off = exports.off = function () {
  44. if (!isServer && document.removeEventListener) {
  45. return function (element, event, handler) {
  46. if (element && event) {
  47. element.removeEventListener(event, handler, false);
  48. }
  49. };
  50. } else {
  51. return function (element, event, handler) {
  52. if (element && event) {
  53. element.detachEvent('on' + event, handler);
  54. }
  55. };
  56. }
  57. }();
  58. /* istanbul ignore next */
  59. var once = exports.once = function once(el, event, fn) {
  60. var listener = function listener() {
  61. if (fn) {
  62. fn.apply(this, arguments);
  63. }
  64. off(el, event, listener);
  65. };
  66. on(el, event, listener);
  67. };
  68. /* istanbul ignore next */
  69. function hasClass(el, cls) {
  70. if (!el || !cls) return false;
  71. if (cls.indexOf(' ') !== -1) throw new Error('className should not contain space.');
  72. if (el.classList) {
  73. return el.classList.contains(cls);
  74. } else {
  75. return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
  76. }
  77. };
  78. /* istanbul ignore next */
  79. function addClass(el, cls) {
  80. if (!el) return;
  81. var curClass = el.className;
  82. var classes = (cls || '').split(' ');
  83. for (var i = 0, j = classes.length; i < j; i++) {
  84. var clsName = classes[i];
  85. if (!clsName) continue;
  86. if (el.classList) {
  87. el.classList.add(clsName);
  88. } else if (!hasClass(el, clsName)) {
  89. curClass += ' ' + clsName;
  90. }
  91. }
  92. if (!el.classList) {
  93. el.setAttribute('class', curClass);
  94. }
  95. };
  96. /* istanbul ignore next */
  97. function removeClass(el, cls) {
  98. if (!el || !cls) return;
  99. var classes = cls.split(' ');
  100. var curClass = ' ' + el.className + ' ';
  101. for (var i = 0, j = classes.length; i < j; i++) {
  102. var clsName = classes[i];
  103. if (!clsName) continue;
  104. if (el.classList) {
  105. el.classList.remove(clsName);
  106. } else if (hasClass(el, clsName)) {
  107. curClass = curClass.replace(' ' + clsName + ' ', ' ');
  108. }
  109. }
  110. if (!el.classList) {
  111. el.setAttribute('class', trim(curClass));
  112. }
  113. };
  114. /* istanbul ignore next */
  115. var getStyle = exports.getStyle = ieVersion < 9 ? function (element, styleName) {
  116. if (isServer) return;
  117. if (!element || !styleName) return null;
  118. styleName = camelCase(styleName);
  119. if (styleName === 'float') {
  120. styleName = 'styleFloat';
  121. }
  122. try {
  123. switch (styleName) {
  124. case 'opacity':
  125. try {
  126. return element.filters.item('alpha').opacity / 100;
  127. } catch (e) {
  128. return 1.0;
  129. }
  130. default:
  131. return element.style[styleName] || element.currentStyle ? element.currentStyle[styleName] : null;
  132. }
  133. } catch (e) {
  134. return element.style[styleName];
  135. }
  136. } : function (element, styleName) {
  137. if (isServer) return;
  138. if (!element || !styleName) return null;
  139. styleName = camelCase(styleName);
  140. if (styleName === 'float') {
  141. styleName = 'cssFloat';
  142. }
  143. try {
  144. var computed = document.defaultView.getComputedStyle(element, '');
  145. return element.style[styleName] || computed ? computed[styleName] : null;
  146. } catch (e) {
  147. return element.style[styleName];
  148. }
  149. };
  150. /* istanbul ignore next */
  151. function setStyle(element, styleName, value) {
  152. if (!element || !styleName) return;
  153. if ((typeof styleName === 'undefined' ? 'undefined' : _typeof(styleName)) === 'object') {
  154. for (var prop in styleName) {
  155. if (styleName.hasOwnProperty(prop)) {
  156. setStyle(element, prop, styleName[prop]);
  157. }
  158. }
  159. } else {
  160. styleName = camelCase(styleName);
  161. if (styleName === 'opacity' && ieVersion < 9) {
  162. element.style.filter = isNaN(value) ? '' : 'alpha(opacity=' + value * 100 + ')';
  163. } else {
  164. element.style[styleName] = value;
  165. }
  166. }
  167. };
  168. var isScroll = exports.isScroll = function isScroll(el, vertical) {
  169. if (isServer) return;
  170. var determinedDirection = vertical !== null && vertical !== undefined;
  171. var overflow = determinedDirection ? vertical ? getStyle(el, 'overflow-y') : getStyle(el, 'overflow-x') : getStyle(el, 'overflow');
  172. return overflow.match(/(scroll|auto|overlay)/);
  173. };
  174. var getScrollContainer = exports.getScrollContainer = function getScrollContainer(el, vertical) {
  175. if (isServer) return;
  176. var parent = el;
  177. while (parent) {
  178. if ([window, document, document.documentElement].includes(parent)) {
  179. return window;
  180. }
  181. if (isScroll(parent, vertical)) {
  182. return parent;
  183. }
  184. parent = parent.parentNode;
  185. }
  186. return parent;
  187. };
  188. var isInContainer = exports.isInContainer = function isInContainer(el, container) {
  189. if (isServer || !el || !container) return false;
  190. var elRect = el.getBoundingClientRect();
  191. var containerRect = void 0;
  192. if ([window, document, document.documentElement, null, undefined].includes(container)) {
  193. containerRect = {
  194. top: 0,
  195. right: window.innerWidth,
  196. bottom: window.innerHeight,
  197. left: 0
  198. };
  199. } else {
  200. containerRect = container.getBoundingClientRect();
  201. }
  202. return elRect.top < containerRect.bottom && elRect.bottom > containerRect.top && elRect.right > containerRect.left && elRect.left < containerRect.right;
  203. };