plugin_edit_ctrl.ts 4.4 KB

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