plugin_edit_ctrl.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import Remarkable from 'remarkable';
  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 $sce,
  24. $routeParams,
  25. navModelSrv,
  26. ) {
  27. this.navModel = navModelSrv.getNav('cfg', 'plugins', 0);
  28. this.model = {};
  29. this.pluginId = $routeParams.pluginId;
  30. this.tabIndex = 0;
  31. this.tabs = ['Readme'];
  32. this.preUpdateHook = () => Promise.resolve();
  33. this.postUpdateHook = () => Promise.resolve();
  34. }
  35. init() {
  36. return this.backendSrv.get(`/api/plugins/${this.pluginId}/settings`).then(result => {
  37. this.model = result;
  38. this.pluginIcon = this.getPluginIcon(this.model.type);
  39. this.navModel.breadcrumbs.push({text: this.model.name});
  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}/markdown/readme`).then(res => {
  60. var md = new Remarkable();
  61. this.readmeHtml = this.$sce.trustAsHtml(md.render(res));
  62. });
  63. }
  64. getPluginIcon(type) {
  65. switch (type) {
  66. case 'datasource': return 'icon-gf icon-gf-datasources';
  67. case 'panel': return 'icon-gf icon-gf-panel';
  68. case 'app': return 'icon-gf icon-gf-apps';
  69. case 'page': return 'icon-gf icon-gf-endpoint-tiny';
  70. case 'dashboard': return 'icon-gf icon-gf-dashboard';
  71. default: return 'icon-gf icon-gf-apps';
  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);