all files / src/explore/ exploreUtils.js

82.28% Statements 65/79
64.58% Branches 31/48
50% Functions 4/8
80.56% Lines 58/72
1 branch Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123                                    11×   11× 11×   11×                                                                                          
/* eslint camelcase: 0 */
import URI from 'urijs';
 
export function getChartKey(explore) {
  const slice = explore.slice;
  return slice ? ('slice_' + slice.slice_id) : 'slice';
}
 
export function getAnnotationJsonUrl(slice_id, form_data, isNative) {
  if (slice_id === null || slice_id === undefined) {
    return null;
  }
  const uri = URI(window.location.search);
  const endpoint = isNative ? 'annotation_json' : 'slice_json';
  return uri.pathname(`/superset/${endpoint}/${slice_id}`)
    .search({
      form_data: JSON.stringify(form_data,
        (key, value) => value === null ? undefined : value),
    }).toString();
}
 
export function getURIDirectory(formData, endpointType = 'base') {
  // Building the directory part of the URI
  let directory = '/superset/explore/';
  if (['json', 'csv', 'query'].indexOf(endpointType) >= 0) {
    directory = '/superset/explore_json/';
  }
  return directory;
}
 
export function getExploreLongUrl(formData, endpointType) {
  if (!formData.datasource) {
    return null;
  }
 
  const uri = new URI('/');
  const directory = getURIDirectory(formData, endpointType);
  const search = uri.search(true);
  search.form_data = JSON.stringify(formData);
  if (endpointType === 'standalone') {
    search.standalone = 'true';
  }
  return uri.directory(directory).search(search).toString();
}
 
export function getExploreUrlAndPayload({
  formData,
  endpointType = 'base',
  force = false,
  curUrl = null,
  requestParams = {},
}) {
  if (I!formData.datasource) {
    return null;
  }
 
  // The search params from the window.location are carried through,
  // but can be specified with curUrl (used for unit tests to spoof
  // the window.location).
  let uri = new URI([location.protocol, '//', location.host].join(''));
  if (curUrl) {
    uri = URI(URI(curUrl).search());
  }
 
  const directory = getURIDirectory(formData, endpointType);
 
  // Building the querystring (search) part of the URI
  const search = uri.search(true);
  if (IformData.slice_id) {
    search.form_data = JSON.stringify({ slice_id: formData.slice_id });
  }
  if (force) {
    search.force = 'true';
  }
  if (endpointType === 'csv') {
    search.csv = 'true';
  }
  if (endpointType === 'standalone') {
    search.standalone = 'true';
  }
  if (IendpointType === 'query') {
    search.query = 'true';
  }
  const paramNames = Object.keys(requestParams);
  if (IparamNames.length) {
    paramNames.forEach((name) => {
      if (requestParams.hasOwnProperty(name)) {
        search[name] = requestParams[name];
      }
    });
  }
  uri = uri.search(search).directory(directory);
  const payload = { ...formData };
 
  return {
    url: uri.toString(),
    payload,
  };
}
 
export function exportChart(formData, endpointType) {
  const { url, payload } = getExploreUrlAndPayload({ formData, endpointType });
 
  const exploreForm = document.createElement('form');
  exploreForm.action = url;
  exploreForm.method = 'POST';
  exploreForm.target = '_blank';
  const token = document.createElement('input');
  token.type = 'hidden';
  token.name = 'csrf_token';
  token.value = (document.getElementById('csrf_token') || {}).value;
  exploreForm.appendChild(token);
  const data = document.createElement('input');
  data.type = 'hidden';
  data.name = 'form_data';
  data.value = JSON.stringify(payload);
  exploreForm.appendChild(data);
 
  document.body.appendChild(exploreForm);
  exploreForm.submit();
  document.body.removeChild(exploreForm);
}