UNPKG

integralui-web-grid

Version:

IntegralUI Web - Grid is a native Web Component that displays tabular data sets

15 lines (12 loc) 8.24 kB
/* filename: integralui.common.service.js version : 21.4.0 Copyright © 2016-2021 Lidor Systems. All rights reserved. This file is part of the "IntegralUI Web" Library. The contents of this file are subject to the IntegralUI Web License, and may not be used except in compliance with the License. A copy of the License should have been installed in the product's root installation directory or it can be found at http://www.lidorsystems.com/products/web/studio/license-agreement.aspx. This SOFTWARE is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Any infringement will be prosecuted under applicable laws. */ class IntegralUICommonService{calcMargin(t){if(t)return{top:getComputedStyle(t)["margin-top"]?parseInt(getComputedStyle(t)["margin-top"],10):getComputedStyle(t).marginTop?parseInt(getComputedStyle(t).marginTop,10):0,right:getComputedStyle(t)["margin-right"]?parseInt(getComputedStyle(t)["margin-right"],10):getComputedStyle(t).marginRight?parseInt(getComputedStyle(t).marginRight,10):0,bottom:getComputedStyle(t)["margin-bottom"]?parseInt(getComputedStyle(t)["margin-bottom"],10):getComputedStyle(t).marginBottom?parseInt(getComputedStyle(t).marginBottom,10):0,left:getComputedStyle(t)["margin-left"]?parseInt(getComputedStyle(t)["margin-left"],10):getComputedStyle(t).marginLeft?parseInt(getComputedStyle(t).marginLeft,10):0};else return{top:0,right:0,bottom:0,left:0}}checkHit(t,e,r){if(t>=r.left&&t<=r.right&&e>=r.top&&e<=r.bottom)return!0;else return!1}checkNumInRange(t,e,r){return t>e-r&&t<e+r?!0:!1}colorToRgb(t,e,r){if(r<0)r+=1;if(r>1)r-=1;if(r<1/6)return t+6*(e-t)*r;if(r<.5)return e;if(r<2/3)return t+(e-t)*(2/3-r)*6;else return t}convertRgbaToHex(t){let e="#";this.convertRgbaToNum(t,!0).forEach(t=>{t=t.toString(16);e+=1===t.length?"0"+t:t});return e}convertRgbaToHsl(t){let e,r,o=this.convertRgbaToNum(t,!0),n=o[0]/255,i=o[1]/255,g=o[2]/255,l=Math.max(n,i,g),a=Math.min(n,i,g),u=(l+a)/2;if(l===a)e=r=0;else{let t=l-a;r=u>.5?t/(2-l-a):t/(l+a);switch(l){case n:e=(i-g)/t+(i<g?6:0);break;case i:e=(g-n)/t+2;break;case g:e=(n-i)/t+4;break;default:this._defaultFunc()}e/=6}if((e=Math.round(360*e))<0)e+=360;r=Math.floor(Math.round(100*r));u=Math.floor(Math.round(100*u));return e+String.fromCharCode(176)+", "+r+"%, "+u+"%"}convertRgbaToNum(t,e){let r=(t=(t=t.replace(/[.*+?^${}()|[\]\\]/g,"")).replace("rgba","").replace("rgb","")).split(",").map(t=>parseInt(t));if(e)r=r.filter((t,e)=>e<3);r.forEach(t=>this.setNumInRange(t,0,255));return r}convertHexToRgba(t){let e=0,r=0,o=0;if(4===(t=t.replace(/[.*+?^${}()|[\]\\]/g,"")).length){e=Number("0x"+t[1]+t[1]);r=Number("0x"+t[2]+t[2]);o=Number("0x"+t[3]+t[3])}else if(7===t.length){e=Number("0x"+t[1]+t[2]);r=Number("0x"+t[3]+t[4]);o=Number("0x"+t[5]+t[6])}return"rgba("+parseInt(e,10).toString()+","+parseInt(r,10).toString()+","+parseInt(o,10).toString()+",1)"}convertHslToRgba(t){let e=(t=(t=t.replace(/[.*+?^${}()|[\]\\]/g,"")).replace(String.fromCharCode(176),"").replace("%","").replace("%","")).split(",").filter((t,e)=>e<3).map(t=>parseInt(t)),r=e[0],o=e[1]/100,n=e[2]/100,i=0,g=0,l=0;if(0===o)i=g=l=n;else{let t=n<.5?n*(1+o):n+o-n*o,e=2*n-t;i=this.colorToRgb(e,t,r+1/3);g=this.colorToRgb(e,t,r);l=this.colorToRgb(e,t,r-1/3)}return"rgba("+(i=Math.round(255*i))+", "+(g=Math.round(255*g))+", "+(l=Math.round(255*l))+", 1)"}convertValue(t,e){let r=null!==t&&void 0!==t?t:null;if(e){let o=e.locales||"en-US",n=e.options||{};if(this.isNumber(r))r=Intl.NumberFormat(o,n).format(r);else if(this.isDate(r))r=Intl.DateTimeFormat(o,n).format(r);else r=this.convertValueToString(t)}else r=this.convertValueToString(t);return r}convertValueToString(t){return null!==t&&void 0!==t?t.toString():""}_defaultFunc(){}getBodyElem(t){let e=null;for(;t;){if(null===t.offsetParent){e=t;break}t=t.offsetParent}return e}getBorderWidth(t){if(t)return{top:getComputedStyle(t)["border-top-width"]?parseInt(getComputedStyle(t)["border-top-width"],10):getComputedStyle(t).borderTopWidth?parseInt(getComputedStyle(t).borderTopWidth,10):0,right:getComputedStyle(t)["border-right-width"]?parseInt(getComputedStyle(t)["border-right-width"],10):getComputedStyle(t).borderRightWidth?parseInt(getComputedStyle(t).borderRightWidth,10):0,bottom:getComputedStyle(t)["border-bottom-width"]?parseInt(getComputedStyle(t)["border-bottom-width"],10):getComputedStyle(t).borderTopWidth?parseInt(getComputedStyle(t).borderTopWidth,10):0,left:getComputedStyle(t)["border-left-width"]?parseInt(getComputedStyle(t)["border-left-width"],10):getComputedStyle(t).borderLeftWidth?parseInt(getComputedStyle(t).borderLeftWidth,10):0};else return{top:0,right:0,bottom:0,left:0}}getClientPos(t,e){let r=this.getShiftPos(),o=this.getMousePos(t);o.x-=r.x;o.y-=r.y;let n=this.getPageRect(e);return{x:o.x-n.left,y:o.y-n.top}}getColorAtPosition(t,e,r){if(t){const o=t.getImageData(e,r,1,1).data;return"rgba("+o[0]+","+o[1]+","+o[2]+",1)"}return""}getMargin(t){return t?this.calcMargin(t):{top:0,right:0,bottom:0,left:0}}getMouseOffsetPos(t,e){let r=this.getShiftPos(),o=this.getMousePos(t);o.x-=r.x;o.y-=r.y;let n=this.getPageRect(e);return{x:o.x-n.left,y:o.y-n.top}}getMousePos(t){return{x:t.pageX?t.pageX:t.originalEvent?t.originalEvent.pageX:0,y:t.pageY?t.pageY:t.originalEvent?t.originalEvent.pageY:0}}getObjFromListById(t,e){let r=null;if(t)for(let o=0;o<t.length;o++)if(t[o].id===e){r=t[o];break}return r}getObjFromByName(t,e,r){let o=null;if(t)for(let n=0;n<t.length;n++)if(t[n][e]===r){o=t[n];break}return o}getPadding(t){if(t)return{top:getComputedStyle(t)["padding-top"]?parseInt(getComputedStyle(t)["padding-top"],10):getComputedStyle(t).paddingTop?parseInt(getComputedStyle(t).paddingTop,10):0,right:getComputedStyle(t)["padding-right"]?parseInt(getComputedStyle(t)["padding-right"],10):getComputedStyle(t).paddingRight?parseInt(getComputedStyle(t).paddingRight,10):0,bottom:getComputedStyle(t)["padding-bottom"]?parseInt(getComputedStyle(t)["padding-bottom"],10):getComputedStyle(t).paddingBottom?parseInt(getComputedStyle(t).paddingBottom,10):0,left:getComputedStyle(t)["padding-left"]?parseInt(getComputedStyle(t)["padding-left"],10):getComputedStyle(t).paddingLeft?parseInt(getComputedStyle(t).paddingLeft,10):0};else return{top:0,right:0,bottom:0,left:0}}getPageRect(t){return t?t.getBoundingClientRect():{top:0,right:0,bottom:0,left:0,height:0,width:0,x:0,y:0}}getShiftPos(){return{x:document.body.scrollLeft>0?document.body.scrollLeft:document.documentElement.scrollLeft>0?document.documentElement.scrollLeft:0,y:document.body.scrollTop>0?document.body.scrollTop:document.documentElement.scrollTop>0?document.documentElement.scrollTop:0}}getTouchData(t){return t.changedTouches?t.changedTouches:t.originalEvent?t.originalEvent.changedTouches:null}getUniqueId(t){let e=t||"-";function r(){return(65536*(1+Math.random())|0).toString(16).substring(1)}return r()+r()+e+r()+e+r()+e+r()+e+r()+r()+r()}isDate(t){return t&&"[object Date]"===Object.prototype.toString.call(t)&&!isNaN(t)}isTrue(t){return!1===t?!1:!0}isEmpty(t){return t&&0===Object.keys(t).length&&t.constructor===Object}isEqual(t,e){if(t&&e)return t.toString()===e.toString();else return!1}isFieldAvailable(t,e){return void 0!==t?t:e}isFunc(t){return t?"function"===typeof t||t instanceof Function:!1}isIndexInRange(t,e){return e?t>=0&&t<e.length:!1}isNumber(t){return void 0!==t&&!Number.isNaN(t)?"number"===typeof t||t instanceof Number:!1}isObject(t){return t?"object"===typeof t&&null!==t&&!Array.isArray(t):!1}isString(t){return t?"string"===typeof t||t instanceof String:!1}isValue(t){return null!==t&&void 0!==t}moveObject(t,e,r){if(this.isIndexInRange(t,r)&&this.isIndexInRange(e,r))r.splice(e,0,r.splice(t,1)[0])}pointToView(t){let e=this.getShiftPos();return{x:t.x-e.x,y:t.y-e.y}}escapeRegExp(t){return t.replace(/[.*+\-?^${}()|[\]\\]/g,"\\$&")}replaceAll(t,e,r){return t.replace(new RegExp(this.escapeRegExp(e),"g"),r)}setNumInRange(t,e,r){return isNaN(t)?0:Math.min(Math.max(t,e),r)}}export default IntegralUICommonService;