module.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import {PanelCtrl} from 'app/plugins/sdk';
  4. // Set and populate defaults
  5. var panelDefaults = {
  6. mode : "markdown", // 'html', 'markdown', 'text'
  7. content : "# title",
  8. };
  9. export class TextPanelCtrl extends PanelCtrl {
  10. static templateUrl = `public/app/plugins/panel/text/module.html`;
  11. converter: any;
  12. content: string;
  13. /** @ngInject */
  14. constructor($scope, $injector, private templateSrv, private $sce) {
  15. super($scope, $injector);
  16. _.defaults(this.panel, panelDefaults);
  17. }
  18. initEditMode() {
  19. super.initEditMode();
  20. this.icon = 'fa fa-text-width';
  21. this.addEditorTab('Options', 'public/app/plugins/panel/text/editor.html');
  22. this.editorTabIndex = 1;
  23. }
  24. refresh() {
  25. this.render();
  26. }
  27. render() {
  28. if (this.panel.mode === 'markdown') {
  29. this.renderMarkdown(this.panel.content);
  30. } else if (this.panel.mode === 'html') {
  31. this.updateContent(this.panel.content);
  32. } else if (this.panel.mode === 'text') {
  33. this.renderText(this.panel.content);
  34. }
  35. this.renderingCompleted();
  36. }
  37. renderText(content) {
  38. content = content
  39. .replace(/&/g, '&amp;')
  40. .replace(/>/g, '&gt;')
  41. .replace(/</g, '&lt;')
  42. .replace(/\n/g, '<br/>');
  43. this.updateContent(content);
  44. }
  45. renderMarkdown(content) {
  46. var text = content
  47. .replace(/&/g, '&amp;')
  48. .replace(/>/g, '&gt;')
  49. .replace(/</g, '&lt;');
  50. if (this.converter) {
  51. this.updateContent(this.converter.makeHtml(text));
  52. } else {
  53. System.import('vendor/showdown').then(Showdown => {
  54. this.converter = new Showdown.converter();
  55. this.$scope.$apply(() => {
  56. this.updateContent(this.converter.makeHtml(text));
  57. });
  58. });
  59. }
  60. }
  61. updateContent(html) {
  62. try {
  63. this.content = this.$sce.trustAsHtml(this.templateSrv.replace(html, this.panel.scopedVars));
  64. } catch (e) {
  65. console.log('Text panel error: ', e);
  66. this.content = this.$sce.trustAsHtml(html);
  67. }
  68. }
  69. }
  70. export {TextPanelCtrl as PanelCtrl}