module.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. }
  17. initEditMode() {
  18. this.icon = 'fa fa-text-width';
  19. this.addEditorTab('Options', 'public/app/plugins/panel/text/editor.html');
  20. }
  21. refresh() {
  22. this.render();
  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. renderText(content) {
  35. content = content
  36. .replace(/&/g, '&amp;')
  37. .replace(/>/g, '&gt;')
  38. .replace(/</g, '&lt;')
  39. .replace(/\n/g, '<br/>');
  40. this.updateContent(content);
  41. }
  42. renderMarkdown(content) {
  43. var text = content
  44. .replace(/&/g, '&amp;')
  45. .replace(/>/g, '&gt;')
  46. .replace(/</g, '&lt;');
  47. if (this.converter) {
  48. this.updateContent(this.converter.makeHtml(text));
  49. } else {
  50. System.import('vendor/showdown').then(Showdown => {
  51. this.converter = new Showdown.converter();
  52. this.$scope.$apply(() => {
  53. this.updateContent(this.converter.makeHtml(text));
  54. });
  55. });
  56. }
  57. }
  58. updateContent(html) {
  59. try {
  60. this.content = this.$sce.trustAsHtml(this.templateSrv.replace(html, this.panel.scopedVars));
  61. } catch (e) {
  62. console.log('Text panel error: ', e);
  63. this.content = this.$sce.trustAsHtml(html);
  64. }
  65. }
  66. }
  67. class TextPanel extends PanelDirective {
  68. templateUrl = `public/app/plugins/panel/text/module.html`;
  69. controller = TextPanelCtrl;
  70. }
  71. export {TextPanel as Panel}