Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

De-angularize DocViewer #42116

Merged
merged 36 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
045957e
Add angular tab
kertal Jul 29, 2019
f63c2ff
Add render tab
kertal Jul 29, 2019
cf06aec
Refactore docViewer directive
kertal Jul 29, 2019
4d8ecb7
Migrate DocViewerAngularTab to typescript
kertal Jul 30, 2019
5144985
Move data-test-subj in htmls
kertal Jul 30, 2019
d621799
Add component property for DocViewer tab
kertal Jul 30, 2019
996cb48
JSON tab adaption to use component prop
kertal Jul 30, 2019
65ab0f2
Refactor Table tab to compile and render angular
kertal Jul 30, 2019
8933e30
Adaption of tests
kertal Jul 30, 2019
29a37f3
Remove unused variable
kertal Jul 31, 2019
1e57bed
Move angular helper function to seperate file
kertal Jul 31, 2019
479f711
Convert to typescript
kertal Jul 31, 2019
93ebaf4
Refactor DocViewsRegistryProvider -> slim version
kertal Jul 31, 2019
bf8844e
Fix merge conflict
kertal Jul 31, 2019
96fcf0a
Remove doc_viewer browser tests
kertal Aug 1, 2019
cd7e6c5
Remove browser test to be replaced by jest
kertal Aug 1, 2019
f14523a
Convert more files to TypeScript, refactor types
kertal Aug 1, 2019
e16c806
Merge remote-tracking branch 'upstream/master' into kertal-pr-doc_viewer
kertal Aug 1, 2019
544d528
Merge remote-tracking branch 'upstream/master' into kertal-pr-doc_viewer
kertal Aug 2, 2019
066a613
Cleanup, remove duplicate an angular render
kertal Aug 2, 2019
57ff240
Merge remote-tracking branch 'upstream/master' into kertal-pr-doc_viewer
kertal Aug 2, 2019
04897ac
Fix typecheck error
kertal Aug 2, 2019
5fc669a
Migrate to reactDirective
kertal Aug 6, 2019
783fa4d
Improve doc_views registry
kertal Aug 13, 2019
dd79dda
Refactor, add tests and documation to doc_viewer
kertal Aug 13, 2019
7f81051
Merge remote-tracking branch 'upstream/master' into kertal-pr-doc_viewer
kertal Aug 13, 2019
d07dbe0
Prevent multiple renderings by shouldComponentUpdate
kertal Aug 15, 2019
b9855ff
Apply spencers angular optimizations
kertal Aug 15, 2019
b8664c9
Remove redundant EuiTabbedContent props
kertal Aug 19, 2019
3cdf1ac
Merge remote-tracking branch 'upstream/master' into kertal-pr-doc_viewer
kertal Aug 19, 2019
d6ad0e6
Remove redundant angular compiler, fix test
kertal Aug 19, 2019
e5fd439
Add hasError to shouldComponentUpdate
kertal Aug 19, 2019
1530a06
Remove double license header
kertal Aug 19, 2019
2a6ce7c
Fixing typos and types
kertal Aug 19, 2019
bc32540
Use ComponentType
kertal Aug 19, 2019
6615a2f
Fix merge conflict
kertal Aug 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,13 @@ import { JsonCodeEditor } from './json_code_editor';
/*
* Registration of the the doc view: json
* - used to display an ES hit as pretty printed JSON at Discover
* - registered as angular directive to stay compatible with community plugins
*/
DocViewsRegistryProvider.register(function(reactDirective: any) {
const reactDir = reactDirective(JsonCodeEditor, ['hit']);
// setting of reactDir.scope is required to assign $scope props
// to the react component via render-directive in doc_viewer.js
reactDir.scope = {
hit: '=',
};
DocViewsRegistryProvider.register(function() {
return {
title: i18n.translate('kbnDocViews.json.jsonTitle', {
defaultMessage: 'JSON',
}),
order: 20,
directive: reactDir,
component: JsonCodeEditor,
};
});
79 changes: 36 additions & 43 deletions src/legacy/core_plugins/kbn_doc_views/public/views/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,57 +19,50 @@

import _ from 'lodash';
import { DocViewsRegistryProvider } from 'ui/registry/doc_views';

import '../filters/trust_as_html';
import tableHtml from './table.html';
import { i18n } from '@kbn/i18n';
import { injectAngularElement } from './table_helper';

function controller($scope) {
$scope.mapping = $scope.indexPattern.fields.byName;
$scope.flattened = $scope.indexPattern.flattenHit($scope.hit);
$scope.formatted = $scope.indexPattern.formatHit($scope.hit);
$scope.fields = _.keys($scope.flattened).sort();

$scope.canToggleColumns = function canToggleColumn() {
return _.isFunction($scope.onAddColumn) && _.isFunction($scope.onRemoveColumn);
};

$scope.toggleColumn = function toggleColumn(columnName) {
if ($scope.columns.includes(columnName)) {
$scope.onRemoveColumn(columnName);
} else {
$scope.onAddColumn(columnName);
}
};

$scope.isColumnActive = function isColumnActive(columnName) {
return $scope.columns.includes(columnName);
};

$scope.showArrayInObjectsWarning = function (row, field) {
const value = $scope.flattened[field];
return Array.isArray(value) && typeof value[0] === 'object';
};
}

DocViewsRegistryProvider.register(function () {
return {
title: i18n.translate('kbnDocViews.table.tableTitle', {
defaultMessage: 'Table'
defaultMessage: 'Table',
}),
order: 10,
directive: {
template: tableHtml,
scope: {
hit: '=',
indexPattern: '=',
filter: '=',
columns: '=',
onAddColumn: '=',
onRemoveColumn: '=',
},
controller: function ($scope) {
$scope.mapping = $scope.indexPattern.fields.byName;
$scope.flattened = $scope.indexPattern.flattenHit($scope.hit);
$scope.formatted = $scope.indexPattern.formatHit($scope.hit);
$scope.fields = _.keys($scope.flattened).sort();

$scope.canToggleColumns = function canToggleColumn() {
return (
_.isFunction($scope.onAddColumn)
&& _.isFunction($scope.onRemoveColumn)
);
};

$scope.toggleColumn = function toggleColumn(columnName) {
if ($scope.columns.includes(columnName)) {
$scope.onRemoveColumn(columnName);
} else {
$scope.onAddColumn(columnName);
}
};

$scope.isColumnActive = function isColumnActive(columnName) {
return $scope.columns.includes(columnName);
};

$scope.showArrayInObjectsWarning = function (row, field) {
const value = $scope.flattened[field];
return Array.isArray(value) && typeof value[0] === 'object';
};
}
}
render: (domNode, props) => {
const cleanupFnPromise = injectAngularElement(domNode, tableHtml, props, controller);
return () => {
cleanupFnPromise.then(cleanup => cleanup());
};
},
};
});
44 changes: 44 additions & 0 deletions src/legacy/core_plugins/kbn_doc_views/public/views/table_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 angular from 'angular';
import chrome from 'ui/chrome';

/**
* compiling and injecting the give angular template into the given dom node
* returning a function to cleanup the injected angular element
*/
export async function injectAngularElement(domNode, template, scopeProps, controller) {
const $injector = await chrome.dangerouslyGetActiveInjector();
const rootScope = $injector.get('$rootScope');
const newScope = Object.assign(rootScope.$new(), scopeProps);
if(typeof controller === 'function') {
controller(newScope);
}

const linkFn = $injector.get('$compile')(template)(newScope);
newScope.$digest();
angular
.element(domNode)
.empty()
.append(linkFn);

return () => {
newScope.$destroy();
};
}
kertal marked this conversation as resolved.
Show resolved Hide resolved
Loading