plugin_edit_ctrl.ts 3.5 KB

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