From 79b301dd0ef824f6cea0e627a25e6f04e6fbcd13 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Fri, 28 Feb 2020 08:18:55 -0700 Subject: [PATCH] move logic into lib --- .../components/field_chooser/field_chooser.js | 65 +------------ .../field_chooser/lib/visualize_url_utils.ts | 94 +++++++++++++++++++ 2 files changed, 97 insertions(+), 62 deletions(-) create mode 100644 src/legacy/core_plugins/kibana/public/discover/np_ready/components/field_chooser/lib/visualize_url_utils.ts diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/components/field_chooser/field_chooser.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/components/field_chooser/field_chooser.js index 27e357afefc49..03649d5fc4d15 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/components/field_chooser/field_chooser.js +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/components/field_chooser/field_chooser.js @@ -16,7 +16,6 @@ * specific language governing permissions and limitations * under the License. */ -import uuid from 'uuid/v4'; import _ from 'lodash'; import $ from 'jquery'; import rison from 'rison-node'; @@ -26,7 +25,7 @@ import './discover_field_search_directive'; import './discover_index_pattern_directive'; import fieldChooserTemplate from './field_chooser.html'; import { IndexPatternFieldList } from '../../../../../../../../plugins/data/public'; -import { getServices } from '../../../kibana_services'; +import { getMapsAppUrl, isFieldVisualizable, isMapsAppRegistered } from './lib/visualize_url_utils'; export function createFieldChooserDirective($location, config, $route) { return { @@ -183,71 +182,13 @@ export function createFieldChooserDirective($location, config, $route) { $scope.indexPattern.popularizeField(fieldName, 1); }; - function getMapsAppUrl() { - const services = getServices(); - const mapsAppVisAlias = services.visualizations.types.getAliases().find(({ name }) => { - return name === 'maps'; - }); - return mapsAppVisAlias ? mapsAppVisAlias.aliasUrl : null; - } - - function isFieldVisualizable(field) { - const mapsAppUrl = getMapsAppUrl(); - if (mapsAppUrl && (field.type === 'geo_point' || field.type === 'geo_shape')) { - return true; - } - - return field.visualizable; - } - function getVisualizeUrl(field) { if (!$scope.state) { return ''; } - const mapsAppUrl = getMapsAppUrl(); - if (mapsAppUrl && (field.type === 'geo_point' || field.type === 'geo_shape')) { - const mapAppParams = new URLSearchParams(); - - // Copy global state - const locationSplit = window.location.href.split('discover?'); - if (locationSplit.length > 1) { - const discoverParams = new URLSearchParams(locationSplit[1]); - mapAppParams.set('_g', discoverParams.get('_g')); - } - - // Copy filters and query in app state - const mapsAppState = { - filters: $scope.state.filters || [], - }; - if ($scope.state.query) { - mapsAppState.query = $scope.state.query; - } - mapAppParams.set('_a', rison.encode(mapsAppState)); - - // create initial layer descriptor - const hasColumns = - $scope.columns && $scope.columns.length && $scope.columns[0] !== '_source'; - mapAppParams.set( - 'initialLayers', - rison.encode([ - { - id: uuid(), - label: $scope.indexPattern.title, - sourceDescriptor: { - id: uuid(), - type: 'ES_SEARCH', - geoField: field.name, - tooltipProperties: hasColumns ? $scope.columns : [], - indexPatternId: $scope.indexPattern.id, - }, - visible: true, - type: 'VECTOR', - }, - ]) - ); - - return getServices().addBasePath(`${mapsAppUrl}?${mapAppParams.toString()}`); + if ((field.type === 'geo_point' || field.type === 'geo_shape') && isMapsAppRegistered()) { + return getMapsAppUrl(field, $scope.indexPattern, $scope.state, $scope.columns); } let agg = {}; diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/components/field_chooser/lib/visualize_url_utils.ts b/src/legacy/core_plugins/kibana/public/discover/np_ready/components/field_chooser/lib/visualize_url_utils.ts new file mode 100644 index 0000000000000..66630f722e608 --- /dev/null +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/components/field_chooser/lib/visualize_url_utils.ts @@ -0,0 +1,94 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import uuid from 'uuid/v4'; +import rison from 'rison-node'; +import { IFieldType, IIndexPattern } from 'src/plugins/data/public'; +import { getServices } from '../../../../kibana_services'; + +function getMapsAppBaseUrl() { + const mapsAppVisAlias = getServices() + .visualizations.types.getAliases() + .find(({ name }) => { + return name === 'maps'; + }); + return mapsAppVisAlias ? mapsAppVisAlias.aliasUrl : null; +} + +export function isMapsAppRegistered() { + return getServices() + .visualizations.types.getAliases() + .some(({ name }) => { + return name === 'maps'; + }); +} + +export function isFieldVisualizable(field: IFieldType) { + if ((field.type === 'geo_point' || field.type === 'geo_shape') && isMapsAppRegistered()) { + return true; + } + return field.visualizable; +} + +export function getMapsAppUrl( + field: IFieldType, + indexPattern: IIndexPattern, + appState: any, + columns: string[] +) { + const mapAppParams = new URLSearchParams(); + + // Copy global state + const locationSplit = window.location.href.split('discover?'); + if (locationSplit.length > 1) { + const discoverParams = new URLSearchParams(locationSplit[1]); + mapAppParams.set('_g', discoverParams.get('_g')); + } + + // Copy filters and query in app state + const mapsAppState = { + filters: appState.filters || [], + }; + if (appState.query) { + mapsAppState.query = appState.query; + } + mapAppParams.set('_a', rison.encode(mapsAppState)); + + // create initial layer descriptor + const hasColumns = columns && columns.length && columns[0] !== '_source'; + mapAppParams.set( + 'initialLayers', + rison.encode([ + { + id: uuid(), + label: indexPattern.title, + sourceDescriptor: { + id: uuid(), + type: 'ES_SEARCH', + geoField: field.name, + tooltipProperties: hasColumns ? columns : [], + indexPatternId: indexPattern.id, + }, + visible: true, + type: 'VECTOR', + }, + ]) + ); + + return getServices().addBasePath(`${getMapsAppBaseUrl()}?${mapAppParams.toString()}`); +}