plugin_edit_ctrl.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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: '',
  31. breadcrumbs: [{ title: 'Plugins', url: 'plugins' }, { title: model.name }],
  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. let hasDashboards = _.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 (let 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. var md = new Remarkable();
  85. this.readmeHtml = this.$sce.trustAsHtml(md.render(res));
  86. });
  87. }
  88. getPluginIcon(type) {
  89. switch (type) {
  90. case 'datasource':
  91. return 'icon-gf icon-gf-datasources';
  92. case 'panel':
  93. return 'icon-gf icon-gf-panel';
  94. case 'app':
  95. return 'icon-gf icon-gf-apps';
  96. case 'page':
  97. return 'icon-gf icon-gf-endpoint-tiny';
  98. case 'dashboard':
  99. return 'icon-gf icon-gf-dashboard';
  100. default:
  101. return 'icon-gf icon-gf-apps';
  102. }
  103. }
  104. update() {
  105. this.preUpdateHook()
  106. .then(() => {
  107. var updateCmd = _.extend(
  108. {
  109. enabled: this.model.enabled,
  110. pinned: this.model.pinned,
  111. jsonData: this.model.jsonData,
  112. secureJsonData: this.model.secureJsonData,
  113. },
  114. {}
  115. );
  116. return this.backendSrv.post(`/api/plugins/${this.pluginId}/settings`, updateCmd);
  117. })
  118. .then(this.postUpdateHook)
  119. .then(res => {
  120. window.location.href = window.location.href;
  121. });
  122. }
  123. importDashboards() {
  124. return Promise.resolve();
  125. }
  126. setPreUpdateHook(callback: () => any) {
  127. this.preUpdateHook = callback;
  128. }
  129. setPostUpdateHook(callback: () => any) {
  130. this.postUpdateHook = callback;
  131. }
  132. updateAvailable() {
  133. var modalScope = this.$scope.$new(true);
  134. modalScope.plugin = this.model;
  135. this.$rootScope.appEvent('show-modal', {
  136. src: 'public/app/features/plugins/partials/update_instructions.html',
  137. scope: modalScope,
  138. });
  139. }
  140. enable() {
  141. this.model.enabled = true;
  142. this.model.pinned = true;
  143. this.update();
  144. }
  145. disable() {
  146. this.model.enabled = false;
  147. this.model.pinned = false;
  148. this.update();
  149. }
  150. }
  151. angular.module('grafana.controllers').controller('PluginEditCtrl', PluginEditCtrl);