[ADF-1039] Search results highlighting (#2080)

* Provide a way to highlight the searched word

* Add unit test
This commit is contained in:
Maurizio Vitale
2017-07-13 15:07:48 +01:00
committed by Eugenio Romano
parent 55cd57b49d
commit 8a1281475c
10 changed files with 79 additions and 1 deletions

View File

@@ -57,6 +57,7 @@ import { DataColumnListComponent } from './src/components/data-column/data-colum
import { DataColumnComponent } from './src/components/data-column/data-column.component';
import { UploadDirective } from './src/directives/upload.directive';
import { FileSizePipe } from './src/pipes/file-size.pipe';
import { HighlightPipe } from './src/pipes/text-highlight.pipe';
import { AlfrescoMdlButtonDirective } from './src/components/material/mdl-button.directive';
import { AlfrescoMdlMenuDirective } from './src/components/material/mdl-menu.directive';
@@ -131,6 +132,7 @@ export function createTranslateLoader(http: Http, logService: LogService) {
DataColumnComponent,
DataColumnListComponent,
FileSizePipe,
HighlightPipe,
AdfToolbarComponent
],
providers: providers(),
@@ -150,6 +152,7 @@ export function createTranslateLoader(http: Http, logService: LogService) {
DataColumnComponent,
DataColumnListComponent,
FileSizePipe,
HighlightPipe,
AdfToolbarComponent
]
})

View File

@@ -0,0 +1,40 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed 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 { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'highlight'
})
export class HighlightPipe implements PipeTransform {
constructor() { }
transform(text: string, search: string): any {
if (search && text) {
let pattern = search.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
pattern = pattern.split(' ').filter((t) => {
return t.length > 0;
}).join('|');
const regex = new RegExp(pattern, 'gi');
let result = text.replace(regex, (match) => `<span class="highlight">${match}</span>`);
return result;
} else {
return text;
}
}
}