plugin_edit_ctrl.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import Remarkable from 'remarkable';
  4. import { getPluginSettings } from './PluginSettingsCache';
  5. export class PluginEditCtrl {
  6. model: any;
  7. pluginIcon: string;
  8. pluginId: any;
  9. includes: any;
  10. readmeHtml: any;
  11. includedDatasources: any;
  12. tab: string;
  13. navModel: any;
  14. hasDashboards: any;
  15. preUpdateHook: () => any;
  16. postUpdateHook: () => any;
  17. /** @ngInject */
  18. constructor(private $scope, private $rootScope, private backendSrv, private $sce, private $routeParams, navModelSrv) {
  19. this.pluginId = $routeParams.pluginId;
  20. this.preUpdateHook = () => Promise.resolve();
  21. this.postUpdateHook = () => Promise.resolve();
  22. this.init();
  23. }
  24. setNavModel(model) {
  25. let defaultTab = 'readme';
  26. this.navModel = {
  27. main: {
  28. img: model.info.logos.large,
  29. subTitle: model.info.author.name,
  30. url: '',
  31. text: model.name,
  32. breadcrumbs: [{ title: 'Plugins', url: 'plugins' }],
  33. children: [
  34. {
  35. icon: 'fa fa-fw fa-file-text-o',
  36. id: 'readme',
  37. text: 'Readme',
  38. url: `plugins/${this.model.id}/edit?tab=readme`,
  39. },
  40. ],
  41. },
  42. };
  43. if (model.type === 'app') {
  44. this.navModel.main.children.push({
  45. icon: 'gicon gicon-cog',
  46. id: 'config',
  47. text: 'Config',
  48. url: `plugins/${this.model.id}/edit?tab=config`,
  49. });
  50. const hasDashboards: any = _.find(model.includes, { type: 'dashboard' });
  51. if (hasDashboards) {
  52. this.navModel.main.children.push({
  53. icon: 'gicon gicon-dashboard',
  54. id: 'dashboards',
  55. text: 'Dashboards',
  56. url: `plugins/${this.model.id}/edit?tab=dashboards`,
  57. });
  58. }
  59. defaultTab = 'config';
  60. }
  61. this.tab = this.$routeParams.tab || defaultTab;
  62. for (const tab of this.navModel.main.children) {
  63. if (tab.id === this.tab) {
  64. tab.active = true;
  65. }
  66. }
  67. }
  68. init() {
  69. return getPluginSettings(this.pluginId).then(result => {
  70. this.model = result;
  71. this.pluginIcon = this.getPluginIcon(this.model.type);
  72. this.model.dependencies.plugins.forEach(plug => {
  73. plug.icon = this.getPluginIcon(plug.type);
  74. });
  75. this.includes = _.map(result.includes, plug => {
  76. plug.icon = this.getPluginIcon(plug.type);
  77. return plug;
  78. });
  79. this.setNavModel(this.model);
  80. return this.initReadme();
  81. });
  82. }
  83. initReadme() {
  84. return this.backendSrv.get(`/api/plugins/${this.pluginId}/markdown/readme`).then(res => {
  85. const md = new Remarkable({
  86. linkify: true,
  87. });
  88. this.readmeHtml = this.$sce.trustAsHtml(md.render(res));
  89. });
  90. }
  91. getPluginIcon(type) {
  92. switch (type) {
  93. case 'datasource':
  94. return 'gicon gicon-datasources';
  95. case 'panel':
  96. return 'icon-gf icon-gf-panel';
  97. case 'app':
  98. return 'icon-gf icon-gf-apps';
  99. case 'page':
  100. return 'icon-gf icon-gf-endpoint-tiny';
  101. case 'dashboard':
  102. return 'gicon gicon-dashboard';
  103. default:
  104. return 'icon-gf icon-gf-apps';
  105. }
  106. }
  107. update() {
  108. this.preUpdateHook()
  109. .then(() => {
  110. const updateCmd = _.extend(
  111. {
  112. enabled: this.model.enabled,
  113. pinned: this.model.pinned,
  114. jsonData: this.model.jsonData,
  115. secureJsonData: this.model.secureJsonData,
  116. },
  117. {}
  118. );
  119. return this.backendSrv.post(`/api/plugins/${this.pluginId}/settings`, updateCmd);
  120. })
  121. .then(this.postUpdateHook)
  122. .then(res => {
  123. window.location.href = window.location.href;
  124. });
  125. }
  126. importDashboards() {
  127. return Promise.resolve();
  128. }
  129. setPreUpdateHook(callback: () => any) {
  130. this.preUpdateHook = callback;
  131. }
  132. setPostUpdateHook(callback: () => any) {
  133. this.postUpdateHook = callback;
  134. }
  135. updateAvailable() {
  136. const modalScope = this.$scope.$new(true);
  137. modalScope.plugin = this.model;
  138. this.$rootScope.appEvent('show-modal', {
  139. src: 'public/app/features/plugins/partials/update_instructions.html',
  140. scope: modalScope,
  141. });
  142. }
  143. enable() {
  144. this.model.enabled = true;
  145. this.model.pinned = true;
  146. this.update();
  147. }
  148. disable() {
  149. this.model.enabled = false;
  150. this.model.pinned = false;
  151. this.update();
  152. }
  153. }
  154. angular.module('grafana.controllers').controller('PluginEditCtrl', PluginEditCtrl);