module.ts 2.1 KB

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