module.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /** @scratch /panels/5
  2. * include::panels/text.asciidoc[]
  3. */
  4. /** @scratch /panels/text/0
  5. * == text
  6. * Status: *Stable*
  7. *
  8. * The text panel is used for displaying static text formated as markdown, sanitized html or as plain
  9. * text.
  10. *
  11. */
  12. define([
  13. 'angular',
  14. 'app',
  15. 'underscore',
  16. 'require',
  17. 'services/filterSrv'
  18. ],
  19. function (angular, app, _, require) {
  20. 'use strict';
  21. var module = angular.module('grafana.panels.text', []);
  22. app.useModule(module);
  23. module.controller('text', function($scope, filterSrv) {
  24. $scope.panelMeta = {
  25. description : "A static text panel that can use plain text, markdown, or (sanitized) HTML"
  26. };
  27. // Set and populate defaults
  28. var _d = {
  29. mode : "markdown", // 'html', 'markdown', 'text'
  30. content : "",
  31. style: {},
  32. };
  33. _.defaults($scope.panel,_d);
  34. $scope.init = function() {
  35. $scope.initBaseController(this, $scope);
  36. $scope.ready = false;
  37. $scope.$on('refresh', $scope.render);
  38. $scope.render();
  39. };
  40. $scope.render = function() {
  41. if ($scope.panel.mode === 'markdown') {
  42. $scope.renderMarkdown($scope.panel.content);
  43. }
  44. else if ($scope.panel.mode === 'html') {
  45. $scope.updateContent($scope.panel.content);
  46. }
  47. else if ($scope.panel.mode === 'text') {
  48. $scope.renderText($scope.panel.content);
  49. }
  50. };
  51. $scope.renderText = function(content) {
  52. content = content
  53. .replace(/&/g, '&')
  54. .replace(/>/g, '>')
  55. .replace(/</g, '&lt;')
  56. .replace(/\n/g, '<br/>');
  57. $scope.updateContent(content);
  58. };
  59. $scope.renderMarkdown = function(content) {
  60. require(['./lib/showdown'], function (Showdown) {
  61. var converter = new Showdown.converter();
  62. var text = content
  63. .replace(/&/g, '&amp;')
  64. .replace(/>/g, '&gt;')
  65. .replace(/</g, '&lt;');
  66. $scope.updateContent(converter.makeHtml(text));
  67. });
  68. };
  69. $scope.updateContent = function(html) {
  70. try {
  71. $scope.content = filterSrv.applyTemplateToTarget(html);
  72. if(!$scope.$$phase) {
  73. $scope.$apply();
  74. }
  75. } catch(e) {
  76. }
  77. };
  78. $scope.openEditor = function() {
  79. };
  80. });
  81. });