module.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, $injector, private templateSrv, private $sce) {
  14. super($scope, $injector);
  15. _.defaults(this.panel, panelDefaults);
  16. this.render();
  17. }
  18. initEditorTabs() {
  19. this.icon = 'fa fa-text-width';
  20. this.addEditorTab('Options', () => {
  21. return { templateUrl: 'public/app/plugins/panel/text/editor.html' };
  22. });
  23. }
  24. render() {
  25. if (this.panel.mode === 'markdown') {
  26. this.renderMarkdown(this.panel.content);
  27. } else if (this.panel.mode === 'html') {
  28. this.updateContent(this.panel.content);
  29. } else if (this.panel.mode === 'text') {
  30. this.renderText(this.panel.content);
  31. }
  32. this.renderingCompleted();
  33. }
  34. refresh() {
  35. this.render();
  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. class TextPanel extends PanelDirective {
  71. templateUrl = `app/plugins/panel/text/module.html`;
  72. controller = TextPanelCtrl;
  73. }
  74. export {TextPanel as Panel}