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

Metadata widget #4212

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
41 changes: 0 additions & 41 deletions scripts/api/filters.ts

This file was deleted.

66 changes: 64 additions & 2 deletions scripts/api/vocabularies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,81 @@ function getVocabularyItemLabel(term: IVocabularyItem, item: IArticle): string {
return getVocabularyItemNameTranslated(term, language);
}

const vocabularyItemsToString = (
array: Array<IVocabularyItem>,
propertyName?: keyof IVocabularyItem,
schemeName?: string,
): string =>
getVocabularyItemsByPropertyName(array, propertyName, schemeName).join(', ');

const getVocabularyItemsByPropertyName = (
array: Array<IVocabularyItem>,
propertyName?: keyof IVocabularyItem,
schemeName?: string,
): Array<string> => {
let subjectMerged = [];

array.forEach((item) => {
const value = propertyName == null ? item : item[propertyName];

if (value) {
subjectMerged.push(value);

if ((schemeName?.length ?? 0) && item.scheme !== schemeName) {
Copy link
Member

Choose a reason for hiding this comment

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

compare the number to something

subjectMerged.pop();
}
}
});

return subjectMerged;
};

const getVocabularyItemsPreview = (
array: Array<IVocabularyItem>,
propertyName?: keyof IVocabularyItem,
schemeName?: string,
returnArray?: boolean,
): Array<string> | string => {
if (returnArray) {
return getVocabularyItemsByPropertyName(array, propertyName, schemeName);
} else {
return vocabularyItemsToString(array, propertyName, schemeName);
}
};

/**
* Selection vocabularies may be configured to be included in content profiles.
*/
function isSelectionVocabulary(vocabulary: IVocabulary) {
function isSelectionVocabulary(vocabulary: IVocabulary): boolean {
return !isCustomFieldVocabulary(vocabulary) && (
vocabulary.selection_type === 'multi selection'
|| vocabulary.selection_type === 'single selection'
);
}

export const vocabularies = {
interface IVocabulariesApi {
getAll: () => OrderedMap<IVocabulary['_id'], IVocabulary>;
isCustomFieldVocabulary:(vocabulary: IVocabulary) => boolean;
isSelectionVocabulary: (vocabulary: IVocabulary) => boolean;
getVocabularyItemLabel: (term: IVocabularyItem, item: IArticle) => string;
getVocabularyItemsPreview: (
array: Array<IVocabularyItem>,
propertyName?: keyof IVocabularyItem,
schemeName?: string,
returnArray?: boolean
) => Array<string> | string;
vocabularyItemsToString: (
array: Array<IVocabularyItem>,
propertyName?: keyof IVocabularyItem,
schemeName?: string,
) => string;
}

export const vocabularies: IVocabulariesApi = {
getAll,
isCustomFieldVocabulary,
isSelectionVocabulary,
getVocabularyItemLabel,
getVocabularyItemsPreview,
vocabularyItemsToString,
};
3 changes: 2 additions & 1 deletion scripts/apps/archive/directives/HtmlPreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {getAnnotationsFromItem} from 'core/editor3/helpers/editor3CustomData';
import {META_FIELD_NAME} from 'core/editor3/helpers/fieldsMeta';
import ng from 'core/services/ng';
import {gettext} from 'core/utils';
import {IArticle} from 'superdesk-api';

function getAnnotationTypesAsync(scope) {
ng.get('metadata').initialize()
Expand All @@ -24,7 +25,7 @@ interface IAnotationData {
type: string;
}

export function getAllAnnotations(item): Array<IAnotationData> {
export function getAllAnnotations(item: IArticle): Array<IAnotationData> {
const annotations = [];

for (const field in item[META_FIELD_NAME]) {
Expand Down
7 changes: 0 additions & 7 deletions scripts/apps/archive/styles/html-preview.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,3 @@ sup.annotation-id {
padding: 0;
}
}

.annotation-body-react {
border-bottom: 1px dotted;
p {
display: inline;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {gettext} from 'core/utils';
import React from 'react';
import {IArticle} from 'superdesk-api';
import {Label, ToggleBox} from 'superdesk-ui-framework/react';
import './annotations-preview.scss';

interface IProps {
article: IArticle;
Expand All @@ -18,17 +19,17 @@ export class AnnotationsPreview extends React.Component<IProps> {
<div dangerouslySetInnerHTML={{__html: article.archive_description}} />
<ToggleBox title={gettext('Annotations')}>
{
(article?.annotations?.length ?? 0) > 0 && (
getAllAnnotations(article).map((a) => (
<Spacer h gap="4" key={a.id} noWrap>
<Label text={a.type} style="hollow" type="primary" />
(article.annotations?.length ?? 0) > 0 && (
getAllAnnotations(article).map((annotation) => (
<Spacer h gap="4" key={annotation.id} noWrap>
<Label text={annotation.type} style="hollow" type="primary" />
<div>
<span
className="annotation-body-react"
dangerouslySetInnerHTML={{__html: a.body}}
dangerouslySetInnerHTML={{__html: annotation.body}}
/>
<sup className="annotation-id">
{a.id}
{annotation.id}
</sup>
</div>
</Spacer>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.annotation-body-react {
border-bottom: 1px dotted;
p {
display: inline;
}
}
31 changes: 16 additions & 15 deletions scripts/apps/authoring-react/article-widgets/metadata/metadata.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {Fragment} from 'react';
import {IArticleSideWidget, IExtensionActivationResult} from 'superdesk-api';
import {IArticleSideWidget, IExtensionActivationResult, IVocabularyItem} from 'superdesk-api';
import {gettext} from 'core/utils';
import {AuthoringWidgetHeading} from 'apps/dashboard/widget-heading';
import {AuthoringWidgetLayout} from 'apps/dashboard/widget-layout';
Expand Down Expand Up @@ -93,8 +93,6 @@ class MetadataWidget extends React.PureComponent<IProps, IState> {

const {onArticleChange} = this.props;

const getNotForPublicationLabel: string = gettext('Not For Publication');
const getLegalLabel: string = gettext('Legal');
const allVocabularies = sdApi.vocabularies.getAll();

return (
Expand All @@ -109,7 +107,7 @@ class MetadataWidget extends React.PureComponent<IProps, IState> {
<Spacer v gap="16" noWrap>
<Spacer h gap="64" justifyContent="space-between" noWrap>
<Heading type="h6" align="start">
{getNotForPublicationLabel}
{gettext('Not For Publication')}
</Heading>
<Switch
label={{text: ''}} // TODO: Implement accessibility
Expand All @@ -130,7 +128,7 @@ class MetadataWidget extends React.PureComponent<IProps, IState> {

<Spacer h gap="64" justifyContent="space-between" noWrap>
<Heading type="h6" align="start">
{getLegalLabel}
{gettext('Legal')}
</Heading>
<Switch
label={{text: ''}} // TODO: Implement accessibility
Expand Down Expand Up @@ -299,7 +297,7 @@ class MetadataWidget extends React.PureComponent<IProps, IState> {
{keywords && (
<MetadataItem
label={gettext('Word Count')}
value={sdApi.filters.mergeArrayToString(keywords) as string}
value={sdApi.vocabularies.vocabularyItemsToString(keywords)}
/>
)}

Expand All @@ -326,7 +324,7 @@ class MetadataWidget extends React.PureComponent<IProps, IState> {
anpa_category?.name != null && (
<MetadataItem
label={gettext('Category')}
value={sdApi.filters.mergeArrayToString(anpa_category, 'name') as string}
value={sdApi.vocabularies.vocabularyItemsToString(anpa_category, 'name')}
/>
)
}
Expand All @@ -335,11 +333,14 @@ class MetadataWidget extends React.PureComponent<IProps, IState> {
allVocabularies
.filter((cv) => article[cv.schema_field] != null)
.toArray()
.map((v) => (
.map((vocabulary) => (
<MetadataItem
key={v._id}
label={v.display_name}
value={vocabularies.getVocabularyItemLabel(article[v.schema_field], article)}
key={vocabulary._id}
label={vocabulary.display_name}
value={vocabularies.getVocabularyItemLabel(
article[vocabulary.schema_field],
article,
)}
/>
))
}
Expand All @@ -350,7 +351,7 @@ class MetadataWidget extends React.PureComponent<IProps, IState> {
&& (
<MetadataItem
label={gettext('Genre')}
value={sdApi.filters.mergeArrayToString(genre, 'name') as string}
value={sdApi.vocabularies.vocabularyItemsToString(genre, 'name')}
/>
)
}
Expand All @@ -361,7 +362,7 @@ class MetadataWidget extends React.PureComponent<IProps, IState> {
&& (
<MetadataItem
label={gettext('Place')}
value={sdApi.filters.mergeArrayToString(place, 'name') as string}
value={sdApi.vocabularies.vocabularyItemsToString(place, 'name')}
/>
)
}
Expand Down Expand Up @@ -449,8 +450,8 @@ class MetadataWidget extends React.PureComponent<IProps, IState> {
}

{
['picture'].includes(article.type)
&& article?.archive_description !== article?.description_text
article.type === 'picture'
&& article.archive_description !== article.description_text
&& (
<AnnotationsPreview article={article} />
)
Expand Down
6 changes: 3 additions & 3 deletions scripts/apps/authoring/metadata/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import _ from 'lodash';
import PreferedCvItemsConfigDirective from './PreferedCvItemsConfigDirective';
import MetaPlaceDirective from './MetaPlaceDirective';
import {getVocabularySelectionTypes} from '../../vocabularies/constants';
import {gettext, getVocabularyItemNameTranslated} from 'core/utils';
import {gettext} from 'core/utils';
import PlacesServiceFactory from './PlacesService';
import {appConfig} from 'appConfig';
import {ISubject} from 'superdesk-api';
import {IArticle, ISubject, IVocabularyItem} from 'superdesk-api';
import {reactToAngular1} from 'superdesk-ui-framework';
import {MetaDataDropdownSingleSelectReact} from './views/MetaDataDropdownSingleSelectReact';
import {sdApi} from 'api';
Expand Down Expand Up @@ -1367,7 +1367,7 @@ export function MetadataService(api, subscribersService, vocabularies, $rootScop
priorityByValue: function(value) {
return this._priorityByValue[value] || null;
},
getLocaleName: sdApi.vocabularies.getVocabularyItemLabel,
getLocaleName: (term: IVocabularyItem, item: IArticle) => sdApi.vocabularies.getVocabularyItemLabel(term, item),
};

$rootScope.$on('subscriber:create', () => service.fetchSubscribers());
Expand Down
2 changes: 1 addition & 1 deletion scripts/core/filters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default angular.module('superdesk.core.filters', [])
return texts.join('\n');
})
.filter('mergeWords', [function() {
return sdApi.filters.mergeArrayToString;
return sdApi.vocabularies.getVocabularyItemsPreview;
}])
.filter('splitWords', () => function(word) {
var split = [];
Expand Down