Skip to content

Commit

Permalink
move logic into lib
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Feb 28, 2020
1 parent e778e09 commit 79b301d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 {
Expand Down Expand Up @@ -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 = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -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()}`);
}

0 comments on commit 79b301d

Please sign in to comment.