all.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. define(['angular', 'jquery', 'underscore', 'moment'], function (angular, $, _, moment) {
  2. 'use strict';
  3. var module = angular.module('grafana.filters');
  4. module.filter('stringSort', function() {
  5. return function(input) {
  6. return input.sort();
  7. };
  8. });
  9. /*
  10. Filter an array of objects by elasticsearch version requirements
  11. */
  12. module.filter('esVersion', function(esVersion) {
  13. return function(items, require) {
  14. var ret = _.filter(items,function(qt) {
  15. return esVersion.is(qt[require]) ? true : false;
  16. });
  17. return ret;
  18. };
  19. });
  20. module.filter('slice', function() {
  21. return function(arr, start, end) {
  22. if(!_.isUndefined(arr)) {
  23. return arr.slice(start, end);
  24. }
  25. };
  26. });
  27. module.filter('stringify', function() {
  28. return function(arr) {
  29. if(_.isObject(arr) && !_.isArray(arr)) {
  30. return angular.toJson(arr);
  31. } else {
  32. return _.isNull(arr) ? null : arr.toString();
  33. }
  34. };
  35. });
  36. module.filter('moment', function() {
  37. return function(date,mode) {
  38. switch(mode) {
  39. case 'ago':
  40. return moment(date).fromNow();
  41. }
  42. return moment(date).fromNow();
  43. };
  44. });
  45. module.filter('noXml', function() {
  46. var noXml = function(text) {
  47. return _.isString(text)
  48. ? text
  49. .replace(/&/g, '&')
  50. .replace(/</g, '&lt;')
  51. .replace(/>/g, '&gt;')
  52. .replace(/'/g, '&#39;')
  53. .replace(/"/g, '&quot;')
  54. : text;
  55. };
  56. return function(text) {
  57. return _.isArray(text)
  58. ? _.map(text, noXml)
  59. : noXml(text);
  60. };
  61. });
  62. module.filter('urlLink', function() {
  63. var //URLs starting with http://, https://, or ftp://
  64. r1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,
  65. //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
  66. r2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim,
  67. //Change email addresses to mailto:: links.
  68. r3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
  69. var urlLink = function(text) {
  70. var t1,t2,t3;
  71. if(!_.isString(text)) {
  72. return text;
  73. } else {
  74. _.each(text.match(r1), function() {
  75. t1 = text.replace(r1, "<a href=\"$1\" target=\"_blank\">$1</a>");
  76. });
  77. text = t1 || text;
  78. _.each(text.match(r2), function() {
  79. t2 = text.replace(r2, "$1<a href=\"http://$2\" target=\"_blank\">$2</a>");
  80. });
  81. text = t2 || text;
  82. _.each(text.match(r3), function() {
  83. t3 = text.replace(r3, "<a href=\"mailto:$1\">$1</a>");
  84. });
  85. text = t3 || text;
  86. return text;
  87. }
  88. };
  89. return function(text) {
  90. return _.isArray(text)
  91. ? _.map(text, urlLink)
  92. : urlLink(text);
  93. };
  94. });
  95. module.filter('gistid', function() {
  96. var gist_pattern = /(\d{5,})|([a-z0-9]{10,})|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
  97. return function(input) {
  98. if(!(_.isUndefined(input))) {
  99. var output = input.match(gist_pattern);
  100. if(!_.isNull(output) && !_.isUndefined(output)) {
  101. return output[0].replace(/.*\//, '');
  102. }
  103. }
  104. };
  105. });
  106. });