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. 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.getPluginsNav();
  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.model.dependencies.plugins.forEach(plug => {
  41. plug.icon = this.getPluginIcon(plug.type);
  42. });
  43. this.includes = _.map(result.includes, plug => {
  44. plug.icon = this.getPluginIcon(plug.type);
  45. return plug;
  46. });
  47. if (this.model.type === 'app') {
  48. this.hasDashboards = _.find(result.includes, {type: 'dashboard'});
  49. if (this.hasDashboards) {
  50. this.tabs.unshift('Dashboards');
  51. }
  52. this.tabs.unshift('Config');
  53. this.tabIndex = 0;
  54. }
  55. return this.initReadme();
  56. });
  57. }
  58. initReadme() {
  59. return this.backendSrv.get(`/api/plugins/${this.pluginId}/readme`).then(res => {
  60. return System.import('remarkable').then(Remarkable => {
  61. var md = new Remarkable();
  62. this.readmeHtml = this.$sce.trustAsHtml(md.render(res));
  63. });
  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);