From 93260aaf292ea396fa81e1de08b232edbaf72360 Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Mon, 25 Feb 2019 15:34:11 +0100 Subject: [PATCH] refactor import preview strings --- client/src/app/shared/utils/previewStrings.ts | 38 ++++++++++++++++++ .../agenda-import-list.component.ts | 39 ------------------- client/src/app/site/base/base-import-list.ts | 11 ++++++ .../motion-import-list.component.ts | 30 -------------- .../statute-import-list.component.ts | 30 -------------- 5 files changed, 49 insertions(+), 99 deletions(-) create mode 100644 client/src/app/shared/utils/previewStrings.ts diff --git a/client/src/app/shared/utils/previewStrings.ts b/client/src/app/shared/utils/previewStrings.ts new file mode 100644 index 000000000..c7dc6e9fb --- /dev/null +++ b/client/src/app/shared/utils/previewStrings.ts @@ -0,0 +1,38 @@ +import { stripHtmlTags } from './strip-html-tags'; + +/** + * Helper to get a preview string + * + * @param input + * @returns returns the first and last 150 characters of a string; used within + * tooltips for previews + */ +export function getLongPreview(input: string): string { + if (!input || !input.length) { + return ''; + } + if (input.length < 300) { + return stripHtmlTags(input); + } + return ( + stripHtmlTags(input.substring(0, 147)) + + ' [...] ' + + stripHtmlTags(input.substring(input.length - 150, input.length)) + ); +} + +/** + * Get the first characters of a string, for preview purposes + * + * @param input any string + * @returns a string with at most 50 characters + */ +export function getShortPreview(input: string): string { + if (!input || !input.length) { + return ''; + } + if (input.length > 50) { + return stripHtmlTags(input.substring(0, 47)) + '...'; + } + return stripHtmlTags(input); +} diff --git a/client/src/app/site/agenda/components/agenda-import-list/agenda-import-list.component.ts b/client/src/app/site/agenda/components/agenda-import-list/agenda-import-list.component.ts index eaf390bcf..a7a74d1ae 100644 --- a/client/src/app/site/agenda/components/agenda-import-list/agenda-import-list.component.ts +++ b/client/src/app/site/agenda/components/agenda-import-list/agenda-import-list.component.ts @@ -10,7 +10,6 @@ import { DurationService } from 'app/core/ui-services/duration.service'; import { FileExportService } from 'app/core/ui-services/file-export.service'; import { itemVisibilityChoices } from 'app/shared/models/agenda/item'; import { ViewCreateTopic } from '../../models/view-create-topic'; -import { stripHtmlTags } from 'app/shared/utils/strip-html-tags'; /** * Component for the agenda import list view. @@ -49,44 +48,6 @@ export class AgendaImportListComponent extends BaseImportListComponent 50) { - return stripHtmlTags(input.substring(0, 47)) + '...'; - } - return stripHtmlTags(input); - } - - /** - * Fetch the first and last 150 characters of a string; used within - * tooltips for the preview - * - * @param input any string - * @returns a string with the first and last 150 characters of the input - * string - */ - public getLongPreview(input: string): string { - if (!input) { - return ''; - } - if (input.length < 300) { - return stripHtmlTags(input); - } - return ( - stripHtmlTags(input.substring(0, 147)) + - ' [...] ' + - stripHtmlTags(input.substring(input.length - 150, input.length)) - ); - } - /** * Triggers an example csv download */ diff --git a/client/src/app/site/base/base-import-list.ts b/client/src/app/site/base/base-import-list.ts index 7d5df79c6..cef72bc85 100644 --- a/client/src/app/site/base/base-import-list.ts +++ b/client/src/app/site/base/base-import-list.ts @@ -6,6 +6,7 @@ import { BaseViewModel } from './base-view-model'; import { NewEntry, ValueLabelCombination, BaseImportService } from 'app/core/ui-services/base-import.service'; import { Title } from '@angular/platform-browser'; import { TranslateService } from '@ngx-translate/core'; +import { getLongPreview, getShortPreview } from 'app/shared/utils/previewStrings'; export abstract class BaseImportListComponent extends BaseViewComponent implements OnInit { /** @@ -13,6 +14,16 @@ export abstract class BaseImportListComponent extends B */ public dataSource: MatTableDataSource>; + /** + * Helper function for previews + */ + public getLongPreview = getLongPreview; + + /** + * Helper function for previews + */ + public getShortPreview = getShortPreview; + /** * Switch that turns true if a file has been selected in the input */ diff --git a/client/src/app/site/motions/components/motion-import-list/motion-import-list.component.ts b/client/src/app/site/motions/components/motion-import-list/motion-import-list.component.ts index f6f5a5f84..d1656d078 100644 --- a/client/src/app/site/motions/components/motion-import-list/motion-import-list.component.ts +++ b/client/src/app/site/motions/components/motion-import-list/motion-import-list.component.ts @@ -8,7 +8,6 @@ import { BaseImportListComponent } from 'app/site/base/base-import-list'; import { MotionCsvExportService } from '../../services/motion-csv-export.service'; import { MotionImportService } from '../../services/motion-import.service'; import { ViewMotion } from '../../models/view-motion'; -import { stripHtmlTags } from 'app/shared/utils/strip-html-tags'; /** * Component for the motion import list view. @@ -37,35 +36,6 @@ export class MotionImportListComponent extends BaseImportListComponent 50) { - return stripHtmlTags(input.substring(0, 47)) + '...'; - } - return stripHtmlTags(input); - } - - /** - * Returns the first and last 150 characters of a string; used within - * tooltips for the preview - * - * @param input - */ - public getLongPreview(input: string): string { - if (input.length < 300) { - return stripHtmlTags(input); - } - return ( - stripHtmlTags(input.substring(0, 147)) + - ' [...] ' + - stripHtmlTags(input.substring(input.length - 150, input.length)) - ); - } - /** * Triggers an example csv download */ diff --git a/client/src/app/site/motions/components/statute-paragraph-list/statute-import-list/statute-import-list.component.ts b/client/src/app/site/motions/components/statute-paragraph-list/statute-import-list/statute-import-list.component.ts index f50dd3971..96f9829e0 100644 --- a/client/src/app/site/motions/components/statute-paragraph-list/statute-import-list/statute-import-list.component.ts +++ b/client/src/app/site/motions/components/statute-paragraph-list/statute-import-list/statute-import-list.component.ts @@ -7,7 +7,6 @@ import { BaseImportListComponent } from 'app/site/base/base-import-list'; import { ViewStatuteParagraph } from 'app/site/motions/models/view-statute-paragraph'; import { StatuteImportService } from 'app/site/motions/services/statute-import.service'; import { StatuteCsvExportService } from 'app/site/motions/services/statute-csv-export.service'; -import { stripHtmlTags } from 'app/shared/utils/strip-html-tags'; /** * Component for the statute paragraphs import list view. @@ -36,35 +35,6 @@ export class StatuteImportListComponent extends BaseImportListComponent 50) { - return stripHtmlTags(input.substring(0, 47)) + '...'; - } - return stripHtmlTags(input); - } - - /** - * Returns the first and last 150 characters of a string; used within - * tooltips for the preview - * - * @param input - */ - public getLongPreview(input: string): string { - if (input.length < 300) { - return stripHtmlTags(input); - } - return ( - stripHtmlTags(input.substring(0, 147)) + - ' [...] ' + - stripHtmlTags(input.substring(input.length - 150, input.length)) - ); - } - /** * Triggers an example csv download */