all.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. define(['angular', 'jquery', 'lodash', '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. module.filter('slice', function() {
  10. return function(arr, start, end) {
  11. if(!_.isUndefined(arr)) {
  12. return arr.slice(start, end);
  13. }
  14. };
  15. });
  16. module.filter('stringify', function() {
  17. return function(arr) {
  18. if(_.isObject(arr) && !_.isArray(arr)) {
  19. return angular.toJson(arr);
  20. } else {
  21. return _.isNull(arr) ? null : arr.toString();
  22. }
  23. };
  24. });
  25. module.filter('moment', function() {
  26. return function(date,mode) {
  27. switch(mode) {
  28. case 'ago':
  29. return moment(date).fromNow();
  30. }
  31. return moment(date).fromNow();
  32. };
  33. });
  34. module.filter('noXml', function() {
  35. var noXml = function(text) {
  36. return _.isString(text)
  37. ? text
  38. .replace(/&/g, '&')
  39. .replace(/</g, '&lt;')
  40. .replace(/>/g, '&gt;')
  41. .replace(/'/g, '&#39;')
  42. .replace(/"/g, '&quot;')
  43. : text;
  44. };
  45. return function(text) {
  46. return _.isArray(text)
  47. ? _.map(text, noXml)
  48. : noXml(text);
  49. };
  50. });
  51. module.filter('interpolateTemplateVars', function(templateSrv) {
  52. function interpolateTemplateVars(text, scope) {
  53. if (scope.panel) {
  54. return templateSrv.replaceWithText(text, scope.panel.scopedVars);
  55. } else {
  56. return templateSrv.replaceWithText(text, scope.row.scopedVars);
  57. }
  58. }
  59. interpolateTemplateVars.$stateful = true;
  60. return interpolateTemplateVars;
  61. });
  62. });