Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
node_ajax.js198 linesDownload Raw Back to js
1/////////////////////////////////////////////////////////////2//3// pgAdmin 4 - PostgreSQL Tools4//5// Copyright (C) 2013 - 2024, The pgAdmin Development Team6// This software is released under the PostgreSQL Licence7//8//////////////////////////////////////////////////////////////9 10import _ from 'lodash';11import getApiInstance from '../../../static/js/api_instance';12import {generate_url} from 'sources/browser/generate_url';13import pgAdmin from 'sources/pgadmin';14 15/* It generates the URL based on collection node selected */16export function generateCollectionURL(item, type) {17  let opURL = {18    'properties': 'obj',19    'children': 'nodes',20    'drop': 'obj',21  };22  let nodeObj= this;23  let collectionPickFunction = function (treeInfoValue, treeInfoKey) {24    return (treeInfoKey != nodeObj.type);25  };26  let treeInfo = pgAdmin.Browser.tree.getTreeNodeHierarchy(item);27  let actionType = type in opURL ? opURL[type] : type;28  let nodeType = type === 'properties' ? nodeObj.type : nodeObj.node;29  return generate_url(30    pgAdmin.Browser.URL, treeInfo, actionType, nodeType,31    collectionPickFunction32  );33}34 35/* It generates the URL based on tree node selected */36export function generateNodeUrl(treeNodeInfo, actionType, itemNodeData, withId, jumpAfterNode) {37  let opURL = {38      'create': 'obj',39      'drop': 'obj',40      'edit': 'obj',41      'properties': 'obj',42      'statistics': 'stats',43    },44    priority = -Infinity;45  let nodeObj = this;46  let itemID = withId && itemNodeData._type == nodeObj.type ? encodeURIComponent(itemNodeData._id) : '';47  actionType = actionType in opURL ? opURL[actionType] : actionType;48 49  if (nodeObj.parent_type) {50    if (_.isString(nodeObj.parent_type)) {51      let p = treeNodeInfo[nodeObj.parent_type];52      if (p) {53        priority = p.priority;54      }55    } else {56      _.each(nodeObj.parent_type, function(o) {57        let p = treeNodeInfo[o];58        if (p) {59          if (priority < p.priority) {60            priority = p.priority;61          }62        }63      });64    }65  }66 67  let jump_after_priority = priority;68  if(jumpAfterNode && treeNodeInfo[jumpAfterNode]) {69    jump_after_priority = treeNodeInfo[jumpAfterNode].priority;70  }71 72  let nodePickFunction = function(treeInfoValue) {73    return (treeInfoValue.priority <= jump_after_priority || treeInfoValue.priority == priority);74  };75 76  return generate_url(pgAdmin.Browser.URL, treeNodeInfo, actionType, nodeObj.type, nodePickFunction, itemID);77}78 79 80/* Get the nodes list as options required by select controls81 * The options are cached for performance reasons.82 */83export function getNodeAjaxOptions(url, nodeObj, treeNodeInfo, itemNodeData, params={}, transform=(data)=>data) {84  let otherParams = {85    urlWithId: false,86    jumpAfterNode: null,87    useCache: true,88    customGenerateUrl: null,89    ...params90  };91  return new Promise((resolve, reject)=>{92    const api = getApiInstance();93    let fullUrl = '';94    if(url) {95      if(otherParams.customGenerateUrl) {96        fullUrl = otherParams.customGenerateUrl.call(97          nodeObj, treeNodeInfo, url, itemNodeData, otherParams.urlWithId, otherParams.jumpAfterNode98        );99      } else {100        fullUrl = generateNodeUrl.call(101          nodeObj, treeNodeInfo, url, itemNodeData, otherParams.urlWithId, otherParams.jumpAfterNode102        );103      }104    }105 106    if (url) {107      let cacheNode = pgAdmin.Browser.Nodes[otherParams.cacheNode] || nodeObj;108      let cacheLevel = otherParams.cacheLevel || cacheNode.cache_level(treeNodeInfo, otherParams.urlWithId);109      /*110      * We needs to check, if we have already cached data for this url.111      * If yes - use that, and do not bother about fetching it again,112      * and use it.113      */114      let data = cacheNode.cache(nodeObj.type + '#' + url, treeNodeInfo, cacheLevel);115 116      if (_.isUndefined(data) || _.isNull(data)) {117        api.get(fullUrl, {118          params: otherParams.urlParams,119        }).then((res)=>{120          data = res.data;121          if(res.data.data) {122            data = res.data.data;123          }124          otherParams.useCache && cacheNode.cache(nodeObj.type + '#' + url, treeNodeInfo, cacheLevel, data);125          resolve(transform(data));126        }).catch((err)=>{127          reject(err);128        });129      } else {130        // To fetch only options from cache, we do not need time from 'at'131        // attribute but only options.132        resolve(transform(data.data || []));133      }134    }135  });136}137 138/* Get the nodes list based on current selected node id */139export function getNodeListById(nodeObj, treeNodeInfo, itemNodeData, params={}, filter=()=>true) {140  /* Transform the result to add image details */141  const transform = (rows) => {142    let res = [];143 144    _.each(rows, function(r) {145      if (filter(r)) {146        let l = (_.isFunction(nodeObj['node_label']) ?147            nodeObj['node_label'](r) :148            r.label),149          image = (_.isFunction(nodeObj['node_image']) ?150            nodeObj['node_image'](r) :151            (nodeObj['node_image'] || ('icon-' + nodeObj.type)));152 153        res.push({154          'value': r._id,155          'image': image,156          'label': l,157        });158      }159    });160 161    return res;162  };163 164  return getNodeAjaxOptions('nodes', nodeObj, treeNodeInfo, itemNodeData, params, transform);165}166 167/* Get the nodes list based on node name passed */168export function getNodeListByName(node, treeNodeInfo, itemNodeData, params={}, filter=()=>true, postTransform=(res)=>res) {169  let nodeObj = pgAdmin.Browser.Nodes[node];170  let {includeItemKeys} = params;171  /* Transform the result to add image details */172  const transform = (rows) => {173    let res = [];174 175    _.each(rows, function(r) {176      if (filter(r)) {177        let l = (_.isFunction(nodeObj['node_label']) ?178            nodeObj['node_label'](r) :179            r.label),180          image = (_.isFunction(nodeObj['node_image']) ?181            nodeObj['node_image'](r) :182            (nodeObj['node_image'] || ('icon-' + nodeObj.type)));183 184        res.push({185          'value': r.label,186          'image': image,187          'label': l,188          ..._.pick(r, includeItemKeys),189        });190      }191    });192 193    return postTransform(res);194  };195 196  return getNodeAjaxOptions('nodes', nodeObj, treeNodeInfo, itemNodeData, params, transform);197}198 
codekingpro/portable-devtools · Team Ai