diff options
| author | Peter Bacon Darwin | 2014-02-12 22:47:42 +0000 |
|---|---|---|
| committer | Peter Bacon Darwin | 2014-02-16 19:03:41 +0000 |
| commit | 389d4879da4aa620ee95d789b19ff9be44eb730a (patch) | |
| tree | 93352ddc8738a975904a1774d51b93d585ca1075 /docs/app/src/errors.js | |
| parent | a564160511bf1bbed5a4fe5d2981fae1bb664eca (diff) | |
| download | angular.js-389d4879da4aa620ee95d789b19ff9be44eb730a.tar.bz2 | |
chore(doc-gen): new docs
chore(doc-gen): implement dgeni
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 |
