plugin_edit_ctrl.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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(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: '',
  32. breadcrumbs: [{ title: 'Plugins', url: '/plugins' }, { title: model.name }],
  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. let hasDashboards = _.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 (let tab of this.navModel.main.children) {
  63. if (tab.id === this.tab) {
  64. tab.active = true;
  65. }
  66. }
  67. }
  68. init() {
  69. return this.backendSrv.get(`/api/plugins/${this.pluginId}/settings`).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. var md = new Remarkable();
  86. this.readmeHtml = this.$sce.trustAsHtml(md.render(res));
  87. });
  88. }
  89. getPluginIcon(type) {
  90. switch (type) {
  91. case 'datasource':
  92. return 'icon-gf icon-gf-datasources';
  93. case 'panel':
  94. return 'icon-gf icon-gf-panel';
  95. case 'app':
  96. return 'icon-gf icon-gf-apps';
  97. case 'page':
  98. return 'icon-gf icon-gf-endpoint-tiny';
  99. case 'dashboard':
  100. return 'icon-gf icon-gf-dashboard';
  101. default:
  102. return 'icon-gf icon-gf-apps';
  103. }
  104. }
  105. update() {
  106. this.preUpdateHook()
  107. .then(() => {
  108. var updateCmd = _.extend(
  109. {
  110. enabled: this.model.enabled,
  111. pinned: this.model.pinned,
  112. jsonData: this.model.jsonData,
  113. secureJsonData: this.model.secureJsonData,
  114. },
  115. {}
  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);