plugin_edit_ctrl.ts 4.4 KB

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