| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import _ from 'lodash'; |
| import gettext from 'sources/gettext'; |
| import { hasTrojanSource } from 'anti-trojan-source'; |
| import convert from 'convert-units'; |
| import getApiInstance from './api_instance'; |
| import usePreferences from '../../preferences/static/js/store'; |
| import pgAdmin from 'sources/pgadmin'; |
| import { isMac } from './keyboard_shortcuts'; |
|
|
| export function parseShortcutValue(obj) { |
| let shortcut = ''; |
| if (!obj){ |
| return null; |
| } |
| if (obj.alt) { shortcut += 'alt+'; } |
| if (obj.shift) { shortcut += 'shift+'; } |
| if (obj.control) { shortcut += 'ctrl+'; } |
| shortcut += obj?.key.char?.toLowerCase(); |
| return shortcut; |
| } |
|
|
| export function isShortcutValue(obj) { |
| if(!obj) return false; |
| return [obj.alt, obj.control, obj?.key, obj?.key?.char].every((k)=>!_.isUndefined(k)); |
| } |
|
|
| |
| export function toCodeMirrorKey(obj) { |
| let shortcut = ''; |
| if (!obj){ |
| return shortcut; |
| } |
| if (obj.alt) { shortcut += 'Alt-'; } |
| if (obj.shift) { shortcut += 'Shift-'; } |
| if (obj.control) { |
| if(isMac() && obj.ctrl_is_meta) { |
| shortcut += 'Meta-'; |
| } else { |
| shortcut += 'Ctrl-'; |
| } |
| } |
| if(obj?.key.char?.length == 1) { |
| shortcut += obj?.key.char?.toLowerCase(); |
| } else { |
| shortcut += obj?.key.char; |
| } |
| return shortcut; |
| } |
|
|
| export function getEpoch(inp_date) { |
| let date_obj = inp_date || new Date(); |
| return parseInt(date_obj.getTime()/1000); |
| } |
|
|
| |
| export function getGCD(inp_arr) { |
| let gcd_for_two = (a, b) => { |
| return a == 0?b:gcd_for_two(b % a, a); |
| }; |
|
|
| let inp_len = inp_arr.length; |
| if(inp_len <= 2) { |
| return gcd_for_two(inp_arr[0], inp_arr[1]); |
| } |
|
|
| let result = inp_arr[0]; |
| for(let i=1; i<inp_len; i++) { |
| result = gcd_for_two(inp_arr[i], result); |
| } |
|
|
| return result; |
| } |
|
|
| export function getMod(no, divisor) { |
| return ((no % divisor) + divisor) % divisor; |
| } |
|
|
| export function parseFuncParams(label) { |
| let paramArr = [], |
| funcName = '', |
| paramStr = ''; |
|
|
| if(label.endsWith('()')) { |
| funcName = label.substring(0, label.length-2); |
| } else if(!label.endsWith(')')) { |
| funcName = label; |
| } else if(!label.endsWith('()') && label.endsWith(')')) { |
| let i = 0, |
| startBracketPos = label.length; |
|
|
| |
| i = label.length-2; |
| while(i >= 0) { |
| if(label[i] == '(') { |
| startBracketPos = i; |
| break; |
| } else if(label[i] == '"') { |
| |
| i--; |
| while(label[i] != '"') i--; |
| } |
| i--; |
| } |
|
|
| funcName = label.substring(0, startBracketPos); |
| paramStr = label.substring(startBracketPos+1, label.length-1); |
|
|
| let paramStart = 0, |
| paramName = '', |
| paramModes = ['IN', 'OUT', 'INOUT', 'VARIADIC']; |
|
|
| i = 0; |
| while(i < paramStr.length) { |
| if(paramStr[i] == '"') { |
| |
| i++; |
| while(paramStr[i] != '"') i++; |
| } else if (paramStr[i] == ' ') { |
| |
| |
| |
| if(paramName == '' || paramModes.indexOf(paramName) > -1 ) { |
| paramName = paramStr.substring(paramStart, i); |
| paramStart = i+1; |
| } |
| } |
| else if (paramStr[i] == ',') { |
| paramArr.push([paramName, paramStr.substring(paramStart, i)]); |
| paramName = ''; |
| paramStart = i+1; |
| } |
| i++; |
| } |
| paramArr.push([paramName, paramStr.substring(paramStart)]); |
| } |
|
|
| return { |
| 'func_name': funcName, |
| 'param_string': paramStr, |
| 'params': paramArr, |
| }; |
| } |
|
|
| export function quote_ident(value) { |
| |
| let quoteIt = false; |
| if (!isNaN(parseInt(value))){ |
| quoteIt = true; |
| } |
|
|
| if(value.search(/[^a-z0-9_]/g) > -1) { |
| |
| value = value.replace(/"/g, '""'); |
| quoteIt = true; |
| } |
|
|
| if(quoteIt) { |
| return `"${value}"`; |
| } else { |
| return value; |
| } |
| } |
|
|
| export function fully_qualify(pgBrowser, data, item) { |
| const parentData = pgBrowser.tree.getTreeNodeHierarchy(item); |
| let namespace = ''; |
|
|
| if (parentData.schema !== undefined) { |
| namespace = quote_ident(parentData.schema._label); |
| } |
| else if (parentData.view !== undefined) { |
| namespace = quote_ident(parentData.view._label); |
| } |
| else if (parentData.catalog !== undefined) { |
| namespace = quote_ident(parentData.catalog._label); |
| } |
|
|
| if (parentData.package !== undefined && data._type != 'package') { |
| if(namespace == '') { |
| namespace = quote_ident(parentData.package._label); |
| } else { |
| namespace += '.' + quote_ident(parentData.package._label); |
| } |
| } |
|
|
| if(namespace != '') { |
| return namespace + '.' + quote_ident(data._label); |
| } else { |
| return quote_ident(data._label); |
| } |
| } |
|
|
| export function getRandomInt(min, max) { |
| const intArray = new Uint32Array(1); |
| crypto.getRandomValues(intArray); |
|
|
| let range = max - min + 1; |
| return min + (intArray[0] % range); |
| } |
|
|
| export function titleize(i_str) { |
| if(i_str === '' || i_str === null) return i_str; |
| return i_str.split(' ') |
| .map(w => w[0].toUpperCase() + w.substr(1).toLowerCase()) |
| .join(' '); |
| } |
|
|
| export function sprintf(i_str) { |
| try { |
| let replaceArgs = arguments; |
| return i_str.split('%s') |
| .map(function(w, i) { |
| if(i > 0) { |
| if(i < replaceArgs.length) { |
| return [replaceArgs[i], w].join(''); |
| } else { |
| return ['%s', w].join(''); |
| } |
| } else { |
| return w; |
| } |
| }) |
| .join(''); |
| } catch(e) { |
| console.error(e); |
| return i_str; |
| } |
| } |
|
|
| |
| |
| export function CSVToArray(strData, strDelimiter, quoteChar){ |
| strDelimiter = strDelimiter || ','; |
| quoteChar = quoteChar || '"'; |
|
|
| |
| let objPattern = new RegExp( |
| ( |
| |
| '(\\' + strDelimiter + '|\\r?\\n|\\r|^)' + |
| |
| (quoteChar == '"' ? '(?:"([^"]*(?:""[^"]*)*)"|' : '(?:\'([^\']*(?:\'\'[^\']*)*)\'|') + |
| |
| (quoteChar == '"' ? '([^"\\' + strDelimiter + '\\r\\n]*))': '([^\'\\' + strDelimiter + '\\r\\n]*))') |
| ), |
| 'gi' |
| ); |
|
|
| |
| |
| let arrData = [[]]; |
|
|
| |
| |
| if(strData.startsWith(strDelimiter)) { |
| arrData[ arrData.length - 1 ].push(null); |
| } |
|
|
| |
| |
| let arrMatches = null; |
|
|
| |
| |
| while ((arrMatches = objPattern.exec( strData ))){ |
| |
| let strMatchedDelimiter = arrMatches[ 1 ]; |
|
|
| |
| |
| |
| |
| if (strMatchedDelimiter.length && strMatchedDelimiter !== strDelimiter){ |
| |
| |
| arrData.push( [] ); |
| } |
|
|
| let strMatchedValue; |
|
|
| |
| |
| |
| if (arrMatches[ 2 ]){ |
| |
| |
| strMatchedValue = arrMatches[ 2 ].replace(new RegExp( quoteChar+quoteChar, 'g' ), quoteChar); |
| } else { |
| |
| strMatchedValue = arrMatches[ 3 ]; |
| } |
| |
| |
| arrData[ arrData.length - 1 ].push( strMatchedValue ); |
| } |
| |
| return arrData; |
| } |
|
|
| export function hasBinariesConfiguration(pgBrowser, serverInformation) { |
| const module = 'paths'; |
| let preference_name = 'pg_bin_dir'; |
| let msg = gettext('Please configure the PostgreSQL Binary Path in the Preferences dialog.'); |
|
|
| if ((serverInformation.type && serverInformation.type === 'ppas') || |
| serverInformation.server_type === 'ppas') { |
| preference_name = 'ppas_bin_dir'; |
| msg = gettext('Please configure the EDB Advanced Server Binary Path in the Preferences dialog.'); |
| } |
|
|
| const preference = usePreferences.getState().getPreferences(module, preference_name); |
|
|
| if (preference) { |
| if (_.isUndefined(preference.value) || !checkBinaryPathExists(preference.value, serverInformation.version)) { |
| pgAdmin.Browser.notifier.alert(gettext('Configuration required'), msg); |
| return false; |
| } |
| } else { |
| pgAdmin.Browser.notifier.alert( |
| gettext('Preferences Error'), |
| gettext('Failed to load preference %s of module %s', preference_name, module) |
| ); |
| return false; |
| } |
| return true; |
| } |
|
|
| function checkBinaryPathExists(binaryPathArray, selectedServerVersion) { |
| let foundDefaultPath = false, |
| serverSpecificPathExist = false, |
| binPathArray = JSON.parse(binaryPathArray); |
|
|
| _.each(binPathArray, function(binPath) { |
| if (selectedServerVersion >= binPath.version && selectedServerVersion < binPath.next_major_version) { |
| if (!_.isUndefined(binPath.binaryPath) && !_.isNull(binPath.binaryPath) && binPath.binaryPath.trim() !== '') |
| serverSpecificPathExist = true; |
| } |
|
|
| |
| if (binPath.isDefault) { |
| foundDefaultPath = true; |
| } |
| }); |
|
|
| return (serverSpecificPathExist | foundDefaultPath); |
| } |
|
|
| |
| export function evalFunc(obj, func, ...param) { |
| if(_.isFunction(func)) { |
| return func.apply(obj, [...param]); |
| } |
| return func; |
| } |
|
|
| export function getBrowser() { |
| let ua=navigator.userAgent,tem,M=ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; |
| if(/trident/i.test(M[1])) { |
| tem=/\brv[ :]+(\d+)/g.exec(ua) || []; |
| return {name:'IE', version:(tem[1]||'')}; |
| } |
| if(ua.startsWith('Nwjs')) { |
| let nwjs = ua.split('-')[0]?.split(':'); |
| return {name:nwjs[0], version: nwjs[1]}; |
| } |
|
|
| if(M[1]==='Chrome') { |
| tem=ua.match(/\bOPR|Edge\/(\d+)/); |
| if(tem!=null) {return {name:tem[0], version:tem[1]};} |
| } |
|
|
| M=M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?']; |
| if((tem=ua.match(/version\/(\d+)/i))!=null) {M.splice(1,1,tem[1]);} |
| return { |
| name: M[0], |
| version: M[1], |
| }; |
| } |
|
|
| export function checkTrojanSource(content, isPasteEvent) { |
| |
| if (hasTrojanSource({ sourceText: content})) { |
| let msg = gettext('The file opened contains bidirectional Unicode characters which could be interpreted differently than what is displayed. If this is unexpected it is recommended that you review the text in an application that can display hidden Unicode characters before proceeding.'); |
| if (isPasteEvent) { |
| msg = gettext('The pasted text contains bidirectional Unicode characters which could be interpreted differently than what is displayed. If this is unexpected it is recommended that you review the text in an application that can display hidden Unicode characters before proceeding.'); |
| } |
| pgAdmin.Browser.notifier.alert(gettext('Trojan Source Warning'), msg); |
| } |
| } |
|
|
| export function downloadBlob(blob, fileName) { |
| let urlCreator = window.URL || window.webkitURL, |
| downloadUrl = urlCreator.createObjectURL(blob), |
| link = document.createElement('a'); |
|
|
| document.body.appendChild(link); |
|
|
| if (getBrowser() == 'IE' && window.navigator.msSaveBlob) { |
| |
| window.navigator.msSaveBlob(blob, fileName); |
| } else { |
| link.setAttribute('href', downloadUrl); |
| link.setAttribute('download', fileName); |
| link.click(); |
| } |
| document.body.removeChild(link); |
| } |
|
|
| export function toPrettySize(rawSize, from='B') { |
| try { |
| |
| if (from == '') { |
| return Intl.NumberFormat('en', {notation: 'compact'}).format(rawSize); |
| } |
| let conVal = convert(rawSize).from(from).toBest(); |
| conVal.val = Math.round(conVal.val * 100) / 100; |
| return `${conVal.val} ${conVal.unit}`; |
| } |
| catch { |
| return ''; |
| } |
| } |
|
|
| export function compareSizeVals(val1, val2) { |
| const parseAndConvert = (val)=>{ |
| try { |
| let [size, unit] = val.split(' '); |
| return convert(size).from(unit.toUpperCase()).to('B'); |
| } catch { |
| return -1; |
| } |
| }; |
| val1 = parseAndConvert(val1); |
| val2 = parseAndConvert(val2); |
| if(val1 > val2) return 1; |
| return (val1 < val2 ? -1 : 0); |
| } |
|
|
| export function calcFontSize(fontSize) { |
| if(fontSize) { |
| fontSize = parseFloat((Math.round(parseFloat(fontSize + 'e+2')) + 'e-2')); |
| let rounded = Number(fontSize); |
| if(rounded > 0) { |
| return rounded + 'em'; |
| } |
| } |
| return '1em'; |
| } |
|
|
| export function pgHandleItemError(error, args) { |
| let pgBrowser = window.pgAdmin.Browser; |
|
|
| if (!error || !pgBrowser) { |
| return; |
| } |
|
|
| if(error.response.headers['content-type'] == 'application/json') { |
| let jsonResp = error.response.data; |
| if ( |
| jsonResp && ( |
| error.response.status == 503 ? ( |
| jsonResp.info == 'CONNECTION_LOST' && |
| 'server' in args.info && jsonResp.data.sid >= 0 && |
| jsonResp.data.sid == args.info.server._id |
| ) : ( |
| error.response.status == 428 && |
| jsonResp.errormsg && |
| jsonResp.errormsg == gettext('Connection to the server has been lost.') |
| ) |
| ) |
| ) { |
| if ( |
| args.preHandleConnectionLost && |
| typeof(args.preHandleConnectionLost) == 'function' |
| ) { |
| args.preHandleConnectionLost.apply(this, arguments); |
| } |
|
|
| |
| let server = pgBrowser.Nodes['server'], |
| ctx = { |
| resp: jsonResp, |
| error: error, |
| args: args, |
| }, |
| reconnectServer = function() { |
| let ctx_local = this, |
| onServerConnect = function(_sid, _i, _d) { |
| |
| if (this.args.info.server._id == _sid) { |
| pgBrowser.Events.off( |
| 'pgadmin:server:connected', onServerConnect |
| ); |
| pgBrowser.Events.off( |
| 'pgadmin:server:connect:cancelled', onConnectCancel |
| ); |
|
|
| |
| if ( |
| this.resp.data.database && |
| this.resp.data.database != _d.db |
| ) { |
| |
| |
| pgBrowser.Events.trigger( |
| 'pgadmin:database:connection:lost', this.args.item, |
| this.resp, true |
| ); |
| } |
| } |
| }.bind(ctx_local), |
| onConnectCancel = function(_sid, _item, _data) { |
| |
| if (_sid == this.args.info.server.id) { |
| pgBrowser.Events.off('pgadmin:server:connected', onServerConnect); |
| pgBrowser.Events.off('pgadmin:server:connect:cancelled', onConnectCancel); |
|
|
| |
| pgBrowser.Events.trigger( |
| 'pgadmin:database:connect:cancelled', _sid, |
| this.resp.data.database || _data.db, _item, _data |
| ); |
| } |
| }.bind(ctx_local); |
|
|
| pgBrowser.Events.on('pgadmin:server:connected', onServerConnect); |
| pgBrowser.Events.on('pgadmin:server:connect:cancelled', onConnectCancel); |
|
|
| |
| |
| pgBrowser.Events.trigger( |
| 'pgadmin:server:connection:lost', this.args.item, this.resp |
| ); |
| }.bind(ctx); |
|
|
| getApiInstance().get(server.generate_url( |
| null, 'connect', args.info.server, true, args.info |
| )) |
| .then(({data: res})=>{ |
| if (res.success && 'connected' in res.data) { |
| if (res.data.connected) { |
| |
| |
| pgBrowser.Events.trigger( |
| 'pgadmin:database:connection:lost', args.item, jsonResp |
| ); |
| return; |
| } |
| } |
|
|
| |
| |
| reconnectServer(); |
| }) |
| .catch(()=>{ |
| reconnectServer(); |
| }); |
| return true; |
| } else if (jsonResp && jsonResp.info == 'CRYPTKEY_MISSING' && error.response.status == 503) { |
| |
| |
| |
| return false; |
| } |
| } |
|
|
| return false; |
| } |
|
|
| export function fullHexColor(shortHex) { |
| if(shortHex?.length == 4) { |
| return shortHex.replace(RegExp('#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])'), '#$1$1$2$2$3$3').toUpperCase(); |
| } |
| return shortHex; |
| } |
|
|
| export function gettextForTranslation(translations, ...replaceArgs) { |
| const text = replaceArgs[0]; |
| let rawTranslation = translations[text] ? translations[text] : text; |
|
|
| if(arguments.length == 2) { |
| return rawTranslation; |
| } |
|
|
| try { |
| return rawTranslation.split('%s') |
| .map(function(w, i) { |
| if(i > 0) { |
| if(i < replaceArgs.length) { |
| return [replaceArgs[i], w].join(''); |
| } else { |
| return ['%s', w].join(''); |
| } |
| } else { |
| return w; |
| } |
| }) |
| .join(''); |
| } catch(e) { |
| console.error(e); |
| return rawTranslation; |
| } |
| } |
|
|
| |
| const requestAnimationFrame = |
| window.requestAnimationFrame || |
| window.mozRequestAnimationFrame || |
| window.webkitRequestAnimationFrame || |
| window.msRequestAnimationFrame; |
|
|
| const cancelAnimationFrame = |
| window.cancelAnimationFrame || window.mozCancelAnimationFrame; |
|
|
| |
| export function requestAnimationAndFocus(ele) { |
| if(!ele) return; |
|
|
| const animateId = requestAnimationFrame(()=>{ |
| ele?.focus?.(); |
| cancelAnimationFrame(animateId); |
| }); |
| } |
|
|
|
|
| export function scrollbarWidth() { |
| |
| const scrollDiv = document.createElement('div'); |
| scrollDiv.setAttribute('style', 'width: 100px; height: 100px; overflow: scroll; position:absolute; top:-9999px;'); |
| document.body.appendChild(scrollDiv); |
| const scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth; |
| document.body.removeChild(scrollDiv); |
| return scrollbarWidth; |
| } |
|
|