plugin_edit_ctrl.ts 3.4 KB

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