refactor import preview strings

This commit is contained in:
Maximilian Krambach 2019-02-25 15:34:11 +01:00
parent 798af8c5ab
commit 93260aaf29
5 changed files with 49 additions and 99 deletions

View File

@ -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);
}

View File

@ -10,7 +10,6 @@ import { DurationService } from 'app/core/ui-services/duration.service';
import { FileExportService } from 'app/core/ui-services/file-export.service'; import { FileExportService } from 'app/core/ui-services/file-export.service';
import { itemVisibilityChoices } from 'app/shared/models/agenda/item'; import { itemVisibilityChoices } from 'app/shared/models/agenda/item';
import { ViewCreateTopic } from '../../models/view-create-topic'; import { ViewCreateTopic } from '../../models/view-create-topic';
import { stripHtmlTags } from 'app/shared/utils/strip-html-tags';
/** /**
* Component for the agenda import list view. * Component for the agenda import list view.
@ -49,44 +48,6 @@ export class AgendaImportListComponent extends BaseImportListComponent<ViewCreat
this.textAreaForm = formBuilder.group({ inputtext: [''] }); this.textAreaForm = formBuilder.group({ inputtext: [''] });
} }
/**
* Get the first characters of a string, for preview purposes
*
* @param input any string
* @returns a string with at most 50 characters
*/
public getShortPreview(input: string): string {
if (!input) {
return '';
}
if (input.length > 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 * Triggers an example csv download
*/ */

View File

@ -6,6 +6,7 @@ import { BaseViewModel } from './base-view-model';
import { NewEntry, ValueLabelCombination, BaseImportService } from 'app/core/ui-services/base-import.service'; import { NewEntry, ValueLabelCombination, BaseImportService } from 'app/core/ui-services/base-import.service';
import { Title } from '@angular/platform-browser'; import { Title } from '@angular/platform-browser';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { getLongPreview, getShortPreview } from 'app/shared/utils/previewStrings';
export abstract class BaseImportListComponent<V extends BaseViewModel> extends BaseViewComponent implements OnInit { export abstract class BaseImportListComponent<V extends BaseViewModel> extends BaseViewComponent implements OnInit {
/** /**
@ -13,6 +14,16 @@ export abstract class BaseImportListComponent<V extends BaseViewModel> extends B
*/ */
public dataSource: MatTableDataSource<NewEntry<V>>; public dataSource: MatTableDataSource<NewEntry<V>>;
/**
* 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 * Switch that turns true if a file has been selected in the input
*/ */

View File

@ -8,7 +8,6 @@ import { BaseImportListComponent } from 'app/site/base/base-import-list';
import { MotionCsvExportService } from '../../services/motion-csv-export.service'; import { MotionCsvExportService } from '../../services/motion-csv-export.service';
import { MotionImportService } from '../../services/motion-import.service'; import { MotionImportService } from '../../services/motion-import.service';
import { ViewMotion } from '../../models/view-motion'; import { ViewMotion } from '../../models/view-motion';
import { stripHtmlTags } from 'app/shared/utils/strip-html-tags';
/** /**
* Component for the motion import list view. * Component for the motion import list view.
@ -37,35 +36,6 @@ export class MotionImportListComponent extends BaseImportListComponent<ViewMotio
super(importer, titleService, translate, matSnackBar); super(importer, titleService, translate, matSnackBar);
} }
/**
* Returns the first characters of a string, for preview purposes
*
* @param input
*/
public getShortPreview(input: string): string {
if (input.length > 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 * Triggers an example csv download
*/ */

View File

@ -7,7 +7,6 @@ import { BaseImportListComponent } from 'app/site/base/base-import-list';
import { ViewStatuteParagraph } from 'app/site/motions/models/view-statute-paragraph'; import { ViewStatuteParagraph } from 'app/site/motions/models/view-statute-paragraph';
import { StatuteImportService } from 'app/site/motions/services/statute-import.service'; import { StatuteImportService } from 'app/site/motions/services/statute-import.service';
import { StatuteCsvExportService } from 'app/site/motions/services/statute-csv-export.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. * Component for the statute paragraphs import list view.
@ -36,35 +35,6 @@ export class StatuteImportListComponent extends BaseImportListComponent<ViewStat
super(importer, titleService, translate, matSnackBar); super(importer, titleService, translate, matSnackBar);
} }
/**
* Returns the first characters of a string, for preview purposes
*
* @param input
*/
public getShortPreview(input: string): string {
if (input.length > 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 * Triggers an example csv download
*/ */