Skip to content

Commit

Permalink
feat(formatter): add a new HyperlinkURI formatter, you need to uriPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Mar 7, 2018
1 parent 9d2aae1 commit 52de978
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Column, Formatter } from './../models/index';

export const hyperlinkFormatter: Formatter = (row: number, cell: number, value: any, columnDef: Column, dataContext: any) => {
const matchUrl = value.match(/^(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?/, 'i');
if (matchUrl && Array.isArray(matchUrl)) {
return `<a href="${matchUrl[0]}">' + value + '</a>`;
if (value && typeof value === 'string') {
const matchUrl = value.match(/^(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?/i);
if (matchUrl && Array.isArray(matchUrl)) {
return `<a href="${matchUrl[0]}">' + value + '</a>`;
}
}
return '';
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Column, Formatter } from './../models/index';

/** Takes an hyperlink URI prefix (passed in column definition "params.uriPrefix") and adds the cell value. The structure will be "<a href="uriPrefix">value</a>" */
export const hyperlinkUriPrefixFormatter: Formatter = (row: number, cell: number, value: any, columnDef: Column, dataContext: any) => {
let uriPrefix = (columnDef && columnDef.params && columnDef.params.uriPrefix) ? columnDef.params.uriPrefix : '';
if (value && uriPrefix && typeof uriPrefix === 'string' && !uriPrefix.includes('<script>')) {
uriPrefix += value;
return '<a href="' + uriPrefix + '">' + value + '</a>';
}
return '';
};
6 changes: 5 additions & 1 deletion aurelia-slickgrid/src/aurelia-slickgrid/formatters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { dateUsFormatter } from './dateUsFormatter';
import { deleteIconFormatter } from './deleteIconFormatter';
import { editIconFormatter } from './editIconFormatter';
import { hyperlinkFormatter } from './hyperlinkFormatter';
import { hyperlinkUriPrefixFormatter } from './hyperlinkUriPrefixFormatter';
import { infoIconFormatter } from './infoIconFormatter';
import { lowercaseFormatter } from './lowercaseFormatter';
import { multipleFormatter } from './multipleFormatter';
Expand Down Expand Up @@ -75,9 +76,12 @@ export const Formatters = {
/** Displays a Font-Awesome edit icon (fa-pencil) */
editIcon: editIconFormatter,

/** Takes a cell value and transforms it into an hyperlink, given that the value starts with 1 of these (http|ftp|https) */
/** Takes an hyperlink cell value and transforms it into a real hyperlink, given that the value starts with 1 of these (http|ftp|https). The structure will be "<a href="hyperlink">hyperlink</a>" */
hyperlink: hyperlinkFormatter,

/** Takes an hyperlink URI prefix (passed in column definition "params.uriPrefix") and adds the cell value. The structure will be "<a href="uriPrefix">value</a>" */
hyperlinkUriPrefix: hyperlinkUriPrefixFormatter,

/** Displays a Font-Awesome edit icon (fa-info-circle) */
infoIcon: infoIconFormatter,

Expand Down

0 comments on commit 52de978

Please sign in to comment.