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

Add DocView links pluggable injection capability #1200

Merged
1 change: 1 addition & 0 deletions .i18nrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"interpreter": "src/legacy/core_plugins/interpreter",
"osd": "src/legacy/core_plugins/opensearch-dashboards",
"osdDocViews": "src/legacy/core_plugins/osd_doc_views",
"osdDocViewsLinks": "src/legacy/core_plugins/osd_doc_views_links",
"management": [
"src/legacy/core_plugins/management",
"src/plugins/management"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
setScopedHistory,
setServices,
setDocViewsRegistry,
setDocViewsLinksRegistry,
} from '../../../../opensearch_dashboards_services';
import { coreMock } from '../../../../../../../core/public/mocks';
import { dataPluginMock } from '../../../../../../data/public/mocks';
Expand Down Expand Up @@ -98,6 +99,18 @@ describe('Doc Table', () => {
},
});

setDocViewsLinksRegistry({
addDocViewLink(view) {
registry.push(view);
},
getDocViewsLinksSorted() {
return registry;
},
resetRegistry: () => {
registry = [];
},
});

getInnerAngularModule(
'app/discover',
core,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,12 @@
</div>
</div>
</div>
<div class="euiFlexItem euiFlexItem--flexGrowZero euiText euiText--small">
<div class="euiFlexGroup euiFlexGroup--gutterLarge euiFlexGroup--directionRow">
<div class="euiFlexItem euiFlexItem--flexGrowZero euiText euiText--small">
<a
class="euiLink"
data-test-subj="docTableRowAction"
ng-href="{{ getContextAppHref() }}"
ng-if="indexPattern.isTimeBased()"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing this logic in the src/plugins/discover/public/plugin.ts, could we include that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

i18n-id="discover.docTable.tableRow.viewSurroundingDocumentsLinkText"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to utilize i18n within the updated component once we have i18n translations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, added.

i18n-default-message="View surrounding documents"
></a>
</div>
<div class="euiFlexItem euiFlexItem--flexGrowZero euiText euiText--small">
<a
class="euiLink"
data-test-subj="docTableRowAction"
ng-href="#/doc/{{indexPattern.id}}/{{row._index}}?id={{uriEncodedId}}"
i18n-id="discover.docTable.tableRow.viewSingleDocumentLinkText"
i18n-default-message="View single document"
></a>
</div>
</div>
<div data-test-subj="docViewerLinks">
<doc-viewer-links
columns="columns"
hit="hit"
index-pattern="indexPattern"
></doc-viewer-links>
</div>
</div>
<div data-test-subj="docViewer">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { DocViewerLinks } from '../components/doc_viewer_links/doc_viewer_links';

export function createDocViewerLinksDirective(reactDirective: any) {
return reactDirective(
(props: any) => {
return <DocViewerLinks {...props} />;
},
[
'hit',
['indexPattern', { watchDepth: 'reference' }],
['columns', { watchDepth: 'collection' }],
],
{
restrict: 'E',
scope: {
hit: '=',
indexPattern: '=',
columns: '=?',
},
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ jest.mock('../../../opensearch_dashboards_services', () => {
registry = [];
},
}),
getDocViewsLinksRegistry: () => ({
addDocViewLink(view: any) {
registry.push(view);
},
getDocViewsLinksSorted() {
return registry;
},
resetRegistry: () => {
registry = [];
},
}),
};
});

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { shallow } from 'enzyme';
import { DocViewerLinks } from './doc_viewer_links';
import { getDocViewsLinksRegistry } from '../../../opensearch_dashboards_services';
import { DocViewLinkRenderProps } from '../../doc_views_links/doc_views_links_types';

jest.mock('../../../opensearch_dashboards_services', () => {
let registry: any[] = [];
return {
getDocViewsLinksRegistry: () => ({
addDocViewLink(view: any) {
registry.push(view);
},
getDocViewsLinksSorted() {
return registry;
},
resetRegistry: () => {
registry = [];
},
}),
};
});

beforeEach(() => {
(getDocViewsLinksRegistry() as any).resetRegistry();
jest.clearAllMocks();
});

test('Render <DocViewerLink/> with 2 different links', () => {
const registry = getDocViewsLinksRegistry();
registry.addDocViewLink({
order: 10,
label: 'generateCb link',
generateCb: () => ({
url: 'aaa',
}),
});
registry.addDocViewLink({ order: 20, label: 'href link', href: 'bbb' });

const renderProps = { hit: {} } as DocViewLinkRenderProps;

const wrapper = shallow(<DocViewerLinks {...renderProps} />);

expect(wrapper).toMatchSnapshot();
});

test('Dont Render <DocViewerLink/> if generateCb.hide', () => {
const registry = getDocViewsLinksRegistry();
registry.addDocViewLink({
order: 10,
label: 'generateCb link',
generateCb: () => ({
url: 'aaa',
hide: true,
}),
});

const renderProps = { hit: {} } as DocViewLinkRenderProps;

const wrapper = shallow(<DocViewerLinks {...renderProps} />);

expect(wrapper).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { EuiFlexGroup, EuiFlexItem, EuiListGroupItem, EuiListGroupItemProps } from '@elastic/eui';
import { getDocViewsLinksRegistry } from '../../../opensearch_dashboards_services';
import { DocViewLinkRenderProps } from '../../doc_views_links/doc_views_links_types';

export function DocViewerLinks(renderProps: DocViewLinkRenderProps) {
const listItems = getDocViewsLinksRegistry()
.getDocViewsLinksSorted()
.filter((item) => !(item.generateCb && item.generateCb(renderProps)?.hide))
.map((item) => {
const { generateCb, href, ...props } = item;
const listItem: EuiListGroupItemProps = {
'data-test-subj': 'docTableRowAction',
...props,
href: generateCb ? generateCb(renderProps).url : href,
};

return listItem;
});

return (
<EuiFlexGroup gutterSize="xs">
{listItems.map((item, index) => (
<EuiFlexItem key={index}>
<EuiListGroupItem {...item} />
</EuiFlexItem>
))}
</EuiFlexGroup>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { DocViewLink } from './doc_views_links_types';

export class DocViewsLinksRegistry {
private docViewsLinks: DocViewLink[] = [];

addDocViewLink(docViewLink: DocViewLink) {
this.docViewsLinks.push(docViewLink);
}

getDocViewsLinksSorted() {
return this.docViewsLinks.sort((a, b) => (Number(a.order) > Number(b.order) ? 1 : -1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { EuiListGroupItemProps } from '@elastic/eui';
import { OpenSearchSearchHit } from '../doc_views/doc_views_types';
import { IndexPattern } from '../../../../data/public';

export interface DocViewLink extends EuiListGroupItemProps {
href?: string;
order: number;
generateCb?(
renderProps: any
): {
url: string;
hide?: boolean;
};
}

export interface DocViewLinkRenderProps {
columns?: string[];
hit: OpenSearchSearchHit;
indexPattern: IndexPattern;
}
4 changes: 3 additions & 1 deletion src/plugins/discover/public/get_inner_angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { createTableRowDirective } from './application/angular/doc_table/compone
import { createPagerFactory } from './application/angular/doc_table/lib/pager/pager_factory';
import { createInfiniteScrollDirective } from './application/angular/doc_table/infinite_scroll';
import { createDocViewerDirective } from './application/angular/doc_viewer';
import { createDocViewerLinksDirective } from './application/angular/doc_viewer_links';
import { createRenderCompleteDirective } from './application/angular/directives/render_complete';
import {
initAngularBootstrap,
Expand Down Expand Up @@ -202,5 +203,6 @@ function createDocTableModule() {
.directive('osdTableRow', createTableRowDirective)
.directive('toolBarPagerButtons', createToolBarPagerButtonsDirective)
.directive('osdInfiniteScroll', createInfiniteScrollDirective)
.directive('docViewer', createDocViewerDirective);
.directive('docViewer', createDocViewerDirective)
.directive('docViewerLinks', createDocViewerLinksDirective);
}
3 changes: 3 additions & 0 deletions src/plugins/discover/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const createSetupContract = (): Setup => {
docViews: {
addDocView: jest.fn(),
},
docViewsLinks: {
addDocViewLink: jest.fn(),
},
};
return setupContract;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { DiscoverServices } from './build_services';
import { createGetterSetter } from '../../opensearch_dashboards_utils/public';
import { search } from '../../data/public';
import { DocViewsRegistry } from './application/doc_views/doc_views_registry';
import { DocViewsLinksRegistry } from './application/doc_views_links/doc_views_links_registry';

let angularModule: any = null;
let services: DiscoverServices | null = null;
Expand Down Expand Up @@ -81,6 +82,10 @@ export const [getUrlTracker, setUrlTracker] = createGetterSetter<{
export const [getDocViewsRegistry, setDocViewsRegistry] = createGetterSetter<DocViewsRegistry>(
'DocViewsRegistry'
);

export const [getDocViewsLinksRegistry, setDocViewsLinksRegistry] = createGetterSetter<
DocViewsLinksRegistry
>('DocViewsLinksRegistry');
/**
* Makes sure discover and context are using one instance of history.
*/
Expand Down
Loading