plugin_edit_ctrl.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.getPluginsNav();
  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.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}/markdown/readme`).then(res => {
  61. var md = new Remarkable();
  62. this.readmeHtml = this.$sce.trustAsHtml(md.render(res));
  63. });
  64. }
  65. getPluginIcon(type) {
  66. switch (type) {
  67. case 'datasource': return 'icon-gf icon-gf-datasources';
  68. case 'panel': return 'icon-gf icon-gf-panel';
  69. case 'app': return 'icon-gf icon-gf-apps';
  70. case 'page': return 'icon-gf icon-gf-endpoint-tiny';
  71. case 'dashboard': return 'icon-gf icon-gf-dashboard';
  72. }
  73. }
  74. update() {
  75. this.preUpdateHook().then(() => {
  76. var updateCmd = _.extend({
  77. enabled: this.model.enabled,
  78. pinned: this.model.pinned,
  79. jsonData: this.model.jsonData,
  80. secureJsonData: this.model.secureJsonData,
  81. }, {});
  82. return this.backendSrv.post(`/api/plugins/${this.pluginId}/settings`, updateCmd);
  83. })
  84. .then(this.postUpdateHook)
  85. .then((res) => {
  86. window.location.href = window.location.href;
  87. });
  88. }
  89. importDashboards() {
  90. return Promise.resolve();
  91. }
  92. setPreUpdateHook(callback: () => any) {
  93. this.preUpdateHook = callback;
  94. }
  95. setPostUpdateHook(callback: () => any) {
  96. this.postUpdateHook = callback;
  97. }
  98. updateAvailable() {
  99. var modalScope = this.$scope.$new(true);
  100. modalScope.plugin = this.model;
  101. this.$rootScope.appEvent('show-modal', {
  102. src: 'public/app/features/plugins/partials/update_instructions.html',
  103. scope: modalScope
  104. });
  105. }
  106. enable() {
  107. this.model.enabled = true;
  108. this.model.pinned = true;
  109. this.update();
  110. }
  111. disable() {
  112. this.model.enabled = false;
  113. this.model.pinned = false;
  114. this.update();
  115. }
  116. }
  117. angular.module('grafana.controllers').controller('PluginEditCtrl', PluginEditCtrl);