diff options
Diffstat (limited to 'docs/app/src/errors.js')
| -rw-r--r-- | docs/app/src/errors.js | 62 | 
1 files changed, 62 insertions, 0 deletions
| diff --git a/docs/app/src/errors.js b/docs/app/src/errors.js new file mode 100644 index 00000000..b91cfb81 --- /dev/null +++ b/docs/app/src/errors.js @@ -0,0 +1,62 @@ +angular.module('errors', ['ngSanitize']) + +.filter('errorLink', ['$sanitize', function ($sanitize) { +  var LINKY_URL_REGEXP = /((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s\.\;\,\(\)\{\}<>]/g, +      MAILTO_REGEXP = /^mailto:/, +      STACK_TRACE_REGEXP = /:\d+:\d+$/; + +  var truncate = function (text, nchars) { +    if (text.length > nchars) { +      return text.substr(0, nchars - 3) + '...'; +    } +    return text; +  }; + +  return function (text, target) { +    var targetHtml = target ? ' target="' + target + '"' : ''; + +    if (!text) return text; + +    return $sanitize(text.replace(LINKY_URL_REGEXP, function (url) { +      if (STACK_TRACE_REGEXP.test(url)) { +        return url; +      } + +      // if we did not match ftp/http/mailto then assume mailto +      if (!/^((ftp|https?):\/\/|mailto:)/.test(url)) url = 'mailto:' + url; + +      return '<a' + targetHtml + ' href="' + url +'">' + +                truncate(url.replace(MAILTO_REGEXP, ''), 60) + +              '</a>'; +    })); +  }; +}]) + + +.directive('errorDisplay', ['$location', 'errorLinkFilter', function ($location, errorLinkFilter) { +  var interpolate = function (formatString) { +    var formatArgs = arguments; +    return formatString.replace(/\{\d+\}/g, function (match) { +      // Drop the braces and use the unary plus to convert to an integer. +      // The index will be off by one because of the formatString. +      var index = +match.slice(1, -1); +      if (index + 1 >= formatArgs.length) { +        return match; +      } +      return formatArgs[index+1]; +    }); +  }; + +  return { +    link: function (scope, element, attrs) { +      var search = $location.search(), +        formatArgs = [attrs.errorDisplay], +        i; + +      for (i = 0; angular.isDefined(search['p'+i]); i++) { +        formatArgs.push(search['p'+i]); +      } +      element.html(errorLinkFilter(interpolate.apply(null, formatArgs), '_blank')); +    } +  }; +}]);
\ No newline at end of file | 
