turbocommons-ts
Version:
General purpose library that implements frequently used and generic software development tasks
1 lines • 5.77 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.BrowserManager=void 0;var StringUtils_1=require("../utils/StringUtils"),NumericUtils_1=require("../utils/NumericUtils"),ArrayUtils_1=require("../utils/ArrayUtils"),BrowserManager=function(){function t(){}return t.prototype.getCurrentUrl=function(){return window.location.href},t.prototype.isDocumentLoaded=function(){return"complete"===document.readyState},t.prototype.isCookie=function(t){return void 0!==this.getCookie(t)},t.prototype.setCookie=function(t,e,o,n,r,i){if(void 0===o&&(o=""),void 0===n&&(n="/"),void 0===r&&(r=""),void 0===i&&(i=!1),!StringUtils_1.StringUtils.isString(t)||StringUtils_1.StringUtils.isEmpty(t))throw new Error("key must be defined");if(void 0!==e&&null!==e||(e=""),!StringUtils_1.StringUtils.isString(e))throw new Error("value must be a string");if(NumericUtils_1.NumericUtils.isNumeric(o)){var a=o;(o=new Date).setDate(o.getDate()+a)}var u=encodeURIComponent(t)+"="+encodeURIComponent(e);return u+=o?"; expires="+o.toUTCString():"",u+=n?"; path="+n:"",u+=r?"; domain="+r:"",u+=i?"; secure":"",document.cookie=u,!0},t.prototype.getCookie=function(t){if(!StringUtils_1.StringUtils.isString(t)||StringUtils_1.StringUtils.isEmpty(t))throw new Error("key must be defined");for(var e=document.cookie.split("; "),o=/\+/g,n=0,r=e.length;n<r;n++){var i=e[n].split("="),a=i.shift()||"";if(decodeURIComponent(a.replace(o," "))===t)return decodeURIComponent(i.join("=").replace(o," "))}},t.prototype.deleteCookie=function(t,e){if(void 0===e&&(e="/"),!StringUtils_1.StringUtils.isString(t)||StringUtils_1.StringUtils.isEmpty(t))throw new Error("key must be defined");return void 0!==this.getCookie(t)&&(this.setCookie(t,"",-1,e),!0)},t.prototype.isCurrentUrlWithHashFragment=function(){return!!window.location.hash},t.prototype.getCurrentUrlHashFragment=function(){return window.location.hash?window.location.hash.substring(1):""},t.prototype.setCurrentUrlHashFragment=function(){},t.prototype.deleteCurrentUrlHashFragment=function(){},t.prototype.isCurrentUrlWithQuery=function(){},t.prototype.getCurrentUrlQueryValues=function(){},t.prototype.reload=function(){location.reload()},t.prototype.getPreferredLanguage=function(){var t="";if(ArrayUtils_1.ArrayUtils.isArray(window.navigator.languages))for(var e=0,o=window.navigator.languages;e<o.length;e++){var n=o[e];if(n.length>=2){t=n;break}}else t=(t=window.navigator.userLanguage||window.navigator.language).split(",")[0];return t.trim().substr(0,2).toLowerCase()},t.prototype.goToUrl=function(t,e,o){if(void 0===e&&(e=!1),void 0===o&&(o=null),null==o)e?window.open(t,"_blank"):window.location.href=t;else{var n=document.createElement("form");n.action=t,n.method="POST",n.style.display="none",e&&(n.target="_blank");for(var r=0,i=Object.getOwnPropertyNames(o);r<i.length;r++){var a=i[r],u=document.createElement("input");u.type="hidden",u.name=a,u.value=o[a],n.appendChild(u)}document.body.appendChild(n),n.submit(),e&&document.body.removeChild(n)}},t.prototype.disableBackButton=function(){history.pushState(null,"",document.URL),window.addEventListener("popstate",this._onPopStatePreventBackButton)},t.prototype._onPopStatePreventBackButton=function(){history.pushState(null,"",document.URL)},t.prototype.enableBackButton=function(){window.removeEventListener("popstate",this._onPopStatePreventBackButton)},t.prototype.disableScroll=function(){},t.prototype.enableScroll=function(){},t.prototype.getScrollPosition=function(){return[window.pageXOffset,window.pageYOffset]},t.prototype.getWindowWidth=function(){return window.innerWidth||document.documentElement.clientWidth||document.getElementsByTagName("body")[0].clientWidth||-1},t.prototype.getWindowHeight=function(){return window.innerHeight||document.documentElement.clientHeight||document.getElementsByTagName("body")[0].clientHeight||-1},t.prototype.getDocumentWidth=function(){return Math.max(document.body.scrollWidth,document.body.offsetWidth,document.documentElement.clientWidth,document.documentElement.scrollWidth,document.documentElement.offsetWidth)},t.prototype.getDocumentHeight=function(){return Math.max(document.body.scrollHeight,document.body.offsetHeight,document.documentElement.clientHeight,document.documentElement.scrollHeight,document.documentElement.offsetHeight)},t.prototype.scrollTo=function(t,e,o){void 0===e&&(e=600),void 0===o&&(o=null);var n=function(t){return--t*t*t+1},r=window.pageXOffset,i=window.pageYOffset,a="now"in window.performance?performance.now():(new Date).getTime(),u=this.getDocumentWidth(),l=this.getDocumentHeight(),s=this.getWindowWidth(),d=this.getWindowHeight(),c=ArrayUtils_1.ArrayUtils.isArray(t)?t[0]:t.offsetLeft,p=ArrayUtils_1.ArrayUtils.isArray(t)?t[1]:t.offsetTop,g=r,m=i;if(u>s&&(g=Math.round(u-c<s?u-s:c)),l>d&&(m=Math.round(l-p<d?l-d:p)),"requestAnimationFrame"in window==!1)return window.scroll(g,m),void(o&&o());!function t(){var u="now"in window.performance?performance.now():(new Date).getTime(),l=Math.min(1,(u-a)/e),s=Math.ceil(n(l)*(g-r)+r),d=Math.ceil(n(l)*(m-i)+i);window.scroll(s,d),Math.ceil(window.pageXOffset)!==g||Math.ceil(window.pageYOffset)!==m?requestAnimationFrame(t):null!==o&&o()}()},t.prototype.copyToClipboard=function(t){return navigator.clipboard.writeText(t)},t.prototype.browseLocalFiles=function(t,e,o){!function t(n,r,i,a){if(a>=n.length)o(r,i);else if(n[a]){r.push(n[a].name);var u=new FileReader;if(u.onload=function(){"TEXT"===e?i.push(u.result):i.push(u.result.split(",",2)[1]),t(n,r,i,a+1)},u.onerror=function(){throw new Error("Error reading file")},"TEXT"===e)u.readAsText(n[a]);else{if("BASE64"!==e)throw new Error('Mode must be either "TEXT" or "BINARY"');u.readAsDataURL(n[a])}}else t(n,r,i,a+1)}(t.target.files,[],[],0)},t}();exports.BrowserManager=BrowserManager;