1
1
var docsApp = {
2
2
controller : { } ,
3
3
directive : { } ,
4
- serviceFactory : { }
4
+ serviceFactory : { } ,
5
+ filter : { }
5
6
} ;
6
7
7
8
docsApp . controller . DocsVersionsCtrl = [ '$scope' , '$window' , 'NG_VERSIONS' , 'NG_VERSION' , function ( $scope , $window , NG_VERSIONS , NG_VERSION ) {
@@ -255,7 +256,40 @@ docsApp.directive.docTutorialReset = function() {
255
256
} ;
256
257
257
258
258
- docsApp . directive . errorDisplay = [ '$location' , function ( $location ) {
259
+ docsApp . filter . errorLink = [ '$sanitize' , function ( $sanitize ) {
260
+ var LINKY_URL_REGEXP = / ( ( f t p | h t t p s ? ) : \/ \/ | ( m a i l t o : ) ? [ A - Z a - z 0 - 9 . _ % + - ] + @ ) \S * [ ^ \s \. \; \, \( \) \{ \} \< \> ] / g,
261
+ MAILTO_REGEXP = / ^ m a i l t o : / ,
262
+ STACK_TRACE_REGEXP = / : \d + : \d + $ / ;
263
+
264
+ var truncate = function ( text , nchars ) {
265
+ if ( text . length > nchars ) {
266
+ return text . substr ( 0 , nchars - 3 ) + '...' ;
267
+ }
268
+ return text ;
269
+ } ;
270
+
271
+ return function ( text , target ) {
272
+ var targetHtml = target ? ' target="' + target + '"' : '' ;
273
+
274
+ if ( ! text ) return text ;
275
+
276
+ return $sanitize ( text . replace ( LINKY_URL_REGEXP , function ( url ) {
277
+ if ( STACK_TRACE_REGEXP . test ( url ) ) {
278
+ return url ;
279
+ }
280
+
281
+ // if we did not match ftp/http/mailto then assume mailto
282
+ if ( ! / ^ ( ( f t p | h t t p s ? ) : \/ \/ | m a i l t o : ) / . test ( url ) ) url = 'mailto:' + url ;
283
+
284
+ return '<a' + targetHtml + ' href="' + url + '">' +
285
+ truncate ( url . replace ( MAILTO_REGEXP , '' ) , 60 ) +
286
+ '</a>' ;
287
+ } ) ) ;
288
+ } ;
289
+ } ] ;
290
+
291
+
292
+ docsApp . directive . errorDisplay = [ '$location' , 'errorLinkFilter' , function ( $location , errorLinkFilter ) {
259
293
var interpolate = function ( formatString ) {
260
294
var formatArgs = arguments ;
261
295
return formatString . replace ( / \{ \d + \} / g, function ( match ) {
@@ -278,7 +312,7 @@ docsApp.directive.errorDisplay = ['$location', function ($location) {
278
312
for ( i = 0 ; angular . isDefined ( search [ 'p' + i ] ) ; i ++ ) {
279
313
formatArgs . push ( search [ 'p' + i ] ) ;
280
314
}
281
- element . text ( interpolate . apply ( null , formatArgs ) ) ;
315
+ element . html ( errorLinkFilter ( interpolate . apply ( null , formatArgs ) , '_blank' ) ) ;
282
316
}
283
317
} ;
284
318
} ] ;
@@ -873,3 +907,7 @@ angular.module('docsApp', ['ngResource', 'ngRoute', 'ngCookies', 'ngSanitize', '
873
907
factory ( docsApp . serviceFactory ) .
874
908
directive ( docsApp . directive ) .
875
909
controller ( docsApp . controller ) ;
910
+
911
+ angular . forEach ( docsApp . filter , function ( docsAppFilter , filterName ) {
912
+ angular . module ( 'docsApp' ) . filter ( filterName , docsAppFilter ) ;
913
+ } ) ;
0 commit comments