plugin_edit_ctrl.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import appEvents from 'app/core/app_events';
  5. export class PluginEditCtrl {
  6. model: any;
  7. pluginIcon: string;
  8. pluginId: any;
  9. includes: any;
  10. readmeHtml: any;
  11. includedDatasources: any;
  12. tabIndex: number;
  13. tabs: any;
  14. navModel: any;
  15. hasDashboards: any;
  16. preUpdateHook: () => any;
  17. postUpdateHook: () => any;
  18. /** @ngInject */
  19. constructor(
  20. private $scope,
  21. private $rootScope,
  22. private backendSrv,
  23. private $routeParams,
  24. private $sce,
  25. private $http,
  26. private navModelSrv,
  27. ) {
  28. this.navModel = navModelSrv.getNav('cfg', 'plugins');
  29. this.model = {};
  30. this.pluginId = $routeParams.pluginId;
  31. this.tabIndex = 0;
  32. this.tabs = ['Readme'];
  33. this.preUpdateHook = () => Promise.resolve();
  34. this.postUpdateHook = () => Promise.resolve();
  35. }
  36. init() {
  37. return this.backendSrv.get(`/api/plugins/${this.pluginId}/settings`).then(result => {
  38. this.model = result;
  39. this.pluginIcon = this.getPluginIcon(this.model.type);
  40. this.navModel.breadcrumbs.push({text: this.model.name});
  41. this.model.dependencies.plugins.forEach(plug => {
  42. plug.icon = this.getPluginIcon(plug.type);
  43. });
  44. this.includes = _.map(result.includes, plug => {
  45. plug.icon = this.getPluginIcon(plug.type);
  46. return plug;
  47. });
  48. if (this.model.type === 'app') {
  49. this.hasDashboards = _.find(result.includes, {type: 'dashboard'});
  50. if (this.hasDashboards) {
  51. this.tabs.unshift('Dashboards');
  52. }
  53. this.tabs.unshift('Config');
  54. this.tabIndex = 0;
  55. }
  56. return this.initReadme();
  57. });
  58. }
  59. initReadme() {
  60. return this.backendSrv.get(`/api/plugins/${this.pluginId}/readme`).then(res => {
  61. return System.import('remarkable').then(Remarkable => {
  62. var md = new Remarkable();
  63. this.readmeHtml = this.$sce.trustAsHtml(md.render(res));
  64. });
  65. });
  66. }
  67. getPluginIcon(type) {
  68. switch (type) {
  69. case 'datasource': return 'icon-gf icon-gf-datasources';
  70. case 'panel': return 'icon-gf icon-gf-panel';
  71. case 'app': return 'icon-gf icon-gf-apps';
  72. case 'page': return 'icon-gf icon-gf-endpoint-tiny';
  73. case 'dashboard': return 'icon-gf icon-gf-dashboard';
  74. }
  75. }
  76. update() {
  77. this.preUpdateHook().then(() => {
  78. var updateCmd = _.extend({
  79. enabled: this.model.enabled,
  80. pinned: this.model.pinned,
  81. jsonData: this.model.jsonData,
  82. secureJsonData: this.model.secureJsonData,
  83. }, {});
  84. return this.backendSrv.post(`/api/plugins/${this.pluginId}/settings`, updateCmd);
  85. })
  86. .then(this.postUpdateHook)
  87. .then((res) => {
  88. window.location.href = window.location.href;
  89. });
  90. }
  91. importDashboards() {
  92. return Promise.resolve();
  93. }
  94. setPreUpdateHook(callback: () => any) {
  95. this.preUpdateHook = callback;
  96. }
  97. setPostUpdateHook(callback: () => any) {
  98. this.postUpdateHook = callback;
  99. }
  100. updateAvailable() {
  101. var modalScope = this.$scope.$new(true);
  102. modalScope.plugin = this.model;
  103. this.$rootScope.appEvent('show-modal', {
  104. src: 'public/app/features/plugins/partials/update_instructions.html',
  105. scope: modalScope
  106. });
  107. }
  108. enable() {
  109. this.model.enabled = true;
  110. this.model.pinned = true;
  111. this.update();
  112. }
  113. disable() {
  114. this.model.enabled = false;
  115. this.model.pinned = false;
  116. this.update();
  117. }
  118. }
  119. angular.module('grafana.controllers').controller('PluginEditCtrl', PluginEditCtrl);