@jecloud/utils
Version:
JECloud 工具库
1 lines • 2.98 kB
JavaScript
import{uniqueId as t,isObject as e,isEmpty as o,kebabCase as r}from"lodash-es";import{addResizeListener as n,removeResizeListener as i}from"./dom-event";import{getOffsetBox as s,highlightElement as p}from"./dom-utils";export function createElement(t){return new u(t)}var u=function(){function u(e){return this._listeners={},e?e.isElement?e:(this.dom="string"==typeof e?document.getElementById(e)||document.querySelector(e):e,this.dom?(this.id=this.dom.id=this.dom.id||t("je-el-"),void(this.isElement=!0)):null):null}return u.prototype.addClass=function(){for(var t,e=[],o=0;o<arguments.length;o++)e[o]=arguments[o];(t=this.dom.classList).add.apply(t,e)},u.prototype.removeClass=function(){for(var t,e=[],o=0;o<arguments.length;o++)e[o]=arguments[o];(t=this.dom.classList).remove.apply(t,e)},u.prototype.hasClass=function(t){return this.dom.classList.contains(t)},u.prototype.toggleClass=function(t,e){return this.dom.classList.toggle(t,e)},u.prototype.getAttribute=function(t){return this.dom.getAttribute(t)},u.prototype.setAttribute=function(t,e){return this.dom.setAttribute(t,e),this},u.prototype.setStyle=function(t,o){var n,i=((n={})[t]=o,n);for(var s in e(t)&&(i=t),i)this.dom.style.setProperty(r(s),i[s]);return this},u.prototype.getStyle=function(t){return this.dom.style.getPropertyValue(r(t))},u.prototype.getHeight=function(){return this.dom.offsetHeight},u.prototype.getWidth=function(){return this.dom.offsetWidth},u.prototype.on=function(t,e){this.dom.addEventListener(t,e);var o=this._listeners[t]||[];o.push(e),this._listeners[t]=o},u.prototype.un=function(t,e){var o=this,r=this._listeners[t]||[],n=-1;r.forEach((function(r,i){if((!e||e==r)&&(o.dom.removeEventListener(t,r),e))return n=i,!1})),e?n>0&&(r.splice(n,1),this._listeners[t]=r):delete this._listeners[t]},u.prototype.addResizeListener=function(t){n(this.dom,t)},u.prototype.removeResizeListener=function(t){i(this.dom,t)},u.prototype.up=function(t,e,r){var n=this.dom.parentNode;if(o(t))return r?n:new u(n);var i,s=n.parentNode,p=0;for(e=e||50;s;){p++;var l=s.querySelectorAll(":scope > "+t);if(null==l||l.forEach((function(t){if(n==t)return i=t,!1})),i||e>0&&e==p)break;s=(n=s).parentNode}return i&&(r?i:new u(i))},u.prototype.down=function(t,e){return this.select(t,e)[0]},u.prototype.parent=function(t){return this.up(null,0,t)},u.prototype.child=function(t,e){return this.down(":scope > "+t,e)},u.prototype.select=function(t,e){var o=this.dom.querySelectorAll(t);if(e)return o;var r=[];return o.forEach((function(t){r.push(new u(t))})),r},u.prototype.getOffsetBox=function(t){var e=t.isElement?t.dom:t;return s(this.dom,e)},u.prototype.highlight=function(){p(this.dom)},u.prototype.scrollBy=function(t,e){void 0===e&&(e="top");var o=t.isElement?t.dom:t,r=s(o,this.dom);this.setStyle("scroll-behavior","smooth"),"top"===e?this.dom.scrollTop=r.y:this.dom.scrollLeft=r.x},u.prototype.destroy=function(){var t;null===(t=this.dom.parentNode)||void 0===t||t.removeChild(this.dom)},u}();export{u as Element};