@@ -75,6 +75,25 @@ function _download_filtered_file(_params, _format) {
7575 f . remove ( ) ;
7676}
7777
78+ function _linkify ( _data ) {
79+ const linksFound = _data . match ( / ( \b ( h t t p s ? ) [ - A - Z 0 - 9 + & @ # \/ % ? = ~ _ | ! : , . ; ] * [ - A - Z 0 - 9 + & @ # \/ % = ~ _ | ] ) / ig) ;
80+ let links = [ ] ;
81+ let output ;
82+ if ( linksFound != null ) {
83+ if ( linksFound . length === 1 && _data . match ( / \. ( j p e g | j p g | g i f | p n g | s v g | a p n g | w e b p | a v i f ) $ / ) ) {
84+ // the whole text is just one link and its a picture, create a thumbnail
85+ output = '<div class="thumbnail zoomthumb"><a href="' + linksFound [ 0 ] + '" target="_blank"><img src="' + linksFound [ 0 ] + '"></a></div>' ;
86+ return { text : output , links : linksFound } ;
87+ }
88+ for ( let i = 0 ; i < linksFound . length ; i ++ ) {
89+ links . push ( '<a href="' + linksFound [ i ] + '" target="_blank">' + linksFound [ i ] + '</a>' ) ;
90+ output = _data . split ( linksFound [ i ] ) . map ( function ( item ) { return item } ) . join ( links [ i ] ) ;
91+ }
92+ return { text : output , links : linksFound } ;
93+ }
94+ return { text : _data , links : [ ] } ;
95+ }
96+
7897function load_datatable ( CKAN_MODULE ) {
7998 const _ = CKAN_MODULE . _ ;
8099 const searchParams = new URLSearchParams ( document . location . search ) ;
@@ -210,18 +229,48 @@ function load_datatable(CKAN_MODULE){
210229 DataTable . render . ellipsis = function ( _cutoff , _rowIndex , _datatoreID ) {
211230 /**
212231 * Custom DataTable render function for ellipsis.
232+ *
233+ * Links will be rendered into possible image thumbnails
234+ * and HTML achor tags. Ellipsis text will split before any anchor elements.
213235 */
214236 return function ( _data , _type , _row , _meta ) {
215237 if ( _type == 'display' ) {
216238 let str = _data . toString ( ) ;
217- let htmlStr = $ ( $ . parseHTML ( str ) ) . text ( ) ;
218- if ( str . length < _cutoff || htmlStr . length < _cutoff ) {
219- return _data ;
239+ let linkifiedData = _linkify ( str ) ;
240+ let strippedData = linkifiedData . text . replace ( / ( < ( [ ^ > ] + ) > ) / gi, '' ) ;
241+ if ( strippedData . length <= _cutoff ) {
242+ return linkifiedData . text ;
243+ }
244+ let preview ;
245+ let remaining ;
246+ if ( linkifiedData . links . length > 0 ) {
247+ // cutoff before anchor element
248+ let linkpos = _cutoff ;
249+ let lastpos = _cutoff ;
250+ let lastlink = '' ;
251+ let addLen = 0 ;
252+ // check if truncation point is in the middle of a link
253+ for ( const aLink of linkifiedData . links ) {
254+ linkpos = str . indexOf ( aLink ) ;
255+ if ( linkpos + aLink . length >= _cutoff ) {
256+ // truncation point is in the middle of a link, truncate to where the link started
257+ break
258+ } else {
259+ addLen = addLen + lastlink . length ? ( lastlink . length ) + 31 : 0 ; // 31 is the number of other chars in the full anchor tag
260+ lastpos = linkpos ;
261+ lastlink = aLink ;
262+ }
263+ }
264+ preview = linkifiedData . text . substr ( 0 , lastpos + addLen ) . trimEnd ( ) ;
265+ // TODO: get remaining...
266+ remaining = linkifiedData . text . substr ( lastpos + addLen ) . trimEnd ( ) ;
267+ } else {
268+ preview = str . substr ( 0 , _cutoff - 1 ) ;
269+ remaining = str . substr ( _cutoff - 1 ) ;
220270 }
221271 let _elementID = 'datatableReadMore_' + _rowIndex + '_' + _datatoreID ;
222272 let expander = '<a class="datatable-readmore-expander" href="javascript:void(0);" data-toggle="collapse" data-bs-toggle="collapse" aria-expanded="false" aria-controls="' + _elementID + '">…</a>' ;
223- let preview = str . substr ( 0 , _cutoff - 1 ) + expander ;
224- let remaining = str . substr ( _cutoff - 1 ) ;
273+ preview += expander ;
225274 return '<div class="datatable-readmore"><span>' + preview + '</span><span class="collapse" id="' + _elementID + '">' + remaining + '<a class="datatable-readmore-minimizer" href="javascript:void(0);" data-toggle="collapse" data-bs-toggle="collapse" aria-expanded="true" aria-controls="' + _elementID + '"><small>[' + readLessLabel + ']</small></a><span></div>' ;
226275 }
227276 return _data ;
0 commit comments